KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > DefaultLifecycleAdapter


1 /*
2  * $Id: DefaultLifecycleAdapter.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.MuleException;
16 import org.mule.config.i18n.Message;
17 import org.mule.config.i18n.Messages;
18 import org.mule.model.DynamicEntryPoint;
19 import org.mule.model.DynamicEntryPointResolver;
20 import org.mule.umo.ComponentException;
21 import org.mule.umo.Invocation;
22 import org.mule.umo.UMODescriptor;
23 import org.mule.umo.UMOEvent;
24 import org.mule.umo.UMOException;
25 import org.mule.umo.UMOMessage;
26 import org.mule.umo.lifecycle.Disposable;
27 import org.mule.umo.lifecycle.Initialisable;
28 import org.mule.umo.lifecycle.InitialisationException;
29 import org.mule.umo.lifecycle.Startable;
30 import org.mule.umo.lifecycle.Stoppable;
31 import org.mule.umo.lifecycle.UMOLifecycleAdapter;
32 import org.mule.umo.model.UMOEntryPointResolver;
33
34 /**
35  * <code>DefaultLifecycleAdapter</code> provides lifecycle methods for all Mule
36  * managed components. It's possible to plugin custom lifecycle adapters, this can
37  * provide additional lifecycle methods triggered by an external source.
38  */

39 public class DefaultLifecycleAdapter implements UMOLifecycleAdapter
40 {
41     /**
42      * logger used by this class
43      */

44     protected static final Log logger = LogFactory.getLog(DefaultLifecycleAdapter.class);
45
46     private Object JavaDoc component;
47     private UMODescriptor descriptor;
48     private boolean isStoppable = false;
49     private boolean isStartable = false;
50     private boolean isDisposable = false;
51
52     private boolean started = false;
53     private boolean disposed = false;
54
55     private DynamicEntryPoint entryPoint;
56
57     public DefaultLifecycleAdapter(Object JavaDoc component, UMODescriptor descriptor) throws UMOException
58     {
59         this(component, descriptor, new DynamicEntryPointResolver());
60     }
61
62     public DefaultLifecycleAdapter(Object JavaDoc component,
63                                    UMODescriptor descriptor,
64                                    UMOEntryPointResolver epResolver) throws UMOException
65     {
66         initialise(component, descriptor, epResolver);
67     }
68
69     protected void initialise(Object JavaDoc component, UMODescriptor descriptor, UMOEntryPointResolver epDiscovery)
70         throws UMOException
71     {
72         if (component == null)
73         {
74             throw new IllegalArgumentException JavaDoc("Component cannot be null");
75         }
76         if (descriptor == null)
77         {
78             throw new IllegalArgumentException JavaDoc("Descriptor cannot be null");
79         }
80         if (epDiscovery == null)
81         {
82             epDiscovery = new DynamicEntryPointResolver();
83         }
84         this.component = component;
85         this.entryPoint = (DynamicEntryPoint)epDiscovery.resolveEntryPoint(descriptor);
86         this.descriptor = descriptor;
87
88         isStartable = Startable.class.isInstance(component);
89         isStoppable = Stoppable.class.isInstance(component);
90         isDisposable = Disposable.class.isInstance(component);
91
92         if (component instanceof UMODescriptorAware)
93         {
94             ((UMODescriptorAware)component).setDescriptor(descriptor);
95         }
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see org.mule.umo.lifecycle.Startable#start()
102      */

103     public void start() throws UMOException
104     {
105         if (isStartable)
106         {
107             try
108             {
109                 ((Startable)component).start();
110             }
111             catch (Exception JavaDoc e)
112             {
113                 throw new MuleException(new Message(Messages.FAILED_TO_START_X, "UMO Component: "
114                                                                                 + descriptor.getName()), e);
115             }
116         }
117         started = true;
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see org.mule.umo.lifecycle.Stoppable#stop()
124      */

125     public void stop() throws UMOException
126     {
127         if (isStoppable)
128         {
129             try
130             {
131                 ((Stoppable)component).stop();
132             }
133             catch (Exception JavaDoc e)
134             {
135                 throw new MuleException(new Message(Messages.FAILED_TO_STOP_X, "UMO Component: "
136                                                                                + descriptor.getName()), e);
137             }
138         }
139         started = false;
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see org.mule.umo.lifecycle.Disposable#dispose()
146      */

147     public void dispose()
148     {
149         if (isDisposable)
150         {
151             try
152             {
153                 ((Disposable)component).dispose();
154             }
155             catch (Exception JavaDoc e)
156             {
157                 logger.error("failed to dispose: " + descriptor.getName(), e);
158             }
159         }
160         disposed = true;
161     }
162
163     /**
164      * @return true if the component has been started
165      */

166     public boolean isStarted()
167     {
168         return started;
169     }
170
171     /**
172      * @return whether the component managed by this lifecycle has been disposed
173      */

174     public boolean isDisposed()
175     {
176         return disposed;
177     }
178
179     public UMODescriptor getDescriptor()
180     {
181         return descriptor;
182     }
183
184     public void handleException(Object JavaDoc message, Exception JavaDoc e)
185     {
186         descriptor.getExceptionListener().exceptionThrown(e);
187     }
188
189     /*
190      * (non-Javadoc)
191      *
192      * @see org.mule.umo.UMOInterceptor#intercept(org.mule.umo.UMOEvent)
193      */

194     public UMOMessage intercept(Invocation invocation) throws UMOException
195     {
196         // Invoke method
197
Object JavaDoc result;
198         UMOEvent event = RequestContext.getEvent();
199
200         try
201         {
202             result = entryPoint.invoke(component, RequestContext.getEventContext());
203         }
204         catch (Exception JavaDoc e)
205         {
206             // should all Exceptions caught here be a ComponentException?!?
207
throw new ComponentException(new Message(Messages.FAILED_TO_INVOKE_X, component.getClass()
208                 .getName()), invocation.getMessage(), event.getComponent(), e);
209         }
210
211         UMOMessage resultMessage = null;
212         if (result == null && entryPoint.isVoid())
213         {
214             resultMessage = new MuleMessage(event.getTransformedMessage(), RequestContext.getEventContext()
215                 .getMessage());
216         }
217         else if (result != null)
218         {
219             if (result instanceof UMOMessage)
220             {
221                 resultMessage = (UMOMessage)result;
222             }
223             else
224             {
225                 resultMessage = new MuleMessage(result, event.getMessage());
226             }
227         }
228         return resultMessage;
229     }
230
231     /*
232      * (non-Javadoc)
233      *
234      * @see org.mule.umo.lifecycle.Initialisable#initialise()
235      */

236     public void initialise() throws InitialisationException
237     {
238         if (Initialisable.class.isInstance(component))
239         {
240             ((Initialisable)component).initialise();
241         }
242     }
243 }
244
Popular Tags