Index: src/org/eclipse/persistence/tools/weaving/jpa/StaticWeaveProcessor.java =================================================================== --- src/org/eclipse/persistence/tools/weaving/jpa/StaticWeaveProcessor.java (revision 5435) +++ src/org/eclipse/persistence/tools/weaving/jpa/StaticWeaveProcessor.java (working copy) @@ -239,85 +239,89 @@ // Starting process. Archive sourceArchive =(new ArchiveFactoryImpl()).createArchive(source); - Iterator entries = sourceArchive.getEntries(); - while (entries.hasNext()){ - String entryName = (String)entries.next(); - InputStream entryInputStream = sourceArchive.getEntry(entryName); - - // Add a directory entry - swoh.addDirEntry(getDirectoryFromEntryName(entryName)); - - // Add a regular entry - JarEntry newEntry = new JarEntry(entryName); - - // Ignore non-class files. - if (!(entryName.endsWith(".class"))) { - swoh.addEntry(entryInputStream, newEntry); - continue; - } - - String className = PersistenceUnitProcessor.buildClassNameFromEntryString(entryName) ; - - byte[] originalClassBytes=null; - byte[] transferredClassBytes=null; - try { - Class thisClass = this.classLoader.loadClass(className); - // If the class is not in the classpath, we simply copy the entry - // to the target(no weaving). - if (thisClass == null){ + try { + Iterator entries = sourceArchive.getEntries(); + while (entries.hasNext()){ + String entryName = (String)entries.next(); + InputStream entryInputStream = sourceArchive.getEntry(entryName); + + // Add a directory entry + swoh.addDirEntry(getDirectoryFromEntryName(entryName)); + + // Add a regular entry + JarEntry newEntry = new JarEntry(entryName); + + // Ignore non-class files. + if (!(entryName.endsWith(".class"))) { swoh.addEntry(entryInputStream, newEntry); - continue; + continue; } - // Try to read the loaded class bytes, the class bytes is required for - // classtransformer to perform transfer. Simply copy entry to the target(no weaving) - // if the class bytes can't be read. - InputStream is = this.classLoader.getResourceAsStream(entryName); - if (is!=null){ - ByteArrayOutputStream baos = null; - try{ - baos = new ByteArrayOutputStream(); - byte[] bytes = new byte[NUMBER_OF_BYTES]; - int bytesRead = is.read(bytes, 0, NUMBER_OF_BYTES); - while (bytesRead >= 0){ - baos.write(bytes, 0, bytesRead); - bytesRead = is.read(bytes, 0, NUMBER_OF_BYTES); + String className = PersistenceUnitProcessor.buildClassNameFromEntryString(entryName) ; + + byte[] originalClassBytes=null; + byte[] transferredClassBytes=null; + try { + Class thisClass = this.classLoader.loadClass(className); + // If the class is not in the classpath, we simply copy the entry + // to the target(no weaving). + if (thisClass == null){ + swoh.addEntry(entryInputStream, newEntry); + continue; + } + + // Try to read the loaded class bytes, the class bytes is required for + // classtransformer to perform transfer. Simply copy entry to the target(no weaving) + // if the class bytes can't be read. + InputStream is = this.classLoader.getResourceAsStream(entryName); + if (is!=null){ + ByteArrayOutputStream baos = null; + try{ + baos = new ByteArrayOutputStream(); + byte[] bytes = new byte[NUMBER_OF_BYTES]; + int bytesRead = is.read(bytes, 0, NUMBER_OF_BYTES); + while (bytesRead >= 0){ + baos.write(bytes, 0, bytesRead); + bytesRead = is.read(bytes, 0, NUMBER_OF_BYTES); + } + originalClassBytes = baos.toByteArray(); + } finally { + baos.close(); } - originalClassBytes = baos.toByteArray(); - } finally { - baos.close(); + } else { + swoh.addEntry(entryInputStream, newEntry); + continue; } - } else { + + // If everything is OK so far, we perform the weaving. we need three parameters in order to + // class to perform weaving for that class, the class name,the class object and class bytes. + transferredClassBytes = classTransformer.transform(className.replace('.', '/'), thisClass, originalClassBytes); + + // If transferredClassBytes is null means the class dose not get woven. + if (transferredClassBytes!=null){ + swoh.addEntry(newEntry, transferredClassBytes); + } else { + swoh.addEntry(entryInputStream, newEntry); + } + } catch (IllegalClassFormatException e) { + AbstractSessionLog.getLog().logThrowable(AbstractSessionLog.WARNING, e); + // Anything went wrong, we need log a warning message, copy the entry to the target and + // process next entry. swoh.addEntry(entryInputStream, newEntry); continue; - } - - // If everything is OK so far, we perform the weaving. we need three parameters in order to - // class to perform weaving for that class, the class name,the class object and class bytes. - transferredClassBytes = classTransformer.transform(className.replace('.', '/'), thisClass, originalClassBytes); - - // If transferredClassBytes is null means the class dose not get woven. - if (transferredClassBytes!=null){ - swoh.addEntry(newEntry, transferredClassBytes); - } else { + } catch (ClassNotFoundException e) { + AbstractSessionLog.getLog().logThrowable(AbstractSessionLog.WARNING, e); swoh.addEntry(entryInputStream, newEntry); + continue; + } finally { + // Need close the inputstream for current entry before processing next one. + entryInputStream.close(); } - } catch (IllegalClassFormatException e) { - AbstractSessionLog.getLog().logThrowable(AbstractSessionLog.WARNING, e); - // Anything went wrong, we need log a warning message, copy the entry to the target and - // process next entry. - swoh.addEntry(entryInputStream, newEntry); - continue; - } catch (ClassNotFoundException e) { - AbstractSessionLog.getLog().logThrowable(AbstractSessionLog.WARNING, e); - swoh.addEntry(entryInputStream, newEntry); - continue; - } finally { - // Need close the inputstream for current entry before processing next one. - entryInputStream.close(); } + } finally { + sourceArchive.close(); + swoh.closeOutputStream(); } - swoh.closeOutputStream(); } Index: src/org/eclipse/persistence/tools/weaving/jpa/StaticWeaveClassTransformer.java =================================================================== --- src/org/eclipse/persistence/tools/weaving/jpa/StaticWeaveClassTransformer.java (revision 5435) +++ src/org/eclipse/persistence/tools/weaving/jpa/StaticWeaveClassTransformer.java (working copy) @@ -106,26 +106,30 @@ } else { classTransformers = new ArrayList(); } - Archive archive =null; + Archive archive = null; try { - archive = (new ArchiveFactoryImpl()).createArchive(inputArchiveURL); + archive = (new ArchiveFactoryImpl()).createArchive(inputArchiveURL); + + List persistenceUnitsList = + PersistenceUnitProcessor.processPersistenceArchive(archive, aclassloader); + if (persistenceUnitsList==null) { + throw PersistenceUnitLoadingException.couldNotGetUnitInfoFromUrl(inputArchiveURL); + } + Iterator persistenceUnitsIterator = persistenceUnitsList.iterator(); + while (persistenceUnitsIterator.hasNext()) { + SEPersistenceUnitInfo unitInfo = persistenceUnitsIterator.next(); + unitInfo.setNewTempClassLoader(aclassloader); + //build class transformer. + ClassTransformer transformer = buildTransformer(unitInfo,this.logWriter,this.logLevel); + classTransformers.add(transformer); + } } catch (ZipException e) { throw StaticWeaveException.exceptionOpeningArchive(inputArchiveURL,e); + } finally { + if (archive != null) { + archive.close(); + } } - - List persistenceUnitsList = - PersistenceUnitProcessor.processPersistenceArchive(archive, aclassloader); - if (persistenceUnitsList==null) { - throw PersistenceUnitLoadingException.couldNotGetUnitInfoFromUrl(inputArchiveURL); - } - Iterator persistenceUnitsIterator = persistenceUnitsList.iterator(); - while (persistenceUnitsIterator.hasNext()) { - SEPersistenceUnitInfo unitInfo = persistenceUnitsIterator.next(); - unitInfo.setNewTempClassLoader(aclassloader); - //build class transformer. - ClassTransformer transformer = buildTransformer(unitInfo,this.logWriter,this.logLevel); - classTransformers.add(transformer); - } } /** Index: src/org/eclipse/persistence/internal/jpa/deployment/URLArchive.java =================================================================== --- src/org/eclipse/persistence/internal/jpa/deployment/URLArchive.java (revision 5435) +++ src/org/eclipse/persistence/internal/jpa/deployment/URLArchive.java (working copy) @@ -74,4 +74,9 @@ public URL getRootURL() { return url; } + + public void close() { + // nothing to close. it's caller's responsibility to close + // any InputStream returned by getEntry(). + } } Index: src/org/eclipse/persistence/internal/jpa/deployment/DirectoryArchive.java =================================================================== --- src/org/eclipse/persistence/internal/jpa/deployment/DirectoryArchive.java (revision 5435) +++ src/org/eclipse/persistence/internal/jpa/deployment/DirectoryArchive.java (working copy) @@ -123,5 +123,10 @@ return f; } + public void close() { + // nothing to close. it's caller's responsibility to close + // any InputStream returned by getEntry(). + } + } Index: src/org/eclipse/persistence/internal/jpa/deployment/JarFileArchive.java =================================================================== --- src/org/eclipse/persistence/internal/jpa/deployment/JarFileArchive.java (revision 5435) +++ src/org/eclipse/persistence/internal/jpa/deployment/JarFileArchive.java (working copy) @@ -89,5 +89,13 @@ public URL getRootURL() { return rootURL; } + + public void close() { + try { + jarFile.close(); + } catch (IOException e) { + // ignore + } + } } Index: src/org/eclipse/persistence/internal/jpa/deployment/JPAInitializer.java =================================================================== --- src/org/eclipse/persistence/internal/jpa/deployment/JPAInitializer.java (revision 5435) +++ src/org/eclipse/persistence/internal/jpa/deployment/JPAInitializer.java (working copy) @@ -139,9 +139,15 @@ public void initialize(Map m, PersistenceInitializationHelper persistenceHelper) { initializeClassLoader(persistenceHelper); final Set pars = PersistenceUnitProcessor.findPersistenceArchives(initializationClassloader); - for (Archive archive: pars) { - AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_initialize", archive); - initPersistenceUnits(archive, m, persistenceHelper); + try { + for (Archive archive: pars) { + AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_initialize", archive); + initPersistenceUnits(archive, m, persistenceHelper); + } + } finally { + for (Archive archive : pars) { + archive.close(); + } } } Index: src/org/eclipse/persistence/internal/jpa/deployment/Archive.java =================================================================== --- src/org/eclipse/persistence/internal/jpa/deployment/Archive.java (revision 5435) +++ src/org/eclipse/persistence/internal/jpa/deployment/Archive.java (working copy) @@ -59,5 +59,10 @@ * @return the URL that this archive represents. */ URL getRootURL(); + + /** + * Close this archive and associated InputStream. + */ + void close(); } Index: src/org/eclipse/persistence/internal/jpa/deployment/DirectoryInsideJarURLArchive.java =================================================================== --- src/org/eclipse/persistence/internal/jpa/deployment/DirectoryInsideJarURLArchive.java (revision 5435) +++ src/org/eclipse/persistence/internal/jpa/deployment/DirectoryInsideJarURLArchive.java (working copy) @@ -106,4 +106,12 @@ public URL getRootURL() { return rootURL; } + + public void close() { + try { + jarFile.close(); + } catch (IOException e) { + // ignore + } + } } Index: src/org/eclipse/persistence/internal/jpa/deployment/PersistenceUnitProcessor.java =================================================================== --- src/org/eclipse/persistence/internal/jpa/deployment/PersistenceUnitProcessor.java (revision 5435) +++ src/org/eclipse/persistence/internal/jpa/deployment/PersistenceUnitProcessor.java (working copy) @@ -188,11 +188,14 @@ } /** - * 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 + * 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. It is the caller's responsibility + * to close all the archives. + * + * @param loader + * the class loader to get the class path from */ public static Set findPersistenceArchives(ClassLoader loader){ Set pars = new HashSet(); @@ -205,8 +208,16 @@ pars.add(archive); } } catch (java.io.IOException exc){ + //clean up first + for (Archive archive : pars) { + archive.close(); + } throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(loader, exc); } catch (URISyntaxException exc) { + //clean up first + for (Archive archive : pars) { + archive.close(); + } throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(loader, exc); } @@ -218,16 +229,20 @@ Archive archive = null; try { archive = new ArchiveFactoryImpl().createArchive(url); + + for (Iterator entries = archive.getEntries(); entries.hasNext();) { + String entry = entries.next(); + if (entry.endsWith(".class")){ // NOI18N + classNames.add(buildClassNameFromEntryString(entry)); + } + } } catch (URISyntaxException e) { throw new RuntimeException("url = [" + url + "]", e); // NOI18N } catch (IOException e) { throw new RuntimeException("url = [" + url + "]", e); // NOI18N - } - - for (Iterator entries = archive.getEntries(); entries.hasNext();) { - String entry = entries.next(); - if (entry.endsWith(".class")){ // NOI18N - classNames.add(buildClassNameFromEntryString(entry)); + } finally { + if (archive != null) { + archive.close(); } } return classNames; Index: src/org/eclipse/persistence/internal/jpa/deployment/JarInputStreamURLArchive.java =================================================================== --- src/org/eclipse/persistence/internal/jpa/deployment/JarInputStreamURLArchive.java (revision 5435) +++ src/org/eclipse/persistence/internal/jpa/deployment/JarInputStreamURLArchive.java (working copy) @@ -108,4 +108,9 @@ public URL getRootURL() { return url; } + + public void close() { + // nothing to close. it's caller's responsibility to close + // any InputStream returned by getEntry(). + } } Index: src/org/eclipse/persistence/internal/jpa/metadata/MetadataProcessor.java =================================================================== --- src/org/eclipse/persistence/internal/jpa/metadata/MetadataProcessor.java (revision 5435) +++ src/org/eclipse/persistence/internal/jpa/metadata/MetadataProcessor.java (working copy) @@ -345,29 +345,29 @@ for (URL rootURL : rootUrls) { logMessage("Searching for default mapping file in " + rootURL); URL ormURL = null; - + + Archive par = null; try { - Archive m_par = null; - m_par = new ArchiveFactoryImpl().createArchive(rootURL); - ormURL = m_par.getEntryAsURL(ormXMLFile); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - - if (ormURL != null) { - logMessage("Found a default mapping file at " + ormURL + " for root URL " + rootURL); - - try { + par = new ArchiveFactoryImpl().createArchive(rootURL); + ormURL = par.getEntryAsURL(ormXMLFile); + + if (ormURL != null) { + logMessage("Found a default mapping file at " + ormURL + " for root URL " + rootURL); + // Read the document through OX and add it to the project., pass persistence unit properties for any orm properties set there XMLEntityMappings entityMappings = XMLEntityMappingsReader.read( ormURL, m_loader, m_project.getPersistenceUnitInfo().getProperties()); entityMappings.setIsEclipseLinkORMFile(ormXMLFile.equals(MetadataHelper.ECLIPSELINK_ORM_FILE)); m_project.addEntityMappings(entityMappings); - } catch (IOException e) { - throw new RuntimeException(e); - } + } + } catch (IOException e) { + throw new RuntimeException(e); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } finally { + if (par != null) { + par.close(); + } } } }