View Javadoc
1   /*
2    * $Id$
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  
22  package org.apache.tiles.servlet.wildcard;
23  
24  import java.io.IOException;
25  import java.net.URL;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import javax.servlet.ServletContext;
30  
31  import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
32  import org.springframework.core.io.Resource;
33  import org.springframework.core.io.support.ResourcePatternResolver;
34  import org.springframework.web.context.support.ServletContextResourcePatternResolver;
35  
36  /**
37   * Servlet-based implementation of the TilesApplicationContext interface that
38   * can resolve resources even using wildcards.
39   *
40   * @version $Rev$ $Date$
41   * @since 2.2.1
42   */
43  public class WildcardServletTilesApplicationContext extends
44          ServletTilesApplicationContext {
45  
46      /**
47       * The pattern resolver.
48       *
49       * @since 2.2.1
50       */
51      protected ResourcePatternResolver resolver;
52  
53      /**
54       * Constructor.
55       *
56       * @param servletContext The servlet context.
57       * @since 2.2.1
58       */
59      public WildcardServletTilesApplicationContext(ServletContext servletContext) {
60          super(servletContext);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public void initialize(ServletContext context) {
66          super.initialize(context);
67  
68          resolver = new ServletContextResourcePatternResolver(context);
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public URL getResource(String path) throws IOException {
74          URL retValue = null;
75          Set<URL> urlSet = getResources(path);
76          if (urlSet != null && !urlSet.isEmpty()) {
77              retValue = urlSet.iterator().next();
78          }
79          return retValue;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public Set<URL> getResources(String path) throws IOException {
85          Set<URL> urlSet = null;
86          Resource[] resources = resolver.getResources(path);
87          if (resources != null && resources.length > 0) {
88              urlSet = new HashSet<URL>();
89              for (int i = 0; i < resources.length; i++) {
90                  urlSet.add(resources[i].getURL());
91              }
92          }
93          return urlSet;
94      }
95  }