Coverage Report - org.mule.lifecycle.phases.MuleContextInitialisePhase
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleContextInitialisePhase
0%
0/24
0%
0/6
0
 
 1  
 /*
 2  
  * $Id: MuleContextInitialisePhase.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.lifecycle.phases;
 11  
 
 12  
 import org.mule.api.agent.Agent;
 13  
 import org.mule.api.component.Component;
 14  
 import org.mule.api.construct.FlowConstruct;
 15  
 import org.mule.api.lifecycle.Disposable;
 16  
 import org.mule.api.lifecycle.Initialisable;
 17  
 import org.mule.api.lifecycle.LifecycleException;
 18  
 import org.mule.api.model.Model;
 19  
 import org.mule.api.routing.OutboundRouter;
 20  
 import org.mule.api.routing.OutboundRouterCollection;
 21  
 import org.mule.api.source.MessageSource;
 22  
 import org.mule.api.transport.Connector;
 23  
 import org.mule.config.i18n.CoreMessages;
 24  
 import org.mule.lifecycle.LifecycleObject;
 25  
 import org.mule.lifecycle.NotificationLifecycleObject;
 26  
 import org.mule.util.annotation.AnnotationMetaData;
 27  
 import org.mule.util.annotation.AnnotationUtils;
 28  
 
 29  
 import java.lang.reflect.Method;
 30  
 import java.util.LinkedHashSet;
 31  
 import java.util.List;
 32  
 import java.util.Set;
 33  
 
 34  
 import javax.annotation.PostConstruct;
 35  
 
 36  
 /**
 37  
  * The MuleContextInitialisePhase defines the lifecycle behaviour when the Mule context is initialised.  The MuleContext is associated
 38  
  * with one or more registries that inherit the lifecycle of the MuleContext.
 39  
  * <p/>
 40  
  * This phase is responsible for initialising objects. Any object that implements {@link org.mule.api.lifecycle.Initialisable} will
 41  
  * have its {@link org.mule.api.lifecycle.Initialisable#initialise()} method called.  Objects are initialised in the order based on type:
 42  
  * {@link org.mule.api.transport.Connector}, {@link org.mule.api.agent.Agent}, {@link org.mule.api.model.Model}, {@link org.mule.api.service.Service}, followed
 43  
  * by any other object that implements {@link org.mule.api.lifecycle.Initialisable}.
 44  
  *
 45  
  * @see org.mule.api.MuleContext
 46  
  * @see org.mule.api.lifecycle.LifecycleManager
 47  
  * @see org.mule.api.lifecycle.Initialisable
 48  
  * @since 3.0
 49  
  */
 50  
 public class MuleContextInitialisePhase extends DefaultLifecyclePhase
 51  
 {
 52  
     public MuleContextInitialisePhase()
 53  
     {
 54  0
         super(Initialisable.PHASE_NAME, Initialisable.class, Disposable.PHASE_NAME);
 55  0
         registerSupportedPhase(NotInLifecyclePhase.PHASE_NAME);
 56  
 
 57  0
         Set<LifecycleObject> startOrderedObjects = new LinkedHashSet<LifecycleObject>();
 58  0
         startOrderedObjects.add(new NotificationLifecycleObject(Connector.class));
 59  0
         startOrderedObjects.add(new NotificationLifecycleObject(Agent.class));
 60  0
         startOrderedObjects.add(new NotificationLifecycleObject(Model.class));
 61  0
         startOrderedObjects.add(new NotificationLifecycleObject(FlowConstruct.class));
 62  0
         startOrderedObjects.add(new NotificationLifecycleObject(Initialisable.class));
 63  0
         setOrderedLifecycleObjects(startOrderedObjects);
 64  0
         setIgnoredObjectTypes(new Class[]{Component.class, MessageSource.class, OutboundRouterCollection.class, OutboundRouter.class});
 65  0
     }
 66  
 
 67  
 
 68  
     @Override
 69  
     public void applyLifecycle(Object o) throws LifecycleException
 70  
     {
 71  
         //retain default Lifecycle behaviour
 72  0
         super.applyLifecycle(o);
 73  0
         if (o == null)
 74  
         {
 75  0
             return;
 76  
         }
 77  0
         if (ignoreType(o.getClass()))
 78  
         {
 79  0
             return;
 80  
         }
 81  
 
 82  
         //Lets check for {@link PostConstruct} annotations on methods of this object and invoke
 83  0
         List<AnnotationMetaData> annos = AnnotationUtils.getMethodAnnotations(o.getClass(), PostConstruct.class);
 84  
 
 85  
         //Note that the registry has a processor that validates that there is at most one {@link PostConstruct} annotation
 86  
         //per object and that the method conforms to a lifecycle method
 87  0
         if (annos.size() == 1)
 88  
         {
 89  0
             AnnotationMetaData anno = annos.get(0);
 90  
 
 91  
             try
 92  
             {
 93  0
                 ((Method) anno.getMember()).invoke(o);
 94  
             }
 95  0
             catch (Exception e)
 96  
             {
 97  0
                 throw new LifecycleException(CoreMessages.failedToInvokeLifecycle(anno.getMember().getName(), o), e, this);
 98  0
             }
 99  
         }
 100  
 
 101  0
     }
 102  
 }