KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > basic > AbstractComponent


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: AbstractComponent.java 601 2006-06-13 12:53:50Z wjoseph $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.basic;
23
24 import java.net.URI 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.NormalizedMessage;
31 import javax.xml.namespace.QName JavaDoc;
32
33 import org.objectweb.petals.component.common.MEPConstants;
34
35 /**
36  * This class provides a basic component, allowing developper to create simply a
37  * new JBI component
38  *
39  * @author wjoseph - eBMWebSourcing
40  *
41  */

42 public abstract class AbstractComponent extends AbstractBasicComponent {
43
44     /**
45      * Default contructor
46      */

47     public AbstractComponent() {
48         super();
49     }
50
51     /**
52      * Create a new AbstractComponent
53      *
54      * @param channel
55      * @param log
56      */

57     public AbstractComponent(DeliveryChannel channel, Logger JavaDoc log) {
58         super(channel, log);
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.objectweb.petals.component.common.basic.AbstractBasicComponent#process(javax.jbi.messaging.MessageExchange)
65      */

66     public boolean process(MessageExchange exchange) throws Exception JavaDoc {
67         if (ExchangeStatus.ACTIVE.equals(exchange.getStatus())) {
68             if (MessageExchange.Role.PROVIDER.equals(exchange.getRole())) {
69                 if (MEPConstants.IN_ONLY_PATTERN.equals(exchange.getPattern())) {
70                     processInOnly(exchange);
71                 } else if (MEPConstants.ROBUST_IN_ONLY_PATTERN.equals(exchange
72                         .getPattern())) {
73                     processRobustInOnly(exchange);
74                 } else if (MEPConstants.IN_OUT_PATTERN.equals(exchange
75                         .getPattern())
76                         || MEPConstants.IN_OPTIONAL_OUT_PATTERN.equals(exchange
77                                 .getPattern())) {
78                     processInOut(exchange);
79                 } else {
80                     Exception JavaDoc e = new Exception JavaDoc(
81                             "MessageExchangePattern not recognized :"
82                                     + exchange.getPattern());
83                     exchange.setError(e);
84                     getDeliveryChannel().send(exchange);
85                     throw e;
86                 }
87                 getDeliveryChannel().send(exchange);
88             }
89         }
90         return true;
91     }
92
93     /**
94      * Process "in-only" MessageExchange
95      *
96      * @param exchange
97      * the MessageExchange
98      * @throws Exception
99      * When errors occur during processing
100      */

101     protected void processInOnly(MessageExchange exchange) throws Exception JavaDoc {
102         try {
103             handleMessage(exchange.getEndpoint().getServiceName(), exchange
104                     .getOperation(), exchange.getMessage("in"), exchange
105                     .getMessage("out"), exchange.getPattern());
106         } catch (Exception JavaDoc e) {
107             // nothing, InOnly can not indicate a fault
108
logError(new Exception JavaDoc(
109                     "Error occured while processing, but InOnly message can not contains Fault.",
110                     e));
111         }
112         exchange.setStatus(ExchangeStatus.DONE);
113     }
114
115     /**
116      * Process "in-out" MessageExchange
117      *
118      * @param exchange
119      * the MessageExchange
120      * @throws Exception
121      * When errors occur during processing
122      */

123     protected void processInOut(MessageExchange exchange) throws Exception JavaDoc {
124         try {
125             if (!ackFaultReception(exchange)) {
126                 NormalizedMessage out = createNormalizedMessage(exchange);
127
128                 boolean outTreated = handleMessage(exchange.getEndpoint()
129                         .getServiceName(), exchange.getOperation(), exchange
130                         .getMessage("in"), out, exchange.getPattern());
131
132                 if (outTreated) {
133                     exchange.setMessage(out, "out");
134                 } else {
135                     exchange.setStatus(ExchangeStatus.DONE);
136                 }
137             }
138         } catch (Exception JavaDoc e) {
139             try {
140                 exchange.setFault(createFault(e, exchange));
141             } catch (Exception JavaDoc e1) {
142                 logError(new Exception JavaDoc(
143                         "Error occured while processing, send a Fault.", e1));
144             }
145         }
146     }
147
148     /**
149      * Process "robust-in-only" MessageExchange
150      *
151      * @param exchange
152      * the MessageExchange
153      * @throws Exception
154      * When errors occur during processing
155      */

156     protected void processRobustInOnly(MessageExchange exchange)
157         throws Exception JavaDoc {
158         try {
159             handleMessage(exchange.getEndpoint().getServiceName(), exchange
160                     .getOperation(), exchange.getMessage("in"), exchange
161                     .getMessage("out"), exchange.getPattern());
162             exchange.setStatus(ExchangeStatus.DONE);
163         } catch (Exception JavaDoc e) {
164             try {
165                 exchange.setFault(createFault(e, exchange));
166             } catch (Exception JavaDoc e1) {
167                 logError(new Exception JavaDoc(
168                         "Error occured while processing, send a Fault.", e1));
169             }
170         }
171     }
172
173     /**
174      * Called to process a message
175      *
176      * @param service
177      * The requested service QName
178      * @param operation
179      * The requested operation QName
180      * @param in
181      * The incoming {@link NormalizedMessage} extracted from the
182      * {@link MessageExchange}
183      * @param out
184      * the outgoing {@link NormalizedMessage} that have to be set if
185      * MessageExchangePattern (MEP) is "InOut" or optionaly set if
186      * MEP is "InOptionalOut". This parameter is null if MEP is
187      * "InOnly" or "RobustInOnly". This parameter is an empty
188      * NormalizedMessage if MEP is "InOut" or "InOptionalOut"
189      * @param messageExhangePattern
190      * The MessageExchange Pattern
191      * @see MEPConstants
192      * @return true if the out response has been set, false otherwise
193      * @throws Exception
194      * a Fault will be generated with the exception, the "out" will
195      * not be treated
196      */

197     protected abstract boolean handleMessage(QName JavaDoc service, QName JavaDoc operation,
198             NormalizedMessage in, NormalizedMessage out,
199             URI JavaDoc messageExhangePattern) throws Exception JavaDoc;
200
201 }
202
Popular Tags