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.RenderersConfig;
023    import org.apache.myfaces.tobago.context.Theme;
024    import org.apache.myfaces.tobago.util.Deprecation;
025    
026    import javax.faces.context.FacesContext;
027    import javax.servlet.ServletContext;
028    import java.util.ArrayList;
029    import java.util.Collections;
030    import java.util.Iterator;
031    import java.util.List;
032    import java.util.Map;
033    
034    public class TobagoConfig {
035      private static final Log LOG = LogFactory.getLog(TobagoConfig.class);
036    
037      public static final String TOBAGO_CONFIG
038          = "org.apache.myfaces.tobago.config.TobagoConfig";
039    
040      private List<Theme> supportedThemes;
041      private List<String> supportedThemeNames;
042      private Theme defaultTheme;
043      private String defaultThemeName;
044      private List<String> resourceDirs;
045      private List<MappingRule> mappingRules;
046      private boolean ajaxEnabled;
047      private boolean fixResourceOrder;
048      private boolean fixLayoutTransparency;
049      private boolean createSessionSecret;
050      private boolean checkSessionSecret;
051      private Map<String, Theme> availableTheme;
052      private RenderersConfig renderersConfig;
053    
054    
055      public TobagoConfig() {
056        supportedThemeNames = new ArrayList<String>();
057        supportedThemes = new ArrayList<Theme>();
058        resourceDirs = new ArrayList<String>();
059        ajaxEnabled = true;
060        fixResourceOrder = false;
061        fixLayoutTransparency = false;
062        createSessionSecret = false;
063        checkSessionSecret = false;
064      }
065    
066      public void addMappingRule(MappingRule mappingRule) {
067        Deprecation.LOG.warn("mapping rules are deprecated");
068        if (LOG.isDebugEnabled()) {
069          LOG.debug("addMappingRule: {" + mappingRule + "}");
070        }
071    
072        if (mappingRules == null) {
073          mappingRules = new ArrayList<MappingRule>();
074        }
075        mappingRules.add(mappingRule);
076      }
077    
078      public void addSupportedThemeName(String name) {
079        supportedThemeNames.add(name);
080      }
081    
082      public void resolveThemes() {
083    
084        defaultTheme = availableTheme.get(defaultThemeName);
085        checkThemeIsAvailable(defaultThemeName, defaultTheme);
086        if (LOG.isDebugEnabled()) {
087          LOG.debug("name = '" + defaultThemeName + "'");
088          LOG.debug("defaultTheme = '" + defaultTheme + "'");
089        }
090    
091        for (String name : supportedThemeNames) {
092          Theme theme = availableTheme.get(name);
093          checkThemeIsAvailable(name, theme);
094          supportedThemes.add(theme);
095          if (LOG.isDebugEnabled()) {
096            LOG.debug("name = '" + name + "'");
097            LOG.debug("supportedThemes.last() = '" + supportedThemes.get(supportedThemes.size() - 1) + "'");
098          }
099        }
100      }
101    
102      private void checkThemeIsAvailable(String name, Theme theme) {
103        if (theme == null) {
104          String error = "Theme not found! name: '" + name + "'. "
105              + "Please ensure you have a tobago-theme.xml file in your "
106              + "theme jar. Found the following themes: " + availableTheme.keySet();
107          LOG.error(error);
108          throw new RuntimeException(error);
109        }
110      }
111    
112    
113      public static TobagoConfig getInstance(FacesContext facesContext) {
114        return (TobagoConfig) facesContext.getExternalContext().getApplicationMap().get(TOBAGO_CONFIG);
115      }
116    
117      public static TobagoConfig getInstance(ServletContext servletContext) {
118        return (TobagoConfig) servletContext.getAttribute(TOBAGO_CONFIG);
119      }
120    
121      public MappingRule getMappingRule(String requestUri) {
122        for (Iterator i = getMappingRules(); i.hasNext();) {
123          MappingRule rule = (MappingRule) i.next();
124          if (rule.getRequestUri().equals(requestUri)) {
125            return rule;
126          }
127        }
128        return null;
129      }
130    
131      public Iterator<MappingRule> getMappingRules() {
132        if (mappingRules == null) {
133          List<MappingRule> objects = Collections.emptyList();
134          return objects.iterator();
135        } else {
136          return mappingRules.iterator();
137        }
138      }
139    
140      public Theme getTheme(String name) {
141        if (name == null) {
142          LOG.debug("searching theme: null");
143          return defaultTheme;
144        }
145        if (defaultTheme.getName().equals(name)) {
146          return defaultTheme;
147        }
148        for (Theme theme : supportedThemes) {
149          if (theme.getName().equals(name)) {
150            return theme;
151          }
152        }
153        LOG.debug("searching theme '" + name + "' not found. "
154            + "Using default: " + defaultTheme);
155        return defaultTheme;
156      }
157    
158      public void setDefaultThemeName(String defaultThemeName) {
159        this.defaultThemeName = defaultThemeName;
160      }
161    
162      public List<Theme> getSupportedThemes() {
163        return Collections.unmodifiableList(supportedThemes);
164      }
165    
166      public void addResourceDir(String resourceDir) {
167        if (!resourceDirs.contains(resourceDir)) {
168          if (LOG.isInfoEnabled()) {
169            LOG.info("adding resourceDir = '" + resourceDir + "'");
170          }
171          resourceDirs.add(resourceDir);
172        }
173      }
174    
175      public List<String> getResourceDirs() {
176        return resourceDirs;
177      }
178    
179      public boolean isAjaxEnabled() {
180        return ajaxEnabled;
181      }
182    
183      public void setAjaxEnabled(String value) {
184        this.ajaxEnabled = Boolean.valueOf(value);
185      }
186    
187      public boolean isFixResourceOrder() {
188        return fixResourceOrder;
189      }
190    
191      public void setFixResourceOrder(String fixResourceOrder) {
192        this.fixResourceOrder = Boolean.valueOf(fixResourceOrder);
193      }
194    
195      public boolean isFixLayoutTransparency() {
196        return fixLayoutTransparency;
197      }
198    
199      public void setFixLayoutTransparency(String fixLayoutTransparency) {
200        this.fixLayoutTransparency = Boolean.valueOf(fixLayoutTransparency);
201      }
202    
203      public boolean isCreateSessionSecret() {
204        return createSessionSecret;
205      }
206    
207      public void setCreateSessionSecret(String createSessionSecret) {
208        this.createSessionSecret = Boolean.valueOf(createSessionSecret);
209      }
210    
211      public boolean isCheckSessionSecret() {
212        return checkSessionSecret;
213      }
214    
215      public void setCheckSessionSecret(String checkSessionSecret) {
216        this.checkSessionSecret = Boolean.valueOf(checkSessionSecret);
217      }
218    
219      @Deprecated
220      public void setLoadThemesFromClasspath(String loadThemesFromClasspath) {
221        Deprecation.LOG.error("Deprecated: setting load-theme-resources-from-classpath is "
222            + "no longer supported");
223      }
224    
225      public Theme getDefaultTheme() {
226        return defaultTheme;
227      }
228    
229      public void setAvailableThemes(Map<String, Theme> availableTheme) {
230        this.availableTheme = availableTheme;
231      }
232    
233      public RenderersConfig getRenderersConfig() {
234        return renderersConfig;
235      }
236    
237      public void setRenderersConfig(RenderersConfig renderersConfig) {
238        this.renderersConfig = renderersConfig;
239      }
240    }
241