1 25 package org.ofbiz.base.location; 26 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import org.ofbiz.base.util.UtilProperties; 33 34 41 42 public class FlexibleLocation { 43 44 protected static Map locationResolvers = new HashMap (); 45 46 protected static Map defaultResolvers = new HashMap (); 47 48 protected static String standardUrlResolverName = StandardUrlLocationResolver.class.getName(); 49 protected static String classpathResolverName = ClasspathLocationResolver.class.getName(); 50 protected static String ofbizHomeResolverName = OFBizHomeLocationResolver.class.getName(); 51 protected static String 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 79 public static URL resolveLocation(String location) throws MalformedURLException { 80 return resolveLocation(location, null); 81 } 82 83 public static URL resolveLocation(String location, ClassLoader loader) throws MalformedURLException { 84 if (location == null || location.length() == 0) { 85 return null; 86 } 87 String 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 locationResolverName = UtilProperties.getPropertyValue("locationresolvers", locationType); 95 if (locationResolverName == null || locationResolverName.length() == 0) { 96 locationResolverName = (String ) defaultResolvers.get(locationType); 98 } 99 100 if (locationResolverName == null || locationResolverName.length() == 0) { 101 throw new MalformedURLException ("Could not find a LocationResolver class name for the location type: " + locationType); 103 } 104 105 try { 107 Class lClass = null; 108 109 try { 110 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 111 lClass = classLoader.loadClass(locationResolverName); 112 } catch (ClassNotFoundException e) { 113 throw new MalformedURLException ("Error loading Location Resolver class \"" + locationResolverName + "\": " + e.toString()); 114 } 115 116 try { 117 resolver = (LocationResolver) lClass.newInstance(); 118 } catch (IllegalAccessException e) { 119 throw new MalformedURLException ("Error loading Location Resolver class \"" + locationResolverName + "\": " + e.toString()); 120 } catch (InstantiationException e) { 121 throw new MalformedURLException ("Error loading Location Resolver class \"" + locationResolverName + "\": " + e.toString()); 122 } 123 } catch (SecurityException e) { 124 throw new MalformedURLException ("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 ("Could not find a LocationResolver for the location type: " + locationType); 143 } 144 } 145 146 151 public static String getLocationType(String 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 stripLocationType(String location) { 165 if (location == null || location.length() == 0) { 166 return ""; 167 } 168 169 StringBuffer strippedSoFar = new StringBuffer (location); 170 171 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 while (strippedSoFar.charAt(0) == '/' && strippedSoFar.charAt(1) == '/') { 181 strippedSoFar.deleteCharAt(0); 182 } 183 184 return strippedSoFar.toString(); 185 } 186 } 187 | Popular Tags |