KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > Collaboration


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak, Eddy Truyen.
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * This class represents a set of contextual informations for a given
30  * thread of a running JAC system.
31  *
32  * <p>For each method call on a JAC object, a new interaction is
33  * started on the current collaboration. At any point of the program,
34  * the programmer can get the current collaboration for the current
35  * thread by using <code>get()</code>.
36  *
37  * <p>The <code>CollaborationParticipant</code> interface provides a
38  * more friendly interface to access the current collaboration. In a
39  * clean design, only the classes that implement this interface may
40  * access the collaboration.
41  *
42  * @see Collaboration#get()
43  * @see CollaborationParticipant
44  *
45  * @author Renaud Pawlak
46  * @author Eddy Truyen
47  */

48
49 public class Collaboration implements java.io.Serializable JavaDoc {
50
51     /** The attribute is global to all the sites (it is serialized and
52         transmitted with the collaboration) */

53     public static Object JavaDoc GLOBAL=new Integer JavaDoc(1);
54
55     transient static Hashtable JavaDoc attrTypes=new Hashtable JavaDoc();
56
57     /**
58      * Returns true if the attribute is global. The attribute is
59      * then transmitted to the remote sites reached by the
60      * collaboration. Note that a global attribute must always be
61      * serializable. If the attribute is not global, it
62      * always stays on the site where it has been defined.
63      *
64      * <p>By default, if the <code>setGlobal</code> method is not
65      * called, all the attributes are local.
66      *
67      * @see #setGlobal(String)
68      */

69     public static boolean isGlobal(String JavaDoc attrName) {
70         if(attrTypes.get(attrName)==GLOBAL) {
71             return true;
72         } else {
73             return false;
74         }
75     }
76
77     /**
78      * Sets an attribute to be global.
79      *
80      * <p>This method should only be called once for each attribute and
81      * before the first initialization of the attribute value in the
82      * collaboration since the globality is an inherent caracteristic
83      * of an attribute.
84      */

85     public static void setGlobal(String JavaDoc attrName) {
86         attrTypes.put(attrName,GLOBAL);
87     }
88    
89     /** Stores collaborations for all threads. */
90     transient static Hashtable JavaDoc collaborations = new Hashtable JavaDoc();
91
92     /** Static initialization of the collaborations. */
93
94     static {
95         Thread JavaDoc current = Thread.currentThread();
96         collaborations.put ( current, new Collaboration() );
97     }
98
99     /**
100      * Get the collaboration for the current thread.
101      *
102      * @return the current collaboration
103      */

104     public static Collaboration get() {
105         Thread JavaDoc current = Thread.currentThread();
106         Collaboration ret = (Collaboration) collaborations.get(current);
107         if ( ret == null ) {
108             collaborations.put ( current, ret = new Collaboration() );
109         }
110         return ret;
111     }
112
113     /**
114      * Set a new collaboration for the current thread.
115      *
116      * @param collaboration the collaboration
117      */

118     public static void set(Collaboration collaboration) {
119         Thread JavaDoc current = Thread.currentThread();
120         collaborations.put(current, collaboration);
121     }
122    
123     /** Store the attributes of the interaction. */
124     private HashMap JavaDoc attrs;
125
126     /**
127      * Creates an new collaboration.
128      *
129      * <p>The programmer should not explicitly create a new
130      * collaboration since this is automatically done by the system for
131      * any new thread.
132      *
133      * <p> At any point of the program, the programmer can get the
134      * current collaboration for the current thread by using
135      * <code>Collaboration.get()</code>.
136      *
137      * @see Collaboration#get()
138      */

139     public Collaboration() {
140         reset();
141     }
142
143     /**
144      * Create a new Collaboration and initialiaze it's attribute from a
145      * parent Collaboration.
146      *
147      * @param parent the parent collaboration
148      */

149     public Collaboration(Collaboration parent) {
150         reset();
151         if (parent!=null) {
152             Iterator JavaDoc it = parent.attributeNames().iterator();
153             while (it.hasNext()) {
154                 String JavaDoc attrName = (String JavaDoc)it.next();
155                 addAttribute(
156                     attrName,parent.getAttribute(attrName));
157             }
158         }
159     }
160
161     /**
162      * Returns a collection of all attribute names
163      */

164     public Collection JavaDoc attributeNames() {
165         HashSet JavaDoc names = new HashSet JavaDoc();
166         names.addAll(attrs.keySet());
167         return names;
168     }
169
170     public static Collection JavaDoc globalAttributeNames() {
171         HashSet JavaDoc names = new HashSet JavaDoc();
172         names.addAll(attrTypes.keySet());
173         return names;
174     }
175
176     /**
177      * Returns the map of attribute's name -> value
178      */

179     public Map JavaDoc getAttributes() {
180         return (Map JavaDoc)attrs.clone();
181     }
182
183     /**
184      * Set some attributes. Do not override current attributes.
185      * @param attributes map of name->value of attributes to set.
186      */

187     public void setAttributes(Map JavaDoc attributes) {
188         Iterator JavaDoc it = attributes.entrySet().iterator();
189         while (it.hasNext()) {
190             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
191             if (!attrs.containsKey(entry.getKey()))
192                 attrs.put(entry.getKey(),entry.getValue());
193         }
194     }
195
196     /**
197      * Reset the Collaboration.
198      * <p>Clears interactions, and all attributes.
199      */

200     public void reset() {
201         attrs = new HashMap JavaDoc();
202     }
203
204     /**
205      * Add a persistent attribute to the collaboration (can be done by
206      * any Jac object).
207      *
208      * <p>NOTE: a persitent attribute is visible for all the objects of a
209      * collaboration.
210      *
211      * @param name the name of the attribute
212      * @param att the value of the attribute
213      */

214     public Object JavaDoc addAttribute(String JavaDoc name, Object JavaDoc att) {
215         if ( name != null ) {
216             attrs.put(name, att);
217         }
218         return att;
219     }
220
221     /**
222      * Return the value of an attribute for the current collaboration.
223      *
224      * <p>This method first seeks into the persistent attributes. If
225      * none matches, it seeks into the transient attributes. If still
226      * not found, finally seeks into the transient local attributes. If
227      * all the lookups failed, return null.
228      *
229      * @param name the name of the attribute
230      * @return the value of the attribute
231      */

232     public Object JavaDoc getAttribute(String JavaDoc name) {
233         if (attrs.containsKey(name)) {
234             return attrs.get(name);
235         }
236         return null;
237     }
238
239     /**
240      * Removes the attribute from the current collaboration.
241      */

242     public void removeAttribute(String JavaDoc name) {
243         attrs.remove(name);
244     }
245
246     String JavaDoc cur_App;
247
248     public final void setCurApp(String JavaDoc app) {
249         this.cur_App = app;
250     }
251
252     public final String JavaDoc getCurApp() {
253         return cur_App;
254     }
255    
256     /**
257      * Returns a textual representation of the collaboration.
258      */

259     public String JavaDoc toString() {
260         return "Collaboration: \n" +
261             "attributes = " + attrs.toString();
262     }
263    
264    
265 }
266
Popular Tags