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