KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > data > AbstractDataObject


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ic2d.data;
32
33 import org.objectweb.proactive.core.UniqueID;
34 import org.objectweb.proactive.ic2d.event.MessageMonitoringListener;
35
36 /**
37  * Holder class for the host data representation
38  */

39 public abstract class AbstractDataObject implements MessageMonitoringController {
40
41   protected DataObjectController controller;
42   
43   protected AbstractDataObject parent;
44   
45   private java.util.HashMap JavaDoc childs;
46   
47   protected String JavaDoc abstractDataObjectName;
48   protected boolean isDestroyed;
49
50   protected boolean monitoringRequestReceiver;
51   protected boolean monitoringRequestSender;
52   protected boolean monitoringReplyReceiver;
53   protected boolean monitoringReplySender;
54   protected boolean viewingInEventList;
55   protected MessageMonitoringListener messageMonitoringListener;
56
57   //
58
// -- CONSTRUCTORS -----------------------------------------------
59
//
60

61   protected AbstractDataObject(AbstractDataObject parent, String JavaDoc abstractDataObjectName) {
62     this();
63     this.parent = parent;
64     this.abstractDataObjectName = abstractDataObjectName;
65     if (abstractDataObjectName == null)
66       this.abstractDataObjectName = this.getClass().getName();
67     else this.abstractDataObjectName = abstractDataObjectName;
68     this.controller = parent.getController();
69     initializeMonitoring(parent);
70     childs = new java.util.HashMap JavaDoc();
71   }
72
73
74   protected AbstractDataObject(AbstractDataObject parent) {
75     this(parent, null);
76   }
77
78
79   protected AbstractDataObject() {
80     initializeMonitoring(null);
81     childs = new java.util.HashMap JavaDoc();
82   }
83
84
85   //
86
// -- PUBLICS METHODS -----------------------------------------------
87
//
88

89   public String JavaDoc toString() {
90     return "DataObject "+abstractDataObjectName+"\n"+childs.toString();
91   }
92   
93   
94   public DataObjectController getController() {
95     return controller;
96   }
97   
98   
99   public AbstractDataObject getParent() {
100     return parent;
101   }
102   
103   
104   /**
105    * return the top level parent
106    */

107    public AbstractDataObject getTopLevelParent() {
108      if (parent == null) {
109        return this;
110      } else {
111        return parent.getTopLevelParent();
112      }
113    }
114  
115  
116   public java.util.Iterator JavaDoc childsIterator() {
117     return childs.values().iterator();
118   }
119   
120   
121   public int getChildObjectsCount() {
122     return childs.size();
123   }
124   
125
126   public synchronized ActiveObject findActiveObjectById(UniqueID id) {
127     java.util.Iterator JavaDoc iterator = childsIterator();
128     while (iterator.hasNext()) {
129       AbstractDataObject o = (AbstractDataObject) iterator.next();
130       ActiveObject activeObject = o.findActiveObjectById(id);
131       if (activeObject != null) return activeObject;
132     }
133     return null;
134   }
135   
136   
137   public abstract void destroyObject();
138
139
140
141   //
142
// -- implements MessageMonitoringController -----------------------------------------------
143
//
144

145   public void viewInEventList(boolean shouldView) {
146     if (isDestroyed) return;
147     if (viewingInEventList != shouldView) {
148       viewingInEventList = shouldView;
149       if (messageMonitoringListener != null) messageMonitoringListener.viewingInEventListChanged(shouldView);
150     }
151     viewInEventListCollection(shouldView, childsIterator());
152   }
153
154
155   public void monitorRequestReceiver(boolean shouldMonitor) {
156     if (isDestroyed) return;
157     if (monitoringRequestReceiver != shouldMonitor) {
158       monitoringRequestReceiver = shouldMonitor;
159       if (messageMonitoringListener != null) messageMonitoringListener.monitoringRequestReceiverChanged(shouldMonitor);
160     }
161     monitorRequestReceiverCollection(shouldMonitor, childsIterator());
162   }
163
164
165   public void monitorRequestSender(boolean shouldMonitor) {
166     if (isDestroyed) return;
167     if (monitoringRequestSender != shouldMonitor) {
168       monitoringRequestSender = shouldMonitor;
169       if (messageMonitoringListener != null) messageMonitoringListener.monitoringRequestSenderChanged(shouldMonitor);
170     }
171     monitorRequestSenderCollection(shouldMonitor, childsIterator());
172   }
173   
174
175   public void monitorReplyReceiver(boolean shouldMonitor) {
176     if (isDestroyed) return;
177     if (monitoringReplyReceiver != shouldMonitor) {
178       monitoringReplyReceiver = shouldMonitor;
179       if (messageMonitoringListener != null) messageMonitoringListener.monitoringReplyReceiverChanged(shouldMonitor);
180     }
181     monitorReplyReceiverCollection(shouldMonitor, childsIterator());
182   }
183   
184
185   public void monitorReplySender(boolean shouldMonitor) {
186     if (isDestroyed) return;
187     if (monitoringReplySender != shouldMonitor) {
188       monitoringReplySender = shouldMonitor;
189       if (messageMonitoringListener != null) messageMonitoringListener.monitoringReplySenderChanged(shouldMonitor);
190     }
191     monitorReplySenderCollection(shouldMonitor, childsIterator());
192   }
193
194
195   public void monitorAll(boolean shouldMonitor) {
196     monitorRequestReceiver(shouldMonitor);
197     monitorRequestSender(shouldMonitor);
198     monitorReplyReceiver(shouldMonitor);
199     monitorReplySender(shouldMonitor);
200   }
201
202   public boolean isMonitoring() {
203     return monitoringRequestReceiver || monitoringRequestSender || monitoringReplyReceiver || monitoringReplySender;
204   }
205
206
207   public boolean isMonitoringRequestReceiver() {
208     return monitoringRequestReceiver;
209   }
210
211
212   public boolean isMonitoringRequestSender() {
213     return monitoringRequestSender;
214   }
215
216
217   public boolean isMonitoringReplyReceiver() {
218     return monitoringReplyReceiver;
219   }
220
221
222   public boolean isMonitoringReplySender() {
223     return monitoringReplySender;
224   }
225
226
227   public boolean isViewedInEventList() {
228     return viewingInEventList;
229   }
230
231
232   //
233
// -- PROTECTED METHODS -----------------------------------------------
234
//
235

236  /**
237   * destroy this object
238   */

239   protected synchronized boolean destroy() {
240     if (isDestroyed) return false;
241     //System.out.println("AbstractDataObject destroy "+abstractDataObjectName);
242
isDestroyed = true;
243     destroyCollection(childsIterator());
244     childs.clear();
245     parent = null;
246     controller = null;
247     return true;
248   }
249       
250    
251  /**
252   * clear Child
253   */

254   protected synchronized void clearChilds() {
255     childs.clear();
256   }
257
258
259  /**
260   * add Child
261   */

262   public synchronized void putChild(Object JavaDoc key, AbstractDataObject child) {
263     if (isDestroyed) return;
264     //System.out.println("AbstractDataObject putChild child="+child.abstractDataObjectName);
265
childs.put(key, child);
266   }
267
268
269  /**
270   * remove Child
271   */

272   protected synchronized AbstractDataObject removeChild(Object JavaDoc key) {
273     AbstractDataObject o;
274     if (isDestroyed) {
275       // we are in the Iterator to destroy all childs object :
276
// we don't want to remove from the collection not to
277
// have an exception from the iterator
278
o = (AbstractDataObject) childs.get(key);
279     } else {
280       // we are asked to remove the child from elsewhere
281
o = (AbstractDataObject) childs.remove(key);
282     }
283     if (o == null) return null;
284     //System.out.println("AbstractDataObject removeChild child="+o.abstractDataObjectName);
285
o.destroy();
286     return o;
287   }
288
289
290  /**
291   * remove Child
292   */

293   protected synchronized AbstractDataObject getChild(Object JavaDoc key) {
294     return (AbstractDataObject) childs.get(key);
295   }
296
297
298   /**
299    * destroys all object known by this object
300    */

301   protected synchronized void destroyCollection(java.util.Iterator JavaDoc iterator) {
302     //System.out.println("AbstractDataObject destroyCollection "+abstractDataObjectName+" childs#"+childs.size());
303
while (iterator.hasNext()) {
304       AbstractDataObject o = (AbstractDataObject) iterator.next();
305       o.destroyObject();
306     }
307   }
308   
309   
310   /**
311    * notifies all known objects of the monitoring request
312    * @param shouldMonitor whether the monitoring is activated or not
313    * @param iterator an iterator on the collection of object to notify
314    */

315   protected synchronized void viewInEventListCollection(boolean shouldView, java.util.Iterator JavaDoc iterator) {
316     while (iterator.hasNext() && ! isDestroyed) {
317       MessageMonitoringController o = (MessageMonitoringController) iterator.next();
318       o.viewInEventList(shouldView);
319     }
320   }
321   
322   
323   /**
324    * notifies all known objects of the monitoring request
325    * @param shouldMonitor whether the monitoring is activated or not
326    * @param iterator an iterator on the collection of object to notify
327    */

328   protected synchronized void monitorRequestReceiverCollection(boolean shouldMonitor, java.util.Iterator JavaDoc iterator) {
329     while (iterator.hasNext() && ! isDestroyed) {
330       MessageMonitoringController o = (MessageMonitoringController) iterator.next();
331       o.monitorRequestReceiver(shouldMonitor);
332     }
333   }
334   
335   
336   /**
337    * notifies all known objects of the monitoring request
338    * @param shouldMonitor whether the monitoring is activated or not
339    * @param iterator an iterator on the collection of object to notify
340    */

341   protected synchronized void monitorRequestSenderCollection(boolean shouldMonitor, java.util.Iterator JavaDoc iterator) {
342     while (iterator.hasNext() && ! isDestroyed) {
343       MessageMonitoringController o = (MessageMonitoringController) iterator.next();
344       o.monitorRequestSender(shouldMonitor);
345     }
346   }
347   
348   
349   /**
350    * notifies all known objects of the monitoring request
351    * @param shouldMonitor whether the monitoring is activated or not
352    * @param iterator an iterator on the collection of object to notify
353    */

354   protected synchronized void monitorReplyReceiverCollection(boolean shouldMonitor, java.util.Iterator JavaDoc iterator) {
355     while (iterator.hasNext() && ! isDestroyed) {
356       MessageMonitoringController o = (MessageMonitoringController) iterator.next();
357       o.monitorReplyReceiver(shouldMonitor);
358     }
359   }
360   
361   
362   /**
363    * notifies all known objects of the monitoring request
364    * @param shouldMonitor whether the monitoring is activated or not
365    * @param iterator an iterator on the collection of object to notify
366    */

367   protected synchronized void monitorReplySenderCollection(boolean shouldMonitor, java.util.Iterator JavaDoc iterator) {
368     while (iterator.hasNext() && ! isDestroyed) {
369       MessageMonitoringController o = (MessageMonitoringController) iterator.next();
370       o.monitorReplySender(shouldMonitor);
371     }
372   }
373   
374   
375   /**
376    * notifies all known objects of the monitoring request
377    * @param shouldMonitor whether the monitoring is activated or not
378    * @param iterator an iterator on the collection of object to notify
379    */

380   protected synchronized void monitorAllCollection(boolean shouldMonitor, java.util.Iterator JavaDoc iterator) {
381     while (iterator.hasNext() && ! isDestroyed) {
382       MessageMonitoringController o = (MessageMonitoringController) iterator.next();
383       o.monitorAll(shouldMonitor);
384     }
385   }
386
387   
388   // notification methods to the parent
389

390   protected void monitoringMessageEventChanged(ActiveObject object, boolean value) {
391     if (parent != null && ! isDestroyed) parent.monitoringMessageEventChanged(object, value);
392   }
393   
394
395
396   //
397
// -- PRIVATE METHODS -----------------------------------------------
398
//
399

400
401   private void initializeMonitoring(AbstractDataObject parent) {
402     if (parent != null) {
403       monitoringRequestReceiver = parent.monitoringRequestReceiver;
404       monitoringRequestSender = parent.monitoringRequestSender;
405       monitoringReplyReceiver = parent.monitoringReplyReceiver;
406       monitoringReplySender = parent.monitoringReplySender;
407       viewingInEventList = parent.viewingInEventList;
408     } else {
409       // monitor everything by default
410
monitoringRequestReceiver = true;
411       monitoringRequestSender = true;
412       monitoringReplyReceiver = true;
413       monitoringReplySender = true;
414       // not adding in event list by default
415
viewingInEventList = false;
416     }
417   }
418
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432
433
Popular Tags