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