1 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 ; 34 import javax.xml.namespace.QName ; 35 import javax.xml.transform.Source ; 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 51 public abstract class PojoSupport extends BaseLifeCycle implements ComponentLifeCycle { 52 53 private ComponentContext context; 54 private ObjectName extensionMBeanName; 55 private QName service; 56 private String endpoint; 57 private MessageExchangeFactory exchangeFactory; 58 private String 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 service, String endpoint) { 68 this.service = service; 69 this.endpoint = endpoint; 70 } 71 72 76 public String getDescription(){ 77 return description; 78 } 79 80 81 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 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 112 120 public Object getBody(NormalizedMessage message) throws MessagingException { 121 Source 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 139 public void setBody(NormalizedMessage message, Object body) throws MessagingException { 140 Source 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 public ObjectName getExtensionMBeanName() { 157 return extensionMBeanName; 158 } 159 160 public void setExtensionMBeanName(ObjectName extensionMBeanName) { 161 this.extensionMBeanName = extensionMBeanName; 162 } 163 164 public ComponentContext getContext() { 165 return context; 166 } 167 168 public QName getService() { 169 return service; 170 } 171 172 public void setService(QName service) { 173 this.service = service; 174 } 175 176 public String getEndpoint() { 177 return endpoint; 178 } 179 180 public void setEndpoint(String endpoint) { 181 this.endpoint = endpoint; 182 } 183 184 185 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 210 protected void init() throws JBIException { 211 super.init(); 212 } 213 214 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 247 public void answer(MessageExchange exchange, NormalizedMessage answer) throws MessagingException { 248 exchange.setMessage(answer, "out"); 249 getDeliveryChannel().send(exchange); 250 } 251 252 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 268 public void fail(MessageExchange exchange, Exception 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 286 protected boolean isInAndOut(MessageExchange exchange) { 287 return exchange instanceof InOut || exchange instanceof InOptionalOut; 288 } 289 290 } 291 | Popular Tags |