001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.context;
021    
022    import org.apache.myfaces.tobago.config.TobagoConfig;
023    
024    import javax.faces.context.FacesContext;
025    import javax.servlet.ServletContext;
026    import javax.servlet.ServletException;
027    import java.util.Map;
028    
029    public class ResourceManagerFactory {
030    
031      public static final String RESOURCE_MANAGER = "org.apache.myfaces.tobago.context.ResourceManager";
032    
033      private ResourceManagerFactory() {
034      }
035    
036      private static boolean initialized;
037    
038      public static ResourceManager getResourceManager(FacesContext facesContext) {
039        assert initialized;
040        return (ResourceManager) facesContext.getExternalContext().getApplicationMap().get(RESOURCE_MANAGER);
041      }
042    
043      public static ResourceManager getResourceManager(ServletContext servletContext) {
044        assert initialized;
045        return (ResourceManager) servletContext.getAttribute(RESOURCE_MANAGER);
046      }
047    
048      public static void init(ServletContext servletContext, TobagoConfig tobagoConfig)
049          throws ServletException {
050        assert !initialized;
051        ResourceManagerImpl resourceManager = new ResourceManagerImpl(tobagoConfig);
052    
053        ThemeBuilder themeBuilder = new ThemeBuilder();
054        ResourceLocator resourceLocator = new ResourceLocator(servletContext, resourceManager, themeBuilder);
055        resourceLocator.locate();
056        Map<String, Theme> availableThemes = themeBuilder.resolveThemes(tobagoConfig.getRenderersConfig());
057        tobagoConfig.setAvailableThemes(availableThemes);
058        for (Theme theme : availableThemes.values()) {
059          tobagoConfig.addResourceDir(theme.getResourcePath());
060        }
061    
062        servletContext.setAttribute(RESOURCE_MANAGER, resourceManager);
063    
064        initialized = true;
065      }
066    
067      public static void release(ServletContext servletContext) {
068        assert initialized;
069        initialized = false;
070        servletContext.removeAttribute(RESOURCE_MANAGER);
071      }
072    }