[jboss-cvs] jboss-seam/src/main/org/jboss/seam/core ...
Gavin King
gavin.king at jboss.com
Sun Oct 8 10:01:53 EDT 2006
User: gavin
Date: 06/10/08 10:01:53
Modified: src/main/org/jboss/seam/core Init.java Pages.java
Log:
refactored framework
added converters for page parameters
fixed bugs in <factory/>
added <factory value=...>
Revision Changes Path
1.25 +55 -14 jboss-seam/src/main/org/jboss/seam/core/Init.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Init.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/Init.java,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -b -r1.24 -r1.25
--- Init.java 3 Oct 2006 17:16:44 -0000 1.24
+++ Init.java 8 Oct 2006 14:01:53 -0000 1.25
@@ -1,4 +1,4 @@
-//$Id: Init.java,v 1.24 2006/10/03 17:16:44 gavin Exp $
+//$Id: Init.java,v 1.25 2006/10/08 14:01:53 gavin Exp $
package org.jboss.seam.core;
@@ -17,6 +17,7 @@
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.core.Expressions.MethodBinding;
+import org.jboss.seam.core.Expressions.ValueBinding;
/**
* A Seam component that holds Seam configuration settings
@@ -38,7 +39,8 @@
private Map<String, List<ObserverMethod>> observers = new HashMap<String, List<ObserverMethod>>();
private Map<String, FactoryMethod> factories = new HashMap<String, FactoryMethod>();
- private Map<String, FactoryMethodBinding> factoryMethodBindings = new HashMap<String, FactoryMethodBinding>();
+ private Map<String, FactoryBinding> factoryMethodBindings = new HashMap<String, FactoryBinding>();
+ private Map<String, FactoryBinding> factoryValueBindings = new HashMap<String, FactoryBinding>();
public static Init instance()
{
@@ -66,25 +68,55 @@
}
public static class FactoryMethod {
- public Method method;
- public Component component;
- public ScopeType scope;
+ private Method method;
+ private Component component;
+ private ScopeType scope;
+
FactoryMethod(Method method, Component component)
{
this.method = method;
this.component = component;
scope = method.getAnnotation(org.jboss.seam.annotations.Factory.class).scope();
}
+
+ public ScopeType getScope()
+ {
+ return scope;
+ }
+ public Component getComponent()
+ {
+ return component;
+ }
+ public Method getMethod()
+ {
+ return method;
}
+ }
+
+ public static class FactoryBinding {
+ private String expression;
+ private ScopeType scope;
- public static class FactoryMethodBinding {
- public MethodBinding methodBinding;
- public ScopeType scope;
- FactoryMethodBinding(MethodBinding methodBinding, ScopeType scope)
+ FactoryBinding(String expression, ScopeType scope)
{
- this.methodBinding = methodBinding;
+ this.expression = expression;
this.scope = scope;
}
+
+ public MethodBinding getMethodBinding()
+ {
+ //TODO: figure out some way to cache this!!
+ return Expressions.instance().createMethodBinding(expression);
+ }
+ public ValueBinding getValueBinding()
+ {
+ //TODO: figure out some way to cache this!!
+ return Expressions.instance().createValueBinding(expression);
+ }
+ public ScopeType getScope()
+ {
+ return scope;
+ }
}
public FactoryMethod getFactory(String variable)
@@ -92,20 +124,29 @@
return factories.get(variable);
}
- public FactoryMethodBinding getFactoryMethodBinding(String variable)
+ public FactoryBinding getFactoryMethodBinding(String variable)
{
return factoryMethodBindings.get(variable);
}
+ public FactoryBinding getFactoryValueBinding(String variable)
+ {
+ return factoryValueBindings.get(variable);
+ }
+
public void addFactoryMethod(String variable, Method method, Component component)
{
factories.put( variable, new FactoryMethod(method, component) );
}
- public void addFactory(String variable, String methodBindingExpression, ScopeType scope)
+ public void addFactoryMethodBinding(String variable, String methodBindingExpression, ScopeType scope)
+ {
+ factoryMethodBindings.put( variable, new FactoryBinding(methodBindingExpression, scope) );
+ }
+
+ public void addFactoryValueBinding(String variable, String valueBindingExpression, ScopeType scope)
{
- MethodBinding methodBinding = Expressions.instance().createMethodBinding(methodBindingExpression);
- factoryMethodBindings.put( variable, new FactoryMethodBinding(methodBinding, scope) );
+ factoryValueBindings.put( variable, new FactoryBinding(valueBindingExpression, scope) );
}
public static class ObserverMethod {
1.26 +71 -30 jboss-seam/src/main/org/jboss/seam/core/Pages.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Pages.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/Pages.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -b -r1.25 -r1.26
--- Pages.java 7 Oct 2006 18:06:58 -0000 1.25
+++ Pages.java 8 Oct 2006 14:01:53 -0000 1.26
@@ -3,7 +3,7 @@
import static org.jboss.seam.InterceptionType.NEVER;
import java.io.InputStream;
-import java.util.Collection;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@@ -14,6 +14,7 @@
import java.util.TreeSet;
import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -61,7 +62,37 @@
MethodBinding action;
String outcome;
String noConversationViewId;
- Map<String, ValueBinding> parameterValueBindings = new HashMap<String, ValueBinding>();
+ List<PageParameter> pageParameters = new ArrayList<PageParameter>();
+ }
+
+ static final class PageParameter
+ {
+ PageParameter(String name)
+ {
+ this.name = name;
+ }
+
+ final String name;
+ ValueBinding valueBinding;
+ ValueBinding converterValueBinding;
+ String converterId;
+
+ Converter getConverter()
+ {
+ if (converterId!=null)
+ {
+ return FacesContext.getCurrentInstance().getApplication().createConverter(converterId);
+ }
+ else if (converterValueBinding!=null)
+ {
+ return (Converter) converterValueBinding.getValue();
+ }
+ else
+ {
+ Class<?> type = valueBinding.getType();
+ return FacesContext.getCurrentInstance().getApplication().createConverter(type);
+ }
+ }
}
private Map<String, Page> pagesByViewId = new HashMap<String, Page>();
@@ -136,8 +167,15 @@
List<Element> children = page.elements("param");
for (Element param: children)
{
- ValueBinding valueBinding = Expressions.instance().createValueBinding( param.attributeValue("value") );
- entry.parameterValueBindings.put( param.attributeValue("name"), valueBinding );
+ PageParameter pageParameter = new PageParameter( param.attributeValue("name") );
+ pageParameter.valueBinding = Expressions.instance().createValueBinding( param.attributeValue("value") );
+ pageParameter.converterId = param.attributeValue("converterId");
+ String converterExpression = param.attributeValue("converter");
+ if (converterExpression!=null)
+ {
+ pageParameter.converterValueBinding = Expressions.instance().createValueBinding( converterExpression );
+ }
+ entry.pageParameters.add(pageParameter);
}
}
}
@@ -279,11 +317,6 @@
return result==null ? noConversationViewId : result;
}
- private Collection<Map.Entry<String, ValueBinding>> getParameterValueBindings(String viewId)
- {
- return getPage(viewId).parameterValueBindings.entrySet();
- }
-
public Map<String, Object> getParameters(String viewId)
{
return getParameters(viewId, Collections.EMPTY_SET);
@@ -292,15 +325,14 @@
public Map<String, Object> getParameters(String viewId, Set<String> overridden)
{
Map<String, Object> parameters = new HashMap<String, Object>();
- for (Map.Entry<String, ValueBinding> me: getParameterValueBindings(viewId))
+ for ( PageParameter pageParameter: getPage(viewId).pageParameters )
{
- if ( !overridden.contains( me.getKey() ) )
+ if ( !overridden.contains(pageParameter.name) )
{
- Object value = me.getValue().getValue();
- //TODO: handle multi-values!
+ Object value = pageParameter.valueBinding.getValue();
if (value!=null)
{
- parameters.put( me.getKey(), value );
+ parameters.put(pageParameter.name, value);
}
}
}
@@ -309,27 +341,36 @@
public void applyRequestParameterValues(String viewId)
{
+ FacesContext context = FacesContext.getCurrentInstance();
Map<String, String[]> requestParameters = Parameters.getRequestParameters();
- for (Map.Entry<String, ValueBinding> me: getParameterValueBindings(viewId))
+ for ( PageParameter pageParameter: getPage(viewId).pageParameters )
{
- Class type;
- try
+ String[] parameterValues = requestParameters.get(pageParameter.name);
+ if (parameterValues==null || parameterValues.length==0)
{
- type = me.getValue().getType();
+ continue;
}
- catch (RuntimeException e)
+ if (parameterValues.length>1)
{
- type = null;
+ throw new IllegalArgumentException("page parameter may not be multi-valued: " + pageParameter.name);
}
+ String stringValue = parameterValues[0];
- if (type!=null)
- {
- Object value = Parameters.convertMultiValueRequestParameter( requestParameters, me.getKey(), type );
- if (value!=null)
+ Converter converter;
+ try
{
- me.getValue().setValue(value);
+ converter = pageParameter.getConverter();
}
+ catch (RuntimeException re)
+ {
+ //YUCK! due to bad JSF/MyFaces error handling
+ continue;
}
+
+ Object value = converter==null ?
+ stringValue :
+ converter.getAsObject( context, context.getViewRoot(), stringValue );
+ pageParameter.valueBinding.setValue(value);
}
}
@@ -339,12 +380,12 @@
if (pageParameters!=null)
{
- for (Map.Entry<String, ValueBinding> me: getParameterValueBindings(viewId))
+ for (PageParameter pageParameter: getPage(viewId).pageParameters)
{
- Object object = pageParameters.get( me.getKey() );
+ Object object = pageParameters.get(pageParameter.name);
if (object!=null)
{
- me.getValue().setValue(object);
+ pageParameter.valueBinding.setValue(object);
}
}
More information about the jboss-cvs-commits
mailing list