This project has retired. For details please refer to its Attic page.
EnhancedTilesApplicationContext xref
View Javadoc
1   /*
2    * $Id: EnhancedTilesApplicationContext.java 791161 2009-07-04 18:53:36Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.context.enhanced;
22  
23  import java.io.IOException;
24  import java.net.URL;
25  import java.util.Enumeration;
26  import java.util.HashSet;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.tiles.TilesApplicationContext;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /***
35   * ApplicationContext decorator used to provide
36   * enhancements to the standard context.
37   *
38   * Specifically, it provides:
39   * <ul>
40   *   <li>Ability to load resources from the classpath</li>
41   *   <li>Ability to retrieve multiple resources of the same name</li>
42   * </ul>
43   *
44   * Future features will include:
45   * <ul>
46   *   <li>Ability to utilize wildcards in resource pathcs</li>
47   * </ul>
48   *
49   * @since Tiles 2.0
50   * @version $Rev: 791161 $ $Date: 2009-07-04 20:53:36 +0200 (sab, 04 lug 2009) $
51   */
52  public class EnhancedTilesApplicationContext implements TilesApplicationContext {
53  
54      /***
55       * The logging object.
56       */
57      private final Logger log =
58          LoggerFactory.getLogger(EnhancedTilesApplicationContext.class);
59  
60      /***
61       * The root context to be wrapped.
62       */
63      private TilesApplicationContext rootContext;
64  
65      /***
66       * Constructor.
67       *
68       * @param rootContext The root context to use.
69       */
70      public EnhancedTilesApplicationContext(TilesApplicationContext rootContext) {
71          this.rootContext = rootContext;
72      }
73  
74      /*** {@inheritDoc} */
75      public Object getContext() {
76          return rootContext.getContext();
77      }
78  
79      /***
80       * Returns the root context.
81       *
82       * @return The root context.
83       */
84      public TilesApplicationContext getRootContext() {
85          return rootContext;
86      }
87  
88      /***
89       * Sets the root context.
90       *
91       * @param rootContext The root context.
92       */
93      public void setRootContext(TilesApplicationContext rootContext) {
94          this.rootContext = rootContext;
95      }
96  
97      /*** {@inheritDoc} */
98      public Map<String, Object> getApplicationScope() {
99          return rootContext.getApplicationScope();
100     }
101 
102     /*** {@inheritDoc} */
103     public Map<String, String> getInitParams() {
104         return rootContext.getInitParams();
105     }
106 
107     /*** {@inheritDoc} */
108     public URL getResource(String path) throws IOException {
109         URL rootUrl = rootContext.getResource(path);
110         if (rootUrl == null) {
111             Set<URL> resources = getResources(path);
112             resources.remove(null);
113             if (resources.size() > 0) {
114                 rootUrl = resources.toArray(new URL[resources.size()])[0];
115             }
116         }
117         return rootUrl;
118     }
119 
120     /*** {@inheritDoc} */
121     public Set<URL> getResources(String path) throws IOException {
122         Set<URL> resources = new HashSet<URL>();
123         resources.addAll(rootContext.getResources(path));
124         resources.addAll(getClasspathResources(path));
125         return resources;
126     }
127 
128     /***
129      * Searches for resources in the classpath, given a path.
130      *
131      * @param path The path to search into.
132      * @return The set of found URLs.
133      * @throws IOException If something goes wrong during search.
134      */
135     public Set<URL> getClasspathResources(String path) throws IOException {
136         Set<URL> resources = new HashSet<URL>();
137         resources.addAll(searchResources(getClass().getClassLoader(), path));
138 
139         ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
140         if (contextLoader != null) {
141             resources.addAll(searchResources(contextLoader, path));
142         }
143 
144         if (resources.size() == 0 && path.startsWith("/")) {
145             return getClasspathResources(path.substring(1));
146         }
147 
148         return resources;
149     }
150 
151     /***
152      * Searches for resources in the classpath, given a path, using a class
153      * loader.
154      *
155      * @param loader The class loader to use.
156      * @param path The path to search into.
157      * @return The set of found URLs.
158      */
159     protected Set<URL> searchResources(ClassLoader loader, String path) {
160         Set<URL> resources = new HashSet<URL>();
161         try {
162             Enumeration<URL> e = loader.getResources(path);
163             while (e.hasMoreElements()) {
164                 resources.add(e.nextElement());
165             }
166         } catch (IOException e) {
167             log.warn("Unable to retrieved resources from classloader: "
168                     + loader, e);
169         }
170         return resources;
171     }
172 }