Search in sources :

Example 26 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class AppInfoBuilder method buildPersistenceModules.

private void buildPersistenceModules(final AppModule appModule, final AppInfo appInfo) {
    for (final PersistenceModule persistenceModule : appModule.getPersistenceModules()) {
        final String rootUrl = persistenceModule.getRootUrl();
        final Persistence persistence = persistenceModule.getPersistence();
        for (final PersistenceUnit persistenceUnit : persistence.getPersistenceUnit()) {
            final PersistenceUnitInfo info = new PersistenceUnitInfo();
            info.id = appModule.persistenceUnitId(rootUrl, persistenceUnit.getName());
            info.name = persistenceUnit.getName();
            info.watchedResources.addAll(persistenceModule.getWatchedResources());
            info.persistenceUnitRootUrl = rootUrl;
            info.provider = persistenceUnit.getProvider();
            info.transactionType = persistenceUnit.getTransactionType().toString();
            info.webappName = findRelatedWebApp(appModule, rootUrl);
            final Boolean excludeUnlistedClasses = persistenceUnit.isExcludeUnlistedClasses();
            info.excludeUnlistedClasses = persistenceUnit.isScanned() || excludeUnlistedClasses != null && excludeUnlistedClasses;
            info.jtaDataSource = persistenceUnit.getJtaDataSource();
            info.nonJtaDataSource = persistenceUnit.getNonJtaDataSource();
            info.jarFiles.addAll(persistenceUnit.getJarFile());
            info.classes.addAll(persistenceUnit.getClazz());
            info.mappingFiles.addAll(persistenceUnit.getMappingFile());
            info.persistenceXMLSchemaVersion = persistence.getVersion();
            info.sharedCacheMode = persistenceUnit.getSharedCacheMode().toString();
            info.validationMode = persistenceUnit.getValidationMode().toString();
            // Handle Properties
            info.properties.putAll(persistenceUnit.getProperties());
            PersistenceProviderProperties.apply(appModule, info);
            // Persistence Unit Root Url
            appInfo.persistenceUnits.add(info);
        }
    }
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) PersistenceUnitInfo(org.apache.openejb.assembler.classic.PersistenceUnitInfo)

Example 27 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class AutoConfigPersistenceUnitsTest method testMultiple.

/**
 * Existing data source "Orange", jta managed
 * Existing data source "OrangeUnmanaged", not jta managed
 * Existing data source "Lime", jta managed
 * Existing data source "LimeUnmanaged", not jta managed
 *
 * Persistence xml like so:
 *
 * <persistence-unit name="orange-unit">
 * <jta-data-source>Orange</jta-data-source>
 * <non-jta-data-source>OrangeUnmanaged</non-jta-data-source>
 * </persistence-unit>
 * <persistence-unit name="lime-unit">
 * <jta-data-source>Lime</jta-data-source>
 * <non-jta-data-source>LimeUnmanaged</non-jta-data-source>
 * </persistence-unit>
 *
 * This is the happy path.
 *
 * @throws Exception
 */
