1 8 package org.apache.avalon.excalibur.extension; 9 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.util.ArrayList ; 13 import java.util.HashMap ; 14 import java.util.jar.JarFile ; 15 import java.util.jar.Manifest ; 16 import org.apache.avalon.excalibur.io.FileUtil; 17 import org.apache.avalon.excalibur.util.DeweyDecimal; 18 19 30 public class DefaultPackageRepository 31 implements PackageRepository 32 { 33 private static final boolean DEBUG = false; 34 35 38 private final HashMap m_packages = new HashMap (); 39 40 43 private File [] m_path; 44 45 49 private boolean m_needToScan; 50 51 56 public DefaultPackageRepository( final File [] path ) 57 { 58 setPath( path ); 59 } 60 61 71 public synchronized OptionalPackage[] getOptionalPackages( final Extension extension ) 72 { 73 if( m_needToScan ) 74 { 75 scanPath(); 77 } 78 79 final ArrayList results = new ArrayList (); 80 final ArrayList candidates = (ArrayList )m_packages.get( extension.getExtensionName() ); 81 if( null != candidates ) 82 { 83 final int size = candidates.size(); 84 for( int i = 0; i < size; i++ ) 85 { 86 final OptionalPackage optionalPackage = (OptionalPackage)candidates.get( i ); 87 final Extension[] extensions = optionalPackage.getAvailableExtensions(); 88 89 for( int j = 0; j < extensions.length; j++ ) 90 { 91 if( extensions[ j ].isCompatibleWith( extension ) ) 92 { 93 results.add( optionalPackage ); 94 } 95 } 96 } 97 } 98 99 102 return (OptionalPackage[])results.toArray( new OptionalPackage[ 0 ] ); 103 } 104 105 110 protected synchronized void setPath( final File [] path ) 111 { 112 if( null == path ) 113 { 114 throw new NullPointerException ( "path property is null" ); 115 } 116 117 for( int i = 0; i < path.length; i++ ) 118 { 119 final File directory = path[ i ]; 120 121 if( !directory.exists() || !directory.isDirectory() ) 122 { 123 throw new IllegalArgumentException ( "path element " + directory + 124 " must exist and must be a directory" ); 125 } 126 } 127 128 m_path = path; 129 m_needToScan = true; 130 } 131 132 137 protected final synchronized void scanPath() 138 { 139 clearCache(); 140 141 for( int i = 0; i < m_path.length; i++ ) 142 { 143 scanDirectory( m_path[ i ] ); 144 } 145 } 146 147 private synchronized void scanDirectory( final File directory ) 148 { 149 final File [] files = directory.listFiles(); 150 for( int i = 0; i < files.length; i++ ) 151 { 152 final File file = files[ i ]; 153 final String name = file.getName(); 154 155 if( !name.endsWith( ".jar" ) ) 156 { 157 debug( "Skipping " + file + " as it does not end with '.jar'" ); 158 continue; 159 } 160 161 if( !file.isFile() ) 162 { 163 debug( "Skipping " + file + " as it is not a file." ); 164 continue; 165 } 166 167 if( !file.canRead() ) 168 { 169 debug( "Skipping " + file + " as it is not readable." ); 170 continue; 171 } 172 173 try 174 { 175 final OptionalPackage optionalPackage = getOptionalPackage( file ); 176 cacheOptionalPackage( optionalPackage ); 177 } 178 catch( final IOException ioe ) 179 { 180 debug( "Skipping " + file + " as it could not be loaded due to " + ioe ); 181 } 182 } 183 } 184 185 189 protected synchronized final void clearCache() 190 { 191 m_packages.clear(); 192 m_needToScan = true; 193 } 194 195 202 protected synchronized final void cacheOptionalPackage( final OptionalPackage optionalPackage ) 203 { 204 m_needToScan = false; 205 final Extension extension = optionalPackage.getAvailableExtensions()[ 0 ]; 206 ArrayList candidates = (ArrayList )m_packages.get( extension.getExtensionName() ); 207 if( null == candidates ) 208 { 209 candidates = new ArrayList (); 210 m_packages.put( extension.getExtensionName(), candidates ); 211 } 212 213 candidates.add( optionalPackage ); 217 } 218 219 226 private OptionalPackage getOptionalPackage( final File archive ) 227 throws IOException 228 { 229 final File file = archive.getCanonicalFile(); 230 final JarFile jarFile = new JarFile ( file ); 231 final Manifest manifest = jarFile.getManifest(); 232 233 try 234 { 235 if( null == manifest ) return null; 236 final Extension[] available = Extension.getAvailable( manifest ); 237 final Extension[] required = Extension.getRequired( manifest ); 238 239 return new OptionalPackage( file, available, required ); 240 } 241 finally 242 { 243 jarFile.close(); 244 } 245 } 246 247 protected void debug( final String message ) 248 { 249 if( DEBUG ) System.out.println( message ); 250 } 252 } 253 | Popular Tags |