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(FacesContext facesContext) {
40      assert initialized;
41      return (ResourceManager) facesContext.getExternalContext()
42          .getApplicationMap().get(RESOURCE_MANAGER);
43    }
44  
45    public static ResourceManager getResourceManager(ServletContext servletContext) {
46      assert initialized;
47      return (ResourceManager) servletContext.getAttribute(RESOURCE_MANAGER);
48    }
49  
50    public static void init(
51        ServletContext servletContext, TobagoConfigImpl tobagoConfig)
52        throws ServletException {
53      assert !initialized;
54      ResourceManagerImpl resourceManager= new ResourceManagerImpl(tobagoConfig);
55  
56      ThemeBuilder themeBuilder = new ThemeBuilder(tobagoConfig);
57      ResourceLocator resourceLocator = new ResourceLocator(servletContext, resourceManager, themeBuilder);
58      resourceLocator.locate();
59      themeBuilder.resolveThemes();
60  
61      servletContext.setAttribute(RESOURCE_MANAGER, resourceManager);
62  
63      initialized = true;
64    }
65  
66    public static void release(ServletContext servletContext) {
67      assert initialized;
68      initialized = false;
69      servletContext.removeAttribute(RESOURCE_MANAGER);
70    }
71  }