KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > basic > AbstractBasicComponent


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: AbstractBasicComponent.java 601 2006-06-13 12:53:50Z wjoseph $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.basic;
23
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.jbi.JBIException;
28 import javax.jbi.component.Bootstrap;
29 import javax.jbi.component.Component;
30 import javax.jbi.component.ComponentContext;
31 import javax.jbi.component.ComponentLifeCycle;
32 import javax.jbi.component.InstallationContext;
33 import javax.jbi.component.ServiceUnitManager;
34 import javax.jbi.management.DeploymentException;
35 import javax.jbi.messaging.DeliveryChannel;
36 import javax.jbi.messaging.ExchangeStatus;
37 import javax.jbi.messaging.Fault;
38 import javax.jbi.messaging.MessageExchange;
39 import javax.jbi.messaging.MessagingException;
40 import javax.jbi.messaging.NormalizedMessage;
41 import javax.jbi.servicedesc.ServiceEndpoint;
42 import javax.management.ObjectName JavaDoc;
43 import javax.xml.transform.Source JavaDoc;
44
45 import org.objectweb.petals.component.common.listener.IMessageExchangeProcessor;
46 import org.objectweb.petals.component.common.listener.MessageExchangeListener;
47 import org.objectweb.petals.component.common.util.ComponentLogger;
48 import org.objectweb.petals.component.common.util.SourceHelper;
49
50 import org.w3c.dom.Document JavaDoc;
51 import org.w3c.dom.DocumentFragment JavaDoc;
52
53 /**
54  * This class provides a basic component, allowing developper to create simply a
55  * new JBI component
56  *
57  * @author wjoseph - eBMWebSourcing
58  *
59  */

