1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.sf.beanform.integration;
16
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import net.sf.beanform.prop.BeanProperty;
28
29 /***
30 * Represents a chain of integration elements, the makeup of which will vary according to
31 * the execution environment.
32 *
33 * Note: If <a href="http://jcp.org/en/jsr/detail?id=303">JSR 303: Bean Validation</a> ever
34 * gets added to Java SE, add it to the integration chain.
35 *
36 * @author Daniel Gredler
37 */
38 public final class IntegratorChain {
39
40 private final static Log LOG = LogFactory.getLog( IntegratorChain.class );
41 private final static List<Integrator> INTEGRATORS;
42
43 static {
44
45
46 List<Integrator> list = new ArrayList<Integrator>();
47 if( isSupported( "Hibernate", "org.hibernate.validator.Max" ) ) {
48 list.add( new HibernateIntegrator() );
49 }
50 if( isSupported( "EJB3", "javax.persistence.Column" ) ) {
51 list.add( new Ejb3Integrator() );
52 }
53 INTEGRATORS = Collections.unmodifiableList( list );
54 }
55
56 static boolean isSupported( String featureName, String className ) {
57 Class c;
58 try {
59 c = Class.forName( className );
60 }
61 catch( ClassNotFoundException e ) {
62 c = null;
63 }
64 catch( NoClassDefFoundError e ) {
65 c = null;
66 }
67 boolean supported = ( c != null );
68 if( supported ) LOG.debug( featureName + " support detected." );
69 return supported;
70 }
71
72 IntegratorChain() {
73
74 }
75
76 public static String getValidation( BeanProperty prop ) {
77 Map<String, String> validations = new HashMap<String, String>();
78 for( Integrator integrator : INTEGRATORS ) {
79 Map<String, String> temp = integrator.getValidation( prop );
80 validations.putAll( temp );
81 }
82 StringBuilder sb = new StringBuilder();
83 for( Iterator<String> i = validations.values().iterator(); i.hasNext(); ) {
84 sb.append( i.next() );
85 if( i.hasNext() ) sb.append( "," );
86 }
87 if( sb.length() == 0 ) return null;
88 else return sb.toString();
89 }
90
91 public static Integer getMaxLength( BeanProperty prop ) {
92 Integer maxLength = null;
93 for( Integrator integrator : INTEGRATORS ) {
94 Integer temp = integrator.getMaxLength( prop );
95 if( temp != null ) maxLength = temp;
96 }
97 return maxLength;
98 }
99
100 public static boolean isNullable( BeanProperty prop ) {
101 boolean nullable = true;
102 for( Integrator integrator : INTEGRATORS ) {
103 boolean temp = integrator.isNullable( prop );
104 nullable = nullable && temp;
105 }
106 return nullable;
107 }
108
109 }