1 17 package org.apache.servicemix.components.util; 18 19 import org.apache.servicemix.jbi.NoInMessageAvailableException; 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.DocumentFragment ; 22 23 import javax.jbi.component.Component; 24 import javax.jbi.component.ComponentLifeCycle; 25 import javax.jbi.component.ServiceUnitManager; 26 import javax.jbi.messaging.DeliveryChannel; 27 import javax.jbi.messaging.InOnly; 28 import javax.jbi.messaging.InOut; 29 import javax.jbi.messaging.MessageExchange; 30 import javax.jbi.messaging.MessageExchangeFactory; 31 import javax.jbi.messaging.MessagingException; 32 import javax.jbi.messaging.NormalizedMessage; 33 import javax.jbi.servicedesc.ServiceEndpoint; 34 import javax.xml.namespace.QName ; 35 36 41 public abstract class ComponentSupport extends PojoSupport implements Component { 42 43 private ComponentLifeCycle lifeCycle; 44 private ServiceUnitManager serviceManager; 45 private MessageTransformer messageTransformer = CopyTransformer.getInstance(); 46 47 protected ComponentSupport() { 48 } 49 50 51 protected ComponentSupport(QName service, String endpoint) { 52 super(service, endpoint); 53 } 54 55 58 public ComponentLifeCycle getLifeCycle() { 59 synchronized (this) { 60 if (lifeCycle == null) { 61 lifeCycle = createComponentLifeCycle(); 62 } 63 } 64 return lifeCycle; 65 } 66 67 70 public ServiceUnitManager getServiceUnitManager() { 71 initializeServiceUnitManager(); 72 return serviceManager; 73 } 74 75 79 public ServiceEndpoint resolveEndpointReference(DocumentFragment fragment) { 80 return null; 81 } 82 83 91 92 public Document getServiceDescription(ServiceEndpoint endpoint) { 93 return null; 94 } 95 96 108 public boolean isExchangeWithConsumerOkay(ServiceEndpoint endpoint, 109 MessageExchange exchange) { 110 return true; 111 } 112 113 126 public boolean isExchangeWithProviderOkay(ServiceEndpoint endpoint, 127 MessageExchange exchange) { 128 return true; 129 } 130 131 132 135 protected synchronized void initializeServiceUnitManager() { 136 if (this.serviceManager == null) { 137 this.serviceManager = createServiceUnitManager(); 138 } 139 } 140 141 protected ServiceUnitManager createServiceUnitManager() { 142 return new ServiceUnitManagerSupport(); 143 } 144 145 protected ComponentLifeCycle createComponentLifeCycle() { 146 return this; 147 } 148 149 150 153 protected NormalizedMessage getInMessage(MessageExchange exchange) throws NoInMessageAvailableException { 154 NormalizedMessage message = exchange.getMessage("in"); 155 if (message == null) { 156 throw new NoInMessageAvailableException(exchange); 157 } 158 return message; 159 } 160 161 public MessageTransformer getMessageTransformer() { 162 return messageTransformer; 163 } 164 165 public void setMessageTransformer(MessageTransformer transformer) { 166 this.messageTransformer = transformer; 167 } 168 169 178 public void invoke(MessageExchange exchange, NormalizedMessage in, QName service, QName interfaceName, QName operation) throws MessagingException { 179 InOnly outExchange = createInOnlyExchange(service, interfaceName, operation); 180 forwardToExchange(exchange, outExchange, in, operation); 181 } 182 183 186 public InOnly createInOnlyExchange(QName service, QName interfaceName, QName operation) throws MessagingException { 187 DeliveryChannel channel = getDeliveryChannel(); 188 MessageExchangeFactory factory = null; 189 if (service != null) { 190 factory = channel.createExchangeFactoryForService(service); 191 } 192 else if (interfaceName != null) { 193 factory = channel.createExchangeFactory(interfaceName); 194 } 195 else { 196 factory = getExchangeFactory(); 197 } 198 InOnly outExchange = factory.createInOnlyExchange(); 199 if (service != null) { 200 outExchange.setService(service); 201 } 202 if (interfaceName != null) { 203 outExchange.setInterfaceName(interfaceName); 204 } 205 if (operation != null) { 206 outExchange.setOperation(operation); 207 } 208 return outExchange; 209 } 210 211 212 215 public InOut createInOutExchange(QName service, QName interfaceName, QName operation) throws MessagingException { 216 DeliveryChannel channel = getDeliveryChannel(); 217 MessageExchangeFactory factory = null; 218 if (service != null) { 219 factory = channel.createExchangeFactoryForService(service); 220 } 221 else if (interfaceName != null) { 222 factory = channel.createExchangeFactory(interfaceName); 223 } 224 else { 225 factory = getExchangeFactory(); 226 } 227 InOut outExchange = factory.createInOutExchange(); 228 if (service != null) { 229 outExchange.setService(service); 230 } 231 if (interfaceName != null) { 232 outExchange.setInterfaceName(interfaceName); 233 } 234 if (operation != null) { 235 outExchange.setOperation(operation); 236 } 237 return outExchange; 238 } 239 240 protected void forwardToExchange(MessageExchange exchange, InOnly outExchange, NormalizedMessage in, QName operationName) throws MessagingException { 241 if (operationName != null) { 242 exchange.setOperation(operationName); 243 } 244 forwardToExchange(exchange, outExchange, in); 245 } 246 247 protected void forwardToExchange(MessageExchange exchange, InOnly outExchange, NormalizedMessage in) throws MessagingException { 248 NormalizedMessage out = outExchange.createMessage(); 249 outExchange.setInMessage(out); 250 getMessageTransformer().transform(exchange, in, out); 251 getDeliveryChannel().send(outExchange); 252 } 253 254 } 255 | Popular Tags |