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.coordinator;
18  
19  import org.apache.axis2.addressing.EndpointReference;
20  import org.apache.kandula.context.AbstractContext;
21  import org.apache.kandula.context.ContextFactory;
22  import org.apache.kandula.context.CoordinationContext;
23  import org.apache.kandula.faults.AbstractKandulaException;
24  import org.apache.kandula.storage.StorageFactory;
25  import org.apache.kandula.storage.Store;
26  
27  /***
28   * @author <a href="mailto:thilina@opensource.lk"> Thilina Gunarathne </a>
29   */
30  
31  public class Coordinator {
32  	private Store store;
33  
34  	public Coordinator() {
35  		StorageFactory storageFactory = StorageFactory.getInstance();
36  		store = storageFactory.getStore();
37  	}
38  
39  	/***
40  	 * @param coorContext
41  	 * @return the interposed Coordination Context created <p/>Participants
42  	 *         decided to use this Coordinator as a interposed
43  	 *         sub-coordinator.The newly created CoordinationContext will
44  	 *         contain the same ActivityIdentifier & Protocol type. Registration
45  	 *         EPR of the earlier CoordinationContext will be replaced by the
46  	 *         RegistrationEPR of this Coordinator.
47  	 */
48  	public AbstractContext createCoordinationContext(
49  			CoordinationContext coorContext) throws AbstractKandulaException {
50  		ContextFactory factory = ContextFactory.getInstance();
51  		AbstractContext context = factory.createActivity(coorContext);
52  		store.put(context.getCoordinationContext().getActivityID(), context);
53  		return context;
54  	}
55  
56  	/***
57  	 * @param coordinationType
58  	 * @return the Coordination Context created <p/>Initiators can use this to
59  	 *         Create new Distributed transactions.This will take in the
60  	 *         Coordination Type and will create an instance of the reapective
61  	 *         ActivityContext. The Coordination Context created by this can be
62  	 *         used to convey the information about the transaction between
63  	 *         initiator,Participants and coordinator
64  	 * @throws AbstractKandulaException
65  	 */
66  	public AbstractContext createCoordinationContext(String coordinationType,
67  			long expires) throws AbstractKandulaException {
68  		ContextFactory factory = ContextFactory.getInstance();
69  		AbstractContext context = factory.createActivity(coordinationType);
70  		context.getCoordinationContext().setExpires(expires);
71  		store.put(context.getCoordinationContext().getActivityID(), context);
72  		return context;
73  	}
74  
75  	private AbstractContext getCoordinationContext(String id) {
76  		return (AbstractContext) store.get(id);
77  	}
78  
79  	/***
80  	 * @param protocol
81  	 * @param participantEPR
82  	 * @param id
83  	 * @return Should return the particular Coordiators End Point Reference <p/>
84  	 *         This method provides the functional logic for participants to
85  	 *         register for a particular transaction activity which was created
86  	 *         by a initiator. Registration request will be forwarded to
87  	 *         repective protocol coordinators.
88  	 * @throws AbstractKandulaException
89  	 */
90  	public EndpointReference registerParticipant(String id, String protocol,
91  			EndpointReference participantEPR) throws AbstractKandulaException {
92  
93  		AbstractContext context = getCoordinationContext(id);
94  		if (context == null) {
95  			throw new IllegalStateException(
96  					"No Activity Found for this Activity ID");
97  		}
98  
99  		Registerable registerableCoordinator = Registerable.Factory
100 				.newRegisterable(context.getCoordinationType());
101 		return registerableCoordinator.register(context, protocol,
102 				participantEPR);
103 	}
104 }