public RMIConnection createConnection() {
    RMIServerManager serverManager = null;
    Context initialNamingContext = null;
    try {
      initialNamingContext = new InitialContext();
    } catch (NamingException exception) {
      System.out.println("Naming Exception " + exception.toString());
    }
    ;
    // Set the client security manager
    try {
      System.setSecurityManager(new RMISecurityManager());
    } catch (Exception exception) {
      System.out.println("Security violation " + exception.toString());
    }

    // Get the remote factory object from the Registry
    try {
      serverManager = (RMIServerManager) initialNamingContext.lookup("SERVER-MANAGER");
    } catch (Exception exception) {
      throw new TestProblemException(exception.toString());
    }

    RMIConnection rmiConnection = null;
    try {
      rmiConnection = new RMIConnection(serverManager.createRemoteSessionController());
    } catch (RemoteException exception) {
      System.out.println("Error in invocation " + exception.toString());
    }

    return rmiConnection;
  }
Exemple #2
0
  public static void main(String[] args) throws NamingException, RemoteException {
    System.setProperty("java.security.policy", "client.policy");
    System.setSecurityManager(new SecurityManager());
    Context namingContext = new InitialContext();

    System.out.print("RMI registry bindings: ");
    NamingEnumeration<NameClassPair> e = namingContext.list("rmi://localhost/");
    while (e.hasMore()) System.out.println(e.next().getName());

    String url = "rmi://localhost/central_warehouse";
    Warehouse centralWarehouse = (Warehouse) namingContext.lookup(url);

    Scanner in = new Scanner(System.in);
    System.out.print("Enter keywords: ");
    List<String> keywords = Arrays.asList(in.nextLine().split("\\s+"));
    Product prod = centralWarehouse.getProduct(keywords);

    System.out.println(prod.getDescription() + ": " + prod.getPrice());
  }