View Javadoc

1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   *  
16   */
17  package org.apache.kandula.utility;
18  
19  import java.io.IOException;
20  import java.net.InetAddress;
21  import java.net.UnknownHostException;
22  import java.security.MessageDigest;
23  import java.security.NoSuchAlgorithmException;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.Random;
28  
29  import javax.xml.namespace.QName;
30  
31  import org.apache.axiom.om.OMElement;
32  import org.apache.axiom.om.OMNamespace;
33  import org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory;
34  import org.apache.axiom.soap.SOAPFactory;
35  import org.apache.axis2.addressing.AddressingConstants;
36  import org.apache.axis2.addressing.EndpointReference;
37  import org.apache.axis2.addressing.AddressingConstants.Final;
38  import org.apache.kandula.Constants;
39  import org.apache.kandula.wsat.completion.CompletionInitiatorServiceListener;
40  
41  /***
42   * @author <a href="mailto:thilina@apache.org"> Thilina Gunarathne </a>
43   */
44  
45  public class EndpointReferenceFactory {
46  
47  	private static EndpointReferenceFactory instance = null;
48  
49  	private KandulaConfiguration configuration;
50  
51  	private EndpointReferenceFactory() {
52  
53  		configuration = KandulaConfiguration.getInstance();
54  
55  	}
56  
57  	public static EndpointReferenceFactory getInstance() {
58  		if (instance == null)
59  			instance = new EndpointReferenceFactory();
60  		return instance;
61  	}
62  
63  	public EndpointReference getRegistrationEndpoint(String id) {
64  
65  		EndpointReference epr = new EndpointReference(configuration
66  				.getLocationForEPR()
67  				+ "/axis2/services/RegistrationCoordinator");
68  		EndpointReferenceFactory.addReferenceProperty(epr,
69  				Constants.TRANSACTION_ID_PARAMETER, id);
70  		return epr;
71  	}
72  
73  	/***
74  	 * Sets up a listener in the transaction initiator to recieve notify
75  	 * messages mentioning the outcome of the transaction These messages include
76  	 * "aborted" and "commited".
77  	 * 
78  	 * @throws IOException
79  	 */
80  	public EndpointReference getCompletionInitiatorEndpoint(String id)
81  			throws IOException {
82  		CompletionInitiatorServiceListener serviceListener = CompletionInitiatorServiceListener
83  				.getInstance();
84  		EndpointReference epr = serviceListener.getEpr();
85  		EndpointReferenceFactory.addReferenceProperty(epr,
86  				Constants.REQUESTER_ID_PARAMETER, id);
87  		return epr;
88  	}
89  
90  	public EndpointReference getCompletionEndpoint(String id) {
91  
92  		EndpointReference epr = new EndpointReference(configuration
93  				.getLocationForEPR()
94  				+ "/axis2/services/CompletionCoordinator");
95  		EndpointReferenceFactory.addReferenceProperty(epr,
96  				Constants.TRANSACTION_ID_PARAMETER, id);
97  		return epr;
98  	}
99  
100 	public EndpointReference get2PCCoordinatorEndpoint(String activityId,
101 			String enlistmentId) {
102 		// Activity ID to find Activity Context , EnlistmentID to find
103 		// participant in activity
104 		EndpointReference epr = new EndpointReference(configuration
105 				.getLocationForEPR()
106 				+ "/axis2/services/AtomicTransactionCoordinator");
107 		EndpointReferenceFactory.addReferenceProperty(epr,
108 				Constants.TRANSACTION_ID_PARAMETER, activityId);
109 		EndpointReferenceFactory.addReferenceProperty(epr,
110 				Constants.ENLISTMENT_ID_PARAMETER, enlistmentId);
111 		return epr;
112 	}
113 
114 	public EndpointReference get2PCParticipantEndpoint(String id) {
115 
116 		EndpointReference epr = new EndpointReference(configuration
117 				.getLocationForEPR()
118 				+ "/axis2/services/AtomicTransactionParticipant");
119 		EndpointReferenceFactory.addReferenceProperty(epr,
120 				Constants.REQUESTER_ID_PARAMETER, id);
121 		return epr;
122 	}
123 
124 	/***
125 	 * MD5 a random string with localhost/date etc will return 128 bits
126 	 * construct a string of 18 characters from those bits.
127 	 * 
128 	 * @return string
129 	 */
130 	public static String getRandomStringOf18Characters() {
131 		Random myRand = new Random();
132 		long rand = myRand.nextLong();
133 		String sid;
134 		try {
135 			sid = InetAddress.getLocalHost().toString();
136 		} catch (UnknownHostException e) {
137 			sid = Thread.currentThread().getName();
138 		}
139 		long time = System.currentTimeMillis();
140 		StringBuffer sb = new StringBuffer();
141 		sb.append(sid);
142 		sb.append(":");
143 		sb.append(Long.toString(time));
144 		sb.append(":");
145 		sb.append(Long.toString(rand));
146 		MessageDigest md5 = null;
147 		try {
148 			md5 = MessageDigest.getInstance("MD5");
149 		} catch (NoSuchAlgorithmException e) {
150 			// System.out.println("Error: " + e);
151 			// todo heve to be properly handle
152 		}
153 		md5.update(sb.toString().getBytes());
154 		byte[] array = md5.digest();
155 		StringBuffer sb2 = new StringBuffer();
156 		for (int j = 0; j < array.length; ++j) {
157 			int b = array[j] & 0xFF;
158 			sb2.append(Integer.toHexString(b));
159 		}
160 		int begin = myRand.nextInt();
161 		if (begin < 0)
162 			begin = begin * -1;
163 		begin = begin % 8;
164 		return new String(sb2.toString().substring(begin, begin + 18))
165 				.toUpperCase();
166 	}
167 
168 	public static void addReferenceProperty(EndpointReference epr, QName key,
169 			String Value) {
170 		// We'll have to live with reference parameters for the moment
171 		// Since Axis2 Addressing does not support ref properties well
172 		HashMap refProperties;
173 		if ((refProperties = (HashMap) epr.getAllReferenceParameters()) == null) {
174 			refProperties = new HashMap();
175 		}
176 		OMLinkedListImplFactory factory = new OMLinkedListImplFactory();
177 		OMElement omElement = factory.createOMElement(key, null);
178 		omElement.setText(Value);
179 		refProperties.put(key, omElement);
180 		epr.setReferenceParameters(refProperties);
181 	}
182 
183 	public static EndpointReference endpointFromOM(OMElement eprElement) {
184 		EndpointReference epr;
185 		epr = new EndpointReference(eprElement.getFirstChildWithName(
186 				new QName("Address")).getText());
187 		HashMap referenceProperties = new HashMap();
188 		OMElement referencePropertiesElement = eprElement
189 				.getFirstChildWithName(new QName("ReferenceParameters"));
190 		Iterator propertyIter = referencePropertiesElement.getChildElements();
191 		while (propertyIter.hasNext()) {
192 			OMElement element = (OMElement) propertyIter.next();
193 
194 			// TODO do we need to detach the OMElement
195 			referenceProperties.put(element.getQName(), element
196 					.cloneOMElement());
197 		}
198 		// will have to live with Ref parameters for some time :: Till axis2
199 		// @ing gets stable
200 		epr.setReferenceParameters(referenceProperties);
201 		return epr;
202 	}
203 
204 	public static void endpointToOM(EndpointReference epr, OMElement parentEPR,
205 			SOAPFactory factory) {
206 		OMNamespace wsAddressing = factory.createOMNamespace(
207 				AddressingConstants.Submission.WSA_NAMESPACE,
208 				AddressingConstants.WSA_DEFAULT_PREFIX);
209 		OMElement addressElement = factory.createOMElement("Address",
210 				wsAddressing);
211 		addressElement.setText(epr.getAddress());
212 		parentEPR.addChild(addressElement);
213 		Map referenceValues = epr.getAllReferenceParameters();
214 		if (referenceValues != null) {
215 			OMElement refPropertyElement = factory.createOMElement(
216 					"ReferenceParameters", wsAddressing);
217 			parentEPR.addChild(refPropertyElement);
218 			Iterator iterator = referenceValues.keySet().iterator();
219 			while (iterator.hasNext()) {
220 				QName key = (QName) iterator.next();
221 				OMElement omElement = (OMElement) referenceValues.get(key);
222 				refPropertyElement.addChild(omElement);
223 				if (Final.WSA_NAMESPACE.equals(wsAddressing)) {
224 					omElement.addAttribute(
225 							Final.WSA_IS_REFERENCE_PARAMETER_ATTRIBUTE,
226 							Final.WSA_TYPE_ATTRIBUTE_VALUE, wsAddressing);
227 				}
228 			}
229 		}
230 	}
231 }