KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > util > ResourceUtil


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.components.util;
9
10 import org.apache.avalon.framework.configuration.ConfigurationException;
11 import java.io.File JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13
14 /**
15  * A utility class for working with resources in default sar layout.
16  *
17  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
18  * @version $Revision: 1.1 $ $Date: 2002/09/06 09:42:35 $
19  */

20 public class ResourceUtil
21 {
22     private static final String JavaDoc SAR_PROTOCOL = "sar:";
23     private static final String JavaDoc SAR_INF = "SAR-INF/";
24     private static final String JavaDoc CLASSES = SAR_INF + "classes";
25     private static final String JavaDoc LIB = SAR_INF + "lib";
26
27     /**
28      * Expand any URLs with sar: protocol so that
29      * they accurately match the actual location
30      *
31      * @param codeBase the input url
32      * @return the result url, modified to file url if it
33      * is protocol "sar:"
34      * @throws ConfigurationException if invalidly specified URL
35      */

36     public static String JavaDoc expandSarURL( final String JavaDoc codeBase,
37                                        final File JavaDoc baseDirectory,
38                                        final File JavaDoc workDirectory )
39         throws ConfigurationException
40     {
41         if( codeBase.startsWith( SAR_PROTOCOL ) )
42         {
43             final File JavaDoc file =
44                 getFileForResource( codeBase.substring( 4 ),
45                                     baseDirectory,
46                                     workDirectory );
47             try
48             {
49                 return file.toURL().toString();
50             }
51             catch( MalformedURLException JavaDoc e )
52             {
53                 throw new ConfigurationException( e.getMessage(), e );
54             }
55         }
56         else
57         {
58             return codeBase;
59         }
60     }
61
62     public static File JavaDoc getFileForResource( final String JavaDoc location,
63                                            final File JavaDoc baseDirectory,
64                                            final File JavaDoc workDirectory )
65     {
66         String JavaDoc filename =
67             location.replace( '/', File.separatorChar );
68         if( filename.startsWith( "/" ) )
69         {
70             filename = filename.substring( 1 );
71         }
72
73         final File JavaDoc baseDir =
74             getBaseDirectoryFor( location, baseDirectory, workDirectory );
75         return new File JavaDoc( baseDir, filename );
76     }
77
78     private static File JavaDoc getBaseDirectoryFor( final String JavaDoc location,
79                                              final File JavaDoc baseDirectory,
80                                              final File JavaDoc 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