KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > xfire > JbiProxyTest


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.xfire;
18
19 import javax.jbi.component.ComponentContext;
20 import javax.jbi.messaging.ExchangeStatus;
21 import javax.jbi.messaging.InOut;
22 import javax.naming.InitialContext JavaDoc;
23 import javax.xml.namespace.QName JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.apache.servicemix.client.DefaultServiceMixClient;
28 import org.apache.servicemix.jbi.container.JBIContainer;
29 import org.apache.servicemix.jbi.jaxp.StringSource;
30 import org.apache.servicemix.jsr181.Jsr181Endpoint;
31 import org.apache.servicemix.jsr181.Jsr181LifeCycle;
32 import org.apache.servicemix.jsr181.Jsr181SpringComponent;
33 import org.codehaus.xfire.XFire;
34
35 public class JbiProxyTest extends TestCase {
36
37     protected JBIContainer container;
38     
39     protected void setUp() throws Exception JavaDoc {
40         container = new JBIContainer();
41         container.setUseMBeanServer(false);
42         container.setCreateMBeanServer(false);
43         container.setMonitorInstallationDirectory(false);
44         container.setNamingContext(new InitialContext JavaDoc());
45         container.setEmbedded(true);
46         container.setFlowName("st");
47         container.init();
48     }
49     
50     protected void tearDown() throws Exception JavaDoc {
51         if (container != null) {
52             container.shutDown();
53         }
54     }
55     
56     public void testProxy() throws Exception JavaDoc {
57         container.start();
58
59         Jsr181SpringComponent component1 = new Jsr181SpringComponent();
60         Jsr181Endpoint endpoint1 = new Jsr181Endpoint();
61         endpoint1.setPojo(new EchoService());
62         component1.setEndpoints(new Jsr181Endpoint[] { endpoint1 });
63         container.activateComponent(component1, "JSR181Component-1");
64         
65         Jsr181SpringComponent component2 = new Jsr181SpringComponent();
66         Jsr181Endpoint endpoint2 = new Jsr181Endpoint();
67         endpoint2.setPojo(new ProxyPojoService());
68         endpoint2.setServiceInterface(ProxyPojo.class.getName());
69         component2.setEndpoints(new Jsr181Endpoint[] { endpoint2 });
70         container.activateComponent(component2, "JSR181Component-2");
71         
72         DefaultServiceMixClient client = new DefaultServiceMixClient(container);
73         InOut me = client.createInOutExchange();
74         me.setInterfaceName(new QName JavaDoc("http://xfire.jsr181.servicemix.apache.org", "ProxyPojoPortType"));
75         me.getInMessage().setContent(new StringSource("<echo xmlns='http://jsr181.servicemix.apache.org'><echoin0>world</echoin0></echo>"));
76         client.sendSync(me);
77         if (me.getError() != null) {
78             throw me.getError();
79         }
80         assertTrue(me.getStatus() == ExchangeStatus.ACTIVE);
81         client.done(me);
82     }
83     
84     public static interface Echo {
85         String JavaDoc echo(String JavaDoc msg);
86     }
87     
88     public static class EchoService implements Echo {
89         public String JavaDoc echo(String JavaDoc msg) {
90             return msg;
91         }
92     }
93     
94     public static interface ProxyPojo {
95         String JavaDoc echo(String JavaDoc s);
96     }
97     
98     public static class ProxyPojoService implements ProxyPojo {
99         private ComponentContext context;
100         private Echo proxy;
101
102         public ComponentContext getContext() {
103             return context;
104         }
105
106         public void setContext(ComponentContext context) throws Exception JavaDoc {
107             this.context = context;
108             if (context != null) {
109                 try {
110                     XFire xfire = Jsr181LifeCycle.createXFire(context);
111                     QName JavaDoc service = new QName JavaDoc("http://xfire.jsr181.servicemix.apache.org", "EchoService");
112                     proxy = (Echo) JbiProxy.create(xfire, context, null, service, null, Echo.class);
113                 } catch (Exception JavaDoc e) {
114                     e.printStackTrace();
115                     throw e;
116                 } catch (Error JavaDoc e) {
117                     e.printStackTrace();
118                     throw e;
119                 }
120             } else {
121                 proxy = null;
122             }
123         }
124         
125         public String JavaDoc echo(String JavaDoc s) {
126             return proxy.echo(s);
127         }
128     }
129     
130 }
131
Popular Tags