KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > corba > poa > Collective


1 /* Collective.java */
2 package org.enhydra.shark.corba.poa;
3
4 import org.omg.PortableServer.Servant JavaDoc;
5
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 /**
10  * Collective
11  *
12  * @author V.Puskas
13  * @version 0.1
14  */

15 public interface Collective {
16    public void __recruit(org.omg.CORBA.Object JavaDoc obj);
17
18    public void __leave(org.omg.CORBA.Object JavaDoc _obj);
19
20    public void __disband(org.omg.CORBA.ORB JavaDoc _orb);
21
22
23    public final class CollectiveCORBA implements Collective {
24
25       private transient HashSet JavaDoc myObjects = new HashSet JavaDoc();
26
27       public synchronized void __recruit(org.omg.CORBA.Object JavaDoc obj) {
28          myObjects.add(obj);
29       }
30
31       public synchronized void __disband(org.omg.CORBA.ORB JavaDoc _orb) {
32          for (Iterator JavaDoc it = myObjects.iterator(); it.hasNext();) {
33             ((org.omg.CORBA.Object JavaDoc)it.next())._release();
34             it.remove();
35          }
36          myObjects = null;
37       }
38
39       public synchronized void __leave(org.omg.CORBA.Object JavaDoc obj) {
40          Iterator JavaDoc i=myObjects.iterator();
41          Object JavaDoc toRemove=obj;
42          while (i.hasNext()) {
43             org.omg.CORBA.Object JavaDoc o=(org.omg.CORBA.Object JavaDoc)i.next();
44             if (obj._is_equivalent(o) ) {
45              // System.out.println("found object in __leave \n"+obj.toString());
46
toRemove = o;
47                break;
48             }
49          }
50
51             boolean r = myObjects.remove(toRemove);
52             if (r==false) System.out.println("Warning: object is not removed from collection! \n"+obj.toString());
53
54       }
55    
56    }
57
58 }
Popular Tags