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.view.facelets.impl;
20  
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.lang.reflect.Method;
24  import java.net.URL;
25  import java.net.URLConnection;
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.logging.Level;
29  import java.util.logging.Logger;
30  import java.util.regex.Pattern;
31  
32  import javax.el.ELException;
33  import javax.faces.FacesException;
34  import javax.faces.FactoryFinder;
35  import javax.faces.view.facelets.FaceletCache;
36  import javax.faces.view.facelets.FaceletCacheFactory;
37  import javax.faces.view.facelets.FaceletException;
38  import javax.faces.view.facelets.FaceletHandler;
39  import javax.faces.view.facelets.ResourceResolver;
40  
41  import org.apache.myfaces.shared.resource.ResourceLoaderUtils;
42  import org.apache.myfaces.view.facelets.Facelet;
43  import org.apache.myfaces.view.facelets.FaceletFactory;
44  import org.apache.myfaces.view.facelets.compiler.Compiler;
45  import org.apache.myfaces.view.facelets.util.ParameterCheck;
46  
47  /**
48   * Default FaceletFactory implementation.
49   * 
50   * @author Jacob Hookom
51   * @version $Id: DefaultFaceletFactory.java 1341409 2012-05-22 11:18:13Z lu4242 $
52   */
53  public final class DefaultFaceletFactory extends FaceletFactory
54  {
55      private static final long INFINITE_DELAY = -1;
56      private static final long NO_CACHE_DELAY = 0;
57      
58      //protected final Log log = LogFactory.getLog(DefaultFaceletFactory.class);
59      protected final Logger log = Logger.getLogger(DefaultFaceletFactory.class.getName());
60  
61      private URL _baseUrl;
62  
63      private Compiler _compiler;
64      
65      //private Map<String, DefaultFacelet> _facelets;
66      
67      //private Map<String, DefaultFacelet> _viewMetadataFacelets;
68      
69      private Map<String, DefaultFacelet> _compositeComponentMetadataFacelets;
70  
71      private long _refreshPeriod;
72  
73      private Map<String, URL> _relativeLocations;
74  
75      private javax.faces.view.facelets.ResourceResolver _resolver;
76      
77      private FaceletCache<Facelet> _faceletCache;
78      
79      public DefaultFaceletFactory(Compiler compiler, ResourceResolver resolver) throws IOException
80      {
81          this(compiler, resolver, -1);
82      }
83  
84      public DefaultFaceletFactory(Compiler compiler, ResourceResolver resolver, long refreshPeriod)
85      {
86          ParameterCheck.notNull("compiler", compiler);
87          ParameterCheck.notNull("resolver", resolver);
88  
89          _compiler = compiler;
90  
91          //_facelets = new HashMap<String, DefaultFacelet>();
92          
93          //_viewMetadataFacelets = new HashMap<String, DefaultFacelet>();
94          
95          _compositeComponentMetadataFacelets = new HashMap<String, DefaultFacelet>();
96  
97          _relativeLocations = new HashMap<String, URL>();
98  
99          _resolver = resolver;
100 
101         _baseUrl = resolver.resolveUrl("/");
102 
103         _refreshPeriod = refreshPeriod < 0 ? INFINITE_DELAY : refreshPeriod * 1000;
104         
105         // facelet cache. Lookup here, because after all this is a "part" of the facelet factory implementation.
106         FaceletCacheFactory cacheFactory
107                 = (FaceletCacheFactory) FactoryFinder.getFactory(FactoryFinder.FACELET_CACHE_FACTORY);
108         _faceletCache = (FaceletCache<Facelet>) cacheFactory.getFaceletCache();
109         
110         FaceletCache.MemberFactory<Facelet> faceletFactory = new FaceletCache.MemberFactory<Facelet>()
111         {
112             public Facelet newInstance(URL url) throws IOException
113             {
114                 return _createFacelet(url);
115             }
116         };
117         FaceletCache.MemberFactory<Facelet> viewMetadataFaceletFactory = new FaceletCache.MemberFactory<Facelet>()
118         {
119             public Facelet newInstance(URL url) throws IOException
120             {
121                 return _createViewMetadataFacelet(url);
122             }
123         };
124         
125         // Note that FaceletCache.setMemberFactories method is protected, and this is the place where call
126         // this method has sense, because DefaultFaceletFactory is the responsible to create Facelet instances.
127         // The only way to do it is using reflection, and it has sense, because in this way it is possible to
128         // setup a java SecurityManager that prevents call this method (because it is protected, and to do that
129         // the code first check for "suppressAccessChecks" permission).
130         try
131         {
132             Method setMemberFactoriesMethod = FaceletCache.class.getDeclaredMethod("setMemberFactories",
133                     new Class[]{FaceletCache.MemberFactory.class, FaceletCache.MemberFactory.class});
134             setMemberFactoriesMethod.setAccessible(true);
135             setMemberFactoriesMethod.invoke(_faceletCache, faceletFactory, viewMetadataFaceletFactory);
136         } 
137         catch (Exception e)
138         {
139             throw new FacesException("Cannot call setMemberFactories method, Initialization of FaceletCache failed.",
140                                      e);
141         }
142 
143         if (log.isLoggable(Level.FINE))
144         {
145             log.fine("Using ResourceResolver: " + _resolver);
146             log.fine("Using Refresh Period: " + _refreshPeriod);
147         }
148     }
149 
150     /**
151      * Compiler this factory uses
152      * 
153      * @return final Compiler instance
154      */
155     public Compiler getCompiler()
156     {
157         return _compiler;
158     }
159 
160     /*
161      * (non-Javadoc)
162      * 
163      * @see org.apache.myfaces.view.facelets.FaceletFactory#getFacelet(java.lang.String)
164      */
165     public Facelet getFacelet(String uri) throws IOException, FaceletException, FacesException, ELException
166     {
167         URL url = (URL) _relativeLocations.get(uri);
168         if (url == null)
169         {
170             url = resolveURL(_baseUrl, uri);
171             if (url != null)
172             {
173                 Map<String, URL> newLoc = new HashMap<String, URL>(_relativeLocations);
174                 newLoc.put(uri, url);
175                 _relativeLocations = newLoc;
176             }
177             else
178             {
179                 throw new IOException("'" + uri + "' not found.");
180             }
181         }
182         return this.getFacelet(url);
183     }
184 
185     /**
186      * Create a Facelet from the passed URL. This method checks if the cached Facelet needs to be refreshed before
187      * returning. If so, uses the passed URL to build a new instance;
188      * 
189      * @param url
190      *            source url
191      * @return Facelet instance
192      * @throws IOException
193      * @throws FaceletException
194      * @throws FacesException
195      * @throws ELException
196      */
197     public Facelet getFacelet(URL url) throws IOException, FaceletException, FacesException, ELException
198     {
199         return _faceletCache.getFacelet(url);
200     }
201 
202     public long getRefreshPeriod()
203     {
204         return _refreshPeriod;
205     }
206 
207     /**
208      * Resolves a path based on the passed URL. If the path starts with '/', then resolve the path against
209      * {@link javax.faces.context.ExternalContext#getResource(java.lang.String)
210      * javax.faces.context.ExternalContext#getResource(java.lang.String)}. Otherwise create a new URL via
211      * {@link URL#URL(java.net.URL, java.lang.String) URL(URL, String)}.
212      * 
213      * @param source
214      *            base to resolve from
215      * @param path
216      *            relative path to the source
217      * @return resolved URL
218      * @throws IOException
219      */
220     public URL resolveURL(URL source, String path) throws IOException
221     {
222         if (path.startsWith("/"))
223         {
224             URL url = _resolver.resolveUrl(path);
225             if (url == null)
226             {
227                 throw new FileNotFoundException(path + " Not Found in ExternalContext as a Resource");
228             }
229             return url;
230         }
231         else
232         {
233             return new URL(source, path);
234         }
235     }
236 
237     /**
238      * Template method for determining if the Facelet needs to be refreshed.
239      * 
240      * @param facelet
241      *            Facelet that could have expired
242      * @return true if it needs to be refreshed
243      */
244     protected boolean needsToBeRefreshed(DefaultFacelet facelet)
245     {
246         // if set to 0, constantly reload-- nocache
247         if (_refreshPeriod == NO_CACHE_DELAY)
248         {
249             return true;
250         }
251 
252         // if set to -1, never reload
253         if (_refreshPeriod == INFINITE_DELAY)
254         {
255             return false;
256         }
257 
258         long target = facelet.getCreateTime() + _refreshPeriod;
259         if (System.currentTimeMillis() > target)
260         {
261             // Should check for file modification
262 
263             try
264             {
265                 URLConnection conn = facelet.getSource().openConnection();
266                 long lastModified = ResourceLoaderUtils.getResourceLastModified(conn);
267 
268                 return lastModified == 0 || lastModified > target;
269             }
270             catch (IOException e)
271             {
272                 throw new FaceletException("Error Checking Last Modified for " + facelet.getAlias(), e);
273             }
274         }
275 
276         return false;
277     }
278 
279     /**
280      * Uses the internal Compiler reference to build a Facelet given the passed URL.
281      * 
282      * @param url
283      *            source
284      * @return a Facelet instance
285      * @throws IOException
286      * @throws FaceletException
287      * @throws FacesException
288      * @throws ELException
289      */
290     private DefaultFacelet _createFacelet(URL url) throws IOException, FaceletException, FacesException, ELException
291     {
292         if (log.isLoggable(Level.FINE))
293         {
294             log.fine("Creating Facelet for: " + url);
295         }
296 
297         String alias = "/" + _removeFirst(url.getFile(), _baseUrl.getFile());
298         try
299         {
300             FaceletHandler h = _compiler.compile(url, alias);
301             DefaultFacelet f = new DefaultFacelet(this, _compiler.createExpressionFactory(), url, alias, alias, h);
302             return f;
303         }
304         catch (FileNotFoundException fnfe)
305         {
306             throw new FileNotFoundException("Facelet " + alias + " not found at: " + url.toExternalForm());
307         }
308     }
309     
310     /**
311      * @since 2.0
312      * @param url
313      * @return
314      * @throws IOException
315      * @throws FaceletException
316      * @throws FacesException
317      * @throws ELException
318      */
319     private DefaultFacelet _createViewMetadataFacelet(URL url)
320             throws IOException, FaceletException, FacesException, ELException
321     {
322         if (log.isLoggable(Level.FINE))
323         {
324             log.fine("Creating Facelet used to create View Metadata for: " + url);
325         }
326 
327         // The alias is used later for informative purposes, so we append 
328         // some prefix to identify later where the errors comes from.
329         String faceletId = "/"+ _removeFirst(url.getFile(), _baseUrl.getFile());
330         String alias = "/viewMetadata" + faceletId;
331         try
332         {
333             FaceletHandler h = _compiler.compileViewMetadata(url, alias);
334             DefaultFacelet f = new DefaultFacelet(this, _compiler.createExpressionFactory(), url, alias, 
335                     faceletId, h);
336             return f;
337         }
338         catch (FileNotFoundException fnfe)
339         {
340             throw new FileNotFoundException("Facelet " + alias + " not found at: " + url.toExternalForm());
341         }
342 
343     }
344     
345     /**
346      * @since 2.0.1
347      * @param url
348      * @return
349      * @throws IOException
350      * @throws FaceletException
351      * @throws FacesException
352      * @throws ELException
353      */
354     private DefaultFacelet _createCompositeComponentMetadataFacelet(URL url)
355             throws IOException, FaceletException, FacesException, ELException
356     {
357         if (log.isLoggable(Level.FINE))
358         {
359             log.fine("Creating Facelet used to create Composite Component Metadata for: " + url);
360         }
361 
362         // The alias is used later for informative purposes, so we append 
363         // some prefix to identify later where the errors comes from.
364         String alias = "/compositeComponentMetadata/" + _removeFirst(url.getFile(), _baseUrl.getFile());
365         try
366         {
367             FaceletHandler h = _compiler.compileCompositeComponentMetadata(url, alias);
368             DefaultFacelet f = new DefaultFacelet(this, _compiler.createExpressionFactory(), url, alias,
369                     alias, h, true);
370             return f;
371         }
372         catch (FileNotFoundException fnfe)
373         {
374             throw new FileNotFoundException("Facelet " + alias + " not found at: " + url.toExternalForm());
375         }
376     }
377 
378     /**
379      * Works in the same way as getFacelet(String uri), but redirect
380      * to getViewMetadataFacelet(URL url)
381      * @since 2.0
382      */
383     @Override
384     public Facelet getViewMetadataFacelet(String uri) throws IOException
385     {
386         URL url = (URL) _relativeLocations.get(uri);
387         if (url == null)
388         {
389             url = resolveURL(_baseUrl, uri);
390             if (url != null)
391             {
392                 Map<String, URL> newLoc = new HashMap<String, URL>(_relativeLocations);
393                 newLoc.put(uri, url);
394                 _relativeLocations = newLoc;
395             }
396             else
397             {
398                 throw new IOException("'" + uri + "' not found.");
399             }
400         }
401         return this.getViewMetadataFacelet(url);
402     }
403 
404     /**
405      * @since 2.0
406      */
407     @Override
408     public Facelet getViewMetadataFacelet(URL url) throws IOException,
409             FaceletException, FacesException, ELException
410     {
411         return _faceletCache.getViewMetadataFacelet(url);
412     }
413     
414     /**
415      * Works in the same way as getFacelet(String uri), but redirect
416      * to getViewMetadataFacelet(URL url)
417      * @since 2.0.1
418      */
419     @Override
420     public Facelet getCompositeComponentMetadataFacelet(String uri) throws IOException
421     {
422         URL url = (URL) _relativeLocations.get(uri);
423         if (url == null)
424         {
425             url = resolveURL(_baseUrl, uri);
426             if (url != null)
427             {
428                 Map<String, URL> newLoc = new HashMap<String, URL>(_relativeLocations);
429                 newLoc.put(uri, url);
430                 _relativeLocations = newLoc;
431             }
432             else
433             {
434                 throw new IOException("'" + uri + "' not found.");
435             }
436         }
437         return this.getCompositeComponentMetadataFacelet(url);
438     }
439 
440     /**
441      * @since 2.0.1
442      */
443     @Override
444     public Facelet getCompositeComponentMetadataFacelet(URL url) throws IOException,
445             FaceletException, FacesException, ELException
446     {
447         ParameterCheck.notNull("url", url);
448         
449         String key = url.toString();
450         
451         DefaultFacelet f = _compositeComponentMetadataFacelets.get(key);
452         
453         if (f == null || this.needsToBeRefreshed(f))
454         {
455             f = this._createCompositeComponentMetadataFacelet(url);
456             if (_refreshPeriod != NO_CACHE_DELAY)
457             {
458                 Map<String, DefaultFacelet> newLoc
459                         = new HashMap<String, DefaultFacelet>(_compositeComponentMetadataFacelets);
460                 newLoc.put(key, f);
461                 _compositeComponentMetadataFacelets = newLoc;
462             }
463         }
464         
465         return f;
466     }
467 
468     /**
469      * Removes the first appearance of toRemove in string.
470      *
471      * Works just like string.replaceFirst(toRemove, ""), except that toRemove
472      * is not treated as a regex (which could cause problems with filenames).
473      *
474      * @param string
475      * @param toRemove
476      * @return
477      */
478     private String _removeFirst(String string, String toRemove)
479     {
480         // do exactly what String.replaceFirst(toRemove, "") internally does,
481         // except treating the search as literal text and not as regex
482 
483         return Pattern.compile(toRemove, Pattern.LITERAL).matcher(string).replaceFirst("");
484     }
485 
486 }