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.skin;
20  
21  import java.util.WeakHashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import javax.faces.context.FacesContext;
26  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
27  
28  
29  /**
30   * Factory for creating Skin objects.
31   * 
32   */
33  abstract public class SkinFactory
34  {
35    /**
36     * Retrieve the current SkinFactory.
37     */
38    static public SkinFactory getFactory()
39    {
40      synchronized (_FACTORIES)
41      {
42        return _FACTORIES.get(_getClassLoader());
43      }
44    }
45  
46    /**
47     * Store the current SkinFactory.
48     */
49    static public void setFactory(SkinFactory factory)
50    {
51      synchronized (_FACTORIES)
52      {
53        ClassLoader cl = _getClassLoader();
54        if (_FACTORIES.get(cl) != null)
55        {
56          throw new IllegalStateException(_LOG.getMessage(
57            "FACTORY_ALREADY_AVAILABlE_FOR_THIS_CLASS_LOADER"));
58        }
59  
60        _FACTORIES.put(cl, factory);
61      }
62    }
63  
64    /**
65     * <p>Register the specified {@link Skin} instance, associated with
66     * the specified <code>skinId</code>, to be supported by this
67     * {@link SkinFactory}, replacing any previously registered
68     * {@link Skin} for this identifier.</p>
69     *
70     * @param skinId Identifier of the {@link Skin} to register
71     * @param skin {@link Skin} instance that we are registering
72     */
73    public abstract void addSkin(String skinId, Skin skin);
74  
75  
76    /**
77     * <p>Return a {@link Skin} instance for the specified skinId. 
78     * If there is no registered {@link
79     * Skin} for the specified identifier, return
80     * <code>null</code>.  The set of available skin identifiers
81     * is available via the <code>getSkinIds()</code> method.</p>
82     *
83     * @param context FacesContext for the request currently being
84     * processed, or <code>null</code> if none is available.
85     * @param skinId Skin identifier of the requested
86     *  {@link Skin} instance
87     */
88    public abstract Skin getSkin(FacesContext context, String skinId);
89  
90    /**
91     * <p>Return a {@link Skin} instance for the specified skinFamily and
92     * renderKitId. 
93     * If there is no registered {@link
94     * Skin} for the specified identifier, return
95     * <code>null</code>.  The set of available skin identifiers
96     * is available via the <code>getSkinIds()</code> method.</p>
97     *
98     * @param context FacesContext for the request currently being
99     * processed, or <code>null</code> if none is available.
100    * @param family family of the requested {@link Skin} instance
101    * @param renderKitId RenderKit identifier of the requested
102    *  {@link Skin} instance
103    */
104   public abstract Skin getSkin(
105     FacesContext context, 
106     String family, 
107     String renderKitId);
108   
109 
110   /**
111    * <p>Return a {@link Skin} instance for the specified skinFamily and
112    * renderKitId, and skin version. The best matched skin is returned. 
113    * If there is no registered {@link
114    * Skin} for the specified identifier, return
115    * <code>null</code>.  The set of available skin identifiers
116    * is available via the <code>getSkinIds()</code> method.</p>
117    *
118    * @param context FacesContext for the request currently being
119    * processed, or <code>null</code> if none is available.
120    * @param family family of the requested {@link Skin} instance
121    * @param renderKitId RenderKit identifier of the requested
122    *  {@link Skin} instance
123    *  @param version. A string that denotes the skin version name. It can be "default" or 
124    *  the name of the version (see the Skin's SkinVersion#getName) or null which 
125    *  returns the skin with no version set.
126    *  
127    */
128   public abstract Skin getSkin(
129     FacesContext context, 
130     String family,
131     String renderKitId,
132     String version);
133   
134   /**
135    * <p>Return an <code>Iterator</code> over the set of skin
136    * identifiers registered with this factory.
137    * </p>
138    */
139   public abstract Iterator<String> getSkinIds();
140 
141 
142   static private ClassLoader _getClassLoader()
143   {
144     return Thread.currentThread().getContextClassLoader();
145   }
146 
147   static private final Map<ClassLoader, SkinFactory> _FACTORIES = 
148     new WeakHashMap<ClassLoader, SkinFactory>();
149   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
150     SkinFactory.class);
151 }