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.context;
20  
21  import java.util.WeakHashMap;
22  import java.util.Map;
23  
24  import javax.faces.context.ExternalContext;
25  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
26  
27  /**
28   * Factory for creating RequestContext objects.
29   * 
30   */
31  abstract public class RequestContextFactory
32  {
33    /**
34     * Retrieve the current RequestContextFactory.
35     */
36    static public RequestContextFactory getFactory()
37    {
38      synchronized (_FACTORIES)
39      {
40        return _FACTORIES.get(_getClassLoader());
41      }
42    }
43  
44    /**
45     * Store the current RequestContextFactory.
46     */
47    static public void setFactory(RequestContextFactory factory)
48    {
49      synchronized (_FACTORIES)
50      {
51        ClassLoader cl = _getClassLoader();
52        if (_FACTORIES.get(cl) != null)
53        {
54          throw new IllegalStateException(_LOG.getMessage(
55            "FACTORY_ALREADY_AVAILABLE_FOR_CLASS_LOADER"));
56        }
57  
58        _FACTORIES.put(cl, factory);
59      }
60    }
61    
62    /**
63     * Create a RequestContext from a ServletContext and ServletRequest.
64     * 
65     * @param context an object which must be a ServletContext
66     * @param request an object which must be a ServletRequest
67     * 
68     * @deprecated This method does not work in a Portal environment.  It will
69     *             only work with a ServletRequest.  Please use 
70     *             {@link #createContext(ExternalContext)} which is container
71     *             agnostic.
72     */
73    @Deprecated
74    abstract public RequestContext createContext(Object context,
75                                                  Object request);
76    
77    /**
78     * Creates a RequestContext.
79     * 
80     * @param ec The current ExternalContext.
81     */
82    abstract public RequestContext createContext(ExternalContext ec);
83    
84    static private ClassLoader _getClassLoader()
85    {
86      return Thread.currentThread().getContextClassLoader();
87    }
88  
89    static private final Map<ClassLoader, RequestContextFactory> _FACTORIES = 
90      new WeakHashMap<ClassLoader, RequestContextFactory>();
91    private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
92      RequestContextFactory.class);
93  }