2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.remoting.impl;
19  
20  import java.net.URL;
21  import java.util.ResourceBundle;
22  import javax.faces.context.FacesContext;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.shale.remoting.Constants;
26  import org.apache.shale.remoting.Processor;
27  
28  /***
29   * <p>Implementation of {@link Processor} which serves resources from the
30   * class path of the web application.  View identifiers shoud be a fully
31   * qualified path, beginning with a slash ("/") character (for example,
32   * <code>/org/apache/shale/remoting/MyResource.css</code>).</p>
33   */
34  public class ClassResourceProcessor extends AbstractResourceProcessor {
35  
36  
37      // ------------------------------------------------------------ Constructors
38  
39  
40  
41      // ------------------------------------------------------ Instance Variables
42  
43  
44      /***
45       * <p><code>ResourceBundle</code> containing our localized messages.</p>
46       */
47      private ResourceBundle bundle = ResourceBundle.getBundle("org.apache.shale.remoting.Bundle");
48  
49  
50      /***
51       * <p>Log instance for this class.</p>
52       */
53      private transient Log log = null;
54  
55  
56      // -------------------------------------------------------------- Properties
57  
58  
59      /***
60       * <p>Force our default excludes list to be included.</p>
61       *
62       * @param excludes Application specified excludes list
63       */
64      public void setExcludes(String excludes) {
65  
66          if ((excludes != null) && (excludes.length() > 0)) {
67              super.setExcludes(Constants.CLASS_RESOURCES_EXCLUDES_DEFAULT
68                                + "," + excludes);
69          } else {
70              super.setExcludes(Constants.CLASS_RESOURCES_EXCLUDES_DEFAULT);
71          }
72  
73      }
74  
75  
76      // -------------------------------------------------------- Abstract Methods
77  
78  
79      /*** {@inheritDoc} */
80      protected URL getResourceURL(FacesContext context, String resourceId) {
81  
82          // Disallow access to Java classes
83          String resourceIdLower = resourceId.toLowerCase();
84          if (resourceIdLower.endsWith(".class")) {
85              if (log().isWarnEnabled()) {
86                  log().warn(bundle.getString("resource.refuse"));
87                  log().warn(resourceId);
88              }
89              return null;
90          }
91  
92          // Acquire a reference to the ClassLoader for this web application
93          ClassLoader cl = Thread.currentThread().getContextClassLoader();
94  
95          // Return a URL to the class loader resource (if it exists)
96          try {
97              URL url = cl.getResource(resourceId.substring(1));
98              if (log().isDebugEnabled()) {
99                  log().debug("getResource(" + resourceId + ") --> " + url);
100             }
101             return url;
102         } catch (Exception e) {
103             if (log().isErrorEnabled()) {
104                 log().error(bundle.getString("resource.exception"), e);
105                 log().error(resourceId);
106             }
107             return null;
108         }
109 
110     }
111 
112 
113 
114     // --------------------------------------------------------- Private Methods
115 
116 
117 
118     /***
119      * <p>Return the <code>Log</code> instance to use, creating one if needed.</p>
120      */
121     private Log log() {
122 
123         if (this.log == null) {
124             log = LogFactory.getLog(ClassResourceProcessor.class);
125         }
126         return log;
127 
128     }
129 
130 
131 }