Search in sources :

Example 11 with Persistence

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

the class JMXDataSourceTest method persistence.

@Module
public Persistence persistence() {
    final PersistenceUnit unit = new PersistenceUnit("JMXDataSourceTest-unit");
    unit.addClass(EntityToValidate.class);
    unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
    unit.getProperties().setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
    unit.setExcludeUnlistedClasses(true);
    final Persistence persistence = new Persistence(unit);
    persistence.setVersion("2.0");
    return persistence;
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) Module(org.apache.openejb.testing.Module)

Example 12 with Persistence

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

the class PassivationWithEmTest method persistence.

@Module
public Persistence persistence() throws Exception {
    final PersistenceUnit unit = new PersistenceUnit("passivation-unit");
    unit.addClass(MyEntity.class);
    unit.setTransactionType(TransactionType.JTA);
    unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
    unit.getProperties().setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
    unit.setExcludeUnlistedClasses(true);
    final Persistence persistence = new org.apache.openejb.jee.jpa.unit.Persistence(unit);
    persistence.setVersion("2.0");
    return persistence;
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) Module(org.apache.openejb.testing.Module)

Example 13 with Persistence

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

the class ReadDescriptors method deploy.

@SuppressWarnings({ "unchecked" })
public AppModule deploy(final AppModule appModule) throws OpenEJBException {
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        if (ejbModule.getEjbJar() == null) {
            readEjbJar(ejbModule, appModule);
        }
        if (ejbModule.getOpenejbJar() == null) {
            readOpenejbJar(ejbModule);
        }
        if (ejbModule.getBeans() == null) {
            readBeans(ejbModule);
        }
        readValidationConfigType(ejbModule);
        readCmpOrm(ejbModule);
        readResourcesXml(ejbModule);
    }
    for (final ClientModule clientModule : appModule.getClientModules()) {
        readAppClient(clientModule, appModule);
        readValidationConfigType(clientModule);
        readResourcesXml(clientModule);
    }
    for (final ConnectorModule connectorModule : appModule.getConnectorModules()) {
        readConnector(connectorModule, appModule);
        readValidationConfigType(connectorModule);
        readResourcesXml(connectorModule);
    }
    for (final WebModule webModule : appModule.getWebModules()) {
        readWebApp(webModule, appModule);
        readValidationConfigType(webModule);
        readResourcesXml(webModule);
    }
    final List<Object> persistenceUrls = (List<Object>) appModule.getAltDDs().get("persistence.xml");
    if (persistenceUrls != null) {
        for (final Object persistenceUrl : persistenceUrls) {
            final boolean url = persistenceUrl instanceof URL;
            final Source source = getSource(persistenceUrl);
            final String moduleName;
            final String path;
            final String rootUrl;
            if (url) {
                final URL pUrl = (URL) persistenceUrl;
                File file = URLs.toFile(pUrl);
                path = file.getAbsolutePath();
                if (file.getName().endsWith("persistence.xml")) {
                    final File parentFile = file.getParentFile();
                    final String parent = parentFile.getName();
                    if (parent.equalsIgnoreCase("WEB-INF") || parent.equalsIgnoreCase("META-INF")) {
                        file = parentFile.getParentFile();
                    } else {
                        // we don't really know so simply go back (users will often put persistence.xml in root resource folder with arquillian)
                        file = file.getParentFile();
                    }
                }
                moduleName = file.toURI().toString();
                String tmpRootUrl = moduleName;
                final String extForm = pUrl.toExternalForm();
                if (extForm.contains("WEB-INF/classes/META-INF/")) {
                    if (!ROOT_URL_FROM_WEBINF) {
                        tmpRootUrl = extForm.substring(0, extForm.indexOf("/META-INF"));
                    } else {
                        tmpRootUrl = extForm.substring(0, extForm.indexOf("/classes/META-INF"));
                    }
                }
                if (tmpRootUrl.endsWith(".war")) {
                    tmpRootUrl = tmpRootUrl.substring(0, tmpRootUrl.length() - ".war".length());
                }
                rootUrl = tmpRootUrl;
            } else {
                moduleName = "";
                rootUrl = "";
                path = null;
            }
            try {
                final Persistence persistence = JaxbPersistenceFactory.getPersistence(Persistence.class, source.get());
                final PersistenceModule persistenceModule = new PersistenceModule(appModule, rootUrl, persistence);
                persistenceModule.getWatchedResources().add(moduleName);
                if (url && "file".equals(((URL) persistenceUrl).getProtocol())) {
                    persistenceModule.getWatchedResources().add(path);
                }
                appModule.addPersistenceModule(persistenceModule);
            } catch (final Exception e1) {
                DeploymentLoader.logger.error("Unable to load Persistence Unit from EAR: " + appModule.getJarLocation() + ", module: " + moduleName + ". Exception: " + e1.getMessage(), e1);
            }
        }
    }
    final List<URL> persistenceFragmentUrls = (List<URL>) appModule.getAltDDs().get("persistence-fragment.xml");
    if (persistenceFragmentUrls != null) {
        for (final URL persistenceFragmentUrl : persistenceFragmentUrls) {
            try {
                final PersistenceFragment persistenceFragment = JaxbPersistenceFactory.getPersistence(PersistenceFragment.class, persistenceFragmentUrl);
                // merging
                for (final PersistenceUnitFragment fragmentUnit : persistenceFragment.getPersistenceUnitFragment()) {
                    for (final PersistenceModule persistenceModule : appModule.getPersistenceModules()) {
                        final Persistence persistence = persistenceModule.getPersistence();
                        for (final PersistenceUnit unit : persistence.getPersistenceUnit()) {
                            if (!fragmentUnit.getName().equals(unit.getName())) {
                                continue;
                            }
                            if (!persistenceFragment.getVersion().equals(persistence.getVersion())) {
                                logger.error("persistence unit version and fragment version are different, fragment will be ignored");
                                continue;
                            }
                            if ("file".equals(persistenceFragmentUrl.getProtocol())) {
                                persistenceModule.getWatchedResources().add(URLs.toFile(persistenceFragmentUrl).getAbsolutePath());
                            }
                            for (final String clazz : fragmentUnit.getClazz()) {
                                if (!unit.getClazz().contains(clazz)) {
                                    logger.info("Adding class " + clazz + " to persistence unit " + fragmentUnit.getName());
                                    unit.getClazz().add(clazz);
                                }
                            }
                            for (final String mappingFile : fragmentUnit.getMappingFile()) {
                                if (!unit.getMappingFile().contains(mappingFile)) {
                                    logger.info("Adding mapping file " + mappingFile + " to persistence unit " + fragmentUnit.getName());
                                    unit.getMappingFile().add(mappingFile);
                                }
                            }
                            for (final String jarFile : fragmentUnit.getJarFile()) {
                                if (!unit.getJarFile().contains(jarFile)) {
                                    logger.info("Adding jar file " + jarFile + " to persistence unit " + fragmentUnit.getName());
                                    unit.getJarFile().add(jarFile);
                                }
                            }
                            if (fragmentUnit.isExcludeUnlistedClasses()) {
                                unit.setExcludeUnlistedClasses(true);
                                logger.info("Excluding unlisted classes for persistence unit " + fragmentUnit.getName());
                            }
                        // else let the main persistence unit decide
                        }
                    }
                }
            } catch (final Exception e1) {
                DeploymentLoader.logger.error("Unable to load Persistence Unit Fragment from EAR: " + appModule.getJarLocation() + ", fragment: " + persistenceFragmentUrl.toString() + ". Exception: " + e1.getMessage(), e1);
            }
        }
    }
    return appModule;
}
Also used : URL(java.net.URL) InputSource(org.xml.sax.InputSource) OpenEJBException(org.apache.openejb.OpenEJBException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Persistence(org.apache.openejb.jee.jpa.unit.Persistence) PersistenceUnitFragment(org.apache.openejb.jee.jpa.fragment.PersistenceUnitFragment) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) PersistenceFragment(org.apache.openejb.jee.jpa.fragment.PersistenceFragment) List(java.util.List) File(java.io.File)

