Coverage Report - net.sf.beanform.integration.IntegratorChain
 
Classes in this File Line Coverage Branch Coverage Complexity
IntegratorChain
95% 
100% 
0
 
 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  1
     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  1
         List<Integrator> list = new ArrayList<Integrator>();
 47  1
         if( isSupported( "Hibernate", "org.hibernate.validator.Max" ) ) {
 48  1
             list.add( new HibernateIntegrator() );
 49  
         }
 50  1
         if( isSupported( "EJB3", "javax.persistence.Column" ) ) {
 51  1
             list.add( new Ejb3Integrator() );
 52  
         }
 53  1
         INTEGRATORS = Collections.unmodifiableList( list );
 54  1
     }
 55  
 
 56  
     static boolean isSupported( String featureName, String className ) {
 57  
         Class c;
 58  
         try {
 59  4
             c = Class.forName( className );
 60  
         }
 61  1
         catch( ClassNotFoundException e ) {
 62  1
             c = null;
 63  
         }
 64  0
         catch( NoClassDefFoundError e ) {
 65  0
             c = null;
 66  4
         }
 67  4
         boolean supported = ( c != null );
 68  4
         if( supported ) LOG.debug( featureName + " support detected." );
 69  4
         return supported;
 70  
     }
 71  
 
 72  1
     IntegratorChain() {
 73  
         // Empty.
 74  1
     }
 75  
 
 76  
     public static String getValidation( BeanProperty prop ) {
 77  175
         Map<String, String> validations = new HashMap<String, String>();
 78  175
         for( Integrator integrator : INTEGRATORS ) {
 79  350
             Map<String, String> temp = integrator.getValidation( prop );
 80  350
             validations.putAll( temp );
 81  350
         }
 82  175
         StringBuilder sb = new StringBuilder();
 83  175
         for( Iterator<String> i = validations.values().iterator(); i.hasNext(); ) {
 84  40
             sb.append( i.next() );
 85  40
             if( i.hasNext() ) sb.append( "," );
 86  
         }
 87  175
         if( sb.length() == 0 ) return null;
 88  24
         else return sb.toString();
 89  
     }
 90  
 
 91  
     public static Integer getMaxLength( BeanProperty prop ) {
 92  73
         Integer maxLength = null;
 93  73
         for( Integrator integrator : INTEGRATORS ) {
 94  146
             Integer temp = integrator.getMaxLength( prop );
 95  146
             if( temp != null ) maxLength = temp;
 96  146
         }
 97  73
         return maxLength;
 98  
     }
 99  
 
 100  
     public static boolean isNullable( BeanProperty prop ) {
 101  9
         boolean nullable = true;
 102  9
         for( Integrator integrator : INTEGRATORS ) {
 103  18
             boolean temp = integrator.isNullable( prop );
 104  18
             nullable = nullable && temp;
 105  18
         }
 106  9
         return nullable;
 107  
     }
 108  
 
 109  
 }