コード例 #1
0
ファイル: UseCustomRef.java プロジェクト: FauxFaux/jdk9-jdk
  public static void main(String[] args) {
    Ping obj = null;
    Registry registry = null;

    try {
      /*
       * create registry
       */
      TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");

      System.err.println("creating Registry...");

      registry = TestLibrary.createRegistryOnUnusedPort();
      int port = TestLibrary.getRegistryPort(registry);
      /*
       * create object with custom ref and bind in registry
       */
      System.err.println("creating UseCustomRef...");
      UseCustomRef cr = new UseCustomRef();
      RemoteRef ref = cr.getRef();
      if (!(ref instanceof CustomServerRef)) {
        TestLibrary.bomb("test failed: reference not " + "instanceof CustomServerRef");
      }

      String name = "//:" + port + "/UseCustomRef";
      //      String name = "UseCustomRef";
      System.err.println("binding object in registry...");
      Naming.rebind(name, cr);

      /*
       * look up object and invoke its ping method
       */
      System.err.println("ping object...");
      obj = (Ping) Naming.lookup(name);
      obj.ping();

      /*
       * pass object with custom ref in remote call
       */
      System.err.println("pass object in remote call...");
      obj.receiveAndPing(cr);

      /*
       * write remote object with custom ref to output stream
       */
      System.err.println("writing remote object to stream...");
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      ObjectOutputStream out = new ObjectOutputStream(bout);
      out.writeObject(cr);
      out.flush();
      out.close();

      /*
       * read back remote object from output stream
       */
      System.err.println("reading remote object from stream...");
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
      cr = (UseCustomRef) in.readObject();

      /*
       * re-export object and ping
       */
      System.err.println("re-export object read...");
      cr.exportObject();
      System.err.println("look up object again...");
      Naming.rebind(name, cr);
      System.err.println("ping object read...");
      obj = (Ping) Naming.lookup(name);
      obj.ping();
      System.err.println("TEST PASSED");
      Naming.unbind(name);
      cr = null;

    } catch (Exception e) {
      TestLibrary.bomb("test failed with exception: ", e);
    } finally {
      TestLibrary.unexport(obj);
      TestLibrary.unexport(registry);

      registry = null;
      obj = null;
    }
  }
コード例 #2
0
  /**
   * Entry point for the "juicer" server process. Create and export an apple user implementation in
   * an rmiregistry running on localhost.
   */
  public static void main(String[] args) {
    String durationString = null;
    boolean othervm = false;
    boolean exit = false;
    try {
      // parse command line args
      for (int i = 0; i < args.length; i++) {
        String arg = args[i];
        if (arg.equals("-hours")) {
          if (durationString != null) {
            usage();
          }
          i++;
          int hours = Integer.parseInt(args[i]);
          durationString = hours + " hours";
          testDuration = hours * 60 * 60 * 1000;
        } else if (arg.equals("-seconds")) {
          if (durationString != null) {
            usage();
          }
          i++;
          long seconds = Long.parseLong(args[i]);
          durationString = seconds + " seconds";
          testDuration = seconds * 1000;
        } else if (arg.equals("-maxLevel")) {
          i++;
          maxLevel = Integer.parseInt(args[i]);
        } else if (arg.equals("-othervm")) {
          othervm = true;
        } else if (arg.equals("-exit")) {
          exit = true;
        } else {
          usage();
        }
      }
      if (durationString == null) {
        durationString = testDuration + " milliseconds";
      }
    } catch (Throwable t) {
      usage();
      throw new RuntimeException("TEST FAILED: Bad argument");
    }

    AppleUserImpl user = null;
    long startTime = 0;
    Thread server = null;
    int exitValue = 0;
    try {
      user = new AppleUserImpl();

      synchronized (user) {
        // create new registry and bind new AppleUserImpl in registry
        Registry registry = TestLibrary.createRegistryOnUnusedPort();
        registryPort = TestLibrary.getRegistryPort(registry);
        LocateRegistry.getRegistry(registryPort).rebind("AppleUser", user);

        // start the other server if applicable
        if (othervm) {
          // the other server must be running in a separate process
          logger.log(Level.INFO, "Application server must be " + "started in separate process");
        } else {
          Class app = Class.forName("ApplicationServer");
          java.lang.reflect.Constructor appConstructor =
              app.getDeclaredConstructor(new Class[] {Integer.TYPE});
          server = new Thread((Runnable) appConstructor.newInstance(registryPort));
          logger.log(Level.INFO, "Starting application server " + "in same process");
          server.start();
        }

        // wait for other server to call startTest method
        logger.log(Level.INFO, "Waiting for application server " + "process to start");
        while (!startTestNotified) {
          user.wait();
        }
      }

      startTime = System.currentTimeMillis();
      logger.log(Level.INFO, "Test starting");

      // wait for exception to be reported or first thread to complete
      logger.log(
          Level.INFO,
          "Waiting " + durationString + " for " + "test to complete or exception to be thrown");

      synchronized (lock) {
        while (status == null && !finished) {
          lock.wait();
        }
      }

      if (status != null) {
        throw new RuntimeException("TEST FAILED: " + "juicer server reported an exception", status);
      } else {
        logger.log(Level.INFO, "TEST PASSED");
      }
    } catch (Exception e) {
      logger.log(Level.INFO, "TEST FAILED");
      exitValue = 1;
      if (exit) {
        e.printStackTrace();
      }
      throw new RuntimeException("TEST FAILED: " + "unexpected exception", e);
    } finally {
      long actualDuration = System.currentTimeMillis() - startTime;
      logger.log(Level.INFO, "Test finished");
      try {
        UnicastRemoteObject.unexportObject(user, true);
      } catch (NoSuchObjectException ignore) {
      }
      logger.log(
          Level.INFO,
          "Test duration was "
              + (actualDuration / 1000)
              + " seconds "
              + "("
              + (actualDuration / 3600000)
              + " hours)");
      System.gc();
      System.gc();
      if (exit) {
        System.exit(exitValue);
      }
    }
  }