1 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 43 public class NodeObject extends AbstractDataObject { 44 45 protected Node node; 46 protected java.util.HashMap activeObjects; 47 48 protected NodeObjectListener listener; 49 50 51 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 66 public String toString() { 67 return "Name="+node.getNodeInformation().getURL()+"\n"+super.toString(); 68 } 69 70 71 public Object createNewRemoteObject(String classname) { 72 try { 73 Object 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 104 public void registerListener(NodeObjectListener listener) { 105 this.messageMonitoringListener = listener; 106 this.listener = listener; 107 notifyListenerOfExistingChilds(); 108 } 109 110 111 112 116 public String getURL() { 117 return node.getNodeInformation().getProtocol()+":"+node.getNodeInformation().getURL(); 119 } 120 121 122 public String getName() { 123 return node.getNodeInformation().getName(); 124 } 125 126 public String getProtocol(){ 127 return node.getNodeInformation().getProtocol(); 128 } 129 130 134 public ActiveObject addActiveObject(String 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 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 192 private synchronized void notifyListenerOfExistingChilds() { 193 if (getChildObjectsCount() == 0) return; 194 java.util.Iterator iterator = childsIterator(); 195 while (iterator.hasNext()) { 196 ActiveObject activeObject = (ActiveObject) iterator.next(); 197 listener.activeObjectAdded(activeObject); 198 } 199 } 200 } 201 | Popular Tags |