KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActiveObjectCreationException;
34 import org.objectweb.proactive.ProActive;
35 import org.objectweb.proactive.core.UniqueID;
36 import org.objectweb.proactive.core.node.Node;
37 import org.objectweb.proactive.core.node.NodeException;
38 import org.objectweb.proactive.ic2d.event.NodeObjectListener;
39
40 /**
41  * Holder class for the host data representation
42  */

43 public class NodeObject extends AbstractDataObject {
44
45   protected Node node;
46   protected java.util.HashMap JavaDoc activeObjects;
47
48   protected NodeObjectListener listener;
49
50
51   //
52
// -- CONSTRUCTORS -----------------------------------------------
53
//
54

55   public NodeObject(VMObject parent, Node node) {
56     super(parent);
57     this.node = node;
58     controller.log("Node Object "+node.getNodeInformation().getURL()+" created.");
59   }
60
61
62   //
63
// -- PUBLIC METHOD -----------------------------------------------
64
//
65

66   public String JavaDoc toString() {
67     return "Name="+node.getNodeInformation().getURL()+"\n"+super.toString();
68   }
69   
70
71   public Object JavaDoc createNewRemoteObject(String JavaDoc classname) {
72     try {
73       Object JavaDoc o = ProActive.newActive(classname, null, node);
74       controller.log("Active Object "+classname+" succesfully created.");
75       return o;
76     } catch (ActiveObjectCreationException e) {
77       controller.log("Cannot create active object "+classname, e);
78       return null;
79     } catch (NodeException e) {
80       controller.log("Problem with this node "+getURL());
81       return null;
82     }
83   }
84   
85   
86   public boolean isInsideSameVM(NodeObject o) {
87     return getTypedParent().getID().equals(o.getTypedParent().getID());
88   }
89
90   public boolean isInsideSameNode(NodeObject o) {
91     return getTypedParent().getID().equals(o.getTypedParent().getID()) &&
92             getURL().equals(o.getURL());
93   }
94
95   public ActiveObject findActiveObjectById(UniqueID id) {
96     return getActiveObject(id);
97   }
98
99
100   //
101
// Event Listener
102
//
103

104   public void registerListener(NodeObjectListener listener) {
105     this.messageMonitoringListener = listener;
106     this.listener = listener;
107     notifyListenerOfExistingChilds();
108   }
109   
110
111   
112   //
113
// Accessor methods
114
//
115

116   public String JavaDoc getURL() {
117     //System.out.println(node.getNodeInformation().getProtocol()+":"+node.getNodeInformation().getURL());
118
return node.getNodeInformation().getProtocol()+":"+node.getNodeInformation().getURL();
119   }
120
121   
122   public String JavaDoc getName() {
123     return node.getNodeInformation().getName();
124   }
125
126   public String JavaDoc getProtocol(){
127     return node.getNodeInformation().getProtocol();
128   }
129
130   //
131
// ActiveObject related methods
132
//
133

134   public ActiveObject addActiveObject(String JavaDoc classname, UniqueID bodyID, boolean isActive) {
135     ActiveObject activeObject = (ActiveObject) getChild(bodyID);
136     if (activeObject == null) {
137       activeObject = new ActiveObject(this, bodyID, classname, isActive);
138       putChild(bodyID, activeObject);
139       getTypedParent().registerActiveObject(bodyID, this);
140       ((IC2DObject) getTopLevelParent()).activeObjectAdded(activeObject);
141       if (listener != null) listener.activeObjectAdded(activeObject);
142     }
143     return activeObject;
144   }
145
146
147   public void removeActiveObject(UniqueID bodyID) {
148     ActiveObject activeObject = (ActiveObject) removeChild(bodyID);
149     if (activeObject == null) {
150       controller.log("Cannot find object id="+bodyID);
151     } else {
152       getTypedParent().unregisterActiveObject(bodyID);
153       ((IC2DObject) getTopLevelParent()).activeObjectRemoved(activeObject);
154       if (listener != null) listener.activeObjectRemoved(activeObject);
155     }
156   }
157
158
159   public ActiveObject getActiveObject(UniqueID id) {
160     return (ActiveObject) getChild(id);
161   }
162
163
164   public void destroyObject() {
165     getTypedParent().removeNodeObject(getName());
166   }
167
168
169   //
170
// -- PROTECTED METHOD -----------------------------------------------
171
//
172

173   protected synchronized boolean destroy() {
174     if (super.destroy()) {
175       listener = null;
176       return true;
177     } else {
178      return false;
179     }
180   }
181
182
183   protected VMObject getTypedParent() {
184     return (VMObject)parent;
185   }
186
187
188   //
189
// -- PRIVATE METHOD -----------------------------------------------
190
//
191

192   private synchronized void notifyListenerOfExistingChilds() {
193     if (getChildObjectsCount() == 0) return;
194     java.util.Iterator JavaDoc iterator = childsIterator();
195     while (iterator.hasNext()) {
196       ActiveObject activeObject = (ActiveObject) iterator.next();
197       listener.activeObjectAdded(activeObject);
198     }
199   }
200 }
201
Popular Tags