1 28 package net.sf.jasperreports.engine.util; 29 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.net.URLStreamHandler ; 33 import java.net.URLStreamHandlerFactory ; 34 35 36 42 public class JRResourcesUtil 43 { 44 private static URLStreamHandlerFactory globalURLHandlerFactory; 45 private static ThreadLocalStack localURLHandlerFactoryStack = new ThreadLocalStack(); 46 47 private static ClassLoader globalClassLoader; 48 private static ThreadLocalStack localClassLoaderStack = new ThreadLocalStack(); 49 50 51 60 public static URL createURL(String spec, URLStreamHandlerFactory urlHandlerFactory) 61 { 62 URLStreamHandler handler = getURLHandler(spec, urlHandlerFactory); 63 URL url; 64 try 65 { 66 if (handler == null) 67 { 68 url = new URL (spec); 69 } 70 else 71 { 72 url = new URL (null, spec, handler); 73 } 74 } 75 catch (MalformedURLException e) 76 { 77 url = null; 78 } 79 return url; 80 } 81 82 83 91 public static URLStreamHandler getURLHandler(String spec, URLStreamHandlerFactory urlHandlerFactory) 92 { 93 urlHandlerFactory = getURLHandlerFactory(urlHandlerFactory); 94 95 URLStreamHandler handler = null; 96 if (urlHandlerFactory != null) 97 { 98 String protocol = getURLProtocol(spec); 99 if (protocol != null) 100 { 101 handler = urlHandlerFactory.createURLStreamHandler(protocol); 102 } 103 } 104 return handler; 105 } 106 107 108 private static String getURLProtocol(String spec) 109 { 110 String protocol = null; 111 112 spec = spec.trim(); 113 int colon = spec.indexOf(':'); 114 if (colon > 0) 115 { 116 String proto = spec.substring(0, colon); 117 if (protocolValid(proto)) 118 { 119 protocol = proto; 120 } 121 } 122 123 return protocol; 124 } 125 126 private static boolean protocolValid(String protocol) 127 { 128 int length = protocol.length(); 129 if (length < 1) 130 { 131 return false; 132 } 133 134 if (!Character.isLetter(protocol.charAt(0))) 135 { 136 return false; 137 } 138 139 for (int i = 1; i < length; ++i) 140 { 141 char c = protocol.charAt(i); 142 if (!(Character.isLetterOrDigit(c) || c == '+' || c == '-' || c == '.')) 143 { 144 return false; 145 } 146 } 147 148 return true; 149 } 150 151 152 167 public static URLStreamHandlerFactory getURLHandlerFactory(URLStreamHandlerFactory urlHandlerFactory) 168 { 169 if (urlHandlerFactory == null) 170 { 171 urlHandlerFactory = getThreadURLStreamHandlerFactory(); 172 if (urlHandlerFactory == null) 173 { 174 urlHandlerFactory = globalURLHandlerFactory; 175 } 176 } 177 return urlHandlerFactory; 178 } 179 180 181 187 public static URLStreamHandlerFactory getGlobalURLStreamHandlerFactory() 188 { 189 return globalURLHandlerFactory; 190 } 191 192 193 199 public static URLStreamHandlerFactory getThreadURLStreamHandlerFactory() 200 { 201 return (URLStreamHandlerFactory ) localURLHandlerFactoryStack.top(); 202 } 203 204 205 212 public static void setThreadURLHandlerFactory(URLStreamHandlerFactory urlHandlerFactory) 213 { 214 localURLHandlerFactoryStack.push(urlHandlerFactory); 215 } 216 217 218 221 public static void resetThreadURLHandlerFactory() 222 { 223 localURLHandlerFactoryStack.pop(); 224 } 225 226 232 public static void setGlobalURLHandlerFactory(URLStreamHandlerFactory urlHandlerFactory) 233 { 234 globalURLHandlerFactory = urlHandlerFactory; 235 } 236 237 238 253 public static ClassLoader getClassLoader(ClassLoader classLoader) 254 { 255 if (classLoader == null) 256 { 257 classLoader = getThreadClassLoader(); 258 if (classLoader == null) 259 { 260 classLoader = globalClassLoader; 261 } 262 } 263 return classLoader; 264 } 265 266 267 273 public static ClassLoader getGlobalClassLoader() 274 { 275 return globalClassLoader; 276 } 277 278 279 285 public static ClassLoader getThreadClassLoader() 286 { 287 return (ClassLoader ) localClassLoaderStack.top(); 288 } 289 290 291 298 public static void setThreadClassLoader(ClassLoader classLoader) 299 { 300 localClassLoaderStack.push(classLoader); 301 } 302 303 304 307 public static void resetClassLoader() 308 { 309 localClassLoaderStack.pop(); 310 } 311 312 313 319 public static void setGlobalClassLoader(ClassLoader classLoader) 320 { 321 globalClassLoader = classLoader; 322 } 323 324 325 341 public static URL findClassLoaderResource(String location, ClassLoader classLoader, Class clazz) 342 { 343 classLoader = getClassLoader(classLoader); 344 345 URL url = null; 346 347 if (classLoader != null) 348 { 349 url = classLoader.getResource(location); 350 } 351 352 if (url == null) 353 { 354 classLoader = Thread.currentThread().getContextClassLoader(); 355 356 if (classLoader != null) 357 { 358 url = classLoader.getResource(location); 359 } 360 361 if (url == null) 362 { 363 classLoader = clazz.getClassLoader(); 364 if (classLoader == null) 365 { 366 url = clazz.getResource("/" + location); 367 } 368 else 369 { 370 url = classLoader.getResource(location); 371 } 372 } 373 } 374 375 return url; 376 } 377 } 378 | Popular Tags |