View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.resource;
20  
21  import java.io.IOException;
22  
23  import java.net.URL;
24  import java.net.URLConnection;
25  
26  
27  /**
28   * Base class for resource loaders.  Resource loaders can lookup resources
29   * as URLs from arbitrary locations, including JAR files.
30   *
31   */
32  public class ResourceLoader
33  {
34    /**
35     * Returns the shared resource loader that always returns null.
36     *
37     * @return null for any resource path
38     */
39    public static ResourceLoader getNullResourceLoader()
40    {
41      return _NULL_RESOURCE_LOADER;
42    }
43  
44    /**
45     * Finds the resource with the given name.  A resource is some data
46     * (images, audio, text, etc) that can be accessed by class code in a way
47     * that is independent of the location of the code.
48     *
49     * <p> The name of a resource is a '<tt>/</tt>'-separated path name that
50     * identifies the resource.
51     *
52     * <p> This method will first search the this resource loader for the
53     * resource.  That failing, this method will invoke
54     * {@link #findResource(String)} to on the parent resource loader to
55     * find the resource.  </p>
56     *
57     * @param  name  the resource name
58     *
59     * @return  A <tt>URL</tt> object for reading the resource, or
60     *          <tt>null</tt> if the resource could not be found or the invoker
61     *          doesn't have adequate privileges to get the resource.
62     */
63    public URL getResource(
64      String name
65      ) throws IOException
66    {
67      URL url = findResource(name);
68  
69      if (url == null && _parent != null)
70      {
71        url = _parent.getResource(name);
72      }
73  
74      return url;
75    }
76  
77    /**
78     * Returns whether a resource is cachable.
79     * @return
80     */
81    public boolean isCachable()
82    {
83      return _parent == null || _parent.isCachable();
84    }
85  
86    @Override public String toString()
87    {
88      StringBuilder result = new StringBuilder();
89      result.append(super.toString());
90      
91      if (_parent != null)
92      {
93        result.append("("+_parent.toString()+")");
94      }
95      return result.toString();
96    }
97  
98    /**
99     * Finds the resource with the given name. Resource loader implementations
100    * should override this method to specify where to find resources.  </p>
101    *
102    * @param  name
103    *         The resource name
104    *
105    * @return  A <tt>URL</tt> object for reading the resource, or
106    *          <tt>null</tt> if the resource could not be found
107    */
108   protected URL findResource(
109     String name
110     ) throws IOException
111   {
112     return null;
113   }
114 
115   /**
116    * Called by the {@link org.apache.myfaces.trinidad.webapp.ResourceServlet} to get the content type for
117    * the given connection.
118    *
119    * @param loader the loader that provided the URL for the URL connection
120    * @param conn the URL connection
121    * @return the content type for this URL connection
122    */
123   public static String getContentType(
124     ResourceLoader loader,
125     URLConnection  conn)
126   {
127     return loader.getContentType(conn);
128   }
129 
130   /**
131    * Returns the content type of this URL connection.
132    * @return the content type
133    */
134   protected String getContentType(
135     URLConnection conn)
136   {
137     return conn.getContentType();
138   }
139 
140   /**
141    * Returns the parent resource loader, or null if this is a root
142    * resource loader.
143    *
144    * @return  the parent resource loader
145    */
146   protected ResourceLoader getParent()
147   {
148     return _parent;
149   }
150 
151   /**
152    * Constructs a new resource loader with specified parent resource loader.
153    *
154    * @param parent  the parent resource loader
155    */
156   protected ResourceLoader(
157     ResourceLoader parent)
158   {
159     _parent = parent;
160   }
161 
162   /**
163    * Constructs a new root resource loader.
164    */
165   protected ResourceLoader()
166   {
167     this(null);
168   }
169 
170   private static final ResourceLoader _NULL_RESOURCE_LOADER =
171                                                       new ResourceLoader();
172 
173   private final ResourceLoader _parent;
174 
175 }