public void testMultiple() throws Exception {
    final ResourceInfo orangeJta = addDataSource("Orange", OrangeDriver.class, "jdbc:orange:some:stuff", true);
    final ResourceInfo orangeNonJta = addDataSource("OrangeUnmanaged", OrangeDriver.class, "jdbc:orange:some:stuff", false);
    final ResourceInfo limeJta = addDataSource("Lime", LimeDriver.class, "jdbc:lime:some:stuff", true);
    final ResourceInfo limeNonJta = addDataSource("LimeUnmanaged", LimeDriver.class, "jdbc:lime:some:stuff", false);
    assertSame(orangeJta, resources.get(0));
    assertSame(orangeNonJta, resources.get(1));
    assertSame(limeJta, resources.get(2));
    assertSame(limeNonJta, resources.get(3));
    final PersistenceUnit unit1 = new PersistenceUnit("orange-unit");
    unit1.setJtaDataSource("Orange");
    unit1.setNonJtaDataSource("OrangeUnmanaged");
    final PersistenceUnit unit2 = new PersistenceUnit("lime-unit");
    unit2.setJtaDataSource("Lime");
    unit2.setNonJtaDataSource("LimeUnmanaged");
    final AppModule app = new AppModule(this.getClass().getClassLoader(), "test-app");
    app.addPersistenceModule(new PersistenceModule("root", new Persistence(unit1, unit2)));
    // Create app
    final AppInfo appInfo = config.configureApplication(app);
    assembler.createApplication(appInfo);
    // Check results
    final PersistenceUnitInfo orangeUnit = appInfo.persistenceUnits.get(0);
    final PersistenceUnitInfo limeUnit = appInfo.persistenceUnits.get(1);
    assertNotNull(orangeUnit);
    assertEquals(orangeJta.id, orangeUnit.jtaDataSource);
    assertEquals(orangeNonJta.id, orangeUnit.nonJtaDataSource);
    assertNotNull(limeUnit);
    assertEquals(limeJta.id, limeUnit.jtaDataSource);
    assertEquals(limeNonJta.id, limeUnit.nonJtaDataSource);
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) PersistenceUnitInfo(org.apache.openejb.assembler.classic.PersistenceUnitInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Example 28 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class AutoConfigPersistenceUnitsTest method testFromWebAppContextJta.

/**
 * 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 testFromWebAppContextJta() throws Exception {
    final ResourceInfo supplied = addDataSource("orange-web", OrangeDriver.class, "jdbc:orange-web:some:stuff", true);
    assertSame(supplied, resources.get(0));
    final PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
    final ClassLoader cl = this.getClass().getClassLoader();
    final AppModule app = new AppModule(cl, "orange-app");
    app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
    final WebApp webApp = new WebApp();
    webApp.setMetadataComplete(true);
    app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-web"));
    // Create app
    final AppInfo appInfo = config.configureApplication(app);
    assembler.createApplication(appInfo);
    // Check results
    final 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"));
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) WebApp(org.apache.openejb.jee.WebApp) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Example 29 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class AutoConfigPersistenceUnitsTest method testFromWebAppIdThirdParty.

/**
 * Existing data source "orange-id", not controlled by us
 *
 * Application contains a web module with id "orange-id"
 *
 * Persistence xml like so:
 *
 * <persistence-unit name="orange-unit" />
 *
 * The orange-unit app should automatically use orange-id data source and the non-jta-datasource should be null
 *
 * @throws Exception
 */
public void testFromWebAppIdThirdParty() throws Exception {
    final ResourceInfo supplied = addDataSource("orange-id", OrangeDriver.class, "jdbc:orange-web:some:stuff", null);
    assertSame(supplied, resources.get(0));
    final PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
    final ClassLoader cl = this.getClass().getClassLoader();
    final AppModule app = new AppModule(cl, "orange-app");
    app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
    final WebApp webApp = new WebApp();
    webApp.setMetadataComplete(true);
    app.getWebModules().add(new WebModule(webApp, "orange-web", cl, null, "orange-id"));
    // Create app
    final AppInfo appInfo = config.configureApplication(app);
    assembler.createApplication(appInfo);
    final PersistenceUnitInfo unitInfo = appInfo.persistenceUnits.get(0);
    // Check results
    assertEquals(supplied.id, unitInfo.jtaDataSource);
    assertNull(unitInfo.nonJtaDataSource);
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) PersistenceUnitInfo(org.apache.openejb.assembler.classic.PersistenceUnitInfo) WebApp(org.apache.openejb.jee.WebApp) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Example 30 with PersistenceUnit

use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.

the class AutoConfigPersistenceUnitsTest method testFromWebAppIdNonJta.

/**
 * Existing data source "orange-id", non-jta managed
 *
 * Application contains a web module with id "orange-id"
 *
 * Persistence xml like so:
 *
 * <persistence-unit name="orange-unit" />
 *
 * The orange-unit app should automatically use orange-id data source and create a new non-JtaManaged datasource
 *
 * @throws Exception
 */
public void testFromWebAppIdNonJta() throws Exception {
    final ResourceInfo supplied = addDataSource("orange-id", OrangeDriver.class, "jdbc:orange-web:some:stuff", false);
    assertSame(supplied, resources.get(0));
    final PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
    final ClassLoader cl = this.getClass().getClassLoader();
    final AppModule app = new AppModule(cl, "orange-app");
    app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
    final WebApp webApp = new WebApp();
    webApp.setMetadataComplete(true);
    app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-id"));
    // Create app
    final AppInfo appInfo = config.configureApplication(app);
    assembler.createApplication(appInfo);
    // Check results
    final 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"));
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) WebApp(org.apache.openejb.jee.WebApp) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Aggregations

PersistenceUnit (org.apache.openejb.jee.jpa.unit.PersistenceUnit)48 Persistence (org.apache.openejb.jee.jpa.unit.Persistence)36 Module (org.apache.openejb.testing.Module)19 AppModule (org.apache.openejb.config.AppModule)10 EjbJar (org.apache.openejb.jee.EjbJar)10 AppInfo (org.apache.openejb.assembler.classic.AppInfo)9 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)9 EjbModule (org.apache.openejb.config.EjbModule)9 PersistenceModule (org.apache.openejb.config.PersistenceModule)8 WebApp (org.apache.openejb.jee.WebApp)7 ArrayList (java.util.ArrayList)5 StatelessBean (org.apache.openejb.jee.StatelessBean)5 File (java.io.File)4 Properties (java.util.Properties)4 URL (java.net.URL)3 Collection (java.util.Collection)3 List (java.util.List)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 Assembler (org.apache.openejb.assembler.classic.Assembler)3 PersistenceUnitInfo (org.apache.openejb.assembler.classic.PersistenceUnitInfo)3