View Javadoc

1   /*
2    * $Id: AbstractJavaComponent.java 12269 2008-07-10 04:19:03Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
11  package org.mule.component;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.component.JavaComponent;
17  import org.mule.api.component.LifecycleAdapter;
18  import org.mule.api.component.LifecycleAdapterFactory;
19  import org.mule.api.lifecycle.InitialisationException;
20  import org.mule.api.model.EntryPointResolver;
21  import org.mule.api.model.EntryPointResolverSet;
22  import org.mule.api.object.ObjectFactory;
23  import org.mule.api.routing.NestedRouterCollection;
24  import org.mule.config.i18n.CoreMessages;
25  import org.mule.model.resolvers.DefaultEntryPointResolverSet;
26  import org.mule.routing.nested.DefaultNestedRouterCollection;
27  
28  import java.util.Collection;
29  import java.util.Iterator;
30  
31  /**
32   * Abstract implementation of JavaComponent adds JavaComponent specific's:
33   * {@link EntryPointResolverSet}, {@link NestedRouterCollection} and
34   * {@link ObjectFactory}. Provides default implementations of doOnCall and doOnEvent
35   * and defines abstract template methods provided for obtaining and returning the
36   * component object instance.
37   */
38  public abstract class AbstractJavaComponent extends AbstractComponent implements JavaComponent
39  {
40  
41      protected EntryPointResolverSet entryPointResolverSet;
42  
43      protected NestedRouterCollection nestedRouter = new DefaultNestedRouterCollection();
44  
45      protected ObjectFactory objectFactory;
46  
47      protected LifecycleAdapterFactory lifecycleAdapterFactory;
48  
49      public AbstractJavaComponent()
50      {
51          // For Spring only
52          super();
53      }
54  
55      public AbstractJavaComponent(ObjectFactory objectFactory)
56      {
57          this(objectFactory, null, null);
58      }
59  
60      public AbstractJavaComponent(ObjectFactory objectFactory,
61                                   EntryPointResolverSet entryPointResolverSet,
62                                   NestedRouterCollection nestedRouterCollection)
63      {
64          super();
65          this.objectFactory = objectFactory;
66          this.entryPointResolverSet = entryPointResolverSet;
67          if (nestedRouterCollection != null)
68          {
69              this.nestedRouter = nestedRouterCollection;
70          }
71      }
72  
73      protected MuleMessage doOnCall(MuleEvent event) throws Exception
74      {
75          return invokeComponentInstance(event);
76      }
77  
78      protected MuleMessage invokeComponentInstance(MuleEvent event) throws Exception
79      {
80          LifecycleAdapter componentLifecycleAdapter = null;
81          try
82          {
83              componentLifecycleAdapter = borrowComponentLifecycleAdaptor();
84              return componentLifecycleAdapter.intercept(null);
85          }
86          finally
87          {
88              returnComponentLifecycleAdaptor(componentLifecycleAdapter);
89          }
90      }
91  
92      public Class getObjectType()
93      {
94          return objectFactory.getObjectClass();
95      }
96  
97      /**
98       * Creates and initialises a new LifecycleAdaptor instance wrapped the component
99       * object instance obtained from the configured object factory.
100      * 
101      * @return
102      * @throws MuleException
103      * @throws Exception
104      */
105     protected LifecycleAdapter createLifeCycleAdaptor() throws MuleException, Exception
106     {
107         LifecycleAdapter lifecycleAdapter;
108         if (lifecycleAdapterFactory != null)
109         {
110             // Custom lifecycleAdapterFactory set on component
111             lifecycleAdapter = lifecycleAdapterFactory.create(objectFactory.getInstance(), this, entryPointResolverSet);
112         }
113         else
114         {
115             // Inherit lifecycleAdapterFactory from model
116             lifecycleAdapter = service.getModel().getLifecycleAdapterFactory().create(objectFactory.getInstance(),
117                 this, entryPointResolverSet);
118         }
119         lifecycleAdapter.initialise();
120         return lifecycleAdapter;
121     }
122 
123     protected abstract LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception;
124 
125     protected abstract void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) throws Exception;
126 
127     // @Override
128     protected void doInitialise() throws InitialisationException
129     {
130         if (objectFactory == null)
131         {
132             throw new InitialisationException(CoreMessages.objectIsNull("object factory"), this);
133         }
134         // If this component was configured with spring the objectFactory instance
135         // has already been initialised, yet if this component was no configured with
136         // spring then the objectFactory is still uninitialised so we need to
137         // initialise it here.
138         objectFactory.initialise();
139     }
140 
141     // @Override
142     protected void doStart() throws MuleException
143     {
144         // We need to resolve entry point resolvers here rather than in initialise()
145         // because when configuring with spring, although the service has been
146         // injected and is available the injected service construction has not been
147         // completed and model is still in null.
148         if (entryPointResolverSet == null)
149         {
150             entryPointResolverSet = service.getModel().getEntryPointResolverSet();
151         }
152     }
153 
154     // @Override
155     protected void doDispose()
156     {
157         // TODO This can't be implemented currently because AbstractService allows
158         // disposed services to be re-initialised, and re-use of a disposed object
159         // factory is not possible
160         // objectFactory.dispose();
161     }
162 
163     public EntryPointResolverSet getEntryPointResolverSet()
164     {
165         return entryPointResolverSet;
166     }
167 
168     public NestedRouterCollection getNestedRouter()
169     {
170         return nestedRouter;
171     }
172 
173     public void setEntryPointResolverSet(EntryPointResolverSet entryPointResolverSet)
174     {
175         this.entryPointResolverSet = entryPointResolverSet;
176     }
177 
178     public void setNestedRouter(NestedRouterCollection nestedRouter)
179     {
180         this.nestedRouter = nestedRouter;
181     }
182 
183     /**
184      * Allow for incremental addition of resolvers by for example the spring-config
185      * module
186      * 
187      * @param entryPointResolvers Resolvers to add
188      */
189     public void setEntryPointResolvers(Collection entryPointResolvers)
190     {
191         if (null == entryPointResolverSet)
192         {
193             entryPointResolverSet = new DefaultEntryPointResolverSet();
194         }
195         for (Iterator resolvers = entryPointResolvers.iterator(); resolvers.hasNext();)
196         {
197             entryPointResolverSet.addEntryPointResolver((EntryPointResolver) resolvers.next());
198         }
199     }
200 
201     public ObjectFactory getObjectFactory()
202     {
203         return objectFactory;
204     }
205 
206     public void setObjectFactory(ObjectFactory objectFactory)
207     {
208         this.objectFactory = objectFactory;
209     }
210 
211     public LifecycleAdapterFactory getLifecycleAdapterFactory()
212     {
213         return lifecycleAdapterFactory;
214     }
215 
216     public void setLifecycleAdapterFactory(LifecycleAdapterFactory lifecycleAdapterFactory)
217     {
218         this.lifecycleAdapterFactory = lifecycleAdapterFactory;
219     }
220 
221 }