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