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  
20  package org.apache.myfaces.tobago.internal.context;
21  
22  import org.apache.myfaces.tobago.context.ResourceManager;
23  import org.apache.myfaces.tobago.internal.config.TobagoConfigImpl;
24  
25  import javax.faces.context.FacesContext;
26  import javax.servlet.ServletContext;
27  import javax.servlet.ServletException;
28  
29  public final class ResourceManagerFactory {
30  
31    public static final String RESOURCE_MANAGER
32        = "org.apache.myfaces.tobago.context.ResourceManager";
33  
34    private ResourceManagerFactory() {
35    }
36  
37    private static boolean initialized;
38  
39    public static ResourceManager getResourceManager(final FacesContext facesContext) {
40      assert initialized;
41      return (ResourceManager) facesContext.getExternalContext()
42          .getApplicationMap().get(RESOURCE_MANAGER);
43    }
44  
45    public static ResourceManager getResourceManager(final ServletContext servletContext) {
46      assert initialized;
47      return (ResourceManager) servletContext.getAttribute(RESOURCE_MANAGER);
48    }
49  
50    public static void init(
51        final ServletContext servletContext, final TobagoConfigImpl tobagoConfig)
52        throws ServletException {
53      assert !initialized;
54      final ResourceManagerImpl resourceManager = new ResourceManagerImpl(tobagoConfig);
55  
56      final ResourceLocator resourceLocator = new ResourceLocator(servletContext, resourceManager);
57      resourceLocator.locate();
58  
59      servletContext.setAttribute(RESOURCE_MANAGER, resourceManager);
60  
61      initialized = true;
62    }
63  
64    public static void release(final ServletContext servletContext) {
65      assert initialized;
66      initialized = false;
67      servletContext.removeAttribute(RESOURCE_MANAGER);
68    }
69  }