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