1 22 package org.objectweb.petals.component.common.basic.binding; 23 24 import java.util.logging.Logger ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.management.DeploymentException; 28 import javax.jbi.messaging.DeliveryChannel; 29 import javax.jbi.messaging.ExchangeStatus; 30 import javax.jbi.messaging.MessageExchange; 31 import javax.jbi.messaging.MessagingException; 32 import javax.jbi.messaging.NormalizedMessage; 33 import javax.jbi.servicedesc.ServiceEndpoint; 34 35 import org.objectweb.petals.component.common.MEPConstants; 36 import org.objectweb.petals.component.common.basic.AbstractBasicComponent; 37 import org.objectweb.petals.component.common.basic.AbstractServiceUnitManager; 38 39 46 public abstract class AbstractBindingComponent extends AbstractBasicComponent { 47 48 protected JBIListener jbiListener; 49 50 protected ExternalListenerManager externalListenerManager; 51 52 55 public AbstractBindingComponent() { 56 super(); 57 } 58 59 67 public AbstractBindingComponent(DeliveryChannel channel, Logger log) { 68 super(channel, log); 69 } 70 71 77 public abstract JBIListener getJBIListener(); 78 79 86 public abstract ExternalListenerManager getExternalListenerManager(); 87 88 @Override 89 protected AbstractServiceUnitManager createServiceUnitManager() 90 throws DeploymentException { 91 AbstractServiceUnitManager serviceUnitManager = null; 92 try { 93 serviceUnitManager = new SimpleBindingComponentServiceUnitManager( 94 getComponentContext(), log); 95 } catch (JBIException e) { 96 throw new DeploymentException( 97 "Error when creating the Service Unit Manager", e); 98 } 99 100 return serviceUnitManager; 101 } 102 103 @Override 104 public void start() throws JBIException { 105 jbiListener = getJBIListener(); 106 externalListenerManager = getExternalListenerManager(); 107 ((SimpleBindingComponentServiceUnitManager) serviceUnitManager) 108 .setExternalListenerManager(externalListenerManager); 109 super.start(); 110 111 } 112 113 117 public boolean process(MessageExchange exchange) throws Exception { 118 119 if (ExchangeStatus.ACTIVE.equals(exchange.getStatus()) 120 && MessageExchange.Role.PROVIDER.equals(exchange.getRole())) { 121 122 String address = ((SimpleBindingComponentServiceUnitManager) serviceUnitManager) 123 .getAddressForEndpoint(exchange.getEndpoint()); 124 125 if (MEPConstants.IN_ONLY_PATTERN.equals(exchange.getPattern())) { 126 processInOnly(exchange, address); 127 128 } else if (MEPConstants.ROBUST_IN_ONLY_PATTERN.equals(exchange 129 .getPattern())) { 130 processRobustInOnly(exchange, address); 131 132 } else if (MEPConstants.IN_OUT_PATTERN 133 .equals(exchange.getPattern()) 134 || MEPConstants.IN_OPTIONAL_OUT_PATTERN.equals(exchange 135 .getPattern())) { 136 processInOut(exchange, address); 137 138 } else { 139 Exception e = new Exception ( 140 "MessageExchangePattern not recognized :" 141 + exchange.getPattern()); 142 exchange.setError(e); 143 getDeliveryChannel().send(exchange); 144 throw e; 145 } 146 getDeliveryChannel().send(exchange); 147 } 148 return true; 149 } 150 151 158 protected void processInOnly(MessageExchange exchange, String address) 159 throws MessagingException { 160 NormalizedMessage in = exchange.getMessage("in"); 161 try{ 162 jbiListener.onJBIMessage(address, in, null, exchange.getOperation(), 163 exchange.getPattern()); 164 }catch(MessagingException e){ 165 } 167 exchange.setStatus(ExchangeStatus.DONE); 168 } 169 170 177 protected void processRobustInOnly(MessageExchange exchange,String address) throws MessagingException{ 178 NormalizedMessage in = exchange.getMessage("in"); 179 try { 180 jbiListener.onJBIMessage(address, in, null, exchange.getOperation(), exchange 181 .getPattern()); 182 exchange.setStatus(ExchangeStatus.DONE); 184 } catch (MessagingException e) { 185 exchange.setFault(createFault(new Exception ( 187 "Error sending the message", e), exchange)); 188 } 189 } 190 191 198 protected void processInOut(MessageExchange exchange, String address) 199 throws MessagingException { 200 201 NormalizedMessage out = exchange.createMessage(); 202 NormalizedMessage in = exchange.getMessage("in"); 203 204 try { 205 boolean responseSet = jbiListener.onJBIMessage(address, in, out, exchange 206 .getOperation(), exchange.getPattern()); 207 208 if (responseSet || MEPConstants.IN_OUT_PATTERN.equals(exchange 209 .getPattern())) { 210 exchange.setMessage(out, "out"); 212 } else { 213 exchange.setStatus(ExchangeStatus.DONE); 215 } 216 } catch (MessagingException e) { 217 exchange.setFault(createFault(new Exception ( 219 "Error sending the message", e), exchange)); 220 } 221 222 } 223 224 238 public void sendMessage(String address, MessageExchange messageExchange) 239 throws MessagingException { 240 ServiceEndpoint ep = ((SimpleBindingComponentServiceUnitManager) serviceUnitManager) 241 .getEndpointForAddress(address); 242 if (ep != null) { 243 messageExchange.setEndpoint(ep); 244 messageExchange.setService(ep.getServiceName()); 245 try { 246 getDeliveryChannel().send(messageExchange); 247 } catch (JBIException e) { 248 throw new MessagingException( 249 "Failed to send the mapped message to the delivery channel."); 250 } 251 } else { 252 throw new MessagingException( 253 "No endpoint associated with this address"); 254 } 255 } 256 } 257 | Popular Tags |