1 22 23 package org.objectweb.petals.engine.sampleclient; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.File ; 27 import java.util.List ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 import javax.activation.DataHandler ; 32 import javax.activation.FileDataSource ; 33 import javax.jbi.JBIException; 34 import javax.jbi.component.Component; 35 import javax.jbi.component.ComponentContext; 36 import javax.jbi.component.ComponentLifeCycle; 37 import javax.jbi.component.ServiceUnitManager; 38 import javax.jbi.messaging.DeliveryChannel; 39 import javax.jbi.messaging.InOnly; 40 import javax.jbi.messaging.InOptionalOut; 41 import javax.jbi.messaging.InOut; 42 import javax.jbi.messaging.MessageExchange; 43 import javax.jbi.messaging.MessagingException; 44 import javax.jbi.messaging.NormalizedMessage; 45 import javax.jbi.messaging.RobustInOnly; 46 import javax.jbi.servicedesc.ServiceEndpoint; 47 import javax.management.ObjectName ; 48 import javax.xml.namespace.QName ; 49 import javax.xml.parsers.DocumentBuilder ; 50 import javax.xml.parsers.DocumentBuilderFactory ; 51 import javax.xml.transform.Source ; 52 53 import org.objectweb.petals.component.common.util.ComponentLogger; 54 import org.objectweb.petals.component.common.util.SourceHelper; 55 import org.objectweb.petals.engine.sampleclient.gui.Console; 56 import org.objectweb.petals.tools.jbicommon.util.XMLUtil; 57 import org.w3c.dom.Document ; 58 import org.w3c.dom.DocumentFragment ; 59 import org.xml.sax.InputSource ; 60 61 68 public class SampleClient implements Component, ComponentLifeCycle { 69 70 private ComponentContext context; 71 72 private DeliveryChannel channel; 73 74 @SuppressWarnings ("unused") 75 private ServiceEndpoint endpointReference; 76 77 private SampleClientListener listener; 78 79 private Console console; 80 81 private ComponentLogger logger; 82 83 public void init(ComponentContext context) throws JBIException { 84 85 this.context = context; 86 Logger logger = context.getLogger("", null); 87 this.logger = new ComponentLogger(logger, logger.getName(), logger 88 .getResourceBundleName(), context.getComponentName()); 89 logger.log(Level.INFO, ""); 90 } 91 92 public void shutDown() throws JBIException { 93 logger.log(Level.INFO, "shutDown"); 94 this.listener.stopProcessing(); 95 console.setVisible(false); 96 console.dispose(); 97 98 } 99 100 public void start() throws JBIException { 101 logger.log(Level.INFO, "start"); 102 103 try { 104 console = new Console(this); 105 this.channel = this.context.getDeliveryChannel(); 106 this.listener = new SampleClientListener(this.channel, console, 107 logger); 108 Thread listenerThread = new Thread (this.listener, context 109 .getComponentName() 110 + "-JBI listener thread"); 111 listenerThread.start(); 112 console.setVisible(true); 113 } catch (MessagingException e) { 114 throw new JBIException(e); 115 } 116 117 } 118 119 public void stop() throws JBIException { 120 logger.log(Level.INFO, "stop"); 121 listener.stopProcessing(); 122 console.dispose(); 123 channel.close(); 124 this.listener.stopProcessing(); 125 126 } 127 128 public ComponentLifeCycle getLifeCycle() { 129 return this; 130 } 131 132 public Document getServiceDescription(ServiceEndpoint arg0) { 133 return null; 134 } 135 136 public ServiceUnitManager getServiceUnitManager() { 137 return null; 138 } 139 140 public boolean isExchangeWithConsumerOkay(ServiceEndpoint arg0, 141 MessageExchange arg1) { 142 return false; 143 } 144 145 public boolean isExchangeWithProviderOkay(ServiceEndpoint arg0, 146 MessageExchange arg1) { 147 logger.log(Level.INFO, "SampleClient accept the exchange"); 148 return true; 149 } 150 151 public ServiceEndpoint resolveEndpointReference(DocumentFragment arg0) { 152 return null; 153 } 154 155 public ObjectName getExtensionMBeanName() { 156 return null; 157 } 158 159 public static final String INONLY = "InOnly"; 160 161 public static final String INOUT = "InOut"; 162 163 public static final String INOPTIONALOUT = "InOptionalOut"; 164 165 public static final String ROBUSTINONLY = "RobustInOnly"; 166 167 180 public void send(String service, String op, String content, String type, 181 List <File > attachmentFiles, long syncTime) { 182 logger.log(Level.INFO, "SampleClient try to send"); 183 try { 184 Source source = SourceHelper.createSource(content); 185 186 MessageExchange msg = null; 187 188 if (type.equals(INONLY)) 189 msg = channel.createExchangeFactory().createInOnlyExchange(); 190 else if (type.equals(INOUT)) 191 msg = channel.createExchangeFactory().createInOutExchange(); 192 else if (type.equals(INOPTIONALOUT)) 193 msg = channel.createExchangeFactory() 194 .createInOptionalOutExchange(); 195 else if (type.equals(ROBUSTINONLY)) 196 msg = channel.createExchangeFactory() 197 .createRobustInOnlyExchange(); 198 199 NormalizedMessage nm = msg.createMessage(); 200 201 nm.setContent(source); 203 204 if (attachmentFiles != null) { 206 for (File attachmentFile : attachmentFiles) { 207 try { 208 nm 209 .addAttachment(attachmentFile.getName(), 210 new DataHandler (new FileDataSource ( 211 attachmentFile))); 212 } catch (MessagingException e) { 213 throw new Exception ("Error when attaching file " 214 + attachmentFile.getName()); 215 } 216 } 217 } 218 219 msg.setService(QName.valueOf(service)); 220 221 msg.setOperation(QName.valueOf(op)); 222 223 if (type.equals(INONLY)) 224 ((InOnly) msg).setInMessage(nm); 225 else if (type.equals(INOUT)) 226 ((InOut) msg).setInMessage(nm); 227 else if (type.equals(INOPTIONALOUT)) 228 ((InOptionalOut) msg).setInMessage(nm); 229 else if (type.equals(ROBUSTINONLY)) 230 ((RobustInOnly) msg).setInMessage(nm); 231 232 ServiceEndpoint[] eps = context.getEndpointsForService(msg 234 .getService()); 235 if (eps == null || eps.length == 0) { 236 console 237 .showWarning("No Endpoint satisfy the following service : " 238 + service); 239 } else { 240 if (syncTime < 0) { 241 channel.send(msg); 242 } else { 243 boolean ok = channel.sendSync(msg, syncTime); 244 if (ok) { 245 listener.process(msg); 246 } else { 247 console.setResponse("Time out !"); 248 } 249 } 250 } 251 } catch (Exception e) { 252 logger.log(Level.SEVERE, e.getMessage()); 253 console.setResponse(e.getMessage()); 254 console.showError(e); 255 } 256 } 257 258 public ServiceEndpoint[] getEndpoints() { 259 return context.getEndpoints(null); 260 } 261 262 public ServiceEndpoint resolveDescription(String text) { 263 InputSource source = createSource(text); 264 265 DocumentFragment result = null; 266 267 try { 268 DocumentBuilderFactory factory = DocumentBuilderFactory 269 .newInstance(); 270 271 DocumentBuilder builder = factory.newDocumentBuilder(); 272 273 Document document = builder.parse(source); 274 275 result = document.createDocumentFragment(); 276 } catch (Exception e) { 277 e.printStackTrace(); 278 } 279 280 return context.resolveEndpointReference(result); 281 282 } 283 284 public String getDescription(ServiceEndpoint se) { 285 Document doc; 286 try { 287 doc = context.getEndpointDescriptor(se); 288 return XMLUtil.parseToString(doc); 289 } catch (Throwable e) { 290 console.showError(e); 291 } 292 return null; 293 } 294 295 public String getInterfaces(ServiceEndpoint se) { 296 String out = ""; 297 try { 298 for (QName interfaces : se.getInterfaces()) { 299 out += interfaces; 300 } 301 return out; 302 } catch (Throwable e) { 303 console.showError(e); 304 } 305 return null; 306 } 307 308 public String getAsReference(ServiceEndpoint se) { 309 try { 310 DocumentFragment doc = se.getAsReference(null); 311 return XMLUtil.parseToString(doc); 312 } catch (Throwable e) { 313 console.showError(e); 314 } 315 return null; 316 } 317 318 protected InputSource createSource(String msg) { 319 InputSource source = new InputSource (); 320 321 byte[] msgByte = msg.getBytes(); 322 323 ByteArrayInputStream in = new ByteArrayInputStream (msgByte); 324 325 source.setByteStream(in); 326 327 return source; 328 } 329 } 330 | Popular Tags |