1 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 ; 23 import javax.xml.namespace.QName ; 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 { 40 container = new JBIContainer(); 41 container.setUseMBeanServer(false); 42 container.setCreateMBeanServer(false); 43 container.setMonitorInstallationDirectory(false); 44 container.setNamingContext(new InitialContext ()); 45 container.setEmbedded(true); 46 container.setFlowName("st"); 47 container.init(); 48 } 49 50 protected void tearDown() throws Exception { 51 if (container != null) { 52 container.shutDown(); 53 } 54 } 55 56 public void testProxy() throws Exception { 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 ("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 echo(String msg); 86 } 87 88 public static class EchoService implements Echo { 89 public String echo(String msg) { 90 return msg; 91 } 92 } 93 94 public static interface ProxyPojo { 95 String echo(String 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 { 107 this.context = context; 108 if (context != null) { 109 try { 110 XFire xfire = Jsr181LifeCycle.createXFire(context); 111 QName service = new QName ("http://xfire.jsr181.servicemix.apache.org", "EchoService"); 112 proxy = (Echo) JbiProxy.create(xfire, context, null, service, null, Echo.class); 113 } catch (Exception e) { 114 e.printStackTrace(); 115 throw e; 116 } catch (Error e) { 117 e.printStackTrace(); 118 throw e; 119 } 120 } else { 121 proxy = null; 122 } 123 } 124 125 public String echo(String s) { 126 return proxy.echo(s); 127 } 128 } 129 130 } 131 | Popular Tags |