1 31 package org.objectweb.proactive.core.runtime.jini; 32 33 import net.jini.core.entry.Entry; 34 import net.jini.core.lease.Lease; 35 import net.jini.core.lookup.ServiceID; 36 import net.jini.core.lookup.ServiceItem; 37 import net.jini.core.lookup.ServiceRegistrar; 38 import net.jini.core.lookup.ServiceRegistration; 39 40 import net.jini.discovery.DiscoveryEvent; 41 42 import net.jini.lease.LeaseRenewalEvent; 43 44 import net.jini.lookup.entry.Name; 45 46 import org.objectweb.proactive.Body; 47 import org.objectweb.proactive.core.body.UniversalBody; 48 import org.objectweb.proactive.core.descriptor.data.VirtualNode; 49 import org.objectweb.proactive.core.mop.ConstructorCall; 50 import org.objectweb.proactive.core.mop.ConstructorCallExecutionFailedException; 51 import org.objectweb.proactive.core.node.NodeException; 52 import org.objectweb.proactive.core.process.UniversalProcess; 53 import org.objectweb.proactive.core.runtime.ProActiveRuntime; 54 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl; 55 import org.objectweb.proactive.core.runtime.VMInformation; 56 import org.objectweb.proactive.core.util.UrlBuilder; 57 import org.objectweb.proactive.ext.security.PolicyServer; 58 import org.objectweb.proactive.ext.security.ProActiveSecurityManager; 59 60 import java.io.IOException ; 61 62 import java.lang.reflect.InvocationTargetException ; 63 64 import java.rmi.RemoteException ; 65 66 import java.security.SecureRandom ; 67 import java.security.cert.X509Certificate ; 68 69 import java.util.ArrayList ; 70 import java.util.Hashtable ; 71 import java.util.Vector ; 72 73 74 80 public class JiniRuntimeImpl extends java.rmi.server.UnicastRemoteObject 81 implements JiniRuntime, java.io.Serializable , 82 net.jini.discovery.DiscoveryListener, net.jini.lease.LeaseListener { 83 protected transient ProActiveRuntimeImpl proActiveRuntime; 84 protected String proActiveRuntimeURL; 85 86 protected java.util.Vector registrarsTable; 89 90 protected java.util.Hashtable jiniRuntimeMap; 94 95 protected java.util.Hashtable jiniNodeMap; 97 98 protected java.util.Hashtable jiniVirtualNodeMap; 101 private boolean isRuntimeRegistered = false; 102 103 protected transient net.jini.lease.LeaseRenewalManager leaseManager = new net.jini.lease.LeaseRenewalManager(); 105 106 public JiniRuntimeImpl() throws java.rmi.RemoteException { 110 this.proActiveRuntime = (ProActiveRuntimeImpl) ProActiveRuntimeImpl.getProActiveRuntime(); 111 this.proActiveRuntimeURL = buildRuntimeURL(); 112 this.jiniRuntimeMap = new java.util.Hashtable (); 113 jiniRuntimeMap.put(proActiveRuntimeURL, new java.util.Vector ()); 114 this.jiniNodeMap = new java.util.Hashtable (); 115 this.jiniVirtualNodeMap = new java.util.Hashtable (); 116 this.registrarsTable = new java.util.Vector (); 117 net.jini.discovery.LookupDiscovery discover = null; 118 try { 119 discover = new net.jini.discovery.LookupDiscovery(net.jini.discovery.LookupDiscovery.ALL_GROUPS); 120 } catch (Exception e) { 123 logger.error(e.toString()); 124 } 125 126 discover.addDiscoveryListener(this); 127 } 128 129 public String createLocalNode(String nodeName, 136 boolean replacePreviousBinding, PolicyServer ps, String vnname, String jobId) 137 throws java.rmi.RemoteException , NodeException { 138 while (!isRuntimeRegistered) { 145 } 146 String nodeURL = null; 147 148 try { 150 nodeURL = buildNodeURL(nodeName); 151 String name = UrlBuilder.getNameFromUrl(nodeURL); 153 154 proActiveRuntime.createLocalNode(name, replacePreviousBinding, ps, 158 vnname, jobId); 159 } catch (java.net.UnknownHostException e) { 160 throw new java.rmi.RemoteException ("Host unknown in " + nodeURL, e); 161 } 162 163 jiniNodeMap.put(nodeURL, registerService(nodeURL)); 164 165 return nodeURL; 166 } 167 168 public void killAllNodes() throws java.rmi.RemoteException { 169 for (java.util.Enumeration e = jiniNodeMap.keys(); e.hasMoreElements();) { 170 String nodeURL = (String ) e.nextElement(); 171 killNode(nodeURL); 172 } 173 } 174 175 public void killNode(String nodeName) throws java.rmi.RemoteException { 176 String nodeUrl = null; 177 try { 178 nodeUrl = buildNodeURL(nodeName); 179 unregisterService(nodeUrl, jiniNodeMap); 180 } catch (java.net.UnknownHostException e) { 181 throw new java.rmi.RemoteException ("Host unknown in " + nodeUrl, e); 182 } 183 proActiveRuntime.killNode(nodeName); 184 } 185 186 public void createVM(UniversalProcess remoteProcess) 187 throws IOException { 188 proActiveRuntime.createVM(remoteProcess); 189 } 190 191 public String [] getLocalNodeNames() { 192 return proActiveRuntime.getLocalNodeNames(); 193 } 194 195 public VMInformation getVMInformation() { 196 return proActiveRuntime.getVMInformation(); 197 } 198 199 public void register(ProActiveRuntime proActiveRuntimeDist, 200 String proActiveRuntimeName, String creatorID, String creationProtocol, 201 String vmName) { 202 proActiveRuntime.register(proActiveRuntimeDist, proActiveRuntimeName, 203 creatorID, creationProtocol, vmName); 204 } 205 206 public ProActiveRuntime[] getProActiveRuntimes() { 207 return proActiveRuntime.getProActiveRuntimes(); 208 } 209 210 public ProActiveRuntime getProActiveRuntime(String proActiveRuntimeName) { 211 return proActiveRuntime.getProActiveRuntime(proActiveRuntimeName); 212 } 213 214 public void killRT(boolean softly) throws java.rmi.RemoteException { 215 killAllNodes(); 216 unregisterAllVirtualNodes(); 217 unregisterService(proActiveRuntimeURL, jiniRuntimeMap); 218 proActiveRuntime.killRT(false); 219 } 220 221 public String getURL() { 222 return proActiveRuntimeURL; 223 } 224 225 public ArrayList getActiveObjects(String nodeName) { 226 return proActiveRuntime.getActiveObjects(nodeName); 227 } 228 229 public ArrayList getActiveObjects(String nodeName, String objectName) { 230 return proActiveRuntime.getActiveObjects(nodeName, objectName); 231 } 232 233 public VirtualNode getVirtualNode(String virtualNodeName) { 234 return proActiveRuntime.getVirtualNode(virtualNodeName); 235 } 236 237 public void registerVirtualNode(String virtualNodeName, 238 boolean replacePreviousBinding) throws java.rmi.RemoteException { 239 String virtualNodeURL = null; 240 241 try { 243 virtualNodeURL = buildNodeURL(virtualNodeName); 244 } catch (java.net.UnknownHostException e) { 245 throw new java.rmi.RemoteException ("Host unknown in " + 246 virtualNodeURL, e); 247 } 248 249 if (replacePreviousBinding) { 250 if (jiniVirtualNodeMap.get(virtualNodeURL) != null) { 251 jiniVirtualNodeMap.remove(virtualNodeURL); 252 } 253 } 254 if (!replacePreviousBinding && 255 (jiniVirtualNodeMap.get(virtualNodeURL) != null)) { 256 throw new java.rmi.RemoteException ("VirtualNode " + virtualNodeURL + 257 " already registered as Jini service"); 258 } 259 260 jiniVirtualNodeMap.put(virtualNodeURL, registerService(virtualNodeURL)); 261 } 262 263 public void unregisterVirtualNode(String virtualNodeName) 264 throws java.rmi.RemoteException { 265 proActiveRuntime.unregisterVirtualNode(UrlBuilder.removeVnSuffix( 266 virtualNodeName)); 267 String virtualNodeURL = null; 268 proActiveRuntime.unregisterVirtualNode(UrlBuilder.removeVnSuffix( 269 virtualNodeName)); 270 try { 272 virtualNodeURL = buildNodeURL(virtualNodeName); 273 unregisterService(virtualNodeURL, jiniVirtualNodeMap); 274 } catch (java.net.UnknownHostException e) { 275 throw new java.rmi.RemoteException ("Host unknown in " + 276 virtualNodeURL, e); 277 } 278 } 279 280 public void unregisterAllVirtualNodes() throws RemoteException { 281 for (java.util.Enumeration e = jiniVirtualNodeMap.keys(); 282 e.hasMoreElements();) { 283 String vnNodeURL = (String ) e.nextElement(); 284 unregisterVirtualNode(vnNodeURL); 285 } 286 } 287 288 public UniversalBody createBody(String nodeName, 289 ConstructorCall bodyConstructorCall, boolean isNodeLocal) 290 throws ConstructorCallExecutionFailedException, 291 InvocationTargetException { 292 return proActiveRuntime.createBody(nodeName, bodyConstructorCall, 293 isNodeLocal); 294 } 295 296 public UniversalBody receiveBody(String nodeName, Body body) { 297 return proActiveRuntime.receiveBody(nodeName, body); 298 } 299 300 public void discovered(DiscoveryEvent evt) { 304 ServiceRegistrar[] registrars = evt.getRegistrars(); 305 306 for (int n = 0; n < registrars.length; n++) { 309 ServiceRegistrar registrar = registrars[n]; 310 311 ServiceRegistration reg = null; 312 try { 313 ServiceItem item = new ServiceItem(newServiceID(), this, 316 new Entry[] { new Name(proActiveRuntimeURL) }); 317 reg = registrar.register(item, Lease.FOREVER); 318 } catch (Exception e) { 319 logger.error("register exception " + e.toString()); 321 continue; 322 } 323 ((Vector ) jiniRuntimeMap.get(proActiveRuntimeURL)).add(reg); 324 325 registrarsTable.add(registrar); 327 328 registerServiceAfterDiscovery(jiniNodeMap, registrar); 329 registerServiceAfterDiscovery(jiniVirtualNodeMap, registrar); 330 leaseManager.renewUntil(reg.getLease(), Lease.FOREVER, this); 332 isRuntimeRegistered = true; 333 } 334 335 isRuntimeRegistered = true; 339 } 340 341 public void discarded(DiscoveryEvent evt) { 342 } 343 344 public void notify(LeaseRenewalEvent evt) { 345 logger.info("Lease expired " + evt.toString()); 346 logger.info(evt.getException().getMessage()); 347 } 348 349 protected ServiceID newServiceID() { 353 354 355 SecureRandom secRand = new SecureRandom (); 356 357 358 byte[] secRandBuf16 = new byte[16]; 359 360 361 byte[] secRandBuf8 = new byte[8]; 362 363 secRand.nextBytes(secRandBuf16); 364 secRandBuf16[6] &= 0x0f; 365 secRandBuf16[6] |= 0x40; 366 secRandBuf16[8] &= 0x3f; 367 secRandBuf16[8] |= 0x80; 368 secRandBuf16[10] |= 0x80; 369 long mostSig = 0; 370 for (int i = 0; i < 8; i++) { 371 mostSig = (mostSig << 8) | (secRandBuf16[i] & 0xff); 372 } 373 long leastSig = 0; 374 for (int i = 8; i < 16; i++) { 375 leastSig = (leastSig << 8) | (secRandBuf16[i] & 0xff); 376 } 377 return new ServiceID(mostSig, leastSig); 378 } 379 380 382 385 public X509Certificate getCreatorCertificate() 386 throws java.rmi.RemoteException { 387 return proActiveRuntime.getCreatorCertificate(); 388 } 389 390 public PolicyServer getPolicyServer() throws java.rmi.RemoteException { 391 return proActiveRuntime.getPolicyServer(); 392 } 393 394 public void setProActiveSecurityManager(ProActiveSecurityManager ps) 395 throws java.rmi.RemoteException { 396 proActiveRuntime.setProActiveSecurityManager(ps); 397 } 398 399 public String getVNName(String Nodename) throws java.rmi.RemoteException { 400 return proActiveRuntime.getVNName(Nodename); 401 } 402 403 406 public void setDefaultNodeVirtualNodeName(String s) 407 throws RemoteException { 408 proActiveRuntime.setDefaultNodeVirtualNodeName(s); 409 } 410 411 414 public void updateLocalNodeVirtualName() throws RemoteException { 415 } 417 418 421 public PolicyServer getNodePolicyServer(String nodeName) 422 throws RemoteException { 423 return proActiveRuntime.getNodePolicyServer(nodeName); 424 } 425 426 429 public void enableSecurityIfNeeded() throws RemoteException { 430 proActiveRuntime.enableSecurityIfNeeded(); 431 } 432 433 436 public X509Certificate getNodeCertificate(String nodeName) 437 throws RemoteException { 438 return proActiveRuntime.getNodeCertificate(nodeName); 439 } 440 441 445 public ArrayList getEntities(String nodeName) throws RemoteException { 446 return proActiveRuntime.getEntities(nodeName); 447 } 448 449 453 public ArrayList getEntities(UniversalBody uBody) throws RemoteException { 454 return proActiveRuntime.getEntities(uBody); 455 } 456 457 460 public ArrayList getEntities() throws RemoteException { 461 return proActiveRuntime.getEntities(); 462 } 463 464 467 public String getJobID(String nodeUrl) throws RemoteException { 468 return proActiveRuntime.getJobID(nodeUrl); 469 } 470 471 private void registerServiceAfterDiscovery(Hashtable jiniObjectTable, 478 ServiceRegistrar registrar) { 479 ServiceRegistration reg = null; 480 ServiceID serviceID = null; 481 if (!jiniObjectTable.isEmpty()) { 482 synchronized (jiniObjectTable) { 483 for (java.util.Enumeration e = jiniObjectTable.keys(); 484 e.hasMoreElements();) { 485 String objectURL = (String ) e.nextElement(); 486 Vector serviceRegistrationTable = (Vector ) jiniObjectTable.get(objectURL); 487 if (!serviceRegistrationTable.isEmpty()) { 488 serviceID = ((ServiceRegistration) serviceRegistrationTable.get(0)).getServiceID(); 489 } else { 490 serviceID = newServiceID(); 491 } 492 ServiceItem item = new ServiceItem(serviceID, this, 493 new Entry[] { new Name(objectURL) }); 494 try { 495 reg = registrar.register(item, Lease.FOREVER); 496 } catch (Exception ex) { 497 logger.info("register exception " + ex.toString()); 498 continue; 499 } 500 logger.info(" Service Registered " + objectURL); 501 502 leaseManager.renewUntil(reg.getLease(), Lease.FOREVER, this); 504 ((Vector ) jiniObjectTable.get(objectURL)).add(reg); 505 } 506 } 507 } 508 } 509 510 private String buildRuntimeURL() { 511 String host = getVMInformation().getInetAddress().getCanonicalHostName(); 512 String name = getVMInformation().getName(); 513 return UrlBuilder.buildUrl(host, name, "jini:"); 514 } 515 516 private String buildNodeURL(String url) 517 throws java.net.UnknownHostException { 518 int i = url.indexOf('/'); 519 if (i == -1) { 520 String host = getVMInformation().getInetAddress().getCanonicalHostName(); 522 return UrlBuilder.buildUrl(host, url, "jini:"); 523 } else { 524 return UrlBuilder.checkUrl(url); 525 } 526 } 527 528 private Vector registerService(String objectUrl) 529 throws java.rmi.RemoteException { 530 int counter = 0; 533 ServiceID serviceID = newServiceID(); 534 Vector serviceRegistrationTable = new Vector (); 535 536 for (int n = 0; n < registrarsTable.size(); n++) { 538 ServiceRegistrar registrar = (ServiceRegistrar) registrarsTable.get(n); 539 ServiceRegistration reg = null; 540 try { 541 ServiceItem item = new ServiceItem(serviceID, this, 542 new Entry[] { new Name(objectUrl) }); 543 reg = registrar.register(item, Lease.FOREVER); 544 counter++; 545 } catch (Exception e) { 546 logger.info("register exception " + e.toString()); 547 continue; 548 } 549 550 if (counter == 0) { 552 throw new java.rmi.RemoteException ("register exception "); 553 } 554 logger.info("Service registered " + objectUrl); 555 leaseManager.renewUntil(reg.getLease(), Lease.FOREVER, this); 558 serviceRegistrationTable.add(reg); 559 } 560 return serviceRegistrationTable; 561 } 562 563 private void unregisterService(String objectUrl, Hashtable jiniObjectTable) 564 throws java.rmi.RemoteException { 565 if (!jiniObjectTable.isEmpty()) { 566 synchronized (jiniObjectTable) { 567 try { 568 Vector serviceRegistrationTable = (Vector ) jiniObjectTable.get(objectUrl); 569 if (!serviceRegistrationTable.isEmpty()) { 570 for (int i = 0; i < serviceRegistrationTable.size(); 571 i++) { 572 ServiceRegistration reg = (ServiceRegistration) serviceRegistrationTable.get(i); 573 reg.getLease().cancel(); 574 } 575 if (objectUrl.indexOf("PA_RT") < 0) { 576 logger.info("Lease cancelled for " + objectUrl); 577 } 578 } 579 } catch (net.jini.core.lease.UnknownLeaseException e) { 580 throw new java.rmi.RemoteException ( 581 "Unable to get the Lease for virtualNode " + objectUrl, 582 e); 583 } finally { 584 jiniObjectTable.remove(objectUrl); 585 } 586 } 587 } 588 } 589 } 590 | Popular Tags |