001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.util;
018:
019: import javax.jbi.JBIException;
020: import javax.jbi.component.ComponentContext;
021: import javax.jbi.component.ComponentLifeCycle;
022: import javax.jbi.messaging.DeliveryChannel;
023: import javax.jbi.messaging.ExchangeStatus;
024: import javax.jbi.messaging.Fault;
025: import javax.jbi.messaging.InOnly;
026: import javax.jbi.messaging.InOptionalOut;
027: import javax.jbi.messaging.InOut;
028: import javax.jbi.messaging.MessageExchange;
029: import javax.jbi.messaging.MessageExchangeFactory;
030: import javax.jbi.messaging.MessagingException;
031: import javax.jbi.messaging.NormalizedMessage;
032: import javax.jbi.servicedesc.ServiceEndpoint;
033: import javax.management.ObjectName;
034: import javax.xml.namespace.QName;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.apache.servicemix.jbi.FaultException;
039: import org.apache.servicemix.jbi.NotInitialisedYetException;
040: import org.apache.servicemix.jbi.management.BaseLifeCycle;
041:
042: /**
043: * A useful base class for a POJO based JBI component which contains most of the basic plumbing
044: *
045: * @version $Revision: 564900 $
046: */
047: public abstract class PojoSupport extends BaseLifeCycle implements
048: ComponentLifeCycle {
049:
050: protected Log logger = LogFactory.getLog(getClass());
051:
052: private ComponentContext context;
053: private ObjectName extensionMBeanName;
054: private QName service;
055: private String endpoint;
056: private MessageExchangeFactory exchangeFactory;
057: private String description = "POJO Component";
058: private ServiceEndpoint serviceEndpoint;
059: private DeliveryChannel channel;
060:
061: protected PojoSupport() {
062: }
063:
064: protected PojoSupport(QName service, String endpoint) {
065: this .service = service;
066: this .endpoint = endpoint;
067: }
068:
069: /**
070: * Get the description
071: * @return the description
072: */
073: public String getDescription() {
074: return description;
075: }
076:
077: /**
078: * Called when the Component is initialized
079: *
080: * @param cc
081: * @throws JBIException
082: */
083: public void init(ComponentContext cc) throws JBIException {
084: this .context = cc;
085: this .channel = this .context.getDeliveryChannel();
086: init();
087: if (service != null && endpoint != null) {
088: serviceEndpoint = context.activateEndpoint(service,
089: endpoint);
090: }
091: }
092:
093: /**
094: * Shut down the item. The releases resources, preparing to uninstall
095: *
096: * @exception javax.jbi.JBIException if the item fails to shut down.
097: */
098: public void shutDown() throws javax.jbi.JBIException {
099: if (serviceEndpoint != null) {
100: context.deactivateEndpoint(serviceEndpoint);
101: }
102: exchangeFactory = null;
103: super .shutDown();
104: }
105:
106: // Helper methods
107: //-------------------------------------------------------------------------
108:
109: /**
110: * A helper method to return the body of the message as a POJO which could be a
111: * bean or some DOMish model of the body.
112: *
113: * @param message the message on which to extract the body
114: * @return the body of the message as a POJO or DOM object
115: * @throws MessagingException
116: */
117: public Object getBody(NormalizedMessage message)
118: throws MessagingException {
119: return MessageHelper.getBody(message);
120: }
121:
122: /**
123: * Sets the body of the message as a POJO
124: *
125: * @param message the message on which to set the body
126: * @param body the POJO or DOMish model to set
127: * @throws MessagingException
128: */
129: public void setBody(NormalizedMessage message, Object body)
130: throws MessagingException {
131: MessageHelper.setBody(message, body);
132: }
133:
134: // Properties
135: //-------------------------------------------------------------------------
136: public ObjectName getExtensionMBeanName() {
137: return extensionMBeanName;
138: }
139:
140: public void setExtensionMBeanName(ObjectName extensionMBeanName) {
141: this .extensionMBeanName = extensionMBeanName;
142: }
143:
144: public ComponentContext getContext() {
145: return context;
146: }
147:
148: public QName getService() {
149: return service;
150: }
151:
152: public void setService(QName service) {
153: this .service = service;
154: }
155:
156: public String getEndpoint() {
157: return endpoint;
158: }
159:
160: public void setEndpoint(String endpoint) {
161: this .endpoint = endpoint;
162: }
163:
164: /**
165: * Provide access to the default message exchange exchangeFactory, lazily creating one.
166: */
167: public MessageExchangeFactory getExchangeFactory()
168: throws MessagingException {
169: if (exchangeFactory == null && context != null) {
170: exchangeFactory = getDeliveryChannel()
171: .createExchangeFactory();
172: }
173: return exchangeFactory;
174: }
175:
176: public DeliveryChannel getDeliveryChannel()
177: throws MessagingException {
178: if (channel == null) {
179: throw new NotInitialisedYetException();
180: }
181: return channel;
182: }
183:
184: /**
185: * A helper method to allow a component to initialise prior to the endpoint being activated
186: * but after the component context has been configured.
187: *
188: * @throws JBIException
189: */
190: protected void init() throws JBIException {
191: super .init();
192: }
193:
194: /**
195: * A helper method to indicate that the message exchange is complete
196: * which will set the status to {@link ExchangeStatus#DONE} and send the message
197: * on the delivery channel.
198: *
199: * @param exchange
200: * @throws MessagingException
201: */
202: public void done(MessageExchange exchange)
203: throws MessagingException {
204: exchange.setStatus(ExchangeStatus.DONE);
205: getDeliveryChannel().send(exchange);
206: }
207:
208: public void send(MessageExchange exchange)
209: throws MessagingException {
210: getDeliveryChannel().send(exchange);
211: }
212:
213: public boolean sendSync(MessageExchange exchange)
214: throws MessagingException {
215: return getDeliveryChannel().sendSync(exchange);
216: }
217:
218: public boolean sendSync(MessageExchange exchange, long timeMillis)
219: throws MessagingException {
220: return getDeliveryChannel().sendSync(exchange, timeMillis);
221: }
222:
223: /**
224: * A helper method to indicate that the message exchange should be
225: * continued with the given response and send the message
226: * on the delivery channel.
227: *
228: * @param exchange
229: * @throws MessagingException
230: */
231: public void answer(MessageExchange exchange,
232: NormalizedMessage answer) throws MessagingException {
233: exchange.setMessage(answer, "out");
234: getDeliveryChannel().send(exchange);
235: }
236:
237: /**
238: * A helper method which fails and completes the given exchange with the specified fault
239: */
240: public void fail(MessageExchange exchange, Fault fault)
241: throws MessagingException {
242: if (exchange instanceof InOnly || fault == null) {
243: exchange.setError(new FaultException(
244: "Fault occured for in-only exchange", exchange,
245: fault));
246: } else {
247: exchange.setFault(fault);
248: }
249: getDeliveryChannel().send(exchange);
250: }
251:
252: /**
253: * A helper method which fails and completes the given exchange with the specified error
254: * @throws MessagingException
255: */
256: public void fail(MessageExchange exchange, Exception error)
257: throws MessagingException {
258: if (exchange instanceof InOnly
259: || !(error instanceof FaultException)) {
260: exchange.setError(error);
261: } else {
262: FaultException faultException = (FaultException) error;
263: exchange.setFault(faultException.getFault());
264: }
265: getDeliveryChannel().send(exchange);
266: }
267:
268: /**
269: * A helper method which will return true if the exchange is capable of both In and Out such as InOut,
270: * InOptionalOut etc.
271: *
272: * @param exchange
273: * @return true if the exchange can handle both input and output
274: */
275: protected boolean isInAndOut(MessageExchange exchange) {
276: return exchange instanceof InOut
277: || exchange instanceof InOptionalOut;
278: }
279:
280: }
|