KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > PojoSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.util;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.component.ComponentContext;
21 import javax.jbi.component.ComponentLifeCycle;
22 import javax.jbi.messaging.DeliveryChannel;
23 import javax.jbi.messaging.ExchangeStatus;
24 import javax.jbi.messaging.Fault;
25 import javax.jbi.messaging.InOnly;
26 import javax.jbi.messaging.InOptionalOut;
27 import javax.jbi.messaging.InOut;
28 import javax.jbi.messaging.MessageExchange;
29 import javax.jbi.messaging.MessageExchangeFactory;
30 import javax.jbi.messaging.MessagingException;
31 import javax.jbi.messaging.NormalizedMessage;
32 import javax.jbi.servicedesc.ServiceEndpoint;
33 import javax.management.ObjectName JavaDoc;
34 import javax.xml.namespace.QName JavaDoc;
35 import javax.xml.transform.Source JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.servicemix.JavaSource;
40 import org.apache.servicemix.jbi.FaultException;
41 import org.apache.servicemix.jbi.NotInitialisedYetException;
42 import org.apache.servicemix.jbi.management.BaseLifeCycle;
43 import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
44 import org.apache.servicemix.jbi.messaging.PojoMarshaler;
45
46 /**
47  * A useful base class for a POJO based JBI component which contains most of the basic plumbing
48  *
49  * @version $Revision: 427469 $
50  */

