|
Scala example source code file (RegistryDelegate.scala)
The Scala RegistryDelegate.scala source code/* __ *\ ** ________ ___ / / ___ Scala API ** ** / __/ __// _ | / / / _ | (c) 2002-2011, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** \* */ // $Id: RegistryDelegate.scala 18234 2009-07-07 13:21:57Z michelou $ package scala.runtime.remoting import java.rmi.{RMISecurityManager, Remote, RemoteException} import java.rmi.registry.{LocateRegistry, Registry} import java.rmi.server.UnicastRemoteObject /** * <p> * This class implements the registry delegate concept * (see http://www.genady.net/rmi/v20/docs/delegate/RegistryDelegate.html) * </p> * <p> * In order to enforce some level of security, the standard RMI registry * implementation (e.g. <code>rmiregistry.exe) only allows processes * on the same host to register objects in the registry (think of a bank * running a registry on one of its servers, and doesn't want anybody * modifying it). So, by design, if a process tries to * <code>bind(String, Remote) an object to a remote registry, * an exception will be thrown. * </p> * <p> * However, the design of a distributed system may require remote clients to * register themselves in a central registry. If such system is deployed in a * controlled and trusted environment (e.g., a firewalled intranet with tight * access control), the security risk may be acceptable. * </p> * <p> * The simplest technical solution to the remote registration problem is to * have a registry delegate. A registry delegate is an object that serves as * a proxy for the real registry. The delegate itself usually appears in the * registry under a well known name. It implements the Registry interface and * simply delegates all method calls to the appropriate methods of the real * registry. The delegate is allowed to perform bind and unbind operations * because it is running on the same host as the registry. * </p> * <p> * The common scenario for starting a registry and creating the delegate is * starting a class with the following <code>main(Array[String]) method: * </p> * <pre> * @throws(classOf[AccessException], classOf[RemoteException], classOf[AlreadyBoundException]) * <b>object namingService { * <b>def main(args: Array[String]) { * <b>if (System.getSecurityManager() == null) * System.setSecurityManager(<b>new RMISecurityManager()) * * <b>val registry = LocateRegistry.createRegistry(REGISTRY_PORT) * registry.bind(DELEGATE_NAME, <b>new RegistryDelegate()); * * do { * <b>try { * Thread.sleep(Long.MAX_VALUE) * } <b>catch { * <b>case e: InterruptedException => // do nothing * <b>case e: Throwable => e.printStackTrace(); sys.exit(1) * } * } while (<b>true) * } * }</pre> * <p> * The common usage scenario looks something like: * </p>* Registry remoteRegistry = LocateRegistry.getRegistry("remotehost.mycompany.com"); * Registry delegate = (Registry) remoteRegistry.lookup(DELEGATE_NAME); * delegate.bind("someName", <b>new SomeRemoteObject());* <p> * The <code>getRegistryDelegate(String) method is a helper method * that fetches the registry delegate for you. * </p> * <p> * The <code>main(Array[String]) method of this class will create a * local registry on the default port, create a registry delegate and bind * it under the well known name that you chose in the wizard * (<code>DELEGATE_NAME). * </p> * * @author Genady Beryozkin, rmi-info@genady.net */ object RMIDelegate { /** The name under which the delegate appears in the registry. */ val DELEGATE_NAME = "foo" /** This method retrieves the registry delegate from a registry that is * running on a remote host. */ @throws(classOf[RemoteException]) def getRegistryDelegate(remoteHost: String): Registry = getRegistryDelegate(remoteHost, Registry.REGISTRY_PORT) /** This method retrieves the registry delegate from a registry that is * running on a remote host. */ @throws(classOf[RemoteException]) def getRegistryDelegate(remoteHost: String, remotePort: Int): Registry = { val registry = LocateRegistry.getRegistry(remoteHost, remotePort) (registry lookup DELEGATE_NAME).asInstanceOf[Registry] } /** A simple way to run a registry and bind a registry delegate. */ @throws(classOf[RemoteException]) def main(args: Array[String]) { var port = Registry.REGISTRY_PORT if (args.length > 0) { if (args(0) equals "-help") { println("Usage: rmidelegate <options> Other Scala examples (source code examples)Here is a short list of links related to this Scala RegistryDelegate.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.