KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > wsif > WSIFBinding


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.components.wsif;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.jbi.JBIException;
25 import javax.jbi.messaging.Fault;
26 import javax.jbi.messaging.MessageExchange;
27 import javax.jbi.messaging.MessagingException;
28 import javax.jbi.messaging.NormalizedMessage;
29 import javax.wsdl.Binding;
30 import javax.wsdl.Definition;
31 import javax.wsdl.WSDLException;
32
33 import org.apache.servicemix.components.util.TransformComponentSupport;
34 import org.apache.wsif.WSIFException;
35 import org.apache.wsif.WSIFMessage;
36 import org.apache.wsif.WSIFOperation;
37 import org.apache.wsif.WSIFService;
38 import org.apache.wsif.WSIFServiceFactory;
39 import org.apache.wsif.util.WSIFUtils;
40 import org.springframework.core.io.Resource;
41
42 /**
43  * Consumers JBI messages and sends them as a synchronous request/response into WSIF
44  * then forwards the response
45  *
46  * @version $Revision: 429277 $
47  */

48 public class WSIFBinding extends TransformComponentSupport {
49
50     private WSIFMarshaler marshaler = new WSIFMarshaler();
51     private WSIFService serviceHelper;
52     private Definition definition;
53     private Resource definitionResource;
54     private WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
55     private WSIFOperationMap operationMap;
56
57     protected void init() throws JBIException {
58         try {
59             if (definition == null) {
60                 if (definitionResource == null) {
61                     throw new JBIException("You must specify a definition or definitionResource property");
62                 }
63                 String JavaDoc uri = definitionResource.getURL().toString();
64                 InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(definitionResource.getInputStream());
65                 definition = WSIFUtils.readWSDL(uri, reader);
66             }
67
68             if (serviceHelper == null) {
69                 serviceHelper = factory.getService(definition);
70             }
71
72             for (Iterator JavaDoc iter = serviceHelper.getAvailablePortNames(); iter.hasNext();) {
73                 System.out.println("Available port name: " + iter.next());
74             }
75
76             operationMap = new WSIFOperationMap(serviceHelper);
77
78             Map JavaDoc bindings = definition.getBindings();
79             for (Iterator JavaDoc iter = bindings.entrySet().iterator(); iter.hasNext();) {
80                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
81                 Binding binding = (Binding) entry.getValue();
82                 operationMap.addBinding(binding);
83             }
84         }
85         catch (IOException JavaDoc e) {
86             throw new JBIException(e);
87         }
88         catch (WSDLException e) {
89             throw new JBIException(e);
90         }
91     }
92
93     /**
94      * @deprecated use getMarshaler instead
95      */

96     public WSIFMarshaler getMarshaller() {
97         return marshaler;
98     }
99
100     /**
101      * @deprecated use setMarshaler instead
102      */

103     public void setMarshaller(WSIFMarshaler marshaler) {
104         this.marshaler = marshaler;
105     }
106
107     public WSIFMarshaler getMarshaler() {
108         return marshaler;
109     }
110
111     public void setMarshaler(WSIFMarshaler marshaler) {
112         this.marshaler = marshaler;
113     }
114
115     public Definition getDefinition() {
116         return definition;
117     }
118
119     public void setDefinition(Definition definition) {
120         this.definition = definition;
121     }
122
123     public Resource getDefinitionResource() {
124         return definitionResource;
125     }
126
127     public void setDefinitionResource(Resource definitionResource) {
128         this.definitionResource = definitionResource;
129     }
130
131     public WSIFServiceFactory getFactory() {
132         return factory;
133     }
134
135     public void setFactory(WSIFServiceFactory factory) {
136         this.factory = factory;
137     }
138
139     public WSIFService getServiceHelper() {
140         return serviceHelper;
141     }
142
143     public void setServiceHelper(WSIFService serviceHelper) {
144         this.serviceHelper = serviceHelper;
145     }
146
147     // Implementation methods
148
//-------------------------------------------------------------------------
149
protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
150         try {
151             WSIFOperationInfo operationInfo = operationMap.getOperationForExchange(exchange);
152
153             WSIFOperation operation = operationInfo.createWsifOperation();
154             WSIFMessage inMessage = operation.createInputMessage();
155             Object JavaDoc body = getBody(in);
156             marshaler.fromNMS(operationInfo, inMessage, in, body);
157
158             WSIFMessage outMessage = operation.createInputMessage();
159             WSIFMessage faultMessage = operation.createInputMessage();
160             boolean answer = operation.executeRequestResponseOperation(inMessage, outMessage, faultMessage);
161             if (answer) {
162                 marshaler.toNMS(exchange, out, operationInfo, outMessage);
163             }
164             else {
165                 Fault fault = exchange.createFault();
166                 marshaler.toNMS(exchange, fault, operationInfo, outMessage);
167                 exchange.setFault(fault);
168             }
169             return true;
170         }
171         catch (WSIFException e) {
172             throw new MessagingException(e);
173         }
174     }
175
176 }
177
Popular Tags