Refine search
boolean weaverRegistered = true; for (PersistenceUnitInfo pui : mergedPus.values()) { if (pui.getProperties().containsKey(AvailableSettings.USE_CLASS_ENHANCER) && "true".equalsIgnoreCase(pui.getProperties().getProperty(AvailableSettings.USE_CLASS_ENHANCER))) { pui.addTransformer(new InterceptFieldClassFileTransformer(pui.getManagedClassNames())); boolean isTransformerQualified = !(transformer instanceof NullClassTransformer) && pui.getPersistenceUnitName().equals("blPU") && !(transformer instanceof BroadleafPersistenceUnitDeclaringClassTransformer) ) || pui.getPersistenceUnitName().equals(((BroadleafPersistenceUnitDeclaringClassTransformer) transformer).getPersistenceUnitName()) ); if (isTransformerQualified) { pui.addTransformer(transformer);
assertEquals("pu1", pu1.getPersistenceUnitName()); assertEquals("com.acme.AcmePersistence", pu1.getPersistenceProviderClassName()); assertEquals(1, pu1.getMappingFileNames().size()); assertEquals("ormap2.xml", pu1.getMappingFileNames().get(0)); assertEquals(1, pu1.getJarFileUrls().size()); assertEquals(new ClassPathResource("order.jar").getURL(), pu1.getJarFileUrls().get(0)); assertFalse(pu1.excludeUnlistedClasses()); assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, pu1.getTransactionType()); Properties props = pu1.getProperties(); assertEquals(2, props.keySet().size()); assertEquals("on", props.getProperty("com.acme.persistence.sql-logging")); assertEquals("bar", props.getProperty("foo")); assertNull(pu1.getNonJtaDataSource()); assertSame(ds, pu1.getJtaDataSource()); assertFalse("Exclude unlisted should default false in 1.0.", pu1.excludeUnlistedClasses()); assertSame(PersistenceUnitTransactionType.JTA, pu2.getTransactionType()); assertEquals("com.acme.AcmePersistence", pu2.getPersistenceProviderClassName()); assertEquals(1, pu2.getMappingFileNames().size());
@Override public ClassLoader getClassLoader() { return persistenceUnitInfo.getClassLoader(); }
@Override public DataSource getDataSource() { if (this.persistenceUnitInfo != null) { return (this.persistenceUnitInfo.getJtaDataSource() != null ? this.persistenceUnitInfo.getJtaDataSource() : this.persistenceUnitInfo.getNonJtaDataSource()); } return (this.internalPersistenceUnitManager.getDefaultJtaDataSource() != null ? this.internalPersistenceUnitManager.getDefaultJtaDataSource() : this.internalPersistenceUnitManager.getDefaultDataSource()); }
@Override protected EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException { Assert.state(this.persistenceUnitInfo != null, "PersistenceUnitInfo not initialized"); PersistenceProvider provider = getPersistenceProvider(); if (provider == null) { String providerClassName = this.persistenceUnitInfo.getPersistenceProviderClassName(); if (providerClassName == null) { throw new IllegalArgumentException( "No PersistenceProvider specified in EntityManagerFactory configuration, " + "and chosen PersistenceUnitInfo does not specify a provider class name either"); } Class<?> providerClass = ClassUtils.resolveClassName(providerClassName, getBeanClassLoader()); provider = (PersistenceProvider) BeanUtils.instantiateClass(providerClass); } if (logger.isDebugEnabled()) { logger.debug("Building JPA container EntityManagerFactory for persistence unit '" + this.persistenceUnitInfo.getPersistenceUnitName() + "'"); } EntityManagerFactory emf = provider.createContainerEntityManagerFactory(this.persistenceUnitInfo, getJpaPropertyMap()); postProcessEntityManagerFactory(emf, this.persistenceUnitInfo); return emf; }
public Ejb3Configuration getConfiguration() { synchronized(this) { if (configuration == null) { Ejb3Configuration temp = new Ejb3Configuration(); String previousValue = persistenceUnitInfo.getProperties().getProperty("hibernate.hbm2ddl.auto"); persistenceUnitInfo.getProperties().setProperty("hibernate.hbm2ddl.auto", "none"); configuration = temp.configure(persistenceUnitInfo, new HashMap()); configuration.getHibernateConfiguration().buildSessionFactory(); if (previousValue != null) { persistenceUnitInfo.getProperties().setProperty("hibernate.hbm2ddl.auto", previousValue); } } } return configuration; }
public Ejb3Configuration configure(String resource) throws HibernateException { Thread thread = null; ClassLoader contextClassLoader = null; if (overridenClassLoader != null) { thread = Thread.currentThread(); contextClassLoader = thread.getContextClassLoader(); thread.setContextClassLoader( overridenClassLoader ); } try { Properties properties = new Properties(); properties.setProperty( HibernatePersistence.CFG_FILE, resource); configure( properties, new HashMap() ); return this; } finally { if (thread != null) thread.setContextClassLoader( contextClassLoader ); } }
sb.append( "PersistenceUnitInfo [\n\t" ) .append( "name: " ) .append( unitInfo.getPersistenceUnitName() ) .append( "\n\t" ) .append( "persistence provider classname: " ) .append( unitInfo.getPersistenceProviderClassName() ) .append( "\n\t" ) .append( "classloader: " ) .append( unitInfo.getClassLoader() ) .append( "\n\t" ) .append( "Temporary classloader: " ) .append( unitInfo.getNewTempClassLoader() ) .append( "\n\t" ) .append( "excludeUnlistedClasses: " ) .append( unitInfo.excludeUnlistedClasses() ) .append( "\n\t" ) .append( "JTA datasource: " ) .append( unitInfo.getJtaDataSource() ) .append( "\n\t" ) .append( "Non JTA datasource: " ) .append( unitInfo.getNonJtaDataSource() ) .append( "\n\t" ) .append( "Transaction type: " ) .append( unitInfo.getTransactionType() ) .append( "\n\t" ) .append( "PU root URL: " ) .append( unitInfo.getPersistenceUnitRootUrl() ) .append( "\n\t" ); sb.append( "Jar files URLs [" );
/** * Creates an EntityManagerFactory for a persistence unit specified by the property overrides. The EMF is created by parsing all persistence.xml files on * the classpath. * * @param overrides persistence unit property overrides * @param classLoader the persistence unit classloader * @return the entity manager factory * @throws Fabric3Exception if there is an error creating the factory */ private EntityManagerFactory createEntityManagerFactory(PersistenceOverrides overrides, ClassLoader classLoader) throws Fabric3Exception { List<PersistenceUnitInfo> infos = parser.parse(classLoader); String unitName = overrides.getUnitName(); for (PersistenceUnitInfo info : infos) { if (!unitName.equals(info.getPersistenceUnitName())) { // Not the most efficient approach: parse all of the persistence units and only keep the one we are requested in, potentially // resulting in parsing the units multiple times. // This must be done since the overrides may not be loaded for all units continue; } Properties unitProperties = info.getProperties(); unitProperties.setProperty(HIBERNATE_LOOKUP, F3TransactionManagerLookup.class.getName()); unitProperties.putAll(overrides.getProperties()); PersistenceUnitInfoDescriptor descriptor = new PersistenceUnitInfoDescriptor(info); EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilderImpl(descriptor, Collections.emptyMap(), classLoader); return builder.build(); } throw new Fabric3Exception("Persistence unit not defined for: " + unitName); }
public EntityManagerFactoryImpl(final PersistenceUnitInfo info, final Keyspace space, final AnnotationScanner scanner) { if (!info.excludeUnlistedClasses()) { final Class<?>[] classes = new Class<?>[info.getManagedClassNames().size()]; final ClassLoader cl = Thread.currentThread().getContextClassLoader(); int i = 0; for (String name : info.getManagedClassNames()) { try { classes[i++] = cl.loadClass(name); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } em = new EntityManagerImpl(space, this, classes); } else { em = new EntityManagerImpl(space, this, info.getProperties().getProperty(PACKAGES_TO_SCAN).split(","), scanner); } }
String unitName = overrides.getUnitName(); for (PersistenceUnitInfo info : infos) { if (!unitName.equals(info.getPersistenceUnitName())) { DataSource dataSource = info.getJtaDataSource(); if (dataSource == null) { dataSource = info.getNonJtaDataSource(); unitProperties.setProperty(HIBERNATE_LOOKUP, F3TransactionManagerLookup.class.getName()); unitProperties.putAll(overrides.getProperties()); cfg.configure(info, Collections.emptyMap()); return cfg.buildEntityManagerFactory();
/** * @param puInfo * the persistence unit info * @param properties * the properties of the persistence unit * * @since 2.0.0 */ public PersistenceParserImpl(PersistenceUnitInfo puInfo, Map<String, Object> properties) { super(); this.puInfo = puInfo; this.provider = puInfo.getPersistenceProviderClassName(); if (puInfo.getProperties() != null) { for (final Entry<Object, Object> entry : puInfo.getProperties().entrySet()) { this.properties.put((String) entry.getKey(), entry.getValue()); } } if (properties != null) { for (final Entry<String, Object> entry : properties.entrySet()) { this.properties.put(entry.getKey(), entry.getValue()); } } this.hasValidators = this.createHasValidators(puInfo); this.metadata = new MetadataImpl(); this.ormMappingFiles = puInfo.getMappingFileNames(); this.parseOrmXmls(); this.metadata.parse(puInfo); }
@Test public void testExample2() throws Exception { PersistenceUnitReader reader = new PersistenceUnitReader( new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup()); String resource = "/org/springframework/orm/jpa/persistence-example2.xml"; PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource); assertNotNull(info); assertEquals(1, info.length); assertEquals("OrderManagement2", info[0].getPersistenceUnitName()); assertEquals(1, info[0].getMappingFileNames().size()); assertEquals("mappings.xml", info[0].getMappingFileNames().get(0)); assertEquals(0, info[0].getProperties().keySet().size()); assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses()); }
@Test public void testExample5() throws Exception { PersistenceUnitReader reader = new PersistenceUnitReader( new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup()); String resource = "/org/springframework/orm/jpa/persistence-example5.xml"; PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource); assertNotNull(info); assertEquals(1, info.length); assertEquals("OrderManagement5", info[0].getPersistenceUnitName()); assertEquals(2, info[0].getMappingFileNames().size()); assertEquals("order1.xml", info[0].getMappingFileNames().get(0)); assertEquals("order2.xml", info[0].getMappingFileNames().get(1)); assertEquals(2, info[0].getJarFileUrls().size()); assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0)); assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1)); assertEquals("com.acme.AcmePersistence", info[0].getPersistenceProviderClassName()); assertEquals(0, info[0].getProperties().keySet().size()); assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses()); }
@Test public void testExample3() throws Exception { PersistenceUnitReader reader = new PersistenceUnitReader( new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup()); String resource = "/org/springframework/orm/jpa/persistence-example3.xml"; PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource); assertNotNull(info); assertEquals(1, info.length); assertEquals("OrderManagement3", info[0].getPersistenceUnitName()); assertEquals(2, info[0].getJarFileUrls().size()); assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0)); assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1)); assertEquals(0, info[0].getProperties().keySet().size()); assertNull(info[0].getJtaDataSource()); assertNull(info[0].getNonJtaDataSource()); assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses()); }
@Test public void testExample4() throws Exception { SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder(); DataSource ds = new DriverManagerDataSource(); builder.bind("java:comp/env/jdbc/MyDB", ds); PersistenceUnitReader reader = new PersistenceUnitReader( new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup()); String resource = "/org/springframework/orm/jpa/persistence-example4.xml"; PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource); assertNotNull(info); assertEquals(1, info.length); assertEquals("OrderManagement4", info[0].getPersistenceUnitName()); assertEquals(1, info[0].getMappingFileNames().size()); assertEquals("order-mappings.xml", info[0].getMappingFileNames().get(0)); assertEquals(3, info[0].getManagedClassNames().size()); assertEquals("com.acme.Order", info[0].getManagedClassNames().get(0)); assertEquals("com.acme.Customer", info[0].getManagedClassNames().get(1)); assertEquals("com.acme.Item", info[0].getManagedClassNames().get(2)); assertTrue("Exclude unlisted should be true when no value.", info[0].excludeUnlistedClasses()); assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, info[0].getTransactionType()); assertEquals(0, info[0].getProperties().keySet().size()); builder.clear(); }
metadata.setTransactionType(puInfo.getTransactionType()); metadata.getClasses().addAll(puInfo.getManagedClassNames()); metadata.setExcludeUnlistedClasses(puInfo.excludeUnlistedClasses()); metadata.getProperties().putAll(puInfo.getProperties()); if (puInfo.getPersistenceProviderClassName() == null || PROVIDER_IMPLEMENTATION_NAME.equalsIgnoreCase(puInfo.getPersistenceProviderClassName())) metadata.setProvider(puInfo.getPersistenceProviderClassName()); + puInfo.getPersistenceProviderClassName() + ", persistence provider must be " + PROVIDER_IMPLEMENTATION_NAME + "."); metadata.getPackages().addAll(puInfo.getMappingFileNames()); for (URL url : puInfo.getJarFileUrls())
/** * Build a set that contains all the class names at a URL. * @return a Set of class name strings */ public static Set<String> buildClassSet(PersistenceUnitInfo persistenceUnitInfo, Map properties){ Set<String> set = new HashSet<String>(); set.addAll(persistenceUnitInfo.getManagedClassNames()); ClassLoader loader = persistenceUnitInfo.getClassLoader(); Iterator i = persistenceUnitInfo.getJarFileUrls().iterator(); while (i.hasNext()) { set.addAll(getClassNamesFromURL((URL)i.next(), loader, properties)); } if (!persistenceUnitInfo.excludeUnlistedClasses()){ set.addAll(getClassNamesFromURL(persistenceUnitInfo.getPersistenceUnitRootUrl(), loader, properties)); } // No longer need to add classes from XML, as temp class loader is only used for sessions.xml. return set; }
private AmberPersistenceUnit init(PersistenceUnitInfo info) throws Exception { String name = info.getPersistenceUnitName(); AmberPersistenceUnit unit = new AmberPersistenceUnit(this, name); unit.setJPA(true); if (info.getJtaDataSource() != null) unit.setJtaDataSource(info.getJtaDataSource()); if (info.getNonJtaDataSource() != null) unit.setNonJtaDataSource(info.getNonJtaDataSource()); // unit.setEntityMappingsList(entityMappings); unit.init(); ClassLoader tempLoader = info.getNewTempClassLoader(); for (String className : info.getManagedClassNames()) { Class type = Class.forName(className, false, tempLoader); unit.addEntityClass(className, type);//JClassWrapper.create(type)); } unit.generate(); return unit; }
public void setPersistenceEnvironmentInfo(OpenJPAConfiguration conf, PersistenceUnitInfo pui) { // OPENJPA-1460 Fix scope visibility of orm.xml when it is packaged in both ear file and war file if (conf instanceof OpenJPAConfigurationImpl) { Map<String, Object> peMap =((OpenJPAConfigurationImpl)conf).getPersistenceEnvironment(); if (peMap == null) { peMap = new HashMap<String, Object>(); ((OpenJPAConfigurationImpl)conf).setPersistenceEnvironment(peMap); } peMap.put(AbstractCFMetaDataFactory.PERSISTENCE_UNIT_ROOT_URL, pui.getPersistenceUnitRootUrl()); peMap.put(AbstractCFMetaDataFactory.MAPPING_FILE_NAMES, pui.getMappingFileNames()); peMap.put(AbstractCFMetaDataFactory.JAR_FILE_URLS, pui.getJarFileUrls()); } }