KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > location > FlexibleLocation


1 /*
2  * $Id: FlexibleLocation.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.location;
26
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.ofbiz.base.util.UtilProperties;
33
34 /**
35  * A special location resolver that uses Strings like URLs, but with more options
36  *
37  *@author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  *@version $Rev: 5462 $
39  *@since 3.1
40  */

41
42 public class FlexibleLocation {
43     
44     protected static Map JavaDoc locationResolvers = new HashMap JavaDoc();
45     
46     protected static Map JavaDoc defaultResolvers = new HashMap JavaDoc();
47     
48     protected static String JavaDoc standardUrlResolverName = StandardUrlLocationResolver.class.getName();
49     protected static String JavaDoc classpathResolverName = ClasspathLocationResolver.class.getName();
50     protected static String JavaDoc ofbizHomeResolverName = OFBizHomeLocationResolver.class.getName();
51     protected static String JavaDoc componentResolverName = ComponentLocationResolver.class.getName();
52     
53     static {
54         defaultResolvers.put("http", standardUrlResolverName);
55         defaultResolvers.put("https", standardUrlResolverName);
56         defaultResolvers.put("ftp", standardUrlResolverName);
57         defaultResolvers.put("jar", standardUrlResolverName);
58         defaultResolvers.put("file", standardUrlResolverName);
59
60         defaultResolvers.put("classpath", classpathResolverName);
61         defaultResolvers.put("ofbizhome", ofbizHomeResolverName);
62         defaultResolvers.put("component", componentResolverName);
63     }
64     
65     /**
66      * Resolves the gives location into a URL object for use in various ways.
67      *
68      * The general format of the location is like a URL: {locationType}://location/path/file.ext
69      *
70      * Supports standard locationTypes like http, https, ftp, jar, & file
71      * Supports a classpath location type for when desired to be used like any other URL
72      * Supports OFBiz specific location types like ofbizhome and component
73      * Supports additional locationTypes specified in the locationresolvers.properties file
74      *
75      * @param location The location String to parse and create a URL from
76      * @return URL object corresponding to the location String passed in
77      * @throws MalformedURLException
78      */

79     public static URL JavaDoc resolveLocation(String JavaDoc location) throws MalformedURLException JavaDoc {
80         return resolveLocation(location, null);
81     }
82     
83     public static URL JavaDoc resolveLocation(String JavaDoc location, ClassLoader JavaDoc loader) throws MalformedURLException JavaDoc {
84         if (location == null || location.length() == 0) {
85             return null;
86         }
87         String JavaDoc locationType = getLocationType(location);
88         
89         LocationResolver resolver = (LocationResolver) locationResolvers.get(locationType);
90         if (resolver == null) {
91             synchronized (FlexibleLocation.class) {
92                 resolver = (LocationResolver) locationResolvers.get(locationType);
93                 if (resolver == null) {
94                     String JavaDoc locationResolverName = UtilProperties.getPropertyValue("locationresolvers", locationType);
95                     if (locationResolverName == null || locationResolverName.length() == 0) {
96                         // try one of the defaults
97
locationResolverName = (String JavaDoc) defaultResolvers.get(locationType);
98                     }
99
100                     if (locationResolverName == null || locationResolverName.length() == 0) {
101                         // still nothing, give up
102
throw new MalformedURLException JavaDoc("Could not find a LocationResolver class name for the location type: " + locationType);
103                     }
104
105                     // now create a new instance of the class...
106
try {
107                         Class JavaDoc lClass = null;
108
109                         try {
110                             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
111                             lClass = classLoader.loadClass(locationResolverName);
112                         } catch (ClassNotFoundException JavaDoc e) {
113                             throw new MalformedURLException JavaDoc("Error loading Location Resolver class \"" + locationResolverName + "\": " + e.toString());
114                         }
115
116                         try {
117                             resolver = (LocationResolver) lClass.newInstance();
118                         } catch (IllegalAccessException JavaDoc e) {
119                             throw new MalformedURLException JavaDoc("Error loading Location Resolver class \"" + locationResolverName + "\": " + e.toString());
120                         } catch (InstantiationException JavaDoc e) {
121                             throw new MalformedURLException JavaDoc("Error loading Location Resolver class \"" + locationResolverName + "\": " + e.toString());
122                         }
123                     } catch (SecurityException JavaDoc e) {
124                         throw new MalformedURLException JavaDoc("Error loading Location Resolver class \"" + locationResolverName + "\": " + e.toString());
125                     }
126                     
127                     if (resolver != null) {
128                         locationResolvers.put(locationType, resolver);
129                     }
130                 }
131             }
132         }
133         
134         if (resolver != null) {
135             if (loader != null && resolver instanceof ClasspathLocationResolver) {
136                 ClasspathLocationResolver cplResolver = (ClasspathLocationResolver) resolver;
137                 return cplResolver.resolveLocation(location, loader);
138             } else {
139                 return resolver.resolveLocation(location);
140             }
141         } else {
142             throw new MalformedURLException JavaDoc("Could not find a LocationResolver for the location type: " + locationType);
143         }
144     }
145     
146     /**
147      * Find the location type descriptor for the passed location String;
148      * generally is all text before the first ":" character.
149      * If no type descriptor is found, defaults to "classpath".
150      */

151     public static String JavaDoc getLocationType(String JavaDoc location) {
152         if (location == null || location.length() == 0) {
153             return null;
154         }
155         
156         int colonIndex = location.indexOf(":");
157         if (colonIndex > 0) {
158             return location.substring(0, colonIndex);
159         } else {
160             return "classpath";
161         }
162     }
163     
164     public static String JavaDoc stripLocationType(String JavaDoc location) {
165         if (location == null || location.length() == 0) {
166             return "";
167         }
168         
169         StringBuffer JavaDoc strippedSoFar = new StringBuffer JavaDoc(location);
170         
171         // first take care of the colon and everything before it
172
int colonIndex = strippedSoFar.indexOf(":");
173         if (colonIndex == 0) {
174             strippedSoFar.deleteCharAt(0);
175         } else if (colonIndex > 0) {
176             strippedSoFar.delete(0, colonIndex + 1);
177         }
178         
179         // now remove any extra forward slashes, ie as long as the first two are forward slashes remove the first one
180
while (strippedSoFar.charAt(0) == '/' && strippedSoFar.charAt(1) == '/') {
181             strippedSoFar.deleteCharAt(0);
182         }
183         
184         return strippedSoFar.toString();
185     }
186 }
187
Popular Tags