001    package org.apache.myfaces.tobago.config;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.myfaces.tobago.context.Theme;
023    import org.apache.myfaces.tobago.context.RenderersConfig;
024    
025    import javax.faces.context.FacesContext;
026    import java.util.ArrayList;
027    import java.util.Collections;
028    import java.util.Iterator;
029    import java.util.List;
030    import java.util.Map;
031    
032    public class TobagoConfig {
033      private static final Log LOG = LogFactory.getLog(TobagoConfig.class);
034    
035      public static final String TOBAGO_CONFIG
036          = "org.apache.myfaces.tobago.config.TobagoConfig";
037    
038      private List<Theme> supportedThemes;
039      private List<String> supportedThemeNames;
040      private Theme defaultTheme;
041      private String defaultThemeName;
042      private List<String> resourceDirs;
043      private List<MappingRule> mappingRules;
044      private boolean ajaxEnabled;
045      private Map<String, Theme> availableTheme;
046      private RenderersConfig renderersConfig;
047    
048    
049      public TobagoConfig() {
050        supportedThemeNames = new ArrayList<String>();
051        supportedThemes = new ArrayList<Theme>();
052        resourceDirs = new ArrayList<String>();
053        ajaxEnabled = true;
054      }
055    
056      public void addMappingRule(MappingRule mappingRule) {
057        if (LOG.isDebugEnabled()) {
058          LOG.debug("addMappingRule: {" + mappingRule + "}");
059        }
060    
061        if (mappingRules == null) {
062          mappingRules = new ArrayList<MappingRule>();
063        }
064        mappingRules.add(mappingRule);
065      }
066    
067      public void addSupportedThemeName(String name) {
068        supportedThemeNames.add(name);
069      }
070    
071      public void resolveThemes() {
072    
073        defaultTheme = availableTheme.get(defaultThemeName);
074        checkThemeIsAvailable(defaultThemeName, defaultTheme);
075        if (LOG.isDebugEnabled()) {
076          LOG.debug("name = '" + defaultThemeName + "'");
077          LOG.debug("defaultTheme = '" + defaultTheme + "'");
078        }
079    
080        for (String name : supportedThemeNames) {
081          Theme theme = availableTheme.get(name);
082          checkThemeIsAvailable(name, theme);
083          supportedThemes.add(theme);
084          if (LOG.isDebugEnabled()) {
085            LOG.debug("name = '" + name + "'");
086            LOG.debug("supportedThemes.last() = '" + supportedThemes.get(supportedThemes.size() - 1) + "'");
087          }
088        }
089      }
090    
091      private void checkThemeIsAvailable(String name, Theme theme) {
092        if (theme == null) {
093          String error = "Theme not found! name: '" + name + "'. "
094              + "Please ensure you have a tobago-theme.xml file in your "
095              + "theme jar. Found the following themes: " + availableTheme.keySet();
096          LOG.error(error);
097          throw new RuntimeException(error);
098        }
099      }
100    
101    
102      public static TobagoConfig getInstance(FacesContext facesContext) {
103        return (TobagoConfig) facesContext.getExternalContext()
104            .getApplicationMap().get(TOBAGO_CONFIG);
105      }
106    
107      public MappingRule getMappingRule(String requestUri) {
108        for (Iterator i = getMappingRules(); i.hasNext();) {
109          MappingRule rule = (MappingRule) i.next();
110          if (rule.getRequestUri().equals(requestUri)) {
111            return rule;
112          }
113        }
114        return null;
115      }
116    
117      public Iterator<MappingRule> getMappingRules() {
118        if (mappingRules == null) {
119          List<MappingRule> objects = Collections.emptyList();
120          return objects.iterator();
121        } else {
122          return mappingRules.iterator();
123        }
124      }
125    
126      public Theme getTheme(String name) {
127        if (name == null) {
128          LOG.debug("searching theme: null");
129          return defaultTheme;
130        }
131        if (defaultTheme.getName().equals(name)) {
132          return defaultTheme;
133        }
134        for (Theme theme : supportedThemes) {
135          if (theme.getName().equals(name)) {
136            return theme;
137          }
138        }
139        LOG.debug("searching theme '" + name + "' not found. "
140            + "Using default: " + defaultTheme);
141        return defaultTheme;
142      }
143    
144      public void setDefaultThemeName(String defaultThemeName) {
145        this.defaultThemeName = defaultThemeName;
146      }
147    
148      public List<Theme> getSupportedThemes() {
149        return Collections.unmodifiableList(supportedThemes);
150      }
151    
152      public void addResourceDir(String resourceDir) {
153        if (!resourceDirs.contains(resourceDir)) {
154          if (LOG.isInfoEnabled()) {
155            LOG.info("adding resourceDir = '" + resourceDir + "'");
156          }
157          resourceDirs.add(resourceDir);
158        }
159      }
160    
161      public List<String> getResourceDirs() {
162        return resourceDirs;
163      }
164    
165      public boolean isAjaxEnabled() {
166        return ajaxEnabled;
167      }
168    
169      public void setAjaxEnabled(String value) {
170        this.ajaxEnabled = Boolean.valueOf(value);
171      }
172    
173      @Deprecated
174      public void setLoadThemesFromClasspath(String loadThemesFromClasspath) {
175        LOG.warn("Deprecated: setting load-theme-resources-from-classpath is "
176            + "no longer supported");
177      }
178    
179      public Theme getDefaultTheme() {
180        return defaultTheme;
181      }
182    
183      public void setAvailableThemes(Map<String, Theme> availableTheme) {
184        this.availableTheme = availableTheme;
185      }
186    
187      public RenderersConfig getRenderersConfig() {
188        return renderersConfig;
189      }
190    
191      public void setRenderersConfig(RenderersConfig renderersConfig) {
192        this.renderersConfig = renderersConfig;
193      }
194    }
195