001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.object;
019:
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.cxf.BusException;
025: import org.apache.cxf.endpoint.Client;
026: import org.apache.cxf.endpoint.Server;
027: import org.apache.cxf.frontend.ClientFactoryBean;
028: import org.apache.cxf.frontend.ServerFactoryBean;
029: import org.apache.cxf.helpers.CastUtils;
030: import org.apache.cxf.message.ExchangeImpl;
031: import org.apache.cxf.message.Message;
032: import org.apache.cxf.message.MessageImpl;
033: import org.apache.cxf.service.model.BindingInfo;
034: import org.apache.cxf.service.model.BindingOperationInfo;
035: import org.apache.cxf.service.model.EndpointInfo;
036: import org.apache.cxf.service.model.ServiceInfo;
037: import org.apache.cxf.test.AbstractCXFTest;
038: import org.apache.cxf.transport.Conduit;
039: import org.apache.cxf.transport.ConduitInitiator;
040: import org.apache.cxf.transport.ConduitInitiatorManager;
041: import org.apache.cxf.transport.Destination;
042: import org.apache.cxf.transport.DestinationFactory;
043: import org.apache.cxf.transport.DestinationFactoryManager;
044: import org.apache.cxf.transport.MessageObserver;
045: import org.apache.cxf.transport.local.LocalConduit;
046: import org.apache.cxf.transport.local.LocalTransportFactory;
047: import org.junit.Test;
048:
049: public class ObjectBindingTest extends AbstractCXFTest {
050: private Message response;
051:
052: @Test
053: public void testServer() throws Exception {
054: ServerFactoryBean sfb = new ServerFactoryBean();
055: sfb.setBindingId(ObjectBindingFactory.BINDING_ID);
056: sfb.setServiceClass(EchoImpl.class);
057: sfb.setAddress("local://Echo");
058: Server server = sfb.create();
059:
060: List<Object> content = new ArrayList<Object>();
061: content.add("Hello");
062:
063: ServiceInfo serviceInfo = server.getEndpoint()
064: .getEndpointInfo().getService();
065: BindingInfo bi = serviceInfo.getBindings().iterator().next();
066: BindingOperationInfo bop = bi.getOperations().iterator().next();
067:
068: assertNotNull(bop.getOperationInfo());
069:
070: MessageImpl m = new MessageImpl();
071: m.setContent(List.class, content);
072: m.put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE);
073: m.put(ObjectBinding.BINDING, bop.getBinding().getName());
074: m.put(ObjectBinding.OPERATION, bop.getName());
075:
076: ExchangeImpl ex = new ExchangeImpl();
077: ex.setInMessage(m);
078:
079: Conduit c = getLocalConduit("local://Echo");
080: ex.setConduit(c);
081:
082: c.setMessageObserver(new MessageObserver() {
083: public void onMessage(Message message) {
084: response = message;
085: }
086: });
087: c.prepare(m);
088: c.close(m);
089:
090: Thread.sleep(1000);
091: assertNotNull(response);
092:
093: List<?> content2 = CastUtils.cast((List<?>) response
094: .getContent(List.class));
095: assertNotNull(content2);
096: assertEquals(1, content2.size());
097:
098: }
099:
100: @Test
101: public void testClient() throws Exception {
102: ClientFactoryBean cfb = new ClientFactoryBean();
103: cfb.setBindingId(ObjectBindingFactory.BINDING_ID);
104: cfb.setServiceClass(EchoImpl.class);
105: cfb.setAddress("local://Echo");
106: Client client = cfb.create();
107:
108: final List<Object> content = new ArrayList<Object>();
109: content.add("Hello");
110:
111: final Destination d = getLocalDestination("local://Echo");
112:
113: d.setMessageObserver(new MessageObserver() {
114:
115: public void onMessage(Message inMsg) {
116: // formulate the response message
117: MessageImpl outMsg = new MessageImpl();
118: outMsg.setContent(List.class, content);
119: outMsg.put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE);
120:
121: inMsg.getExchange().setInMessage(outMsg);
122: try {
123: Conduit backChannel = d.getBackChannel(inMsg, null,
124: null);
125: backChannel.prepare(outMsg);
126: backChannel.close(outMsg);
127: } catch (IOException e) {
128: // TODO Auto-generated catch block
129: e.printStackTrace();
130: }
131: }
132: });
133:
134: Object[] res = client.invoke("echo", content.toArray());
135: assertNotNull(res);
136: assertEquals(1, res.length);
137: assertEquals("Hello", res[0]);
138:
139: }
140:
141: @Test
142: public void testClientServer() throws Exception {
143: ClientFactoryBean cfb = new ClientFactoryBean();
144: cfb.setBindingId(ObjectBindingFactory.BINDING_ID);
145: cfb.setServiceClass(EchoImpl.class);
146: cfb.setAddress("local://Echo");
147: Client client = cfb.create();
148:
149: ServerFactoryBean sfb = new ServerFactoryBean();
150: sfb.setBindingId(ObjectBindingFactory.BINDING_ID);
151: sfb.setServiceClass(EchoImpl.class);
152: sfb.setAddress("local://Echo");
153: sfb.create();
154:
155: Object[] res = client.invoke("echo", new Object[] { "Hello" });
156: assertNotNull(res);
157: assertEquals(1, res.length);
158: assertEquals("Hello", res[0]);
159: }
160:
161: private Destination getLocalDestination(String string)
162: throws BusException, IOException {
163: DestinationFactoryManager dfm = getBus().getExtension(
164: DestinationFactoryManager.class);
165:
166: DestinationFactory df = dfm
167: .getDestinationFactory(LocalTransportFactory.TRANSPORT_ID);
168: EndpointInfo ei = new EndpointInfo();
169: ei.setAddress(string);
170:
171: return df.getDestination(ei);
172: }
173:
174: private Conduit getLocalConduit(String string) throws BusException,
175: IOException {
176: ConduitInitiatorManager cim = getBus().getExtension(
177: ConduitInitiatorManager.class);
178:
179: ConduitInitiator ci = cim
180: .getConduitInitiator(LocalTransportFactory.TRANSPORT_ID);
181: EndpointInfo ei = new EndpointInfo();
182: ei.setAddress(string);
183: return ci.getConduit(ei);
184: }
185: }
|