View Javadoc

1   /*
2    * $Id: MultiWebApplicationContext.java 454251 2006-10-09 02:10:57Z husted $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.quickstart;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.lang.reflect.Constructor;
23  import java.net.URL;
24  import java.net.URLClassLoader;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.mortbay.jetty.servlet.WebApplicationContext;
29  import org.mortbay.util.FileResource;
30  import org.mortbay.util.JarResource;
31  import org.mortbay.util.Resource;
32  
33  /***
34   */
35  public class MultiWebApplicationContext extends WebApplicationContext {
36  
37  	private static final long serialVersionUID = -4732081314967099271L;
38  
39  	private List pathPriority;
40      private Map paths;
41      private Class resolver;
42  
43      public MultiWebApplicationContext() {
44      }
45  
46      public MultiWebApplicationContext(List pathPriority, Map paths) {
47          super(getFirstRoot(paths));
48          this.pathPriority = pathPriority;
49          this.paths = paths;
50      }
51  
52      public MultiWebApplicationContext(List pathPriority, Map paths, String resolver) {
53          super(getFirstRoot(paths));
54          this.pathPriority = pathPriority;
55          this.paths = paths;
56          try {
57              this.resolver = loadClass(resolver, getClass());
58          } catch (ClassNotFoundException e) {
59              e.printStackTrace();
60          }
61      }
62  
63      private static String getFirstRoot(Map paths) {
64          return (String) ((List) paths.get("/")).get(0);
65      }
66  
67      public Resource getResource(String uriInContext) throws IOException {
68          if (uriInContext.startsWith("/WEB-INF/lib/")) {
69              String jar = uriInContext.substring("/WEB-INF/lib/".length());
70              ClassLoader parent = Thread.currentThread().getContextClassLoader();
71              while (parent != null) {
72                  if (parent instanceof URLClassLoader) {
73                      URL[] urls = ((URLClassLoader) parent).getURLs();
74                      for (int i = 0; i < urls.length; i++) {
75                          URL url = urls[i];
76                          if (url.toExternalForm().endsWith(jar)) {
77                              return JarResource.newResource(url);
78                          }
79                      }
80                  }
81  
82                  parent = parent.getParent();
83              }
84          }
85  
86          // still haven't found what we're looking for?
87          // Alright, let's just hack this to work in IDEA
88          if (uriInContext.equals("/tags")) {
89              // we do this check to support both "quickstart:showcase" and "quickstart" (using quickstart.xml)
90              if (new File("../../core/src/main/resources/META-INF/tags.tld").exists()) {
91                  return FileResource.newResource("../../core/src/main/resources/META-INF/tags.tld");
92              } else {
93                  return FileResource.newResource("src/main/resources/META-INF/tags.tld");
94              }
95          }
96  
97          MultiDirResource resource = newResolver(uriInContext);
98          if (resource.exists()) {
99              return resource;
100         }
101 
102         String aliasedUri= getResourceAlias(uriInContext);
103         if (aliasedUri != null) {
104             return super.getResource(aliasedUri);
105         }
106 
107         return resource;
108     }
109 
110     public MultiDirResource newResolver(String uriInContext) {
111         if (resolver == null) {
112             return new MultiDirResource(this, uriInContext, pathPriority, paths);
113         } else {
114             try {
115                 Constructor c = resolver.getDeclaredConstructor(new Class[]{
116                         MultiWebApplicationContext.class,
117                         String.class,
118                         List.class,
119                         Map.class,
120                 });
121                 return (MultiDirResource) c.newInstance(new Object[] {
122                         this,
123                         uriInContext,
124                         pathPriority,
125                         paths,
126                 });
127             } catch (Exception e) {
128                 e.printStackTrace();
129                 return null;
130             }
131         }
132     }
133 
134     public Resource getBaseResource() {
135         return newResolver("");
136     }
137 
138     public static Class loadClass(String className, Class callingClass)
139             throws ClassNotFoundException {
140         try {
141             return Thread.currentThread().getContextClassLoader().loadClass(className);
142         }
143         catch (ClassNotFoundException e) {
144             try {
145                 return Class.forName(className);
146             }
147             catch (ClassNotFoundException ex) {
148                 try {
149                     return MultiWebApplicationContext.class.getClassLoader().loadClass(className);
150                 }
151                 catch (ClassNotFoundException exc) {
152                     return callingClass.getClassLoader().loadClass(className);
153                 }
154 
155             }
156         }
157     }
158 }