You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2009/11/19 04:04:18 UTC
svn commit: r882038 - in /openejb/trunk/openejb3/container/openejb-core/src:
main/java/org/apache/openejb/config/AutoConfig.java
test/java/org/apache/openejb/config/AutoConfigPersistenceUnitsTest.java
Author: dblevins
Date: Thu Nov 19 03:04:12 2009
New Revision: 882038
URL: http://svn.apache.org/viewvc?rev=882038&view=rev
Log:
Partial support for OPENEJB-1027: Add the application name to the data sources matching heuristics
Patch from Luis Fernando Planella Gonzalez. Thanks, Luis!
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AutoConfigPersistenceUnitsTest.java
Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java?rev=882038&r1=882037&r2=882038&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java Thu Nov 19 03:04:12 2009
@@ -142,7 +142,7 @@
deploy(webModule, appResources);
}
for (PersistenceModule persistenceModule : appModule.getPersistenceModules()) {
- deploy(persistenceModule);
+ deploy(appModule, persistenceModule);
}
return appModule;
}
@@ -872,7 +872,7 @@
throw new OpenEJBException("Unknown enterprise bean type " + enterpriseBean.getClass().getName());
}
- private void deploy(PersistenceModule persistenceModule) throws OpenEJBException {
+ private void deploy(AppModule app, PersistenceModule persistenceModule) throws OpenEJBException {
if (!autoCreateResources) {
return;
}
@@ -1047,6 +1047,29 @@
}
}
+ // No data sources were specified: If the app is running a web module, use it's context name as default
+ if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
+ String webContextRoot = null;
+ if (!app.getWebModules().isEmpty()) {
+ webContextRoot = app.getWebModules().iterator().next().getContextRoot();
+ }
+
+ required.clear();
+ required.put("JtaManaged", "true");
+ jtaDataSourceId = findResourceId(webContextRoot, "DataSource", required, null);
+
+ required.clear();
+ required.put("JtaManaged", "false");
+ nonJtaDataSourceId = findResourceId(webContextRoot, "DataSource", required, null);
+
+ // When no datasource was found with the context name with explicit JtaManaged set, try to find one with it unset
+ if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
+ required.clear();
+ required.put("JtaManaged", NONE);
+ jtaDataSourceId = findResourceId(webContextRoot, "DataSource", required, null);
+ }
+ }
+
//
// If neither of the references are valid yet, then let's take
// the first valid datasource.
Modified: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AutoConfigPersistenceUnitsTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AutoConfigPersistenceUnitsTest.java?rev=882038&r1=882037&r2=882038&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AutoConfigPersistenceUnitsTest.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AutoConfigPersistenceUnitsTest.java Thu Nov 19 03:04:12 2009
@@ -27,6 +27,7 @@
import org.apache.openejb.assembler.classic.SecurityServiceInfo;
import org.apache.openejb.assembler.classic.TransactionServiceInfo;
import org.apache.openejb.config.sys.Resource;
+import org.apache.openejb.jee.WebApp;
import org.apache.openejb.jee.jpa.unit.Persistence;
import org.apache.openejb.jee.jpa.unit.PersistenceUnit;
import org.apache.openejb.loader.SystemInstance;
@@ -156,6 +157,124 @@
}
/**
+ * Existing data source "orange-web", not controlled by us
+ *
+ * Application contains a web module with root context path as "orange-web"
+ *
+ * Persistence xml like so:
+ *
+ * <persistence-unit name="orange-unit" />
+ *
+ * The orange-unit app should automatically use orange-web data source and create a new non-JtaManaged datasource
+ *
+ * @throws Exception
+ */
+ public void testFromWebAppThirdParty() throws Exception {
+
+ ResourceInfo supplied = addDataSource("orange-web", OrangeDriver.class, "jdbc:orange-web:some:stuff", null);
+ assertSame(supplied, resources.get(0));
+
+ PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
+
+ ClassLoader cl = this.getClass().getClassLoader();
+ AppModule app = new AppModule(cl, "orange-app");
+ app.getPersistenceModules().add(new PersistenceModule("root", new Persistence(persistenceUnit)));
+ WebApp webApp = new WebApp();
+ webApp.setMetadataComplete(true);
+ app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-web"));
+
+ // Create app
+ AppInfo appInfo = config.configureApplication(app);
+ assembler.createApplication(appInfo);
+ PersistenceUnitInfo unitInfo = appInfo.persistenceUnits.get(0);
+
+ //Check results
+ assertEquals(supplied.id, unitInfo.jtaDataSource);
+ assertNull(unitInfo.nonJtaDataSource);
+ }
+ /**
+ * Existing data source "orange-web", jta managed
+ *
+ * Application contains a web module with root context path as "orange-web"
+ *
+ * Persistence xml like so:
+ *
+ * <persistence-unit name="orange-unit" />
+ *
+ * The orange-unit app should automatically use orange-web data source and create a new non-JtaManaged datasource
+ *
+ * @throws Exception
+ */
+ public void testFromWebAppJta() throws Exception {
+
+ ResourceInfo supplied = addDataSource("orange-web", OrangeDriver.class, "jdbc:orange-web:some:stuff", true);
+ assertSame(supplied, resources.get(0));
+
+ PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
+
+ ClassLoader cl = this.getClass().getClassLoader();
+ AppModule app = new AppModule(cl, "orange-app");
+ app.getPersistenceModules().add(new PersistenceModule("root", new Persistence(persistenceUnit)));
+ WebApp webApp = new WebApp();
+ webApp.setMetadataComplete(true);
+ app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-web"));
+
+ // Create app
+ AppInfo appInfo = config.configureApplication(app);
+ assembler.createApplication(appInfo);
+
+ // Check results
+ ResourceInfo generated = resources.get(1);
+ assertEquals(supplied.id + "NonJta", generated.id);
+ assertEquals(supplied.service, generated.service);
+ assertEquals(supplied.className, generated.className);
+ assertEquals(supplied.properties.get("JdbcDriver"), generated.properties.get("JdbcDriver"));
+ assertEquals(supplied.properties.get("JdbcUrl"), generated.properties.get("JdbcUrl"));
+ assertEquals("false", generated.properties.get("JtaManaged"));
+ }
+
+ /**
+ * Existing data source "orange-web", non-jta managed
+ *
+ * Application contains a web module with root context path as "orange-web"
+ *
+ * Persistence xml like so:
+ *
+ * <persistence-unit name="orange-unit" />
+ *
+ * The orange-unit app should automatically use orange-web data source and create a new non-JtaManaged datasource
+ *
+ * @throws Exception
+ */
+ public void testFromWebAppNonJta() throws Exception {
+
+ ResourceInfo supplied = addDataSource("orange-web", OrangeDriver.class, "jdbc:orange-web:some:stuff", false);
+ assertSame(supplied, resources.get(0));
+
+ PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
+
+ ClassLoader cl = this.getClass().getClassLoader();
+ AppModule app = new AppModule(cl, "orange-app");
+ app.getPersistenceModules().add(new PersistenceModule("root", new Persistence(persistenceUnit)));
+ WebApp webApp = new WebApp();
+ webApp.setMetadataComplete(true);
+ app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-web"));
+
+ // Create app
+ AppInfo appInfo = config.configureApplication(app);
+ assembler.createApplication(appInfo);
+
+ // Check results
+ ResourceInfo generated = resources.get(1);
+ assertEquals(supplied.id + "Jta", generated.id);
+ assertEquals(supplied.service, generated.service);
+ assertEquals(supplied.className, generated.className);
+ assertEquals(supplied.properties.get("JdbcDriver"), generated.properties.get("JdbcDriver"));
+ assertEquals(supplied.properties.get("JdbcUrl"), generated.properties.get("JdbcUrl"));
+ assertEquals("true", generated.properties.get("JtaManaged"));
+ }
+
+ /**
* Existing data source "Orange", not controlled by us
* Existing data source "OrangeUnmanaged", also not controlled by us
*