KickJava   Java API By Example, From Geeks To Geeks.

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


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: SampleClientListener.java 16:40:52 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.engine.sampleclient;
23
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.jbi.messaging.DeliveryChannel;
28 import javax.jbi.messaging.ExchangeStatus;
29 import javax.jbi.messaging.MessageExchange;
30 import javax.jbi.messaging.MessagingException;
31
32 import org.objectweb.petals.component.common.util.SourceHelper;
33 import org.objectweb.petals.engine.sampleclient.gui.Console;
34
35 /**
36  * Message listener of the SampleClient component
37  *
38  * @author alouis, ddesjardins - eBMWebsourcing
39  */

40 public class SampleClientListener implements Runnable JavaDoc {
41
42     private DeliveryChannel channel;
43
44     private MessageExchange messageExchange;
45
46     private boolean running = true;
47
48     private Console console;
49
50     private Logger JavaDoc logger;
51
52     public SampleClientListener(DeliveryChannel channel, Console c, Logger JavaDoc log) {
53         super();
54         this.channel = channel;
55         console = c;
56         logger = log;
57     }
58
59     public void run() {
60
61         mainLoop: while (running) {
62             try {
63                 messageExchange = channel.accept();
64                 if (messageExchange != null) {
65                     process(messageExchange);
66                 }
67             } catch (MessagingException e) {
68                 logger.severe(this + " run()" + e.getMessage());
69                 continue mainLoop;
70             }
71         }
72
73     }
74
75     public void process(MessageExchange msg) {
76         logger.log(Level.INFO, "process(" + msg + ")");
77
78         String JavaDoc response = null;
79
80         if (ExchangeStatus.DONE.equals(msg.getStatus())) {
81             response = "receive a DONE status.";
82         } else if (ExchangeStatus.ERROR.equals(msg.getStatus())) {
83             response = "receive an ERROR status :"
84                 + msg.getError().getMessage();
85         } else {
86             try {
87                 if (msg.getMessage("OUT") != null)
88                     response = SourceHelper.createString(msg.getMessage("OUT")
89                         .getContent());
90                 else if (msg.getFault() != null)
91                     response = "FAULT : "
92                         + SourceHelper
93                             .createString(msg.getFault().getContent());
94
95                 msg.setStatus(ExchangeStatus.DONE);
96
97                 channel.send(msg);
98             } catch (Exception JavaDoc e) {
99                 logger.log(Level.SEVERE, e.getClass() + ":" + e.getMessage());
100                 response = e.getMessage();
101             }
102         }
103         console.setResponse(response);
104     }
105
106     public void stopProcessing() {
107         this.running = false;
108     }
109
110 }
111
Popular Tags