51 public abstract class PojoSupport extends BaseLifeCycle implements ComponentLifeCycle {
52
53     private ComponentContext context;
54     private ObjectName JavaDoc extensionMBeanName;
55     private QName JavaDoc service;
56     private String JavaDoc endpoint;
57     private MessageExchangeFactory exchangeFactory;
58     private String JavaDoc description = "POJO Component";
59     private ServiceEndpoint serviceEndpoint;
60     private DeliveryChannel channel;
61     
62     protected Log logger = LogFactory.getLog(getClass());
63     
64     protected PojoSupport() {
65     }
66
67     protected PojoSupport(QName JavaDoc service, String JavaDoc endpoint) {
68         this.service = service;
69         this.endpoint = endpoint;
70     }
71     
72     /**
73      * Get the description
74      * @return the description
75      */

76     public String JavaDoc getDescription(){
77         return description;
78     }
79
80
81     /**
82      * Called when the Component is initialized
83      *
84      * @param cc
85      * @throws JBIException
86      */

87     public void init(ComponentContext cc) throws JBIException {
88         this.context = cc;
89         this.channel = this.context.getDeliveryChannel();
90         init();
91         if (service != null && endpoint != null) {
92             serviceEndpoint = context.activateEndpoint(service, endpoint);
93         }
94     }
95
96     /**
97      * Shut down the item. The releases resources, preparing to uninstall
98      *
99      * @exception javax.jbi.JBIException if the item fails to shut down.
100      */

101     public void shutDown() throws javax.jbi.JBIException {
102         if (serviceEndpoint != null) {
103             context.deactivateEndpoint(serviceEndpoint);
104         }
105         exchangeFactory = null;
106         super.shutDown();
107     }
108
109     // Helper methods
110
//-------------------------------------------------------------------------
111

112     /**
113      * A helper method to return the body of the message as a POJO which could be a
114      * bean or some DOMish model of the body.
115      *
116      * @param message the message on which to extract the body
117      * @return the body of the message as a POJO or DOM object
118      * @throws MessagingException
119      */

120     public Object JavaDoc getBody(NormalizedMessage message) throws MessagingException {
121         Source JavaDoc content = message.getContent();
122         if (content instanceof JavaSource) {
123             JavaSource source = (JavaSource) content;
124             return source.getObject();
125         }
126         if (message instanceof NormalizedMessageImpl) {
127             return ((NormalizedMessageImpl) message).getBody();
128         }
129         return message.getProperty(PojoMarshaler.BODY);
130     }
131
132     /**
133      * Sets the body of the message as a POJO
134      *
135      * @param message the message on which to set the body
136      * @param body the POJO or DOMish model to set
137      * @throws MessagingException
138      */

139     public void setBody(NormalizedMessage message, Object JavaDoc body) throws MessagingException {
140         Source JavaDoc content = message.getContent();
141         if (content instanceof JavaSource) {
142             JavaSource source = (JavaSource) content;
143             source.setObject(body);
144         }
145         else if (message instanceof NormalizedMessageImpl) {
146             ((NormalizedMessageImpl) message).setBody(body);
147         }
148         else {
149             message.setProperty(PojoMarshaler.BODY, body);
150         }
151     }
152
153
154     // Properties
155
//-------------------------------------------------------------------------
156
public ObjectName JavaDoc getExtensionMBeanName() {
157         return extensionMBeanName;
158     }
159
160     public void setExtensionMBeanName(ObjectName JavaDoc extensionMBeanName) {
161         this.extensionMBeanName = extensionMBeanName;
162     }
163
164     public ComponentContext getContext() {
165         return context;
166     }
167
168     public QName JavaDoc getService() {
169         return service;
170     }
171
172     public void setService(QName JavaDoc service) {
173         this.service = service;
174     }
175
176     public String JavaDoc getEndpoint() {
177         return endpoint;
178     }
179
180     public void setEndpoint(String JavaDoc endpoint) {
181         this.endpoint = endpoint;
182     }
183
184
185     /**
186      * Provide access to the default message exchange exchangeFactory, lazily creating one.
187      */

188     public MessageExchangeFactory getExchangeFactory() throws MessagingException {
189         if (exchangeFactory == null) {
190             if (context != null) {
191                 exchangeFactory = getDeliveryChannel().createExchangeFactory();
192             }
193         }
194         return exchangeFactory;
195     }
196
197     public DeliveryChannel getDeliveryChannel() throws MessagingException {
198         if (channel == null) {
199             throw new NotInitialisedYetException();
200         }
201         return channel;
202     }
203
204     /**
205      * A helper method to allow a component to initialise prior to the endpoint being activated
206      * but after the component context has been configured.
207      *
208      * @throws JBIException
209      */

210     protected void init() throws JBIException {
211         super.init();
212     }
213
214     /**
215      * A helper method to indicate that the message exchange is complete
216      * which will set the status to {@link ExchangeStatus#DONE} and send the message
217      * on the delivery channel.
218      *
219      * @param exchange
220      * @throws MessagingException
221      */

222     public void done(MessageExchange exchange) throws MessagingException {
223         exchange.setStatus(ExchangeStatus.DONE);
224         getDeliveryChannel().send(exchange);
225     }
226
227     public void send(MessageExchange exchange) throws MessagingException {
228         getDeliveryChannel().send(exchange);
229     }
230     
231     public boolean sendSync(MessageExchange exchange) throws MessagingException {
232         return getDeliveryChannel().sendSync(exchange);
233     }
234
235     public boolean sendSync(MessageExchange exchange, long timeMillis) throws MessagingException {
236         return getDeliveryChannel().sendSync(exchange, timeMillis);
237     }
238
239     /**
240      * A helper method to indicate that the message exchange should be
241      * continued with the given response and send the message
242      * on the delivery channel.
243      *
244      * @param exchange
245      * @throws MessagingException
246      */

247     public void answer(MessageExchange exchange, NormalizedMessage answer) throws MessagingException {
248         exchange.setMessage(answer, "out");
249         getDeliveryChannel().send(exchange);
250     }
251
252     /**
253      * A helper method which fails and completes the given exchange with the specified fault
254      */

255     public void fail(MessageExchange exchange, Fault fault) throws MessagingException {
256         if (exchange instanceof InOnly || fault == null) {
257             exchange.setError(new FaultException("Fault occured for in-only exchange", exchange, fault));
258         } else {
259             exchange.setFault(fault);
260         }
261         getDeliveryChannel().send(exchange);
262     }
263
264     /**
265      * A helper method which fails and completes the given exchange with the specified error
266      * @throws MessagingException
267      */

268     public void fail(MessageExchange exchange, Exception JavaDoc error) throws MessagingException {
269         if (exchange instanceof InOnly || error instanceof FaultException == false) {
270             exchange.setError(error);
271         } else {
272             FaultException faultException = (FaultException) error;
273             exchange.setFault(faultException.getFault());
274         }
275         getDeliveryChannel().send(exchange);
276     }
277
278
279     /**
280      * A helper method which will return true if the exchange is capable of both In and Out such as InOut,
281      * InOptionalOut etc.
282      *
283      * @param exchange
284      * @return true if the exchange can handle both input and output
285      */

286     protected boolean isInAndOut(MessageExchange exchange) {
287         return exchange instanceof InOut || exchange instanceof InOptionalOut;
288     }
289
290 }
291
Popular Tags