Author: rafale Date: Tue Mar 24 19:38:22 2009 New Revision: 757977 URL: http://svn.apache.org/viewvc?rev=757977&view=rev Log: Fix for archetype-234. Thanks for the contribution of Jason Voegele
Modified: maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultArchetypeArtifactManager.java maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/repositorycrawler/DefaultRepositoryCrawler.java Modified: maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultArchetypeArtifactManager.java URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultArchetypeArtifactManager.java?rev=757977&r1=757976&r2=757977&view=diff ============================================================================== --- maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultArchetypeArtifactManager.java (original) +++ maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultArchetypeArtifactManager.java Tue Mar 24 19:38:22 2009 @@ -74,7 +74,7 @@ * @plexus.requirement */ private RepositoryMetadataManager repositoryMetadataManager; - + private Map archetypeCache = new TreeMap(); public File getArchetypeFile ( @@ -95,7 +95,7 @@ version); if (archetype==null) { - archetype = + archetype = downloader.download ( groupId, artifactId, @@ -143,35 +143,43 @@ throws XmlPullParserException, UnknownArchetype, IOException { String pomFileName = null; - ZipFile zipFile = getArchetypeZipFile ( jar ); - Enumeration enumeration = zipFile.entries (); - while ( enumeration.hasMoreElements () ) + ZipFile zipFile = null; + try { - ZipEntry el = (ZipEntry) enumeration.nextElement (); - - String entry = el.getName (); - if ( entry.startsWith ( "META-INF" ) && entry.endsWith ( "pom.xml" ) ) + zipFile = getArchetypeZipFile ( jar ); + Enumeration enumeration = zipFile.entries (); + while ( enumeration.hasMoreElements () ) { - pomFileName = entry; + ZipEntry el = (ZipEntry) enumeration.nextElement (); + + String entry = el.getName (); + if ( entry.startsWith ( "META-INF" ) && entry.endsWith ( "pom.xml" ) ) + { + pomFileName = entry; + } } - } - if ( pomFileName == null ) - { - return null; - } + if ( pomFileName == null ) + { + return null; + } - ZipEntry pom = - zipFile.getEntry ( StringUtils.replace ( pomFileName, File.separator, "/" ) ); - if ( pom == null ) - { - pom = zipFile.getEntry ( StringUtils.replace ( pomFileName, "/", File.separator ) ); + ZipEntry pom = + zipFile.getEntry ( StringUtils.replace ( pomFileName, File.separator, "/" ) ); + if ( pom == null ) + { + pom = zipFile.getEntry ( StringUtils.replace ( pomFileName, "/", File.separator ) ); + } + if ( pom == null ) + { + return null; + } + return pomManager.readPom ( zipFile.getInputStream ( pom ) ); } - if ( pom == null ) + finally { - return null; + closeZipFile( zipFile ); } - return pomManager.readPom ( zipFile.getInputStream ( pom ) ); } public ZipFile getArchetypeZipFile ( File archetypeFile ) @@ -193,9 +201,10 @@ public boolean isFileSetArchetype ( File archetypeFile ) { + ZipFile zipFile = null; try { - ZipFile zipFile = getArchetypeZipFile ( archetypeFile ); + zipFile = getArchetypeZipFile ( archetypeFile ); return isFileSetArchetype ( zipFile ); } @@ -211,6 +220,10 @@ { return false; } + finally + { + closeZipFile( zipFile ); + } } public boolean isFileSetArchetype ( @@ -222,9 +235,10 @@ List repositories ) { + ZipFile zipFile = null; try { - ZipFile zipFile = + zipFile = getArchetypeZipFile ( getArchetypeFile ( groupId, @@ -250,13 +264,18 @@ { return false; } + finally + { + closeZipFile( zipFile ); + } } public boolean isOldArchetype ( File archetypeFile ) { + ZipFile zipFile = null; try { - ZipFile zipFile = getArchetypeZipFile ( archetypeFile ); + zipFile = getArchetypeZipFile ( archetypeFile ); return isOldArchetype ( zipFile ); } @@ -272,6 +291,10 @@ { return false; } + finally + { + closeZipFile( zipFile ); + } } public boolean isOldArchetype ( @@ -283,9 +306,10 @@ List repositories ) { + ZipFile zipFile = null; try { - ZipFile zipFile = + zipFile = getArchetypeZipFile ( getArchetypeFile ( groupId, @@ -311,6 +335,10 @@ { return false; } + finally + { + closeZipFile( zipFile ); + } } public boolean exists ( @@ -330,7 +358,7 @@ archetypeVersion); if (archetype==null) { - archetype = + archetype = downloader.download ( archetypeGroupId, archetypeArtifactId, @@ -363,9 +391,10 @@ public ArchetypeDescriptor getFileSetArchetypeDescriptor ( File archetypeFile ) throws UnknownArchetype { + ZipFile zipFile = null; try { - ZipFile zipFile = getArchetypeZipFile ( archetypeFile ); + zipFile = getArchetypeZipFile ( archetypeFile ); return loadFileSetArchetypeDescriptor ( zipFile ); } @@ -377,6 +406,10 @@ { throw new UnknownArchetype ( e ); } + finally + { + closeZipFile( zipFile ); + } } public org.apache.maven.archetype.metadata.ArchetypeDescriptor getFileSetArchetypeDescriptor ( @@ -389,9 +422,10 @@ ) throws UnknownArchetype { + ZipFile zipFile = null; try { - ZipFile zipFile = + zipFile = getArchetypeZipFile ( getArchetypeFile ( groupId, @@ -413,6 +447,10 @@ { throw new UnknownArchetype ( e ); } + finally + { + closeZipFile( zipFile ); + } } public List getFilesetArchetypeResources ( File archetypeFile ) @@ -420,34 +458,42 @@ { List archetypeResources = new ArrayList (); - ZipFile zipFile = getArchetypeZipFile ( archetypeFile ); - - Enumeration enumeration = zipFile.entries (); - while ( enumeration.hasMoreElements () ) + ZipFile zipFile = null; + try { - ZipEntry entry = (ZipEntry) enumeration.nextElement (); + zipFile = getArchetypeZipFile ( archetypeFile ); - if ( !entry.isDirectory () - && entry.getName ().startsWith ( Constants.ARCHETYPE_RESOURCES ) - ) + Enumeration enumeration = zipFile.entries (); + while ( enumeration.hasMoreElements () ) { - // not supposed to be file.seperator - String resource = - StringUtils.replace ( - entry.getName (), - Constants.ARCHETYPE_RESOURCES + "/", - "" - ); - getLogger ().debug ( "Found resource " + resource ); - // TODO:FIXME - archetypeResources.add ( resource ); - } - else - { - getLogger ().debug ( "Not resource " + entry.getName () ); + ZipEntry entry = (ZipEntry) enumeration.nextElement (); + + if ( !entry.isDirectory () + && entry.getName ().startsWith ( Constants.ARCHETYPE_RESOURCES ) + ) + { + // not supposed to be file.seperator + String resource = + StringUtils.replace ( + entry.getName (), + Constants.ARCHETYPE_RESOURCES + "/", + "" + ); + getLogger ().debug ( "Found resource " + resource ); + // TODO:FIXME + archetypeResources.add ( resource ); + } + else + { + getLogger ().debug ( "Not resource " + entry.getName () ); + } } + return archetypeResources; + } + finally + { + closeZipFile( zipFile ); } - return archetypeResources; } public org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor getOldArchetypeDescriptor ( @@ -455,9 +501,10 @@ ) throws UnknownArchetype { + ZipFile zipFile = null; try { - ZipFile zipFile = getArchetypeZipFile ( archetypeFile ); + zipFile = getArchetypeZipFile ( archetypeFile ); return loadOldArchetypeDescriptor ( zipFile ); } @@ -469,6 +516,10 @@ { throw new UnknownArchetype ( e ); } + finally + { + closeZipFile( zipFile ); + } } public org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor getOldArchetypeDescriptor ( @@ -481,9 +532,10 @@ ) throws UnknownArchetype { + ZipFile zipFile = null; try { - ZipFile zipFile = + zipFile = getArchetypeZipFile ( getArchetypeFile ( groupId, @@ -505,10 +557,26 @@ { throw new UnknownArchetype ( e ); } + finally + { + closeZipFile( zipFile ); + } + } + + private void closeZipFile(ZipFile zipFile) + { + try + { + zipFile.close(); + } + catch (Exception e) + { + getLogger().error( "Fail to close zipFile" ); + } } private File getArchetype(String archetypeGroupId, - String archetypeArtifactId, + String archetypeArtifactId, String archetypeVersion) { String key = archetypeGroupId+":"+archetypeArtifactId+":"+archetypeVersion; if (archetypeCache.containsKey(key)) @@ -523,7 +591,7 @@ } } - private void setArchetype(String archetypeGroupId, + private void setArchetype(String archetypeGroupId, String archetypeArtifactId, String archetypeVersion, File archetype) { Modified: maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/repositorycrawler/DefaultRepositoryCrawler.java URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/repositorycrawler/DefaultRepositoryCrawler.java?rev=757977&r1=757976&r2=757977&view=diff ============================================================================== --- maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/repositorycrawler/DefaultRepositoryCrawler.java (original) +++ maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/repositorycrawler/DefaultRepositoryCrawler.java Tue Mar 24 19:38:22 2009 @@ -20,6 +20,7 @@ package org.apache.maven.archetype.repositorycrawler; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.maven.archetype.catalog.Archetype; import org.apache.maven.archetype.catalog.ArchetypeCatalog; @@ -163,11 +164,13 @@ public boolean writeCatalog ( ArchetypeCatalog archetypeCatalog, File archetypeCatalogFile ) { + FileWriter fileWriter = null; try { ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer (); - catalogWriter.write ( new FileWriter ( archetypeCatalogFile ), archetypeCatalog ); + fileWriter = new FileWriter ( archetypeCatalogFile ); + catalogWriter.write ( fileWriter, archetypeCatalog ); return true; } catch ( IOException ex ) @@ -175,5 +178,9 @@ getLogger ().warn ( "Catalog can not be writen to " + archetypeCatalogFile, ex ); return false; } + finally + { + IOUtils.closeQuietly(fileWriter); + } } }