1 2 24 25 26 27 28 29 package com.lutris.appserver.server.httpPresentation; 30 31 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.util.Hashtable ; 35 import java.util.StringTokenizer ; 36 37 import com.lutris.logging.LogChannel; 38 import com.lutris.logging.Logger; 39 40 49 class PresentationLoader { 50 55 private final String presentationPrefix; 56 57 60 private Mime mime; 61 62 65 private ClassLoader appClassLoader; 66 67 70 private Hashtable presObjCache; 71 72 76 private Hashtable resourceObjCache; 77 78 81 private LogChannel logChannel; 82 83 86 private boolean debug1LoggingEnabled; 87 88 101 protected PresentationLoader(String appPresentationPrefix, 102 ClassLoader applicationClassLoader, 103 boolean cacheClasses, 104 boolean cacheFiles, 105 LogChannel presLogChannel) 106 throws HttpPresentationException { 107 logChannel = presLogChannel; 108 if (logChannel != null) { 109 debug1LoggingEnabled = logChannel.isEnabled(Logger.DEBUG1); 110 } 111 appClassLoader = applicationClassLoader; 112 presentationPrefix = cleanUrlPath(appPresentationPrefix); 113 mime = new Mime(); 114 if (cacheClasses) { 115 presObjCache = new Hashtable (); 116 } 117 if (cacheFiles) { 118 resourceObjCache = new Hashtable (); 119 } 120 } 121 122 129 private String cleanUrlPath(String urlPath) 130 throws HttpPresentationException { 131 134 int i=0; 139 int len = urlPath.length(); 140 141 142 if (len != 0 && urlPath.charAt(0)=='/') { urlPath = urlPath.substring(1); 145 len--; 146 } 147 148 if (len !=0 ) { 150 boolean uglyness = (urlPath.charAt(0) == '/' || urlPath.charAt(len-1) == '/'); 153 while (!uglyness && i < len) { 154 char c = urlPath.charAt(i); 155 if (c =='\\') { 156 uglyness = true; 157 } 158 else if (c=='/' && i > 0 ) { 159 char c0 = urlPath.charAt(i-1); 160 uglyness = ( c0 == '.' || c0 == '/'); 161 } 162 i++; 163 } 164 165 if (!uglyness) { 166 return urlPath; 167 } 168 } 169 171 173 StringTokenizer tokens = 174 new StringTokenizer (urlPath.trim(), "/\\", false); 175 StringBuffer newPath = new StringBuffer (); 176 177 178 while (tokens.hasMoreTokens()) { 181 String name = tokens.nextToken(); 182 if (!(name.equals("/") || name.equals("\\") 183 || name.equals(".") || name.equals(""))) { 184 if (newPath.length() > 0) { 185 newPath.append('/'); 186 } 187 newPath.append(name); 188 } 189 } 190 return newPath.toString(); 191 } 192 193 194 199 private String convertToClassName(String urlPath) { 200 String className = urlPath; 201 int idx, start, end; 202 203 idx = className.lastIndexOf('.'); 205 if (idx > 0) { 206 className = className.substring(0, idx); 207 } 208 209 className = className.replace('/','.'); 212 213 start = 0; 215 end = className.length()-1; 216 if (className.charAt(start) == '.') { 217 start++; 218 } 219 if (className.charAt(end) == '.') { 220 end--; 221 } 222 if (start >= end) { 223 start = 0; 225 end = className.length()-1; 226 } 227 228 return className.substring(start, end+1); 229 } 230 231 239 private synchronized HttpPresentation loadPresentationObject(String urlPath) 240 throws ClassNotFoundException , IllegalAccessException , 241 InstantiationException { 242 String className = convertToClassName(urlPath); 243 244 Class classObj = null; 245 if (presObjCache != null) { 246 classObj = (Class )presObjCache.get(className); 247 } 248 249 if (classObj == null) { 250 if (appClassLoader != null) { 252 classObj = appClassLoader.loadClass(className); 253 } else { 254 classObj = Class.forName(className); 256 } 257 if (presObjCache != null) { 258 presObjCache.put(className, classObj); 259 } 260 } 261 return (HttpPresentation)classObj.newInstance(); 262 } 263 264 268 private HttpPresentation loadResourcePresentation(String urlPath,String mimeType) 269 throws ClassNotFoundException , IllegalAccessException , 270 InstantiationException , HttpPresentationException, 271 FilePresentationException, IOException { 272 HttpPresentation resourceObj; 273 274 if (resourceObjCache != null) { 275 resourceObj = (HttpPresentation)resourceObjCache.get(urlPath); 276 if (resourceObj == null) { 277 resourceObj = new CachedFilePresentation(appClassLoader, 278 urlPath, mimeType); 279 resourceObjCache.put(urlPath, resourceObj); 280 } 281 } else { 282 resourceObj = new CopyFilePresentation(appClassLoader, 283 urlPath, mimeType); 284 } 285 return resourceObj; 286 } 287 288 294 protected boolean isPresentationRequest(HttpPresentationRequest request) 295 throws HttpPresentationException { 296 return mime.getType(request.getPresentationObjectPath()).equals("object/presentation"); 297 } 298 299 303 protected HttpPresentation loadPresentation(String urlPath) 304 throws ClassNotFoundException , IllegalAccessException , 305 InstantiationException , HttpPresentationException, 306 FilePresentationException, IOException { 307 HttpPresentation presObj; 308 String presPath; 309 String mimeType = mime.getType(urlPath); 310 311 try { 312 if (mimeType.equals("object/presentation")) { 313 presPath = presentationPrefix + "/" + cleanUrlPath(urlPath); 314 if (debug1LoggingEnabled) { 315 logChannel.write(Logger.DEBUG1, "loadPresentationObject: " + urlPath); 316 } 317 presObj = loadPresentationObject(presPath); 318 } else { 319 if (debug1LoggingEnabled) { 320 logChannel.write(Logger.DEBUG1, "loadPresentationObject: " 321 + urlPath + " " + mimeType); 322 } 323 328 if (mimeType.equals("application/java-vm") 329 || mimeType.equals("application/java-archive")) { 330 presPath = cleanUrlPath(urlPath); 331 } else { 332 presPath = presentationPrefix + "/" + cleanUrlPath(urlPath); 333 } 334 presObj = loadResourcePresentation(presPath,mimeType); 335 } 336 } catch (ClassNotFoundException except) { 337 if (debug1LoggingEnabled) { 338 logChannel.write(Logger.DEBUG1, " not found: " + urlPath); 339 } 340 throw except; 341 } 342 return presObj; 343 } 344 345 350 protected boolean isPOCacheEnabled() { 351 return (presObjCache != null); 352 } 353 354 359 protected int sizeofPOCache() { 360 if (presObjCache != null) { 361 return presObjCache.size(); 362 } else { 363 return 0; 364 } 365 } 366 367 372 protected boolean isResourceCacheEnabled() { 373 return (resourceObjCache != null); 374 } 375 376 381 protected int sizeofResourceCache() { 382 if (resourceObjCache != null) return resourceObjCache.size(); 383 else return 0; 384 } 385 386 398 protected InputStream getAppFileAsStream(String appFileName) 399 throws IOException , HttpPresentationException { 400 String path = presentationPrefix + "/" + cleanUrlPath(appFileName); 401 InputStream input = appClassLoader.getResourceAsStream(path); 402 if (input == null) { 403 throw new HttpPresentationException("File \"" + appFileName + "\" not found on application class path"); 404 } 405 return input; 406 } 407 408 411 protected void flushCache() { 412 if (presObjCache != null) { 413 presObjCache = new Hashtable (); 414 } 415 if (resourceObjCache != null) { 416 resourceObjCache = new Hashtable (); 417 } 418 } 419 420 425 protected String getMimeType(String urlPath) { 426 return mime.getType(urlPath); 427 } 428 429 430 public void addMimeType(String mimeType, String extension) { 431 mime.addType(mimeType, extension); 432 } 433 434 435 440 protected ClassLoader getAppClassLoader() { 441 return appClassLoader; 442 } 443 } 444 | Popular Tags |