KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > sampleclient > SampleClient


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: SampleClient.java 16:40:52 dutoo $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.engine.sampleclient;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import javax.activation.DataHandler JavaDoc;
32 import javax.activation.FileDataSource JavaDoc;
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 JavaDoc;
48 import javax.xml.namespace.QName JavaDoc;
49 import javax.xml.parsers.DocumentBuilder JavaDoc;
50 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
51 import javax.xml.transform.Source JavaDoc;
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 JavaDoc;
58 import org.w3c.dom.DocumentFragment JavaDoc;
59 import org.xml.sax.InputSource JavaDoc;
60
61 /**
62  * Main class of the SampleClient component, this client is used to manage a
63  * container and to test it
64  *
65  * @author alouis, ddesjardins - eBMWebsourcing
66  * @author Marc Dutoo - Open Wide
67  */

68 public class SampleClient implements Component, ComponentLifeCycle {
69
70     private ComponentContext context;
71
72     private DeliveryChannel channel;
73
74     @SuppressWarnings JavaDoc("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 JavaDoc 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 JavaDoc listenerThread = new Thread JavaDoc(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 JavaDoc 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 JavaDoc arg0) {
152         return null;
153     }
154
155     public ObjectName JavaDoc getExtensionMBeanName() {
156         return null;
157     }
158
159     public static final String JavaDoc INONLY = "InOnly";
160
161     public static final String JavaDoc INOUT = "InOut";
162
163     public static final String JavaDoc INOPTIONALOUT = "InOptionalOut";
164
165     public static final String JavaDoc ROBUSTINONLY = "RobustInOnly";
166
167     /**
168      * Sends a message through Petals JBI
169      *
170      * @param service
171      * the target JBI service
172      * @param op
173      * @param content
174      * the message content
175      * @param type
176      * @param attachmentFiles
177      * the message's attached files (if any)
178      * @param syncTime
179      */

180     public void send(String JavaDoc service, String JavaDoc op, String JavaDoc content, String JavaDoc type,
181         List JavaDoc<File JavaDoc> attachmentFiles, long syncTime) {
182         logger.log(Level.INFO, "SampleClient try to send");
183         try {
184             Source JavaDoc 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             // setting content
202
nm.setContent(source);
203
204             // setting attachments if any
205
if (attachmentFiles != null) {
206                 for (File JavaDoc attachmentFile : attachmentFiles) {
207                     try {
208                         nm
209                             .addAttachment(attachmentFile.getName(),
210                                 new DataHandler JavaDoc(new FileDataSource JavaDoc(
211                                     attachmentFile)));
212                     } catch (MessagingException e) {
213                         throw new Exception JavaDoc("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             // test if a service endpoint satisfy the service
233
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 JavaDoc 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 JavaDoc text) {
263         InputSource JavaDoc source = createSource(text);
264
265         DocumentFragment JavaDoc result = null;
266
267         try {
268             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory
269                 .newInstance();
270
271             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
272
273             Document JavaDoc document = builder.parse(source);
274
275             result = document.createDocumentFragment();
276         } catch (Exception JavaDoc e) {
277             e.printStackTrace();
278         }
279
280         return context.resolveEndpointReference(result);
281
282     }
283
284     public String JavaDoc getDescription(ServiceEndpoint se) {
285         Document JavaDoc doc;
286         try {
287             doc = context.getEndpointDescriptor(se);
288             return XMLUtil.parseToString(doc);
289         } catch (Throwable JavaDoc e) {
290             console.showError(e);
291         }
292         return null;
293     }
294
295     public String JavaDoc getInterfaces(ServiceEndpoint se) {
296         String JavaDoc out = "";
297         try {
298             for (QName JavaDoc interfaces : se.getInterfaces()) {
299                 out += interfaces;
300             }
301             return out;
302         } catch (Throwable JavaDoc e) {
303             console.showError(e);
304         }
305         return null;
306     }
307
308     public String JavaDoc getAsReference(ServiceEndpoint se) {
309         try {
310             DocumentFragment JavaDoc doc = se.getAsReference(null);
311             return XMLUtil.parseToString(doc);
312         } catch (Throwable JavaDoc e) {
313             console.showError(e);
314         }
315         return null;
316     }
317
318     protected InputSource JavaDoc createSource(String JavaDoc msg) {
319         InputSource JavaDoc source = new InputSource JavaDoc();
320
321         byte[] msgByte = msg.getBytes();
322
323         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(msgByte);
324
325         source.setByteStream(in);
326
327         return source;
328     }
329 }
330
Popular Tags