Author: abelevich
Date: 2009-08-04 08:32:33 -0400 (Tue, 04 Aug 2009)
New Revision: 15106
Added:
root/framework/trunk/impl/src/main/java/org/ajax4jsf/config/FrameworkConfiguration.java
Log:
initial commit
Added:
root/framework/trunk/impl/src/main/java/org/ajax4jsf/config/FrameworkConfiguration.java
===================================================================
---
root/framework/trunk/impl/src/main/java/org/ajax4jsf/config/FrameworkConfiguration.java
(rev 0)
+++
root/framework/trunk/impl/src/main/java/org/ajax4jsf/config/FrameworkConfiguration.java 2009-08-04
12:32:33 UTC (rev 15106)
@@ -0,0 +1,279 @@
+package org.ajax4jsf.config;
+
+import java.util.EnumMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import javax.el.ExpressionFactory;
+import javax.el.ValueExpression;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.servlet.ServletContext;
+
+import org.ajax4jsf.util.ELUtils;
+import org.richfaces.util.RichfacesLogger;
+import org.slf4j.Logger;
+
+
+/**
+ * @author Anton Belevich
+ * @since 4.0
+ * framework configuration class
+ */
+
+public class FrameworkConfiguration {
+
+ private static final Logger logger = RichfacesLogger.CONFIG.getLogger();
+
+
+
+ private Map<BooleanInitParam, Boolean> booleanInitParams = new
EnumMap<BooleanInitParam, Boolean>(BooleanInitParam.class);
+
+ private Map<InitParam, String> initParams = new EnumMap<InitParam,
String>(InitParam.class);
+
+ private Map<NumberInitParam, Integer> numberInitParams = new
EnumMap<NumberInitParam, Integer>(NumberInitParam.class);
+
+ ServletContext servletContext;
+
+ private static final Pattern ALLOWABLE_BOOLEANS =
Pattern.compile("true|false");
+
+ private static final Pattern ALLOWABLE_NUMBER = Pattern.compile("[0-9]");
+
+
+ private FrameworkConfiguration(ServletContext servletContext) {
+
+ this.servletContext = servletContext;
+
+ processInitParams(servletContext);
+ processBooleanInitParams(servletContext);
+ processNumberParams(servletContext);
+ }
+
+ public static FrameworkConfiguration getInstance() {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ return getInstance(facesContext.getExternalContext());
+
+ }
+
+ public static FrameworkConfiguration getInstance(ExternalContext extContext) {
+ //TODO create instance of FrameworkConfiguration
+ return null;
+
+ }
+
+ private boolean isBooleanValueValid(BooleanInitParam param, String value) {
+ return !ALLOWABLE_BOOLEANS.matcher(value).matches();
+ }
+
+ private boolean isNumberValueValid(NumberInitParam param, String value) {
+ return !ALLOWABLE_NUMBER.matcher(value).matches();
+ }
+
+ public String getOptionValue(InitParam param) {
+ return initParams.get(param);
+ }
+
+ public int getOptionValue(NumberInitParam param) {
+ return ((Integer)numberInitParams.get(param)).intValue();
+ }
+
+ public boolean isOptionEnabled(BooleanInitParam param) {
+ return ((Boolean)booleanInitParams.get(param)).booleanValue();
+ }
+
+
+ private void processBooleanInitParams(ServletContext servletContext){
+ for(BooleanInitParam param: BooleanInitParam.values()) {
+
+ String paramName = param.qualifiedName;
+ String paramValue = servletContext.getInitParameter(paramName);
+ Boolean value = param.getDefaultValue();
+
+ if(paramValue != null) {
+
+ if(isBooleanValueValid(param, paramValue)) {
+ value = Boolean.parseBoolean(paramValue);
+ } else if(ELUtils.isValueReference(paramValue)) {
+ value = (Boolean)resolveELParam(FacesContext.getCurrentInstance(), paramValue,
java.lang.Boolean.class);
+ } else {
+ //TODO: add log about default value
+ }
+ }
+
+ booleanInitParams.put(param, value);
+ }
+ }
+
+ private void processInitParams(ServletContext context){
+ for(InitParam param: InitParam.values()) {
+
+ String paramName = param.qualifiedName;
+ String paramValue = servletContext.getInitParameter(paramName);
+ String value = param.getDefaultValue();
+
+ if(paramValue != null) {
+ if(ELUtils.isValueReference(paramValue)) {
+ value = (String)resolveELParam(FacesContext.getCurrentInstance(), value,
java.lang.String.class);
+ } else {
+ value = paramValue;
+ }
+ }
+
+ initParams.put(param, value);
+ }
+ }
+
+ private void processNumberParams(ServletContext servletContext){
+ for(NumberInitParam param: NumberInitParam.values()) {
+
+ String paramName = param.qualifiedName;
+ String paramValue = servletContext.getInitParameter(paramName);
+ Integer value = Integer.valueOf(param.getDefaultValue());
+
+ if(paramValue != null) {
+ if(ELUtils.isValueReference(paramValue)) {
+ value = (Integer)resolveELParam(FacesContext.getCurrentInstance(), paramValue,
java.lang.Integer.class);
+ } else if(isNumberValueValid(param, paramValue)){
+ value = Integer.parseInt(paramValue);
+ } else {
+ //TODO log
+ }
+ }
+
+ numberInitParams.put(param, value);
+ }
+ }
+
+ private Object resolveELParam(FacesContext context, String value, Class <?>
expectedClass) {
+ ExpressionFactory factory = context.getApplication().getExpressionFactory();
+ ValueExpression valueExpression = factory.createValueExpression(value, expectedClass);
+ return valueExpression.getValue(context.getELContext());
+ }
+
+
+ public enum NumberInitParam {
+
+ DEFAULT_EXPIRE (
+ "org.ajax4jsf.DEFAULT_EXPIRE",
+ 86400
+ );
+
+ private String qualifiedName;
+ private int defaultValue;
+
+ NumberInitParam(String qualifiedName, int defaultValue) {
+ this.qualifiedName = qualifiedName;
+ this.defaultValue = defaultValue;
+ }
+
+ public int getDefaultValue() {
+ return defaultValue;
+ }
+
+ public String getQualifiedName() {
+ return qualifiedName;
+ }
+ }
+
+ public enum BooleanInitParam {
+
+ ENCRYPT_RESOURCE_DATA(
+ "org.ajax4jsf.ENCRYPT_RESOURCE_DATA",
+ false
+ ),
+
+ COMPRESS_SCRIPT(
+ "org.ajax4jsf.COMPRESS_SCRIPT",
+ true
+ ),
+
+ SERIALIZE_SERVER_STATE(
+ "org.ajax4jsf.SERIALIZE_SERVER_STATE",
+ false
+ );
+
+
+ private String qualifiedName;
+ private boolean defaultValue;
+
+ BooleanInitParam(String qualifiedName, boolean defaultValue) {
+ this.qualifiedName = qualifiedName;
+ this.defaultValue = defaultValue;
+ }
+
+ public boolean getDefaultValue() {
+ return defaultValue;
+ }
+
+ public String getQualifiedName() {
+ return qualifiedName;
+ }
+
+ }
+
+ public enum InitParam {
+
+ SKIN (
+ "org.richfaces.SKIN",
+ "DEFAULT"
+ ),
+
+ LoadScriptStrategy (
+ "org.richfaces.LoadScriptStrategy",
+ "DEFAULT"
+ ),
+
+ LoadStyleStrategy (
+ "org.richfaces.LoadStyleStrategy",
+ "DEFAULT"
+ ),
+
+ LOGFILE (
+ "org.ajax4jsf.LOGFILE",
+ "none"
+ ),
+
+ VIEW_HANDLERS(
+ "org.ajax4jsf.VIEW_HANDLERS",
+ "none"
+ ),
+
+ CONTROL_COMPONENTS(
+ "org.ajax4jsf.CONTROL_COMPONENTS",
+ "none"
+ ),
+
+ ENCRYPT_PASSWORD (
+ "org.ajax4jsf.ENCRYPT_PASSWORD",
+ "random"
+ ),
+
+ GLOBAL_RESOURCE_URI_PREFIX(
+ "org.ajax4jsf.GLOBAL_RESOURCE_URI_PREFIX",
+ "a4j/g"
+ ),
+
+ SESSION_RESOURCE_URI_PREFIX(
+ "org.ajax4jsf.SESSION_RESOURCE_URI_PREFIX",
+ "a4j/s"
+ );
+
+
+ private String qualifiedName;
+ private String defaultValue;
+
+ InitParam(String qualifiedName,String defaultValue) {
+ this.qualifiedName = qualifiedName;
+ this.defaultValue = defaultValue;
+ }
+
+ public String getDefaultValue() {
+ return defaultValue;
+ }
+
+ public String getQualifiedName() {
+ return qualifiedName;
+ }
+ }
+
+}