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.context;
18  
19  import java.util.HashMap;
20  
21  import org.apache.axis2.addressing.EndpointReference;
22  import org.apache.kandula.Status;
23  import org.apache.kandula.utility.EndpointReferenceFactory;
24  
25  /***
26   * @author <a href="mailto:thilina@opensource.lk"> Thilina Gunarathne </a>
27   */
28  public abstract class AbstractContext {
29  	
30  	public static String ACTIVATION_EPR = "activationEPR";
31  	
32  	public static String COORDINATION_EPR = "coordinationEPR";
33  	
34  	public static String REQUESTER_ID = "requesterID";
35  	
36  	protected String activityID;
37  	
38  	protected CoordinationContext coordinationContext = null;
39  	
40  	private boolean locked = false;
41  	
42  	private final HashMap propertyBag;
43  	
44  	private int status = Status.CoordinatorStatus.STATUS_NONE;
45  	
46  	protected AbstractContext() {
47  		propertyBag = new HashMap();
48  	}
49  	
50  	public AbstractContext(String coordinationType) {
51  		propertyBag = new HashMap();
52  		activityID = "urn:"
53  			+ EndpointReferenceFactory.getRandomStringOf18Characters();
54  		EndpointReference registrationEpr = EndpointReferenceFactory
55  		.getInstance().getRegistrationEndpoint(activityID);
56  		coordinationContext = CoordinationContext.Factory.newContext(
57  				activityID, coordinationType, registrationEpr);
58  	}
59  	
60  	public final CoordinationContext getCoordinationContext() {
61  		return coordinationContext;
62  	}
63  	
64  	public abstract String getCoordinationType();
65  	
66  	public abstract String getRegistrationProtocol();
67  	
68  	public final Object getProperty(Object key) {
69  		return propertyBag.get(key);
70  	}
71  	
72  	public final int getStatus() {
73  		return status;
74  	}
75  	
76  	public final synchronized void lock() {
77  		if (locked) {
78  			while (locked) {
79  				try {
80  					wait();
81  				} catch (InterruptedException ex) {
82  					// ignore
83  				}
84  				if (status == Status.CoordinatorStatus.STATUS_NONE)
85  					throw new IllegalStateException();
86  			}
87  		}
88  		
89  		locked = true;
90  	}
91  	
92  	public final void setCoordinationContext(CoordinationContext context) {
93  		this.coordinationContext = context;
94  	}
95  	
96  	public final void setProperty(Object key, Object value) {
97  		propertyBag.put(key, value);
98  		
99  	}
100 	
101 	// we can use a publisher-subscriber in the future to notify listeners abt
102 	// state changes.
103 	public final void setStatus(int value) {
104 		status = value;
105 	}
106 	
107 	public final synchronized void unlock() {
108 		if (!locked)
109 			throw new IllegalStateException();
110 		locked = false;
111 		notify();
112 	}
113 	
114 }