KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > TransformComponentSupport


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.components.util;
18
19 import javax.jbi.messaging.ExchangeStatus;
20 import javax.jbi.messaging.InOnly;
21 import javax.jbi.messaging.MessageExchange;
22 import javax.jbi.messaging.MessagingException;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.xml.namespace.QName JavaDoc;
25
26 import org.apache.servicemix.JbiConstants;
27 import org.apache.servicemix.MessageExchangeListener;
28
29 /**
30  * A useful base class for a transform component.
31  *
32  * @version $Revision: 426415 $
33  */

34 public abstract class TransformComponentSupport extends ComponentSupport implements MessageExchangeListener {
35     
36     private boolean copyProperties = true;
37     private boolean copyAttachments = true;
38
39     protected TransformComponentSupport() {
40     }
41
42     protected TransformComponentSupport(QName JavaDoc service, String JavaDoc endpoint) {
43         super(service, endpoint);
44     }
45
46     public void onMessageExchange(MessageExchange exchange) {
47         // Skip done exchanges
48
if (exchange.getStatus() == ExchangeStatus.DONE) {
49             return;
50         // Handle error exchanges
51
} else if (exchange.getStatus() == ExchangeStatus.ERROR) {
52             return;
53         }
54         try {
55             InOnly outExchange = null;
56             NormalizedMessage in = getInMessage(exchange);
57             NormalizedMessage out;
58             if (isInAndOut(exchange)) {
59                 out = exchange.createMessage();
60             } else {
61                 outExchange = getExchangeFactory().createInOnlyExchange();
62                 out = outExchange.createMessage();
63             }
64             boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC));
65             copyPropertiesAndAttachments(exchange, in, out);
66             if (transform(exchange, in, out)) {
67                 if (isInAndOut(exchange)) {
68                     exchange.setMessage(out, "out");
69                     if (txSync) {
70                         getDeliveryChannel().sendSync(exchange);
71                     } else {
72                         getDeliveryChannel().send(exchange);
73                     }
74                 }
75                 else {
76                     outExchange.setMessage(out, "in");
77                     if (txSync) {
78                         getDeliveryChannel().sendSync(outExchange);
79                     } else {
80                         getDeliveryChannel().send(outExchange);
81                     }
82                     exchange.setStatus(ExchangeStatus.DONE);
83                     getDeliveryChannel().send(exchange);
84                 }
85             } else {
86                 exchange.setStatus(ExchangeStatus.DONE);
87                 getDeliveryChannel().send(exchange);
88             }
89         }
90         catch (Exception JavaDoc e) {
91             try {
92                 fail(exchange, e);
93             } catch (Exception JavaDoc e2) {
94                 logger.warn("Unable to handle error: " + e2, e2);
95                 if (logger.isDebugEnabled()) {
96                     logger.debug("Original error: " + e, e);
97                 }
98             }
99         }
100     }
101
102
103     // Implementation methods
104
//-------------------------------------------------------------------------
105

106     /**
107      * Transforms the given out message
108      */

109     protected abstract boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception JavaDoc;
110
111
112     public boolean isCopyProperties() {
113         return copyProperties;
114     }
115
116
117     public void setCopyProperties(boolean copyProperties) {
118         this.copyProperties = copyProperties;
119         if (getMessageTransformer() instanceof CopyTransformer) {
120             ((CopyTransformer) getMessageTransformer()).setCopyProperties(copyProperties);
121         }
122     }
123
124
125     public boolean isCopyAttachments() {
126         return copyAttachments;
127     }
128
129
130     public void setCopyAttachments(boolean copyAttachments) {
131         this.copyAttachments = copyAttachments;
132         if (getMessageTransformer() instanceof CopyTransformer) {
133             ((CopyTransformer) getMessageTransformer()).setCopyAttachments(copyAttachments);
134         }
135     }
136
137
138     /**
139      * If enabled the properties and attachments are copied to the destination message
140      */

141     protected void copyPropertiesAndAttachments(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
142         if (isCopyProperties()) {
143             CopyTransformer.copyProperties(in, out);
144         }
145         if (isCopyAttachments()) {
146             CopyTransformer.copyAttachments(in, out);
147         }
148     }
149 }
150
Popular Tags