/** * Return the specified PersistenceUnitInfo from this manager's cache of processed persistence * units, keeping it in the cache (i.e. not 'obtaining' it for use but rather just accessing it * for post-processing). * * <p>This can be used in {@link #postProcessPersistenceUnitInfo} implementations, detecting * existing persistence units of the same name and potentially merging them. * * @param persistenceUnitName the name of the desired persistence unit * @return the PersistenceUnitInfo in mutable form, or <code>null</code> if not available */ protected final MutablePersistenceUnitInfo getPersistenceUnitInfo(String persistenceUnitName) { PersistenceUnitInfo pui = this.persistenceUnitInfos.get(persistenceUnitName); if (pui != null && Proxy.isProxyClass(pui.getClass())) { // JPA 2.0 PersistenceUnitInfo decorator with a SpringPersistenceUnitInfo as target Jpa2PersistenceUnitInfoDecorator dec = (Jpa2PersistenceUnitInfoDecorator) Proxy.getInvocationHandler(pui); return dec.getTarget(); } else { // Must be a raw JPA 1.0 SpringPersistenceUnitInfo instance return (MutablePersistenceUnitInfo) pui; } }
@Override @SuppressWarnings("unchecked") public EntityManagerFactory createContainerEntityManagerFactory( PersistenceUnitInfo info, Map properties) { final Map settings = generateSettings(properties); // OSGi ClassLoaders must implement BundleReference settings.put( org.hibernate.jpa.AvailableSettings.SCANNER, new OsgiScanner(((BundleReference) info.getClassLoader()).getBundle())); osgiClassLoader.addClassLoader(info.getClassLoader()); return Bootstrap.getEntityManagerFactoryBuilder(info, settings, osgiClassLoader).build(); }
/** * 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; }
@Override @SuppressWarnings("unchecked") public EntityManagerFactory createContainerEntityManagerFactory( PersistenceUnitInfo info, Map properties) { properties.put("datanucleus.jpa.addClassTransformer", "false"); properties.put( "datanucleus.plugin.pluginRegistryClassName", "org.datanucleus.plugin.OSGiPluginRegistry"); info.addTransformer( new ClassTransformer() { @Override public byte[] transform( ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { // TODO Auto-generated method stub return null; } }); return pp.createContainerEntityManagerFactory(info, properties); }
// Create an EMF through the path a container normally takes - calling // createContainerEntityManagerFactory directly on the persistence impl. @SuppressWarnings("unchecked") protected OpenJPAEntityManagerFactorySPI createContainerEMF( final String pu, final String persistenceFile, Map<String, Object> map) { List<Class<?>> clist = null; OpenJPAEntityManagerFactorySPI oemf = null; Map<String, Object> config = new HashMap(System.getProperties()); if (map != null) { config.putAll(map); // Get the persistent class list clist = (List<Class<?>>) map.remove(PERSISTENT_CLASS_LIST); } PersistenceProductDerivation.ConfigurationParser cfgParser = new PersistenceProductDerivation.ConfigurationParser(config); try { URL url = getResourceURL(persistenceFile); cfgParser.parse(url); List<PersistenceUnitInfoImpl> units = cfgParser.getResults(); PersistenceUnitInfo puinf = null; // Find the pu info that matches the pu name for (PersistenceUnitInfo pui : units) { if (pu.equals(pui.getPersistenceUnitName())) { puinf = pui; break; } } // If there is a persistent class list, add each class to the puinfo if (clist != null) { for (Class<?> cl : clist) { ((PersistenceUnitInfoImpl) puinf).addManagedClassName(cl.getName()); } } oemf = createContainerEMF(pu, puinf, config); } catch (IOException ioe) { throw new RuntimeException("Failed to parse: " + getPersistenceResourceName(), ioe); } if (oemf == null) { throw new NullPointerException( "Expected an entity manager factory " + "for the persistence unit named: \"" + pu + "\""); } return oemf; }
/** * In case persistence unit is not uniquely defined by name this method is called to generate a * unique name. */ public String createUniquePersistenceUnitName(PersistenceUnitInfo puInfo) { return PersistenceUnitProcessor.buildPersistenceUnitName( puInfo.getPersistenceUnitRootUrl(), puInfo.getPersistenceUnitName()); }