1 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 ; 27 import javax.xml.namespace.QName ; 28 import javax.xml.transform.Source ; 29 import javax.xml.transform.TransformerException ; 30 31 import java.io.BufferedReader ; 32 import java.io.InputStreamReader ; 33 import java.util.EventListener ; 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 ; 47 import org.w3c.dom.Node ; 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 56 protected JBIContainer container; 57 protected SourceTransformer transformer = new SourceTransformer(); 58 private boolean useJmx; 59 protected EventListener [] listeners; 60 61 62 public static void main(String [] args) { 63 Jsr181ComplexPojoTest test = new Jsr181ComplexPojoTest(); 64 test.useJmx = true; 65 test.listeners = new EventListener [] { new DotViewEndpointListener() }; 66 67 try { 68 test.setUp(); 69 test.testComplexOneWay(); 70 71 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 reader = new BufferedReader (new InputStreamReader (System.in)); 75 reader.readLine(); 76 77 78 test.tearDown(); 79 } 80 catch (Exception e) { 81 System.err.println("Caught: " + e); 82 e.printStackTrace(); 83 } 84 } 85 protected void setUp() throws Exception { 86 container = new JBIContainer(); 87 container.setUseMBeanServer(useJmx); 88 container.setCreateMBeanServer(useJmx); 89 container.setMonitorInstallationDirectory(false); 90 container.setNamingContext(new InitialContext ()); 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 { 100 if (container != null) { 101 container.shutDown(); 102 } 103 } 104 105 public void testComplexOneWay() throws Exception { 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 ("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 Thread.sleep(100); 126 } 127 128 public void testComplexTwoWay() throws Exception { 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 ("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 Thread.sleep(100); 154 } 155 156 public void testFault() throws Exception { 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 ("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 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 ("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 Thread.sleep(100); 202 } 203 204 protected String textValueOfXPath(Node node, String xpath) throws TransformerException { 205 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 206 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, xpath); 207 Node root = iterator.nextNode(); 208 if (root instanceof Element ) { 209 Element element = (Element ) root; 210 if (element == null) { 211 return ""; 212 } 213 String 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 src) throws Exception ; 225 public Source twoWay(Source src) throws Exception ; 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 src) throws Exception { 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 twoWay(Source src) throws Exception { 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 outSrc = inout.getOutMessage().getContent(); 260 inout.setStatus(ExchangeStatus.DONE); 261 channel.send(inout); 262 return outSrc; 263 } 264 } 265 266 } 267 | Popular Tags |