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