Example 14 with Persistence

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

the class OutputGeneratedDescriptors method writeGenratedCmpMappings.

private void writeGenratedCmpMappings(final AppModule appModule) {
    for (final PersistenceModule persistenceModule : appModule.getPersistenceModules()) {
        try {
            final Persistence persistence = persistenceModule.getPersistence();
            if (hasCmpPersistenceUnit(persistence)) {
                final File tempFile = tempFile("persistence-", ".xml");
                final OutputStream out = IO.write(tempFile);
                try {
                    JpaJaxbUtil.marshal(Persistence.class, persistence, out);
                    logger.info("Dumping Generated CMP persistence.xml to: " + tempFile.getAbsolutePath());
                } catch (final JAXBException e) {
                // no-op
                } finally {
                    IO.close(out);
                }
            }
        } catch (final IOException e) {
        // no-op
        }
    }
    try {
        final File tempFile = tempFile("openejb-cmp-generated-orm-", ".xml");
        final OutputStream out = IO.write(tempFile);
        try {
            JpaJaxbUtil.marshal(EntityMappings.class, appModule.getCmpMappings(), out);
            logger.info("Dumping Generated CMP mappings.xml to: " + tempFile.getAbsolutePath());
        } catch (final JAXBException e) {
        // no-op
        } finally {
            IO.close(out);
        }
    } catch (final IOException e) {
    // no-op
    }
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) OutputStream(java.io.OutputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) File(java.io.File)

Example 15 with Persistence

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

the class AutoConfigPersistenceUnitsTest method testFromWebAppContextThirdParty.

/**
     * Existing data source "orange-web", not controlled by us
     * <p/>
     * Application contains a web module with root context path as "orange-web"
     * <p/>
     * Persistence xml like so:
     * <p/>
     * <persistence-unit name="orange-unit" />
     * <p/>
     * The orange-unit app should automatically use orange-web data source and the non-jta-datasource should be null
     *
     * @throws Exception
     */
public void testFromWebAppContextThirdParty() throws Exception {
    final ResourceInfo supplied = addDataSource("orange-web", 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, "war", "orange-web"));
    // 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)

Aggregations

Persistence (org.apache.openejb.jee.jpa.unit.Persistence)43 PersistenceUnit (org.apache.openejb.jee.jpa.unit.PersistenceUnit)34 Module (org.apache.openejb.testing.Module)18 AppInfo (org.apache.openejb.assembler.classic.AppInfo)10 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)9 EjbJar (org.apache.openejb.jee.EjbJar)7 WebApp (org.apache.openejb.jee.WebApp)7 URL (java.net.URL)5 ArrayList (java.util.ArrayList)5 Assembler (org.apache.openejb.assembler.classic.Assembler)5 Properties (java.util.Properties)4 PersistenceUnit (javax.persistence.PersistenceUnit)4 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)4 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)4 File (java.io.File)3 PersistenceUnitInfo (org.apache.openejb.assembler.classic.PersistenceUnitInfo)3 StatelessBean (org.apache.openejb.jee.StatelessBean)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2