View Javadoc

1   /*
2    * $Id: MuleContextInitialisePhase.java 19191 2010-08-25 21:05:23Z tcarlson $
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          super(Initialisable.PHASE_NAME, Initialisable.class, Disposable.PHASE_NAME);
55          registerSupportedPhase(NotInLifecyclePhase.PHASE_NAME);
56  
57          Set<LifecycleObject> startOrderedObjects = new LinkedHashSet<LifecycleObject>();
58          startOrderedObjects.add(new NotificationLifecycleObject(Connector.class));
59          startOrderedObjects.add(new NotificationLifecycleObject(Agent.class));
60          startOrderedObjects.add(new NotificationLifecycleObject(Model.class));
61          startOrderedObjects.add(new NotificationLifecycleObject(FlowConstruct.class));
62          startOrderedObjects.add(new NotificationLifecycleObject(Initialisable.class));
63          setOrderedLifecycleObjects(startOrderedObjects);
64          setIgnoredObjectTypes(new Class[]{Component.class, MessageSource.class, OutboundRouterCollection.class, OutboundRouter.class});
65      }
66  
67  
68      @Override
69      public void applyLifecycle(Object o) throws LifecycleException
70      {
71          //retain default Lifecycle behaviour
72          super.applyLifecycle(o);
73          if (o == null)
74          {
75              return;
76          }
77          if (ignoreType(o.getClass()))
78          {
79              return;
80          }
81  
82          //Lets check for {@link PostConstruct} annotations on methods of this object and invoke
83          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          if (annos.size() == 1)
88          {
89              AnnotationMetaData anno = annos.get(0);
90  
91              try
92              {
93                  ((Method) anno.getMember()).invoke(o);
94              }
95              catch (Exception e)
96              {
97                  throw new LifecycleException(CoreMessages.failedToInvokeLifecycle(anno.getMember().getName(), o), e, this);
98              }
99          }
100 
101     }
102 }