1 31 package org.objectweb.proactive.ic2d.util; 32 33 import net.jini.core.discovery.LookupLocator; 34 import net.jini.core.lookup.ServiceMatches; 35 import net.jini.core.lookup.ServiceRegistrar; 36 import net.jini.core.lookup.ServiceTemplate; 37 38 import net.jini.discovery.DiscoveryEvent; 39 import net.jini.discovery.DiscoveryListener; 40 import net.jini.discovery.LookupDiscovery; 41 42 import org.apache.log4j.Logger; 43 44 import org.objectweb.proactive.core.node.NodeImpl; 45 import org.objectweb.proactive.core.runtime.ProActiveRuntime; 46 import org.objectweb.proactive.core.runtime.VMInformation; 47 import org.objectweb.proactive.core.runtime.jini.JiniRuntime; 48 import org.objectweb.proactive.core.runtime.jini.JiniRuntimeAdapter; 49 50 import java.rmi.RMISecurityManager ; 51 52 53 public class JiniNodeListener implements DiscoveryListener { 54 static Logger log4jlogger = Logger.getLogger(JiniNodeListener.class.getName()); 55 protected java.util.ArrayList nodes = new java.util.ArrayList (); 56 private String host; 57 private IC2DMessageLogger logger; 58 59 public JiniNodeListener() { 60 this(null); 61 } 62 63 public JiniNodeListener(String _host) { 64 host = _host; 65 if ((System.getSecurityManager() == null) && 66 !("false".equals(System.getProperty("proactive.securitymanager")))) { 67 System.setSecurityManager(new RMISecurityManager ()); 68 } 69 70 LookupDiscovery discover = null; 72 LookupLocator lookup = null; 73 if (host != null) { 74 try { 76 lookup = new LookupLocator("jini://" + host); 77 ServiceRegistrar registrar = lookup.getRegistrar(); 79 Class [] classes = new Class [] { JiniRuntime.class }; 80 JiniRuntime runtime = null; 81 ServiceMatches matches = null; 82 VMInformation info = null; 83 ServiceTemplate template = new ServiceTemplate(null, classes, 84 null); 85 86 matches = registrar.lookup(template, Integer.MAX_VALUE); 88 if (matches.totalMatches > 0) { 89 for (int i = 0; i < matches.items.length; i++) { 91 String jiniName = matches.items[i].attributeSets[0].toString(); 93 94 if ((jiniName.indexOf("PA_RT") == -1) && 96 (jiniName.indexOf("_VN") == -1)) { 97 int k = jiniName.indexOf("="); 99 String name = jiniName.substring(k + 1, 100 jiniName.length() - 1); 101 102 try { 104 if (matches.items[i].service == null) { 105 log4jlogger.warn("Service : NULL !!!"); 106 } else { 107 runtime = (JiniRuntime) matches.items[i].service; 108 info = runtime.getVMInformation(); 110 if (info != null) { 111 try { 114 if ((info.getInetAddress()).equals( 118 java.net.InetAddress.getByName( 119 host))) { 120 ProActiveRuntime part = new JiniRuntimeAdapter(runtime); 121 122 nodes.add(new NodeImpl(part, 124 name, "jini", 125 part.getJobID(name))); 126 } 127 } catch (java.net.UnknownHostException e) { 128 log4jlogger.error("Unknown host " + 129 host); 130 } catch (org.objectweb.proactive.core.ProActiveException e) { 131 e.printStackTrace(); 132 } 133 } 134 } 135 } catch (java.rmi.ConnectException e) { 136 log4jlogger.error( 137 "JiniNodeListener ConnectException "); 138 continue; 140 } 142 } 143 } 144 } else { 145 log4jlogger.error("JiniNodeListener: No JiniNode"); 146 } 147 } catch (java.net.MalformedURLException e) { 148 log4jlogger.error("Lookup failed: " + e.getMessage()); 149 } catch (java.io.IOException e) { 150 log4jlogger.error("Registrar search failed: " + e.getMessage()); 151 } catch (java.lang.ClassNotFoundException e) { 152 log4jlogger.error("Class Not Found: " + e.getMessage()); 153 } 154 } else { 155 try { 157 discover = new LookupDiscovery(LookupDiscovery.ALL_GROUPS); 158 } catch (Exception e) { 159 log4jlogger.fatal(" JiniNodeFinder exception"); 160 e.printStackTrace(); 161 } 162 163 discover.addDiscoveryListener(this); 164 } 165 } 166 167 public void discovered(DiscoveryEvent evt) { 168 ServiceRegistrar[] registrars = evt.getRegistrars(); 169 170 Class [] classes = new Class [] { JiniRuntime.class }; 172 JiniRuntime runtime = null; 173 ServiceMatches matches = null; 174 ServiceTemplate template = new ServiceTemplate(null, classes, null); 175 VMInformation info = null; 176 177 for (int n = 0; n < registrars.length; n++) { 178 ServiceRegistrar registrar = registrars[n]; 180 try { 181 matches = registrar.lookup(template, Integer.MAX_VALUE); 183 if (matches.totalMatches > 0) { 184 for (int i = 0; i < matches.items.length; i++) { 186 String jiniName = matches.items[i].attributeSets[0].toString(); 188 189 if ((jiniName.indexOf("PA_RT") == -1) && 191 (jiniName.indexOf("_VN") == -1)) { 192 int k = jiniName.indexOf("="); 194 String name = jiniName.substring(k + 1, 195 jiniName.length() - 1); 196 197 try { 199 if (matches.items[i].service == null) { 200 log4jlogger.warn("Service : NULL !!!"); 201 } else { 202 runtime = (JiniRuntime) matches.items[i].service; 203 info = runtime.getVMInformation(); 205 if (info != null) { 206 try { 209 if (host != null) { 211 try { 214 if ((info.getInetAddress()).equals( 215 java.net.InetAddress.getByName( 216 host))) { 217 ProActiveRuntime part = new JiniRuntimeAdapter(runtime); 218 nodes.add(new NodeImpl( 220 part, 221 name, "jini",part.getJobID(name))); 222 } 223 } catch (java.net.UnknownHostException e) { 224 log4jlogger.error( 225 "Unknown host " + host); 226 } 227 } else { 228 ProActiveRuntime part = new JiniRuntimeAdapter(runtime); 231 nodes.add(new NodeImpl( 232 part, name, 233 "jini",part.getJobID(name))); 234 } 235 } catch (org.objectweb.proactive.core.ProActiveException e) { 236 e.printStackTrace(); 237 } 238 } 239 } 240 } catch (java.rmi.ConnectException e) { 241 log4jlogger.error( 242 "JiniNodeListener ConnectException "); 243 continue; 245 } 247 } 248 } 249 } else { 250 log4jlogger.error("JiniNodeListener: No JiniNode"); 251 } 252 } catch (java.rmi.RemoteException e) { 253 continue; 255 } 257 } 258 259 } 261 262 public void discarded(DiscoveryEvent evt) { 263 } 264 265 public java.util.ArrayList getNodes() { 266 return nodes; 267 } 268 269 public static void main(String [] args) { 270 JiniNodeListener jnf = new JiniNodeListener(null); 271 272 try { 274 Thread.sleep(100000L); 275 } catch (java.lang.InterruptedException e) { 276 } 278 } 279 } 280 | Popular Tags |