001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.quickstart;
006:
007: import org.mortbay.jetty.servlet.WebApplicationContext;
008: import org.mortbay.util.Resource;
009: import org.mortbay.util.JarResource;
010: import org.mortbay.util.FileResource;
011:
012: import java.io.IOException;
013: import java.io.File;
014: import java.net.URLClassLoader;
015: import java.net.URL;
016: import java.lang.reflect.Constructor;
017: import java.util.Map;
018: import java.util.List;
019:
020: /**
021: * Integration to Jetty.
022: */
023: public class MultiWebApplicationContext extends WebApplicationContext {
024: private List pathPriority;
025: private Map paths;
026: private Class resolver;
027:
028: public MultiWebApplicationContext() {
029: }
030:
031: public MultiWebApplicationContext(List pathPriority, Map paths) {
032: super (getFirstRoot(paths));
033: this .pathPriority = pathPriority;
034: this .paths = paths;
035: }
036:
037: public MultiWebApplicationContext(List pathPriority, Map paths,
038: String resolver) {
039: super (getFirstRoot(paths));
040: this .pathPriority = pathPriority;
041: this .paths = paths;
042: try {
043: this .resolver = loadClass(resolver, getClass());
044: } catch (ClassNotFoundException e) {
045: e.printStackTrace();
046: }
047: }
048:
049: private static String getFirstRoot(Map paths) {
050: return (String) ((List) paths.get("/")).get(0);
051: }
052:
053: public Resource getResource(String uriInContext) throws IOException {
054: if (uriInContext.startsWith("/WEB-INF/lib/")) {
055: String jar = uriInContext.substring("/WEB-INF/lib/"
056: .length());
057: ClassLoader parent = Thread.currentThread()
058: .getContextClassLoader();
059: while (parent != null) {
060: if (parent instanceof URLClassLoader) {
061: URL[] urls = ((URLClassLoader) parent).getURLs();
062: for (int i = 0; i < urls.length; i++) {
063: URL url = urls[i];
064: if (url.toExternalForm().endsWith(jar)) {
065: return JarResource.newResource(url);
066: }
067: }
068: }
069:
070: parent = parent.getParent();
071: }
072: }
073:
074: // still haven't found what we're looking for?
075: // Alright, let's just hack this to work in IDEA
076: if (uriInContext.equals("/webwork")) {
077: // we do this check to support both "quickstart:showcase" and "quickstart" (using quickstart.xml)
078: if (new File("../../src/java/META-INF/taglib.tld").exists()) {
079: return FileResource
080: .newResource("../../src/java/META-INF/taglib.tld");
081: } else {
082: return FileResource
083: .newResource("src/java/META-INF/taglib.tld");
084: }
085: }
086:
087: MultiDirResource resource = newResolver(uriInContext);
088: if (resource.exists()) {
089: return resource;
090: }
091:
092: String aliasedUri = getResourceAlias(uriInContext);
093: if (aliasedUri != null) {
094: return super .getResource(aliasedUri);
095: }
096:
097: return resource;
098: }
099:
100: public MultiDirResource newResolver(String uriInContext) {
101: if (resolver == null) {
102: return new MultiDirResource(this , uriInContext,
103: pathPriority, paths);
104: } else {
105: try {
106: Constructor c = resolver
107: .getDeclaredConstructor(new Class[] {
108: MultiWebApplicationContext.class,
109: String.class, List.class, Map.class, });
110: return (MultiDirResource) c.newInstance(new Object[] {
111: this , uriInContext, pathPriority, paths, });
112: } catch (Exception e) {
113: e.printStackTrace();
114: return null;
115: }
116: }
117: }
118:
119: public Resource getBaseResource() {
120: return newResolver("");
121: }
122:
123: public static Class loadClass(String className, Class callingClass)
124: throws ClassNotFoundException {
125: try {
126: return Thread.currentThread().getContextClassLoader()
127: .loadClass(className);
128: } catch (ClassNotFoundException e) {
129: try {
130: return Class.forName(className);
131: } catch (ClassNotFoundException ex) {
132: try {
133: return MultiWebApplicationContext.class
134: .getClassLoader().loadClass(className);
135: } catch (ClassNotFoundException exc) {
136: return callingClass.getClassLoader().loadClass(
137: className);
138: }
139:
140: }
141: }
142: }
143: }
|