Coverage Report - org.mule.registry.JSR250ValidatorProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
JSR250ValidatorProcessor
0%
0/22
0%
0/18
0
 
 1  
 /*
 2  
  * $Id: JSR250ValidatorProcessor.java 20321 2010-11-24 15:21:24Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 package org.mule.registry;
 11  
 
 12  
 import org.mule.api.registry.InjectProcessor;
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.util.annotation.AnnotationMetaData;
 15  
 import org.mule.util.annotation.AnnotationUtils;
 16  
 
 17  
 import java.lang.reflect.Method;
 18  
 import java.lang.reflect.Modifier;
 19  
 import java.util.List;
 20  
 
 21  
 import javax.annotation.PostConstruct;
 22  
 import javax.annotation.PreDestroy;
 23  
 
 24  
 /**
 25  
  * This registry processor will check for objects that have JSR-250 lifecycle annotations defined and validates that the
 26  
  * following conditions are met (according to the JSR-250 spec)
 27  
  * <ol>
 28  
  * <li>The method MUST NOT have any parameters except in the case of EJB interceptors in which case it takes an InvocationContext object as defined by the EJB specification. Note that Mule is not an EJB container so the EJB case is ignored in Mule.</li>
 29  
  * <li>The return type of the method MUST be void.</li>
 30  
  * <li>The method MUST NOT throw a checked exception.</li>
 31  
  * <li>The method on which PostConstruct is applied MAY be public, protected, package private or private.</li>
 32  
  * <li>The method MUST NOT be static except for the application client.</li>
 33  
  * <li>The method MAY be final or non-final, except in the case of EJBs where it MUST be non-final. Note that Mule is not an EJB container so the EJB case is ignored in Mule.</li>
 34  
  * </ol>
 35  
  */
 36  0
 public class JSR250ValidatorProcessor implements InjectProcessor
 37  
 {
 38  
     public Object process(Object object)
 39  
     {
 40  0
         List<AnnotationMetaData> annos = AnnotationUtils.getMethodAnnotations(object.getClass(), PostConstruct.class);
 41  0
         if (annos.size() > 1)
 42  
         {
 43  0
             throw new IllegalArgumentException(CoreMessages.objectHasMoreThanOnePostConstructAnnotation(object.getClass()).getMessage());
 44  
         }
 45  0
         else if(annos.size()==1)
 46  
         {
 47  0
             validateLifecycleMethod((Method)annos.get(0).getMember());
 48  
         }
 49  
 
 50  0
         annos = AnnotationUtils.getMethodAnnotations(object.getClass(), PreDestroy.class);
 51  0
         if (annos.size() > 1)
 52  
         {
 53  0
             throw new IllegalArgumentException(CoreMessages.objectHasMoreThanOnePreDestroyAnnotation(object.getClass()).getMessage());
 54  
         }
 55  0
         else if(annos.size()==1)
 56  
         {
 57  0
             validateLifecycleMethod((Method)annos.get(0).getMember());
 58  
         }
 59  
 
 60  0
         return object;
 61  
     }
 62  
 
 63  
     public final void validateLifecycleMethod(Method method)
 64  
     {
 65  0
         if(method.getParameterTypes().length != 0)
 66  
         {
 67  0
             throw new IllegalArgumentException(CoreMessages.lifecycleMethodNotVoidOrHasParams(method).getMessage());
 68  
         }
 69  
 
 70  0
         if(!method.getReturnType().equals(Void.TYPE))
 71  
         {
 72  0
             throw new IllegalArgumentException(CoreMessages.lifecycleMethodNotVoidOrHasParams(method).getMessage());
 73  
         }
 74  
 
 75  0
         if(Modifier.isStatic(method.getModifiers()))
 76  
         {
 77  0
             throw new IllegalArgumentException(CoreMessages.lifecycleMethodCannotBeStatic(method).getMessage());
 78  
         }
 79  
 
 80  0
         for (Class<?> aClass : method.getExceptionTypes())
 81  
         {
 82  0
             if(!RuntimeException.class.isAssignableFrom(aClass))
 83  
             {
 84  0
                 throw new IllegalArgumentException(CoreMessages.lifecycleMethodCannotThrowChecked(method).getMessage());                
 85  
             }
 86  
         }
 87  0
     }
 88  
 }