1 8 package org.apache.avalon.phoenix.components.util; 9 10 import org.apache.avalon.framework.configuration.ConfigurationException; 11 import java.io.File ; 12 import java.net.MalformedURLException ; 13 14 20 public class ResourceUtil 21 { 22 private static final String SAR_PROTOCOL = "sar:"; 23 private static final String SAR_INF = "SAR-INF/"; 24 private static final String CLASSES = SAR_INF + "classes"; 25 private static final String LIB = SAR_INF + "lib"; 26 27 36 public static String expandSarURL( final String codeBase, 37 final File baseDirectory, 38 final File workDirectory ) 39 throws ConfigurationException 40 { 41 if( codeBase.startsWith( SAR_PROTOCOL ) ) 42 { 43 final File file = 44 getFileForResource( codeBase.substring( 4 ), 45 baseDirectory, 46 workDirectory ); 47 try 48 { 49 return file.toURL().toString(); 50 } 51 catch( MalformedURLException e ) 52 { 53 throw new ConfigurationException( e.getMessage(), e ); 54 } 55 } 56 else 57 { 58 return codeBase; 59 } 60 } 61 62 public static File getFileForResource( final String location, 63 final File baseDirectory, 64 final File workDirectory ) 65 { 66 String filename = 67 location.replace( '/', File.separatorChar ); 68 if( filename.startsWith( "/" ) ) 69 { 70 filename = filename.substring( 1 ); 71 } 72 73 final File baseDir = 74 getBaseDirectoryFor( location, baseDirectory, workDirectory ); 75 return new File ( baseDir, filename ); 76 } 77 78 private static File getBaseDirectoryFor( final String location, 79 final File baseDirectory, 80 final File workDirectory ) 81 { 82 if( location.startsWith( CLASSES ) || 83 location.startsWith( LIB ) ) 84 { 85 return workDirectory; 86 } 87 else 88 { 89 return baseDirectory; 90 } 91 } 92 } 93 | Popular Tags |