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