60 public abstract class AbstractBasicComponent implements Component,
61         ComponentLifeCycle, Bootstrap, IMessageExchangeProcessor {
62
63     /**
64      * The component's delivery channel
65      */

66     private DeliveryChannel channel;
67
68     /**
69      * the componentContext
70      */

71     private ComponentContext context;
72
73     /**
74      * The component's delivery logger
75      */

76     protected Logger JavaDoc log;
77
78     /**
79      * The component's service unit manager
80      */

81     protected AbstractServiceUnitManager serviceUnitManager;
82
83     /**
84      * The message exhange listener
85      */

86     private MessageExchangeListener listener;
87
88     /**
89      * Default contructor
90      */

91     public AbstractBasicComponent() {
92     }
93
94     /**
95      * Create a new basic component, setting its logger and channel
96      *
97      * @param channel
98      * the component's DeliveryChannel
99      * @param log
100      * the component's logger
101      */

102     public AbstractBasicComponent(DeliveryChannel channel, Logger JavaDoc log) {
103         this.channel = channel;
104         this.log = log;
105     }
106
107     /**
108      * (non-Javadoc)
109      *
110      * @see javax.jbi.component.Bootstrap#cleanUp()
111      */

112     public void cleanUp() throws JBIException {
113
114     }
115
116     /**
117      * Returns the component DeliveryChannel
118      *
119      * @return the DeliveryChannel
120      * @throws JBIException
121      * if context retrieval fails
122      * @throws MessagingException
123      * if {@link DeliveryChannel} retrieval fails
124      */

125     protected DeliveryChannel getDeliveryChannel() throws MessagingException,
126         JBIException {
127         if (channel == null) {
128             channel = getComponentContext().getDeliveryChannel();
129         }
130         return channel;
131     }
132
133     /**
134      * Returns the component context
135      *
136      * @return the {@link ComponentContext}
137      * @throws JBIException
138      * if the {@link Component} isn't correctly initilized
139      */

140     protected ComponentContext getComponentContext() throws JBIException {
141         if (context == null) {
142             throw new JBIException("Component not initilized.");
143         }
144         return context;
145     }
146
147     /**
148      * (non-Javadoc)
149      *
150      * @see javax.jbi.component.ComponentLifeCycle#getExtensionMBeanName()
151      */

152     public ObjectName JavaDoc getExtensionMBeanName() {
153         return null;
154     }
155
156     /**
157      * (non-Javadoc)
158      *
159      * @see javax.jbi.component.Component#getLifeCycle()
160      */

161     public ComponentLifeCycle getLifeCycle() {
162         return this;
163     }
164
165     /*
166      * (non-Javadoc)
167      *
168      * @see javax.jbi.component.Component#getServiceDescription(javax.jbi.servicedesc.ServiceEndpoint)
169      */

170     public Document JavaDoc getServiceDescription(ServiceEndpoint endpoint) {
171         return serviceUnitManager.getServiceDescription(endpoint);
172     }
173
174     /*
175      * (non-Javadoc)
176      *
177      * @see javax.jbi.component.Component#getServiceUnitManager()
178      */

179     public ServiceUnitManager getServiceUnitManager() {
180         return serviceUnitManager;
181     }
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see javax.jbi.component.ComponentLifeCycle#init(javax.jbi.component.ComponentContext)
187      */

188     public void init(ComponentContext ctx) throws JBIException {
189         this.context = ctx;
190         Logger JavaDoc log = context.getLogger("", null);
191         this.log = new ComponentLogger(log, log.getName(), log
192                 .getResourceBundleName(), context.getComponentName());
193         channel = ctx.getDeliveryChannel();
194         serviceUnitManager = createServiceUnitManager();
195         log.log(Level.INFO, "init");
196     }
197
198     /*
199      * (non-Javadoc)
200      *
201      * @see javax.jbi.component.Bootstrap#init(javax.jbi.component.InstallationContext)
202      */

203     public void init(InstallationContext installCtx) throws JBIException {
204
205     }
206
207     /*
208      * (non-Javadoc)
209      *
210      * @see javax.jbi.component.Component#isExchangeWithConsumerOkay(javax.jbi.servicedesc.ServiceEndpoint,
211      * javax.jbi.messaging.MessageExchange)
212      */

213     public boolean isExchangeWithConsumerOkay(ServiceEndpoint endpoint,
214             MessageExchange exchange) {
215         return true;
216     }
217
218     /*
219      * (non-Javadoc)
220      *
221      * @see javax.jbi.component.Component#isExchangeWithProviderOkay(javax.jbi.servicedesc.ServiceEndpoint,
222      * javax.jbi.messaging.MessageExchange)
223      */

224     public boolean isExchangeWithProviderOkay(ServiceEndpoint endpoint,
225             MessageExchange exchange) {
226         return true;
227     }
228
229     /*
230      * (non-Javadoc)
231      *
232      * @see javax.jbi.component.Bootstrap#onInstall()
233      */

234     public void onInstall() throws JBIException {
235
236     }
237
238     /*
239      * (non-Javadoc)
240      *
241      * @see javax.jbi.component.Bootstrap#onUninstall()
242      */

243     public void onUninstall() throws JBIException {
244
245     }
246
247     /**
248      * Ignore DONE,ERROR status. Ignore "consumer" role.
249      */

250     abstract public boolean process(MessageExchange exchange) throws Exception JavaDoc;
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see javax.jbi.component.Component#resolveEndpointReference(org.w3c.dom.DocumentFragment)
256      */

257     public ServiceEndpoint resolveEndpointReference(DocumentFragment JavaDoc epr) {
258         return null;
259     }
260
261     /*
262      * (non-Javadoc)
263      *
264      * @see javax.jbi.component.ComponentLifeCycle#shutDown()
265      */

266     public void shutDown() throws JBIException {
267         log.log(Level.INFO, "shutdown");
268         listener = null;
269     }
270
271     /*
272      * (non-Javadoc)
273      *
274      * @see javax.jbi.component.ComponentLifeCycle#start()
275      */

276     public void start() throws JBIException {
277         log.log(Level.INFO, "start");
278         listener = createMessageExchangeListener(this);
279         listener.listen();
280     }
281
282     /**
283      * (non-Javadoc)
284      *
285      * @see javax.jbi.component.ComponentLifeCycle#stop()
286      */

287     public void stop() throws JBIException {
288         log.log(Level.INFO, "stop");
289         this.listener.terminate();
290     }
291
292     /**
293      * If the received exchange is a fault response from the consumer,
294      * acknowledge it : status DONE
295      *
296      * @param ex
297      * The processed {@link MessageExchange}
298      * @return true if the ack. of the Fault is done; false if no fault is
299      * present in the exchange
300      * @throws Exception
301      */

302     protected boolean ackFaultReception(MessageExchange ex) throws Exception JavaDoc {
303         boolean result = false;
304
305         if (ex.getFault() != null) {
306             ex.setStatus(ExchangeStatus.DONE);
307
308             result = true;
309         }
310         return result;
311     }
312
313     /**
314      * Creates a Fault from an exception
315      *
316      * @param e
317      * The {@link Exception} used to create a new {@link Fault}
318      * @param exchange
319      * The {@link MessageExchange} that allows to create a new
320      * {@link Fault}
321      * @return the created Fault
322      * @throws MessagingException
323      * if an error occured during Fault creation or filling
324      */

325     protected Fault createFault(Exception JavaDoc e, MessageExchange exchange)
326         throws MessagingException {
327         Fault f = exchange.createFault();
328         String JavaDoc faultString = SourceHelper.createSoapFault(e, null);
329         Source JavaDoc content = SourceHelper.createSource(faultString);
330         f.setContent(content);
331         return f;
332     }
333
334     /**
335      * Creates a new MessageExchangeListener
336      *
337      * @param processor
338      * the messageExchange processor
339      * @return the created {@link MessageExchangeListener}
340      */

341     protected MessageExchangeListener createMessageExchangeListener(
342             AbstractBasicComponent processor) {
343         return new MessageExchangeListener(channel, processor,
344                 MessageExchangeListener.IgnoredStatus.DONE_AND_ERROR_IGNORED,
345                 0, context.getComponentName());
346     }
347
348     /**
349      * Creates a new NormalizedMessage from the message Exchange
350      *
351      * @param exchange
352      * The MessageExchange to create the NormalizedMessage
353      * @return a new NormalizedMessage
354      * @throws MessagingException
355      */

356     protected NormalizedMessage createNormalizedMessage(MessageExchange exchange)
357         throws MessagingException {
358         return exchange.createMessage();
359     }
360
361     /**
362      * Create a new service unit manager
363      *
364      * @return new instance of service unit manager
365      * @throws DeploymentException
366      */

367     protected AbstractServiceUnitManager createServiceUnitManager()
368         throws DeploymentException {
369         AbstractServiceUnitManager sum = new SimpleServiceUnitManager(context,
370                 log);
371         return sum;
372     }
373
374     /**
375      * Log errors in the system
376      *
377      * @param e
378      * the error to log
379      */

380     protected void logError(Throwable JavaDoc e) {
381         if (log != null)
382             log.log(Level.SEVERE, e.getMessage(), e);
383         else
384             e.printStackTrace();
385     }
386 }
387
Popular Tags