/** * Get a list of persitence units from the file or directory at the given * url. PersistenceUnits are built based on the presence of persistence.xml * in a META-INF directory at the base of the URL. * @param archive The url of a jar file or directory to check */ public static List<SEPersistenceUnitInfo> getPersistenceUnits(Archive archive, ClassLoader loader){ return processPersistenceArchive(archive, loader); }
/** * Go through the jar file for this PeristeneUnitProcessor and process any * XML provided in it. */ public static List<SEPersistenceUnitInfo> processPersistenceArchive(Archive archive, ClassLoader loader){ URL puRootURL = archive.getRootURL(); try { InputStream pxmlStream = archive.getEntry("META-INF/persistence.xml"); // NOI18N return processPersistenceXML(puRootURL, pxmlStream, loader); } catch (IOException e) { throw PersistenceUnitLoadingException.exceptionLoadingFromUrl(puRootURL.toString(), e); } }
/** * Search the classpath for persistence archives. A persistence archive is * defined as any part of the class path that contains a META-INF directory * with a persistence.xml file in it. Return a list of the URLs of those * files. Use the current thread's context classloader to get the classpath. * We assume it is a URL class loader. */ public static Set<Archive> findPersistenceArchives(){ ClassLoader threadLoader = Thread.currentThread().getContextClassLoader(); return findPersistenceArchives(threadLoader); }
result = new DirectoryArchive(f); } else { result = new JarFileArchive(new JarFile(f)); result = new JarFileArchive(conn.getJarFile()); } else if (je.isDirectory()) { result = new DirectoryInsideJarURLArchive(url); } else { result = new JarInputStreamURLArchive(url); } else if (isJarInputStream(url)){ result = new JarInputStreamURLArchive(url); } else { result = new URLArchive(url);
/** * 永続ユニット情報から抽象永続ユニット名を返します。 * <p> * 永続ユニット情報に抽象永続ユニット名が定義されていない場合は通常の永続ユニット名を抽象永続ユニット名として返します。 * </p> * * @param unitInfo * 永続ユニット情報 * @return 抽象永続ユニット名 */ protected String getAbstractUnitName(final SEPersistenceUnitInfo unitInfo) { final Properties props = unitInfo.getProperties(); if (props != null && props.containsKey(ABSTRACT_UNIT_NAME)) { return props.getProperty(ABSTRACT_UNIT_NAME); } return unitInfo.getPersistenceUnitName(); }
public static Set<String> getClassNamesFromURL(URL url) { Set<String> classNames = new HashSet<String>(); Archive archive = null; try { archive = new ArchiveFactoryImpl().createArchive(url); } catch (URISyntaxException e) { throw new RuntimeException("url = [" + url + "]", e); // NOI18N } catch (IOException e) { throw new RuntimeException("url = [" + url + "]", e); // NOI18N } for (Iterator<String> entries = archive.getEntries(); entries.hasNext();) { String entry = entries.next(); if (entry.endsWith(".class")){ // NOI18N classNames.add(buildClassNameFromEntryString(entry)); } } return classNames; }
/** * This method will search for all of the persistence archives on the classpath and weave * all the classes associated with those persistence units. */ public static void main(String[] args) { Set<Archive> pars = PersistenceUnitProcessor.findPersistenceArchives(); Iterator<Archive> parIterator = pars.iterator(); while (parIterator.hasNext()){ List<SEPersistenceUnitInfo> puInfoList = PersistenceUnitProcessor.getPersistenceUnits(parIterator.next(), Thread.currentThread().getContextClassLoader()); Iterator<SEPersistenceUnitInfo> puInfoListIterator = puInfoList.iterator(); while (puInfoListIterator.hasNext()){ Persistence.createEntityManagerFactory(puInfoListIterator.next().getPersistenceUnitName()); } } }
archive = (new ArchiveFactoryImpl()).createArchive(inputArchiveURL); }catch(ZipException e){ throw StaticWeaveException.exceptionOpeningArchive(inputArchiveURL,e); PersistenceUnitProcessor.processPersistenceArchive(archive, aclassloader); if(persistenceUnitsList==null){ throw PersistenceUnitLoadingException.couldNotGetUnitInfoFromUrl(inputArchiveURL); while (persistenceUnitsIterator.hasNext()){ SEPersistenceUnitInfo unitInfo = (SEPersistenceUnitInfo)persistenceUnitsIterator.next(); unitInfo.setNewTempClassLoader(aclassloader);
/** * INTERNAL: * Process the given persistence unit class name. */ private void processPersistenceUnitClasses(Collection<String> classNames, Set<String> entityClassNames, Set<String> weavableClassNames) { for (String className: classNames) { Class candidateClass = PersistenceUnitProcessor.loadClass(className, m_loader, true); if (PersistenceUnitProcessor.isEntity(candidateClass)) { entityClassNames.add(className); weavableClassNames.add(className); } else if (PersistenceUnitProcessor.isEmbeddable(candidateClass)) { weavableClassNames.add(className); } } }
/** * Search the classpath for persistence archives. A persistence archive is * defined as any part of the class path that contains a META-INF directory * with a persistence.xml file in it. Return a list of {@link Archive} * representing the root of those files. * @param loader the class loader to get the class path from */ public static Set<Archive> findPersistenceArchives(ClassLoader loader){ Set<Archive> pars = new HashSet<Archive>(); try { Enumeration<URL> resources = loader.getResources("META-INF/persistence.xml"); while (resources.hasMoreElements()){ URL pxmlURL = resources.nextElement(); URL puRootURL = computePURootURL(pxmlURL); Archive archive = new ArchiveFactoryImpl().createArchive(puRootURL); pars.add(archive); } } catch (java.io.IOException exc){ throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(loader, exc); } catch (URISyntaxException exc) { throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(loader, exc); } return pars; }
/** * 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, ClassLoader loader){ Set<String> set = new HashSet<String>(); set.addAll(persistenceUnitInfo.getManagedClassNames()); Iterator i = persistenceUnitInfo.getJarFileUrls().iterator(); while (i.hasNext()) { set.addAll(getClassNamesFromURL((URL)i.next())); } if (!persistenceUnitInfo.excludeUnlistedClasses()){ set.addAll(getClassNamesFromURL(persistenceUnitInfo.getPersistenceUnitRootUrl())); } set.addAll(buildPersistentClassSetFromXMLDocuments(persistenceUnitInfo, loader)); return set; }
/** * 指定されたpersistenceUnitInfoにSeasar2の自動登録情報を追加し、親クラスの処理を実行します。 * @see oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer#callPredeploy(oracle.toplink.essentials.ejb.cmp3.persistence.SEPersistenceUnitInfo, java.util.Map, oracle.toplink.essentials.internal.ejb.cmp3.PersistenceInitializationActivator) */ @Override @SuppressWarnings("unchecked") protected boolean callPredeploy(SEPersistenceUnitInfo persistenceUnitInfo, Map m, PersistenceInitializationActivator persistenceActivator) { final String abstractUnitName = getAbstractUnitName(persistenceUnitInfo); addMappingFiles(abstractUnitName, persistenceUnitInfo); addPersistenceClasses(abstractUnitName, persistenceUnitInfo); if (JavaSECMPInitializer.globalInstrumentation == null) { JavaSECMPInitializer.globalInstrumentation = new InstrumentationImpl( persistenceUnitInfo.getManagedClassNames()); } return super.callPredeploy(persistenceUnitInfo, m, persistenceActivator); }
public InputStream getEntry(String entryPath) throws IOException { File f = getFile(entryPath); InputStream is = f.exists() ? new FileInputStream(f) : null; return is; }
private void init(File top, File directory) { File[] dirFiles = directory.listFiles(); for (File file : dirFiles) { if (file.isDirectory()) { continue; // exclude dir entries } // add only the relative path from the top. // note: we use unix style path String entryName = file.getPath().replace(File.separator, "/") // NOI18N .substring(top.getPath().length() + 1); entries.add(entryName); } File[] subDirs = directory.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory(); } }); for (File subDir : subDirs) { init(top, subDir); // recursion } }
public JarInputStreamURLArchive(URL url, Logger logger) throws IOException { logger.entering("JarInputStreamURLArchive", "JarInputStreamURLArchive", // NOI18N new Object[]{url}); this.logger = logger; this.url = url; init(); }
/** * Initialize one persistence unit. * Initialization is a two phase process. First the predeploy process builds the metadata * and creates any required transformers. * Second the deploy process creates a TopLink session based on that metadata. */ protected void initPersistenceUnits(Archive archive, Map m, PersistenceInitializationActivator persistenceActivator) { Iterator<SEPersistenceUnitInfo> persistenceUnits = PersistenceUnitProcessor.getPersistenceUnits(archive, sessionClassLoader).iterator(); while (persistenceUnits.hasNext()){ SEPersistenceUnitInfo persistenceUnitInfo = persistenceUnits.next(); callPredeploy(persistenceUnitInfo, m, persistenceActivator); } }
public DirectoryInsideJarURLArchive(URL url, Logger logger) throws IOException { logger.entering("DirectoryInsideJarURLArchive", "DirectoryInsideJarURLArchive", // NOI18N new Object[]{url}); this.logger = logger; assert(url.getProtocol().equals("jar")); // NOI18N rootURL = url; JarURLConnection conn = JarURLConnection.class.cast(url.openConnection()); jarFile = conn.getJarFile(); logger.logp(Level.FINER, "DirectoryInsideJarURLArchive", "DirectoryInsideJarURLArchive", "jarFile = {0}", jarFile); relativeRootPath = conn.getEntryName(); init(); }
/** * INTERNAL * This method initializes the container. Essentially, it will try to load the * class that contains the list of entities and reflectively call the method that * contains that list. It will then initialize the container with that list. * If succeeded return true, false otherwise. */ public void initialize(Map m, PersistenceInitializationActivator persistenceActivator) { sessionClassLoader = getMainLoader(); final Set<Archive> pars = PersistenceUnitProcessor.findPersistenceArchives(); for (Archive archive: pars){ AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_initialize", archive); initPersistenceUnits(archive, m, persistenceActivator); } }
public URL getEntryAsURL(String entryPath) throws IOException { File f = getFile(entryPath); URL url = f.exists() ? f.toURI().toURL() : null; return url; }
public DirectoryArchive(File directory, Logger logger) throws MalformedURLException { logger.entering("DirectoryArchive", "DirectoryArchive", new Object[]{directory}); this.logger = logger; if (!directory.isDirectory()) { // should never reach here, hence the msg is not internationalized. throw new IllegalArgumentException(directory + " is not a directory." + // NOI18N "If it is a jar file, then use JarFileArchive."); // NOI18N } this.directory = directory; rootURL = directory.toURI().toURL(); logger.logp(Level.FINER, "DirectoryArchive", "DirectoryArchive", "rootURL = {0}", rootURL); init(this.directory, this.directory); // initialize entries }