KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > messaging > ExchangePacket


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.messaging;
18
19 import org.apache.servicemix.components.util.CopyTransformer;
20 import org.apache.servicemix.jbi.framework.ComponentNameSpace;
21
22 import javax.jbi.messaging.ExchangeStatus;
23 import javax.jbi.messaging.Fault;
24 import javax.jbi.messaging.MessagingException;
25 import javax.jbi.messaging.NormalizedMessage;
26 import javax.jbi.servicedesc.ServiceEndpoint;
27 import javax.transaction.Transaction JavaDoc;
28 import javax.xml.namespace.QName JavaDoc;
29
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.Externalizable JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.ObjectInput JavaDoc;
35 import java.io.ObjectInputStream JavaDoc;
36 import java.io.ObjectOutput JavaDoc;
37 import java.io.ObjectOutputStream JavaDoc;
38 import java.net.URI JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.Set JavaDoc;
43
44 /**
45  * ExchangePacket is responsible for carrying MessageExchange payloads
46  *
47  * @version $Revision: 426415 $
48  */

49 public class ExchangePacket implements Externalizable JavaDoc {
50     
51     private static final long serialVersionUID = -9110837382914609624L;
52     
53     protected URI JavaDoc pattern;
54     protected String JavaDoc exchangeId;
55     protected ComponentNameSpace destinationId;
56     protected ComponentNameSpace sourceId;
57     protected ExchangeStatus status = ExchangeStatus.ACTIVE;
58     protected QName JavaDoc serviceName;
59     protected QName JavaDoc interfaceName;
60     protected QName JavaDoc operationName;
61     protected Exception JavaDoc error;
62     protected Map JavaDoc properties;
63     protected NormalizedMessageImpl in;
64     protected NormalizedMessageImpl out;
65     protected FaultImpl fault;
66     protected ServiceEndpoint endpoint;
67     protected transient Transaction JavaDoc transactionContext;
68     protected Boolean JavaDoc persistent;
69     protected boolean aborted;
70
71     
72     public ExchangePacket() {
73     }
74
75     public ExchangePacket(ExchangePacket packet) throws MessagingException {
76         this.destinationId = packet.destinationId;
77         this.endpoint = null; // packet.endpoint;
78
this.error = null;
79         this.exchangeId = null; //???;
80
this.interfaceName = packet.interfaceName;
81         CopyTransformer ct = new CopyTransformer();
82         if (packet.in != null) {
83             in = new NormalizedMessageImpl();
84             ct.transform(null, packet.in, in);
85         }
86         if (packet.out != null) {
87             out = new NormalizedMessageImpl();
88             ct.transform(null, packet.out, out);
89         }
90         if (packet.fault != null) {
91             fault = new FaultImpl();
92             ct.transform(null, packet.fault, fault);
93         }
94         this.operationName = packet.operationName;
95         this.pattern = packet.pattern;
96         if (packet.properties != null && packet.properties.size() > 0) {
97             getProperties().putAll(packet.properties);
98         }
99         this.serviceName = packet.serviceName;
100         this.sourceId = packet.sourceId;
101         this.status = packet.status;
102         this.transactionContext = packet.transactionContext;
103         this.persistent = packet.persistent;
104     }
105
106     /**
107      * @return Returns the endpoint.
108      */

109     public ServiceEndpoint getEndpoint() {
110         return endpoint;
111     }
112
113     /**
114      * @param endpoint The endpoint to set.
115      */

116     public void setEndpoint(ServiceEndpoint endpoint) {
117         this.endpoint = endpoint;
118     }
119
120     /**
121      * @return Returns the transactionContext.
122      */

123     public Transaction JavaDoc getTransactionContext() {
124         return transactionContext;
125     }
126
127     /**
128      * @param transactionContext The transactionContext to set.
129      */

130     public void setTransactionContext(Transaction JavaDoc transactionContext) {
131         this.transactionContext = transactionContext;
132     }
133
134     /**
135      * @return Returns the interfaceName.
136      */

137     public QName JavaDoc getInterfaceName() {
138         return interfaceName;
139     }
140
141     /**
142      * @param interfaceName The interfaceName to set.
143      */

144     public void setInterfaceName(QName JavaDoc interfaceName) {
145         this.interfaceName = interfaceName;
146     }
147
148     /**
149      * @return Returns the operationName.
150      */

151     public QName JavaDoc getOperationName() {
152         return operationName;
153     }
154
155     /**
156      * @param operationName The operationName to set.
157      */

158     public void setOperationName(QName JavaDoc operationName) {
159         this.operationName = operationName;
160     }
161
162     /**
163      * @return Returns the serviceName.
164      */

165     public QName JavaDoc getServiceName() {
166         return serviceName;
167     }
168
169     /**
170      * @param serviceName The serviceName to set.
171      */

172     public void setServiceName(QName JavaDoc serviceName) {
173         this.serviceName = serviceName;
174     }
175
176     /**
177      * @param status The status to set.
178      */

179     public void setStatus(ExchangeStatus status) {
180         this.status = status;
181     }
182
183     /**
184      * @return the status
185      */

186     public ExchangeStatus getStatus() {
187         return status;
188     }
189
190     /**
191      * @return Returns the pattern.
192      */

193     public URI JavaDoc getPattern() {
194         return pattern;
195     }
196
197     /**
198      * @param pattern The pattern to set.
199      */

200     public void setPattern(URI JavaDoc pattern) {
201         this.pattern = pattern;
202     }
203
204     /**
205      * @return Returns the error.
206      */

207     public Exception JavaDoc getError() {
208         return error;
209     }
210
211     /**
212      * @param error The error to set.
213      */

214     public void setError(Exception JavaDoc error) {
215         this.error = error;
216         this.status = ExchangeStatus.ERROR;
217     }
218
219     /**
220      * @return Returns the exchangeId.
221      */

222     public String JavaDoc getExchangeId() {
223         return exchangeId;
224     }
225
226     /**
227      * @param exchangeId The exchangeId to set.
228      */

229     public void setExchangeId(String JavaDoc exchangeId) {
230         this.exchangeId = exchangeId;
231     }
232
233     /**
234      * @return Returns the properties.
235      */

236     public Map JavaDoc getProperties() {
237         if (properties == null) {
238             // No need to have concurrent access, as the
239
// message exchange can only be used from a single thread at a time
240
properties = new HashMap JavaDoc();
241         }
242         return properties;
243     }
244
245     /**
246      * @param name
247      * @return the proerty from the exchange
248      */

249     public Object JavaDoc getProperty(String JavaDoc name) {
250         if (properties != null) {
251             return properties.get(name);
252         }
253         return null;
254     }
255
256     /**
257      * set a named property on the exchange
258      *
259      * @param name
260      * @param value
261      */

262     public void setProperty(String JavaDoc name, Object JavaDoc value) {
263         if (value == null) {
264             if (properties != null) {
265                 properties.remove(name);
266             }
267         } else {
268             getProperties().put(name, value);
269         }
270     }
271
272     /**
273      * @return property names
274      */

275     public Set JavaDoc getPropertyNames() {
276         if (properties != null) {
277             return Collections.unmodifiableSet(properties.keySet());
278         }
279         return Collections.EMPTY_SET;
280     }
281
282     /**
283      * @return Returns the sourceId.
284      */

285     public ComponentNameSpace getSourceId() {
286         return sourceId;
287     }
288
289     /**
290      * @param sourceId The sourceId to set.
291      */

292     public void setSourceId(ComponentNameSpace sourceId) {
293         this.sourceId = sourceId;
294     }
295
296     /**
297      * @return Returns the destinationId.
298      */

299     public ComponentNameSpace getDestinationId() {
300         return destinationId;
301     }
302
303     /**
304      * @param destinationId The destinationId to set.
305      */

306     public void setDestinationId(ComponentNameSpace destinationId) {
307         this.destinationId = destinationId;
308     }
309
310     /**
311      * @return Returns the fault.
312      */

313     public Fault getFault() {
314         return fault;
315     }
316
317     /**
318      * @param fault The fault to set.
319      */

320     public void setFault(FaultImpl fault) {
321         this.fault = fault;
322     }
323
324     /**
325      * @return Returns the in.
326      */

327     public NormalizedMessage getIn() {
328         return in;
329     }
330
331     /**
332      * @param in The in to set.
333      */

334     public void setIn(NormalizedMessageImpl in) {
335         this.in = in;
336     }
337
338     /**
339      * @return Returns the out.
340      */

341     public NormalizedMessage getOut() {
342         return out;
343     }
344
345     /**
346      * @param out The out to set.
347      */

348     public void setOut(NormalizedMessageImpl out) {
349         this.out = out;
350     }
351
352     /**
353      * @return pretty print
354      */

355     public String JavaDoc toString() {
356         return "ExchangePacket[: id=" + exchangeId + ", serviceDest=" + serviceName + ",endpoint=" + endpoint + "]";
357     }
358
359     /**
360      * Write to a Stream
361      * @param output
362      * @throws IOException
363      */

364     public void writeExternal(ObjectOutput JavaDoc output) throws IOException JavaDoc {
365         output.writeUTF(pattern.toString());
366         output.writeUTF(exchangeId != null ? exchangeId : "");
367         output.writeUTF(status.toString());
368         output.writeObject(destinationId);
369         output.writeObject(sourceId);
370         output.writeObject(serviceName);
371         output.writeObject(interfaceName);
372         output.writeObject(operationName);
373         output.writeObject(error);
374         output.writeObject(properties);
375         output.writeObject(in);
376         output.writeObject(out);
377         output.writeObject(fault);
378         output.writeObject(endpoint);
379         output.writeByte((persistent == null) ? 0 : persistent.booleanValue() ? 1 : 2);
380     }
381
382     /**
383      * Read from a stream
384      *
385      * @param input
386      * @throws IOException
387      * @throws ClassNotFoundException
388      */

389     public void readExternal(ObjectInput JavaDoc input) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
390         pattern = URI.create(input.readUTF());
391         exchangeId = input.readUTF();
392         status = ExchangeStatus.valueOf(input.readUTF());
393         destinationId = (ComponentNameSpace) input.readObject();
394         sourceId = (ComponentNameSpace) input.readObject();
395         serviceName = (QName JavaDoc) input.readObject();
396         interfaceName = (QName JavaDoc) input.readObject();
397         operationName = (QName JavaDoc) input.readObject();
398         error = (Exception JavaDoc) input.readObject();
399         properties = (Map JavaDoc) input.readObject();
400         in = (NormalizedMessageImpl) input.readObject();
401         out = (NormalizedMessageImpl) input.readObject();
402         fault = (FaultImpl) input.readObject();
403         endpoint = (ServiceEndpoint) input.readObject();
404         byte p = input.readByte();
405         persistent = (p == 0) ? null : p == 1 ? Boolean.TRUE : Boolean.FALSE;
406     }
407
408     /**
409      * Creates a copy of the packet so it can be sent to another destination
410      * @throws MessagingException
411      */

412     public ExchangePacket copy() throws MessagingException {
413         return new ExchangePacket(this);
414     }
415
416     public Boolean JavaDoc getPersistent() {
417         return persistent;
418     }
419
420     public void setPersistent(Boolean JavaDoc persistent) {
421         this.persistent = persistent;
422     }
423
424     public boolean isAborted() {
425         return aborted;
426     }
427
428     public void setAborted(boolean timedOut) {
429         this.aborted = timedOut;
430     }
431     
432     /**
433      * Retrieve the serialized from of this packet
434      * @return the serialized packet
435      * @throws IOException
436      */

437     public byte[] getData() throws IOException JavaDoc {
438         ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
439         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
440         out.writeObject(this);
441         out.close();
442         return buffer.toByteArray();
443     }
444     
445     /**
446      * Deserialize an ExchangePacket.
447      * @param data the serialized packet
448      * @return the deserialized packet
449      * @throws IOException
450      * @throws ClassNotFoundException
451      */

452     public static ExchangePacket readPacket(byte[] data) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
453         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(data));
454         return (ExchangePacket) ois.readObject();
455     }
456
457 }
Popular Tags