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;
20  
21  import javax.el.ELException;
22  import javax.faces.FacesException;
23  import javax.faces.view.facelets.FaceletException;
24  import java.io.IOException;
25  import java.net.URL;
26  import javax.faces.view.facelets.FaceletContext;
27  
28  /**
29   * FaceletFactory for producing Facelets relative to the context of the underlying implementation.
30   * 
31   * @author Jacob Hookom
32   * @version $Id: FaceletFactory.java 1470662 2013-04-22 19:04:45Z lu4242 $
33   */
34  public abstract class FaceletFactory
35  {
36  
37      private static ThreadLocal<FaceletFactory> instance = new ThreadLocal<FaceletFactory>();
38  
39      /**
40       * Return a Facelet instance as specified by the file at the passed URI.
41       * 
42       * @param uri
43       * @return
44       * @throws IOException
45       * @throws FaceletException
46       * @throws FacesException
47       * @throws ELException
48       */
49      public abstract Facelet getFacelet(String uri) throws IOException;
50      
51      /**
52       * Create a Facelet from the passed URL. This method checks if the cached Facelet needs to be refreshed before
53       * returning. If so, uses the passed URL to build a new instance;
54       * 
55       * @param url
56       *            source url
57       * @return Facelet instance
58       * @throws IOException
59       * @throws FaceletException
60       * @throws FacesException
61       * @throws ELException
62       */
63      public abstract Facelet getFacelet(URL url) throws IOException, FaceletException, FacesException, ELException;
64  
65      /**
66       * Create a Facelet from the passed URL, but take into account the context. This method is
67       * useful in cases where the facelet instance must replace the one in the cache based on 
68       * the context, instead take the one from the cache, like for example when the EL expression
69       * cache is used.
70       * 
71       * @param url
72       *            source url
73       * @return Facelet instance
74       * @throws IOException
75       * @throws FaceletException
76       * @throws FacesException
77       * @throws ELException
78       */
79      public abstract Facelet getFacelet(FaceletContext ctx, URL url) 
80          throws IOException, FaceletException, FacesException, ELException;    
81      
82      /**
83       * Return a Facelet instance as specified by the file at the passed URI. The returned facelet is used
84       * to create view metadata in this form: 
85       * <p>
86       * UIViewRoot(in facet javax_faces_metadata(one or many UIViewParameter instances))
87       * </p>
88       * <p>
89       * This method should be called from FaceletViewMetadata.createMetadataView(FacesContext context)  
90       * </p>
91       * 
92       * @since 2.0
93       * @param uri
94       * @return
95       * @throws IOException
96       */
97      public abstract Facelet getViewMetadataFacelet(String uri) throws IOException;
98      
99      /**
100      * Create a Facelet used to create view metadata from the passed URL. This method checks if the 
101      * cached Facelet needs to be refreshed before returning. If so, uses the passed URL to build a new instance;
102      * 
103      * @since 2.0
104      * @param url source url
105      * @return Facelet instance
106      * @throws IOException
107      * @throws FaceletException
108      * @throws FacesException
109      * @throws ELException
110      */
111     public abstract Facelet getViewMetadataFacelet(URL url)
112             throws IOException, FaceletException, FacesException, ELException;
113 
114     /**
115      * Return a Facelet instance as specified by the file at the passed URI. The returned facelet is used
116      * to create composite component metadata.
117      * <p>
118      * This method should be called from vdl.getComponentMetadata(FacesContext context)  
119      * </p>
120      * 
121      * @since 2.0.1
122      * @param uri
123      * @return
124      * @throws IOException
125      */
126     public abstract Facelet getCompositeComponentMetadataFacelet(String uri) throws IOException;
127     
128     /**
129      * Create a Facelet used to create composite component metadata from the passed URL. This method checks if the 
130      * cached Facelet needs to be refreshed before returning. If so, uses the passed URL to build a new instance.
131      * 
132      * @since 2.0.1
133      * @param url source url
134      * @return Facelet instance
135      * @throws IOException
136      * @throws FaceletException
137      * @throws FacesException
138      * @throws ELException
139      */
140     public abstract Facelet getCompositeComponentMetadataFacelet(URL url)
141             throws IOException, FaceletException, FacesException, ELException;
142     
143     /**
144      * Set the static instance
145      * 
146      * @param factory
147      */
148     public static final void setInstance(FaceletFactory factory)
149     {
150         if (factory == null)
151         {
152             instance.remove();
153         }
154         else
155         {
156             instance.set(factory);
157         }
158     }
159 
160     /**
161      * Get the static instance
162      * 
163      * @return
164      */
165     public static final FaceletFactory getInstance()
166     {
167         return instance.get();
168     }
169 }