View Javadoc

1   /*
2    * $Id: AbstractRenderKitRegistryImpl.java,v 1.18 2011/06/16 09:29:40 jbmeslin Exp $
3    */
4   package org.rcfaces.core.internal.config;
5   
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   import javax.faces.context.FacesContext;
10  import javax.faces.render.RenderKitFactory;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  
15  /**
16   * 
17   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
18   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:40 $
19   */
20  abstract class AbstractRenderKitRegistryImpl {
21      private static final String REVISION = "$Revision: 1.18 $";
22  
23      private static final Log LOG = LogFactory
24              .getLog(AbstractRenderKitRegistryImpl.class);
25  
26      private static final Object LOAD_CONFIG_LOCK = new Object();
27  
28      private transient Map renderKitsById = null;
29  
30      synchronized Map initialize(FacesContext facesContext) {
31          synchronized (LOAD_CONFIG_LOCK) {
32              if (renderKitsById != null) {
33                  return renderKitsById;
34              }
35  
36              Map applicationMap = null;
37              String applicationPropertyId = getApplicationPropertyId();
38              if (applicationPropertyId != null) {
39  
40                  if (facesContext == null) {
41                      facesContext = FacesContext.getCurrentInstance();
42                  }
43  
44                  applicationMap = facesContext.getExternalContext()
45                          .getApplicationMap();
46  
47                  renderKitsById = (Map) applicationMap
48                          .get(applicationPropertyId);
49              }
50  
51              if (renderKitsById == null) {
52                  renderKitsById = new HashMap(16);
53  
54                  if (applicationMap != null) {
55                      applicationMap.put(applicationPropertyId, renderKitsById);
56                  }
57              }
58  
59              return renderKitsById;
60          }
61      }
62  
63      protected String getApplicationPropertyId() {
64          return null;
65      }
66  
67      protected final RenderKit getRenderKit(FacesContext facesContext,
68              String renderKitId) {
69  
70          if (renderKitId == null) {
71              if (facesContext == null) {
72                  facesContext = FacesContext.getCurrentInstance();
73              }
74  
75              renderKitId = facesContext.getViewRoot().getRenderKitId();
76  
77              if (LOG.isDebugEnabled()) {
78                  LOG.debug("ViewRoot render kit id='" + renderKitId + "'.");
79              }
80          }
81  
82          if (renderKitId == null) {
83              renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
84          }
85  
86          Map renderKitsById = initialize(facesContext);
87  
88          RenderKit renderKit = (RenderKit) renderKitsById.get(renderKitId);
89  
90          if (LOG.isDebugEnabled()) {
91              LOG.debug("getRenderKit: returns '" + renderKit
92                      + "' for renderKitId='" + renderKitId + "'.");
93          }
94  
95          return renderKit;
96      }
97  
98      protected RenderKit allocate(String renderKitId) {
99          if (renderKitId == null) {
100             renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
101         }
102 
103         if (renderKitsById == null) {
104             renderKitsById = new HashMap();
105         }
106 
107         RenderKit renderKit = (RenderKit) renderKitsById.get(renderKitId);
108         if (renderKit == null) {
109             renderKit = createRenderKit();
110             renderKitsById.put(renderKitId, renderKit);
111         }
112 
113         return renderKit;
114     }
115 
116     protected abstract RenderKit createRenderKit();
117 
118     /**
119      * 
120      * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
121      * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:40 $
122      */
123     protected static abstract class RenderKit {
124         private static final String REVISION = "$Revision: 1.18 $";
125 
126     }
127 }