[jboss-cvs] jboss-seam/src/main/org/jboss/seam/core ...
Gavin King
gavin.king at jboss.com
Sat Jul 21 17:01:59 EDT 2007
User: gavin
Date: 07/07/21 17:01:59
Modified: src/main/org/jboss/seam/core Validators.java
Log:
JBSEAM-1626
Revision Changes Path
1.15 +25 -59 jboss-seam/src/main/org/jboss/seam/core/Validators.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Validators.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/core/Validators.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- Validators.java 20 Jul 2007 23:14:47 -0000 1.14
+++ Validators.java 21 Jul 2007 21:01:59 -0000 1.15
@@ -3,7 +3,6 @@
import static org.jboss.seam.annotations.Install.BUILT_IN;
import java.beans.FeatureDescriptor;
-import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
@@ -19,15 +18,14 @@
import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
import org.jboss.seam.Component;
+import org.jboss.seam.Instance;
import org.jboss.seam.ScopeType;
-import org.jboss.seam.Seam;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.el.EL;
-import org.jboss.seam.util.Proxy;
/**
* Caches instances of Hibernate Validator ClassValidator
@@ -72,6 +70,25 @@
}
/**
+ * Get the cached ClassValidator instance. If the
+ * argument is an instance of a session bean Seam
+ * component instance, the returned validator will
+ * be aware of constraints defined on the bean class.
+ * Therefore this method is preferred to
+ * getValidator(Class) if the argument might be a
+ * session bean.
+ *
+ * @param model the object to be validated
+ */
+ public <T> ClassValidator<T> getValidator(T model)
+ {
+ Class modelClass = model instanceof Instance ?
+ ( (Instance) model ).getComponent().getBeanClass() :
+ model.getClass();
+ return getValidator( (Class<T>) modelClass );
+ }
+
+ /**
* Get the cached ClassValidator instance.
*
* @param modelClass the class to be validated
@@ -92,9 +109,8 @@
}
/**
- * Create a new ClassValidator, or get it from the
- * Component object for the default role of the
- * class.
+ * Create a new ClassValidator for the given class,
+ * using the current Seam ResourceBundle.
*
* @param modelClass the class to be validated
*/
@@ -104,60 +120,11 @@
java.util.ResourceBundle bundle = SeamResourceBundle.getBundle();
return bundle==null ?
- new ClassValidator(getRealBeanClass(modelClass)) :
- new ClassValidator(getRealBeanClass(modelClass), bundle);
+ new ClassValidator(modelClass) :
+ new ClassValidator(modelClass, bundle);
}
/**
- * Most of the time, the bean class (the source of annotation configurations) is
- * either the class being passed in or a superclass of it. In the case of EJB components,
- * this is not true and there is no way to derive the type of the implementing class from
- * the proxy class. In these cases, we try to lookup the Seam component for that interface
- * and find the implementing bean class as a source of annotations.
- */
- private Class getRealBeanClass(Class modelClass) {
- if (isSimpleProxy(modelClass)) {
- Component matched = findComponentByBusinessInterfaces(modelClass);
- if (matched != null) {
- return matched.getBeanClass();
- }
- }
- return modelClass;
- }
-
-
- /**
- * Given an interfaces, find a component that uses it as the business interface
- */
- private Component findComponentByBusinessInterfaces(Class modelClass) {
- for (String name: Contexts.getApplicationContext().getNames()) {
-
- if (name.endsWith(".component")) {
- Object obj = Contexts.getApplicationContext().get(name);
-
- if (obj instanceof Component) {
- Component component = (Component) obj;
- for (Class iface : modelClass.getInterfaces()) {
- if (component.getBusinessInterfaces().contains(iface)) {
- return component;
- }
- }
- }
- }
- }
- return null;
- }
-
- /**
- * checks to see if the object is a proxy whose only superclass is
- * Object.
- */
- private boolean isSimpleProxy(Class c) {
- return Proxy.deproxy(c).equals(Object.class);
- }
-
-
- /**
* Validate that the given value can be assigned to the property given by the value
* expression.
*
@@ -229,8 +196,7 @@
if (base!=null && property!=null )
{
context.setPropertyResolved(true);
- invalidValues = getValidator( base.getClass() )
- .getPotentialInvalidValues( property.toString(), value );
+ invalidValues = getValidator(base).getPotentialInvalidValues( property.toString(), value );
}
}
More information about the jboss-cvs-commits
mailing list