1 10 11 package org.mule.providers.jbi.components; 12 13 import java.io.OutputStream ; 14 import java.util.Arrays ; 15 16 import javax.jbi.JBIException; 17 import javax.jbi.messaging.MessageExchange; 18 import javax.jbi.messaging.MessagingException; 19 import javax.jbi.messaging.NormalizedMessage; 20 import javax.jbi.servicedesc.ServiceEndpoint; 21 import javax.xml.namespace.QName ; 22 23 import org.apache.commons.lang.SystemUtils; 24 import org.mule.MuleManager; 25 import org.mule.config.converters.QNameConverter; 26 import org.mule.config.i18n.Message; 27 import org.mule.impl.MuleDescriptor; 28 import org.mule.impl.MuleMessage; 29 import org.mule.providers.AbstractMessageReceiver; 30 import org.mule.providers.InternalMessageListener; 31 import org.mule.providers.jbi.JbiMessageAdapter; 32 import org.mule.providers.jbi.JbiUtils; 33 import org.mule.umo.UMOComponent; 34 import org.mule.umo.UMODescriptor; 35 import org.mule.umo.UMOEvent; 36 import org.mule.umo.UMOException; 37 import org.mule.umo.UMOMessage; 38 import org.mule.umo.UMOTransaction; 39 import org.mule.umo.lifecycle.InitialisationException; 40 import org.mule.umo.lifecycle.RecoverableException; 41 import org.mule.umo.provider.UMOMessageReceiver; 42 43 50 public class MuleReceiver extends AbstractEndpointComponent implements InternalMessageListener 51 { 52 53 private AbstractMessageReceiver receiver; 54 55 protected QName targetService; 56 57 protected String targetServiceName; 58 59 public QName getTargetService() 60 { 61 return targetService; 62 } 63 64 public void setTargetService(QName targetService) 65 { 66 this.targetService = targetService; 67 } 68 69 public AbstractMessageReceiver getReceiver() 70 { 71 return receiver; 72 } 73 74 public void setReceiver(AbstractMessageReceiver receiver) 75 { 76 this.receiver = receiver; 77 } 78 79 public String getTargetServiceName() 80 { 81 return targetServiceName; 82 } 83 84 public void setTargetServiceName(String targetServiceName) 85 { 86 this.targetServiceName = targetServiceName; 87 } 88 89 protected void doInit() throws JBIException 90 { 91 super.doInit(); 92 try 93 { 94 if (targetService == null) 95 { 96 if (targetServiceName != null) 97 { 98 targetService = (QName )new QNameConverter().convert(QName .class, targetServiceName); 99 } 100 } 101 102 UMOMessageReceiver receiver = muleEndpoint.getConnector().registerListener( 103 new NullUMOComponent(getName()), muleEndpoint); 104 105 if (receiver == null) 106 { 107 throw new NullPointerException (new Message("jbi", 1, getName()).toString()); 108 } 109 else if (receiver instanceof AbstractMessageReceiver) 110 { 111 this.receiver = (AbstractMessageReceiver)receiver; 112 } 113 else 114 { 115 throw new IllegalArgumentException (new Message("jbi", 2, getName(), 116 AbstractMessageReceiver.class.getName()).toString()); 117 } 118 119 this.receiver.setListener(this); 120 } 121 catch (Exception e) 122 { 123 throw new JBIException(e); 124 } 125 } 126 127 public UMOMessage onMessage(UMOMessage message, 128 UMOTransaction trans, 129 boolean synchronous, 130 OutputStream outputStream) throws UMOException 131 { 132 MessageExchange me = null; 133 try 134 { 135 if (synchronous) 136 { 137 me = exchangeFactory.createInOutExchange(); 138 } 139 else 140 { 141 me = exchangeFactory.createInOnlyExchange(); 142 } 143 if (targetService != null) 144 { 145 me.setService(targetService); 146 ServiceEndpoint endpoint = null; 147 ServiceEndpoint[] eps = context.getEndpointsForService(targetService); 148 if (eps.length == 0) 149 { 150 throw new MessagingException("There are no endpoints registered for targetService: " 152 + targetService); 153 } 154 else 155 { 156 endpoint = eps[0]; 157 } 158 159 if (logger.isDebugEnabled()) 160 { 161 StringBuffer buf = new StringBuffer ("Found the following endpoints for: "); 162 buf.append(targetService).append(SystemUtils.LINE_SEPARATOR); 163 for (int i = 0; i < eps.length; i++) 164 { 165 ServiceEndpoint ep = eps[i]; 166 buf.append(ep.getEndpointName()) 167 .append(";") 168 .append(ep.getServiceName()) 169 .append(";") 170 .append(Arrays.asList(ep.getInterfaces())) 171 .append(SystemUtils.LINE_SEPARATOR); 172 } 173 logger.debug(buf.toString()); 174 } 175 176 logger.debug("Using Jbi Endpoint for targetService: " + targetService + " is: " + endpoint); 177 if (endpoint != null) 178 { 179 me.setEndpoint(endpoint); 180 } 181 } 182 else 183 { 184 logger.debug("Jbi target service is not set Container will need to resolve target"); 185 } 186 187 NormalizedMessage nmessage = me.createMessage(); 188 JbiUtils.populateNormalizedMessage(message, nmessage); 189 190 me.setMessage(nmessage, IN); 191 if (synchronous) 192 { 193 deliveryChannel.sendSync(me, MuleManager.getConfiguration().getSynchronousEventTimeout()); 194 NormalizedMessage result = null; 195 196 result = me.getMessage(OUT); 197 done(me); 198 if (result != null) 199 { 200 return new MuleMessage(new JbiMessageAdapter(result)); 201 } 202 else 203 { 204 return null; 205 } 206 } 207 else 208 { 209 deliveryChannel.send(me); 210 return null; 211 } 212 } 213 catch (MessagingException e) 214 { 215 try 216 { 217 error(me, e); 218 return null; 219 } 220 catch (MessagingException e1) 221 { 222 handleException(e); 223 return null; 224 } 225 } 226 } 227 228 232 class NullUMOComponent implements UMOComponent 233 { 234 237 private static final long serialVersionUID = 6446394166371870045L; 238 239 private UMODescriptor descriptor; 240 241 public NullUMOComponent(String name) 242 { 243 this.descriptor = new MuleDescriptor(name); 244 } 245 246 public UMODescriptor getDescriptor() 247 { 248 return descriptor; 249 } 250 251 public void dispatchEvent(UMOEvent event) throws UMOException 252 { 253 throw new UnsupportedOperationException ("NullComponent:dispatchEvent"); 254 } 255 256 public UMOMessage sendEvent(UMOEvent event) throws UMOException 257 { 258 throw new UnsupportedOperationException ("NullComponent:sendEvent"); 259 } 260 261 public void pause() throws UMOException 262 { 263 } 265 266 public void resume() throws UMOException 267 { 268 } 270 271 public boolean isPaused() 272 { 273 return false; 274 } 275 276 public void start() throws UMOException 277 { 278 } 280 281 public void stop() throws UMOException 282 { 283 } 285 286 public void dispose() 287 { 288 } 290 291 public void initialise() throws InitialisationException, RecoverableException 292 { 293 } 295 296 public boolean isStarted() 297 { 298 return true; 299 } 300 301 public Object getInstance() throws UMOException 302 { 303 return null; 304 } 305 } 306 307 } 308 | Popular Tags |