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.wsat;
18  
19  import java.io.IOException;
20  
21  import org.apache.axiom.om.OMAbstractFactory;
22  import org.apache.axiom.om.OMElement;
23  import org.apache.axiom.om.OMNamespace;
24  import org.apache.axiom.soap.SOAPEnvelope;
25  import org.apache.axiom.soap.SOAPFactory;
26  import org.apache.axis2.AxisFault;
27  import org.apache.axis2.addressing.EndpointReference;
28  import org.apache.axis2.client.OperationClient;
29  import org.apache.axis2.client.Options;
30  import org.apache.axis2.context.ConfigurationContext;
31  import org.apache.axis2.context.ConfigurationContextFactory;
32  import org.apache.axis2.context.MessageContext;
33  import org.apache.axis2.context.ServiceContext;
34  import org.apache.axis2.context.ServiceGroupContext;
35  import org.apache.axis2.deployment.DeploymentException;
36  import org.apache.axis2.description.AxisOperation;
37  import org.apache.axis2.description.AxisService;
38  import org.apache.axis2.description.AxisServiceGroup;
39  import org.apache.kandula.Constants;
40  import org.apache.kandula.faults.AbstractKandulaException;
41  import org.apache.kandula.faults.KandulaGeneralException;
42  
43  /***
44   * @author <a href="mailto:thilina@opensource.lk"> Thilina Gunarathne </a>
45   */
46  public abstract class AbstractATNotifierStub {
47  	protected AxisOperation[] operations;
48  
49  	protected AxisService service;
50  
51  	protected ConfigurationContext configurationContext;
52  
53  	protected ServiceContext serviceContext;
54  
55  	protected EndpointReference toEPR;
56  
57  	public AbstractATNotifierStub(String axis2Home, String axis2Xml,
58  			AxisService service) throws AbstractKandulaException {
59  		// creating the configuration
60  		this.service = service;
61  		try {
62  			configurationContext = ConfigurationContextFactory
63  					.createConfigurationContextFromFileSystem(axis2Home,
64  							axis2Xml);
65  			configurationContext.getAxisConfiguration().addService(service);
66  		} catch (DeploymentException e) {
67  			throw new KandulaGeneralException(e);
68  		} catch (AxisFault e1) {
69  			throw new KandulaGeneralException(e1);
70  		}
71  		ServiceGroupContext sgc = new ServiceGroupContext(
72  				this.configurationContext, (AxisServiceGroup) this.service
73  						.getParent());
74  		this.serviceContext = new ServiceContext(service, sgc);
75  	}
76  
77  	/***
78  	 * Provides common functionality for stubs to send notification messages
79  	 * 
80  	 * @param localName :
81  	 *            name of the notification message
82  	 * @param action :
83  	 *            ws-a action value for the notification message
84  	 * @param opIndex :
85  	 *            operation index in the operations array
86  	 * @param replyToEPR :
87  	 *            notification messages except termination messages should send
88  	 *            this
89  	 * @throws IOException
90  	 */
91  	protected void notify(String localName, String action, int opIndex,
92  			EndpointReference replyToEPR) throws AbstractKandulaException {
93  		MessageContext messageContext;
94  		try {
95  			Options options = new Options();
96  			messageContext = new MessageContext();
97  			final OperationClient client = operations[opIndex].createClient(
98  					serviceContext, options);
99  
100 			SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
101 			SOAPEnvelope env = factory.getDefaultEnvelope();
102 
103 			OMNamespace wsAT = factory.createOMNamespace(Constants.WS_AT,
104 					"wsat");
105 			OMElement request = factory.createOMElement(localName, wsAT);
106 			env.getBody().addChild(request);
107 			messageContext.setEnvelope(env);
108 
109 			options.setTo(this.toEPR);
110 			if (replyToEPR != null) {
111 				options.setReplyTo(replyToEPR);
112 			}
113 			options.setAction(action);
114 			// options.setTranportOut(org.apache.axis2.Constants.TRANSPORT_HTTP);
115 			// System.out.println(operations[opIndex]);
116 			client.addMessageContext(messageContext);
117 			/*
118 			 * hacking till we get fire and forget corretly in Axis2
119 			 */
120 			Thread thread = new Thread(new Runnable() {
121 				public void run() {
122 					try {
123 						client.execute(false);
124 					} catch (AxisFault e) {
125 						// TODO Auto-generated catch block
126 						e.printStackTrace();
127 					}
128 				}
129 			});
130 			thread.start();
131 
132 		} catch (AxisFault e) {
133 			throw new KandulaGeneralException(e);
134 		}
135 	}
136 }