View Javadoc

1   /*
2    * Copyright  2004 The Apache Software Foundation.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.apache.kandula.wscoor;
18  
19  import java.io.File;
20  import java.io.IOException;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.apache.axiom.om.OMAbstractFactory;
25  import org.apache.axiom.om.OMElement;
26  import org.apache.axiom.om.OMNamespace;
27  import org.apache.axiom.soap.SOAPEnvelope;
28  import org.apache.axiom.soap.SOAPFactory;
29  import org.apache.axis2.AxisFault;
30  import org.apache.axis2.addressing.AddressingConstants;
31  import org.apache.axis2.addressing.EndpointReference;
32  import org.apache.axis2.client.OperationClient;
33  import org.apache.axis2.client.Options;
34  import org.apache.axis2.context.ConfigurationContext;
35  import org.apache.axis2.context.ConfigurationContextFactory;
36  import org.apache.axis2.context.MessageContext;
37  import org.apache.axis2.context.ServiceContext;
38  import org.apache.axis2.context.ServiceGroupContext;
39  import org.apache.axis2.deployment.DeploymentException;
40  import org.apache.axis2.description.AxisOperation;
41  import org.apache.axis2.description.AxisService;
42  import org.apache.axis2.description.AxisServiceGroup;
43  import org.apache.axis2.description.InOnlyAxisOperation;
44  import org.apache.axis2.description.OutInAxisOperation;
45  import org.apache.axis2.description.OutOnlyAxisOperation;
46  import org.apache.axis2.description.Parameter;
47  import org.apache.axis2.receivers.AbstractMessageReceiver;
48  import org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver;
49  import org.apache.axis2.wsdl.WSDLConstants;
50  import org.apache.kandula.Constants;
51  import org.apache.kandula.context.AbstractContext;
52  import org.apache.kandula.context.impl.ATActivityContext;
53  import org.apache.kandula.faults.AbstractKandulaException;
54  import org.apache.kandula.faults.KandulaGeneralException;
55  import org.apache.kandula.storage.StorageFactory;
56  import org.apache.kandula.utility.EndpointReferenceFactory;
57  import org.apache.kandula.utility.KandulaListener;
58  
59  /***
60   * @author <a href="mailto:thilina@opensource.lk"> Thilina Gunarathne </a>
61   */
62  public class RegistrationCoordinatorPortTypeRawXMLStub extends
63  		org.apache.axis2.client.Stub {
64  	public static final String AXIS2_HOME = ".";
65  
66  	protected static org.apache.axis2.description.AxisOperation[] _operations;
67  
68  	protected AxisService service;
69  
70  	protected ConfigurationContext configurationContext;
71  
72  	private ServiceContext serviceContext;
73  
74  	private EndpointReference toEPR;
75  
76  	private AxisOperation operation;
77  
78  	/***
79  	 * Constructor
80  	 */
81  	public RegistrationCoordinatorPortTypeRawXMLStub(String axis2Home,
82  			String axis2Xml, EndpointReference targetEndpoint)
83  			throws AbstractKandulaException {
84  		this.toEPR = targetEndpoint;
85  		service = new AxisService("RegistrationCoordinatorPortType");
86  		try {
87  			configurationContext = ConfigurationContextFactory
88  					.createConfigurationContextFromFileSystem(axis2Home,
89  							axis2Xml);
90  			configurationContext.getAxisConfiguration().addService(service);
91  		} catch (DeploymentException e) {
92  			throw new KandulaGeneralException(e);
93  		} catch (AxisFault e1) {
94  			throw new KandulaGeneralException(e1);
95  		}
96  		ServiceGroupContext sgc = new ServiceGroupContext(
97  				this.configurationContext, (AxisServiceGroup) this.service
98  						.getParent());
99  		this.serviceContext = new ServiceContext(service, sgc);
100 
101 	}
102 
103 	public void registerOperation(AbstractContext context,
104 			EndpointReference epr, boolean async) throws IOException, KandulaGeneralException {
105 		EndpointReference replyToEpr;
106 		MessageContext messageContext = new MessageContext();
107 		Options options = new Options();
108 		messageContext.setProperty(AddressingConstants.WS_ADDRESSING_VERSION,
109 				AddressingConstants.Submission.WSA_NAMESPACE);
110 		SOAPEnvelope env = createSOAPEnvelope(context
111 				.getRegistrationProtocol(), epr);
112 		messageContext.setEnvelope(env);
113 		options.setTo(this.toEPR);
114 		options.setAction(Constants.WS_COOR_REGISTER);
115 
116 		if (async) {
117 			operation = new OutOnlyAxisOperation();
118 			operation.setName(new javax.xml.namespace.QName(
119 					"http://schemas.xmlsoap.org/ws/2003/09/wscoor",
120 					"RegisterOperation"));
121 			service.addOperation(operation);
122 			replyToEpr = setupListener();
123 			EndpointReferenceFactory.addReferenceProperty(replyToEpr,
124 					Constants.REQUESTER_ID_PARAMETER, (String) context
125 							.getProperty(AbstractContext.REQUESTER_ID));
126 			options.setReplyTo(replyToEpr);
127 			OperationClient client = operation.createClient(serviceContext,
128 					options);
129 			client.addMessageContext(messageContext);
130 			client.execute(false);
131 		} else {
132 			operation = new OutInAxisOperation();
133 			operation.setName(new javax.xml.namespace.QName(
134 					"http://schemas.xmlsoap.org/ws/2003/09/wscoor",
135 					"RegisterOperation"));
136 			service.addOperation(operation);
137 			OperationClient client = operation.createClient(serviceContext,
138 					options);
139 			client.addMessageContext(messageContext);
140 			client.execute(true);
141 			MessageContext msgContext = client
142 					.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
143 			OMElement response = msgContext.getEnvelope().getBody()
144 					.getFirstChildWithName(new QName(Constants.WS_COOR,"RegisterResponse"));
145 			OMElement serviceElement = response.getFirstChildWithName(new QName(Constants.WS_COOR,"CoordinatorProtocolService"));
146 			if (serviceElement!=null) {
147 				EndpointReference coordinatorService = EndpointReferenceFactory
148 						.endpointFromOM(serviceElement.getFirstElement());
149 				context.setProperty(ATActivityContext.COORDINATION_EPR,
150 						coordinatorService);
151 			}
152 			else 
153 			{
154 				throw new KandulaGeneralException("CoordinatorProtocolService epr was not found in the RegistrationResponse Message");
155 			}
156 		}
157 
158 	}
159 
160 	private SOAPEnvelope createSOAPEnvelope(String protocolType,
161 			EndpointReference epr) {
162 
163 		SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
164 		SOAPEnvelope env = factory.getDefaultEnvelope();
165 		OMNamespace wsCoor = factory.createOMNamespace(Constants.WS_COOR,
166 				"wscoor");
167 		OMElement request = factory.createOMElement("Register", wsCoor);
168 		OMElement protocolTypeElement = factory.createOMElement(
169 				"ProtocolIdentifier", wsCoor);
170 		protocolTypeElement.setText(protocolType);
171 		request.addChild(protocolTypeElement);
172 
173 		OMElement protocolService = factory.createOMElement(
174 				"ParticipantProtocolService", wsCoor);
175 		EndpointReferenceFactory.endpointToOM(epr, protocolService, factory);
176 		request.addChild(protocolService);
177 		env.getBody().addChild(request);
178 		return env;
179 	}
180 
181 	private EndpointReference setupListener() throws IOException {
182 		String serviceName = "RegistrationRequesterPortType";
183 		QName operationName = new QName(Constants.WS_COOR,
184 				"registerResponseOperation");
185 		AxisOperation responseOperationDesc;
186 		String className = RegistrationRequesterPortTypeRawXMLSkeleton.class
187 				.getName();
188 		String mapping = Constants.WS_COOR_REGISTER_RESPONSE;
189 
190 		KandulaListener listener = KandulaListener.getInstance();
191 		AxisService service = new AxisService(serviceName);
192 		service.addParameter(new Parameter(
193 				AbstractMessageReceiver.SERVICE_CLASS, className));
194 		service.setFileName((new File(className)).toURL());
195 
196 		responseOperationDesc = new InOnlyAxisOperation();
197 		responseOperationDesc.setName(operationName);
198 		responseOperationDesc
199 				.setMessageReceiver(new RawXMLINOnlyMessageReceiver());
200 
201 		// Adding the WSA Action mapping to the operation
202 		service.mapActionToOperation(mapping, responseOperationDesc);
203 		service.addOperation(responseOperationDesc);
204 		listener.addService(service);
205 		listener.start();
206 		return new EndpointReference(listener.getHost() + serviceName);
207 	}
208 }