KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > Jsr181ComplexPojoTest


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.jsr181;
18
19 import javax.jbi.component.ComponentContext;
20 import javax.jbi.messaging.DeliveryChannel;
21 import javax.jbi.messaging.ExchangeStatus;
22 import javax.jbi.messaging.InOnly;
23 import javax.jbi.messaging.InOut;
24 import javax.jbi.messaging.MessageExchangeFactory;
25 import javax.jbi.messaging.NormalizedMessage;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.xml.namespace.QName JavaDoc;
28 import javax.xml.transform.Source JavaDoc;
29 import javax.xml.transform.TransformerException JavaDoc;
30
31 import java.io.BufferedReader JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.util.EventListener JavaDoc;
34
35 import junit.framework.TestCase;
36
37 import org.apache.servicemix.client.DefaultServiceMixClient;
38 import org.apache.servicemix.components.util.EchoComponent;
39 import org.apache.servicemix.jbi.container.ActivationSpec;
40 import org.apache.servicemix.jbi.container.JBIContainer;
41 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
42 import org.apache.servicemix.jbi.jaxp.StringSource;
43 import org.apache.servicemix.jbi.util.DOMUtil;
44 import org.apache.servicemix.jbi.view.DotViewEndpointListener;
45 import org.apache.servicemix.tck.ReceiverComponent;
46 import org.w3c.dom.Element JavaDoc;
47 import org.w3c.dom.Node JavaDoc;
48 import org.w3c.dom.traversal.NodeIterator;
49
50 import com.sun.org.apache.xpath.internal.CachedXPathAPI;
51
52 public class Jsr181ComplexPojoTest extends TestCase {
53
54     //private static Log logger = LogFactory.getLog(Jsr181ComponentTest.class);
55

56     protected JBIContainer container;
57     protected SourceTransformer transformer = new SourceTransformer();
58     private boolean useJmx;
59     protected EventListener JavaDoc[] listeners;
60     
61
62     public static void main(String JavaDoc[] args) {
63         Jsr181ComplexPojoTest test = new Jsr181ComplexPojoTest();
64         test.useJmx = true;
65         test.listeners = new EventListener JavaDoc[] { new DotViewEndpointListener() };
66         
67         try {
68             test.setUp();
69             test.testComplexOneWay();
70             
71             // now lets wait for the user to terminate things
72
System.out.println("Press enter to terminate the program.");
73             System.out.println("In the meantime you can use your JMX console to view the current MBeans");
74             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
75             reader.readLine();
76
77             
78             test.tearDown();
79         }
80         catch (Exception JavaDoc e) {
81             System.err.println("Caught: " + e);
82             e.printStackTrace();
83         }
84     }
85     protected void setUp() throws Exception JavaDoc {
86         container = new JBIContainer();
87         container.setUseMBeanServer(useJmx);
88         container.setCreateMBeanServer(useJmx);
89         container.setMonitorInstallationDirectory(false);
90         container.setNamingContext(new InitialContext JavaDoc());
91         container.setEmbedded(true);
92         container.setFlowName("st");
93         if (listeners != null) {
94             container.setListeners(listeners);
95         }
96         container.init();
97     }
98     
99     protected void tearDown() throws Exception JavaDoc {
100         if (container != null) {
101             container.shutDown();
102         }
103     }
104     
105     public void testComplexOneWay() throws Exception JavaDoc {
106         Jsr181SpringComponent component = new Jsr181SpringComponent();
107         Jsr181Endpoint endpoint = new Jsr181Endpoint();
108         endpoint.setPojo(new ComplexPojoImpl());
109         endpoint.setServiceInterface(ComplexPojo.class.getName());
110         component.setEndpoints(new Jsr181Endpoint[] { endpoint });
111         container.activateComponent(component, "JSR181Component");
112         
113         ReceiverComponent receiver = new ReceiverComponent();
114         container.activateComponent(receiver, "Receiver");
115         
116         container.start();
117
118         DefaultServiceMixClient client = new DefaultServiceMixClient(container);
119         InOut me = client.createInOutExchange();
120         me.setInterfaceName(new QName JavaDoc("http://jsr181.servicemix.apache.org", "ComplexPojoPortType"));
121         me.getInMessage().setContent(new StringSource("<oneWay xmlns='http://jsr181.servicemix.apache.org'><in0>world</in0></oneWay>"));
122         client.sendSync(me);
123         
124         // Wait all acks being processed
125
Thread.sleep(100);
126     }
127     
128     public void testComplexTwoWay() throws Exception JavaDoc {
129         Jsr181SpringComponent component = new Jsr181SpringComponent();
130         Jsr181Endpoint endpoint = new Jsr181Endpoint();
131         endpoint.setPojo(new ComplexPojoImpl());
132         endpoint.setServiceInterface(ComplexPojo.class.getName());
133         component.setEndpoints(new Jsr181Endpoint[] { endpoint });
134         container.activateComponent(component, "JSR181Component");
135         
136         EchoComponent echo = new EchoComponent();
137         ActivationSpec as = new ActivationSpec();
138         as.setComponent(echo);
139         as.setService(ReceiverComponent.SERVICE);
140         as.setComponentName("Echo");
141         container.activateComponent(as);
142         
143         container.start();
144
145         DefaultServiceMixClient client = new DefaultServiceMixClient(container);
146         InOut me = client.createInOutExchange();
147         me.setInterfaceName(new QName JavaDoc("http://jsr181.servicemix.apache.org", "ComplexPojoPortType"));
148         me.getInMessage().setContent(new StringSource("<twoWay xmlns='http://jsr181.servicemix.apache.org'><in0>world</in0></twoWay>"));
149         client.sendSync(me);
150         client.done(me);
151         
152         // Wait all acks being processed
153
Thread.sleep(100);
154     }
155     
156     public void testFault() throws Exception JavaDoc {
157         Jsr181SpringComponent component = new Jsr181SpringComponent();
158         Jsr181Endpoint endpoint = new Jsr181Endpoint();
159         endpoint.setPojo(new ComplexPojoImpl());
160         endpoint.setServiceInterface(ComplexPojo.class.getName());
161         component.setEndpoints(new Jsr181Endpoint[] { endpoint });
162         container.activateComponent(component, "JSR181Component");
163         
164         EchoComponent echo = new EchoComponent();
165         ActivationSpec as = new ActivationSpec();
166         as.setComponent(echo);
167         as.setService(ReceiverComponent.SERVICE);
168         as.setComponentName("Echo");
169         container.activateComponent(as);
170         
171         container.start();
172
173         DefaultServiceMixClient client = new DefaultServiceMixClient(container);
174         InOut me = client.createInOutExchange();
175         me.setInterfaceName(new QName JavaDoc("http://jsr181.servicemix.apache.org", "ComplexPojoPortType"));
176         me.getInMessage().setContent(new StringSource("<hel lo>world</hello"));
177         client.sendSync(me);
178         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
179         assertNotNull(me.getFault());
180         Node JavaDoc n = transformer.toDOMNode(me.getFault());
181         System.err.println(transformer.toString(n));
182         assertNotNull(textValueOfXPath(n, "//message"));
183         assertNull(textValueOfXPath(n, "//stack"));
184         client.done(me);
185         
186         ((Jsr181LifeCycle) component.getLifeCycle()).getConfiguration().setPrintStackTraceInFaults(true);
187         
188         me = client.createInOutExchange();
189         me.setInterfaceName(new QName JavaDoc("http://jsr181.servicemix.apache.org", "ComplexPojoPortType"));
190         me.getInMessage().setContent(new StringSource("<hel lo>world</hello"));
191         client.sendSync(me);
192         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
193         assertNotNull(me.getFault());
194         n = transformer.toDOMNode(me.getFault());
195         System.err.println(transformer.toString(n));
196         assertNotNull(textValueOfXPath(n, "//message"));
197         assertNotNull(textValueOfXPath(n, "//stack"));
198         client.done(me);
199         
200         // Wait all acks being processed
201
Thread.sleep(100);
202     }
203     
204     protected String JavaDoc textValueOfXPath(Node JavaDoc node, String JavaDoc xpath) throws TransformerException JavaDoc {
205         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
206         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, xpath);
207         Node JavaDoc root = iterator.nextNode();
208         if (root instanceof Element JavaDoc) {
209             Element JavaDoc element = (Element JavaDoc) root;
210             if (element == null) {
211                 return "";
212             }
213             String JavaDoc text = DOMUtil.getElementText(element);
214             return text;
215         }
216         else if (root != null) {
217             return root.getNodeValue();
218         } else {
219             return null;
220         }
221     }
222     
223     public interface ComplexPojo {
224         public void oneWay(Source JavaDoc src) throws Exception JavaDoc;
225         public Source JavaDoc twoWay(Source JavaDoc src) throws Exception JavaDoc;
226     }
227     
228     public static class ComplexPojoImpl implements ComplexPojo {
229         private ComponentContext context;
230
231         public ComponentContext getContext() {
232             return context;
233         }
234
235         public void setContext(ComponentContext context) {
236             this.context = context;
237         }
238         
239         public void oneWay(Source JavaDoc src) throws Exception JavaDoc {
240             DeliveryChannel channel = context.getDeliveryChannel();
241             MessageExchangeFactory factory = channel.createExchangeFactory();
242             InOnly inonly = factory.createInOnlyExchange();
243             inonly.setService(ReceiverComponent.SERVICE);
244             NormalizedMessage msg = inonly.createMessage();
245             msg.setContent(src);
246             inonly.setInMessage(msg);
247             channel.send(inonly);
248         }
249         
250         public Source JavaDoc twoWay(Source JavaDoc src) throws Exception JavaDoc {
251             DeliveryChannel channel = context.getDeliveryChannel();
252             MessageExchangeFactory factory = channel.createExchangeFactory();
253             InOut inout = factory.createInOutExchange();
254             inout.setService(ReceiverComponent.SERVICE);
255             NormalizedMessage msg = inout.createMessage();
256             msg.setContent(src);
257             inout.setInMessage(msg);
258             channel.sendSync(inout);
259             Source JavaDoc outSrc = inout.getOutMessage().getContent();
260             inout.setStatus(ExchangeStatus.DONE);
261             channel.send(inout);
262             return outSrc;
263         }
264     }
265     
266 }
267
Popular Tags