View Javadoc

1   // Copyright 2006 Daniel Gredler
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
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          // Important note: The methods below are coded such that in the case of
45          // conflicting contributions, the last of the contributions is used.
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          // Empty.
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 }