You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2009/12/06 15:14:43 UTC
svn commit: r887685 - in /cayenne/main/trunk: ./
framework/cayenne-jdk1.5-unpublished/
framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/
framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/
fram...
Author: aadamchik
Date: Sun Dec 6 14:14:42 2009
New Revision: 887685
URL: http://svn.apache.org/viewvc?rev=887685&view=rev
Log:
CAY-1319 Switch Cayenne configuration loading to cayenne-di container
* JNDIDataSourceProvider
Added:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/JNDIDataSourceFactoryTest.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/JNDISetup.java
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/JNDIDataSourceFactory.java
cayenne/main/trunk/pom.xml
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml?rev=887685&r1=887684&r2=887685&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml Sun Dec 6 14:14:42 2009
@@ -57,6 +57,10 @@
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-mock</artifactId>
+ </dependency>
+ <dependency>
<groupId>com.caucho</groupId>
<artifactId>resin-hessian</artifactId>
</dependency>
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/JNDIDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/JNDIDataSourceFactory.java?rev=887685&r1=887684&r2=887685&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/JNDIDataSourceFactory.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/JNDIDataSourceFactory.java Sun Dec 6 14:14:42 2009
@@ -32,6 +32,8 @@
/**
* Looks up DataSource objects via JNDI.
*
+ * @deprecated since 3.1, replaced by
+ * {@link org.apache.cayenne.configuration.JNDIDataSourceFactory}.
*/
public class JNDIDataSourceFactory implements DataSourceFactory {
@@ -97,9 +99,10 @@
// reflection ...
ClassLoader loader = Thread.currentThread().getContextClassLoader();
- DataSourceFactory prefsFactory = (DataSourceFactory) Class
- .forName("org.apache.cayenne.modeler.pref.PreferencesDataSourceFactory", true, loader)
- .newInstance();
+ DataSourceFactory prefsFactory = (DataSourceFactory) Class.forName(
+ "org.apache.cayenne.modeler.pref.PreferencesDataSourceFactory",
+ true,
+ loader).newInstance();
prefsFactory.initializeWithParentConfiguration(parentConfig);
return prefsFactory.getDataSource(location);
Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java?rev=887685&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java Sun Dec 6 14:14:42 2009
@@ -0,0 +1,77 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.configuration;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.access.QueryLogger;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Locates DataSource mapped via JNDI.
+ *
+ * @since 3.1
+ */
+public class JNDIDataSourceFactory implements DataSourceFactory {
+
+ private static final Log logger = LogFactory.getLog(JNDIDataSourceFactory.class);
+
+ public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
+
+ String location = nodeDescriptor.getLocation();
+ if (location == null) {
+ throw new CayenneRuntimeException(
+ "Null 'location' for nodeDescriptor '%s'",
+ nodeDescriptor.getName());
+ }
+
+ try {
+ return loadViaJNDI(location);
+ }
+ catch (Exception ex) {
+ logger.info("failed JNDI lookup of DataSource location '" + location + "'");
+ QueryLogger.logConnectFailure(ex);
+ throw ex;
+ }
+ }
+
+ DataSource loadViaJNDI(String location) throws NamingException {
+ QueryLogger.logConnect(location);
+
+ Context context = new InitialContext();
+ DataSource dataSource;
+ try {
+ Context envContext = (Context) context.lookup("java:comp/env");
+ dataSource = (DataSource) envContext.lookup(location);
+ }
+ catch (NamingException namingEx) {
+ // try looking up the location directly...
+ dataSource = (DataSource) context.lookup(location);
+ }
+
+ QueryLogger.logConnectSuccess();
+ return dataSource;
+ }
+
+}
Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/JNDIDataSourceFactoryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/JNDIDataSourceFactoryTest.java?rev=887685&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/JNDIDataSourceFactoryTest.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/JNDIDataSourceFactoryTest.java Sun Dec 6 14:14:42 2009
@@ -0,0 +1,93 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.configuration;
+
+import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
+
+import junit.framework.TestCase;
+
+import org.apache.cayenne.unit.JNDISetup;
+
+import com.mockrunner.mock.jdbc.MockDataSource;
+
+public class JNDIDataSourceFactoryTest extends TestCase {
+
+ public void testGetDataSource_NameBound() throws Exception {
+
+ DataNodeDescriptor descriptor = new DataNodeDescriptor();
+ descriptor.setLocation("jdbc/TestDS");
+
+ JNDISetup.doSetup();
+
+ MockDataSource dataSource = new MockDataSource();
+ InitialContext context = new InitialContext();
+ context.bind(descriptor.getLocation(), dataSource);
+
+ try {
+
+ JNDIDataSourceFactory factory = new JNDIDataSourceFactory();
+ assertSame(dataSource, factory.getDataSource(descriptor));
+ }
+ finally {
+ // since the context is shared, must clear it after the test
+ context.unbind(descriptor.getLocation());
+ }
+ }
+
+ public void testGetDataSource_NameBoundWithPrefix() throws Exception {
+
+ DataNodeDescriptor descriptor = new DataNodeDescriptor();
+ descriptor.setLocation("jdbc/TestDS");
+
+ JNDISetup.doSetup();
+
+ MockDataSource dataSource = new MockDataSource();
+ InitialContext context = new InitialContext();
+ context.bind("java:comp/env/" + descriptor.getLocation(), dataSource);
+
+ try {
+
+ JNDIDataSourceFactory factory = new JNDIDataSourceFactory();
+ assertSame(dataSource, factory.getDataSource(descriptor));
+ }
+ finally {
+ // since the context is shared, must clear it after the test
+ context.unbind("java:comp/env/" + descriptor.getLocation());
+ }
+ }
+
+ public void testGetDataSource_NameNotBound() throws Exception {
+
+ DataNodeDescriptor descriptor = new DataNodeDescriptor();
+ descriptor.setLocation("jdbc/TestDS");
+
+ JNDISetup.doSetup();
+
+ JNDIDataSourceFactory factory = new JNDIDataSourceFactory();
+
+ try {
+ factory.getDataSource(descriptor);
+ fail("Didn't throw on unbound name");
+ }
+ catch (NameNotFoundException e) {
+ // expected
+ }
+ }
+}
Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/JNDISetup.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/JNDISetup.java?rev=887685&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/JNDISetup.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/unit/JNDISetup.java Sun Dec 6 14:14:42 2009
@@ -0,0 +1,57 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.unit;
+
+import javax.naming.NamingException;
+import javax.naming.spi.NamingManager;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.mock.jndi.SimpleNamingContextBuilder;
+
+/**
+ * A helper class to setup a shared test JNDI environment.
+ */
+public class JNDISetup {
+
+ private static Log logger = LogFactory.getLog(JNDISetup.class);
+
+ private static volatile boolean setup;
+
+ public static void doSetup() {
+ if (!setup) {
+
+ synchronized (JNDISetup.class) {
+
+ if (!setup) {
+ try {
+ NamingManager
+ .setInitialContextFactoryBuilder(new SimpleNamingContextBuilder());
+ }
+ catch (NamingException e) {
+ logger.error("Can't perform JNDI setup, ignoring...", e);
+ }
+
+ setup = true;
+ }
+ }
+ }
+ }
+
+}
Modified: cayenne/main/trunk/pom.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/pom.xml?rev=887685&r1=887684&r2=887685&view=diff
==============================================================================
--- cayenne/main/trunk/pom.xml (original)
+++ cayenne/main/trunk/pom.xml Sun Dec 6 14:14:42 2009
@@ -497,6 +497,13 @@
<version>1.2.6</version>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-mock</artifactId>
+ <version>2.0.8</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>velocity</groupId>