1 31 package org.objectweb.proactive.ic2d.util; 32 33 import org.apache.log4j.Logger; 34 35 import org.objectweb.proactive.core.ProActiveException; 36 import org.objectweb.proactive.core.node.Node; 37 import org.objectweb.proactive.core.node.NodeImpl; 38 import org.objectweb.proactive.core.runtime.ProActiveRuntime; 39 import org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime; 40 import org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntimeAdapter; 41 42 import java.rmi.registry.LocateRegistry ; 43 import java.rmi.registry.Registry ; 44 45 46 49 public class RMIHostNodeFinder implements HostNodeFinder { 50 static Logger log4jlogger = Logger.getLogger(RMIHostNodeFinder.class.getName()); 51 private static final int DEFAULT_RMI_PORT = 1099; 52 private IC2DMessageLogger logger; 53 54 public RMIHostNodeFinder(IC2DMessageLogger logger) { 58 this.logger = logger; 59 } 60 61 public RMIHostNodeFinder() { 62 } 63 64 public Node[] findNodes(String host) throws java.io.IOException { 71 log("RMIHostNodeFinder findNodes for " + host); 73 String hostname = host; 74 int port = DEFAULT_RMI_PORT; 75 int pos = host.lastIndexOf(":"); 76 if (pos != -1) { 77 try { 79 port = Integer.parseInt(host.substring(1 + pos)); 80 } catch (NumberFormatException e) { 81 port = DEFAULT_RMI_PORT; 82 } 83 hostname = host.substring(0, pos); 84 } 85 log("Trying " + hostname + ":" + port); 86 87 Registry registry = LocateRegistry.getRegistry(hostname, port); 89 return findNodes(registry, host); 90 } 91 92 private Node[] findNodes(Registry registry, String host) 96 throws java.io.IOException { 97 log("Listing bindings for " + registry); 99 String [] list = registry.list(); 100 if (log4jlogger.isDebugEnabled()) { 101 log4jlogger.debug("list " + list.length); 102 } 103 if (list.length == 0) { 104 return new Node[0]; 105 } 106 java.util.ArrayList nodes = new java.util.ArrayList (list.length); 107 for (int i = 0; i < list.length; i++) { 108 Object obj; 109 110 if ((list[i].indexOf("PA_RT") == -1) && 112 (list[i].indexOf("SpyListenerNode") == -1) && 113 (list[i].indexOf("_VN") == -1)) { 114 try { 116 obj = registry.lookup(list[i]); 117 } catch (java.rmi.NotBoundException e) { 118 log(" registry.lookup of " + list[i] + " throwed ex=" + e); 120 continue; 121 } 122 123 if (obj instanceof org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime) { 127 log(" -> Found remote node " + list[i]); 129 try { 131 ProActiveRuntime part = new RemoteProActiveRuntimeAdapter((RemoteProActiveRuntime) obj); 132 String url = "//" + host + "/" + list[i]; 133 nodes.add(new NodeImpl(part, url, "rmi", 134 part.getJobID(url))); 135 } catch (ProActiveException e) { 136 log( 137 "Error while trying to create a RuntimeAdapter for " + 138 list[i] + 139 ", check the version of ProActive or jdk"); 140 } 141 } 142 } 143 } 144 Node[] nodeArray = new Node[nodes.size()]; 145 if (nodes.size() > 0) { 146 nodeArray = (Node[]) nodes.toArray(nodeArray); 147 } 148 return nodeArray; 149 } 150 151 private void log(String s) { 152 if (logger != null) { 153 logger.log(s); 154 } else { 155 log4jlogger.info(s); 156 } 157 } 158 159 private void log(String s, Exception e) { 160 if (logger != null) { 161 logger.log(s, e); 162 } else { 163 log4jlogger.info(s); 164 e.printStackTrace(); 165 } 166 } 167 } 168 | Popular Tags |