FileDocCategorySizeDatePackage
ActivatableThisOrThatServerImpl.javaAPI DocExample2662Wed Apr 05 11:25:42 BST 2000None

ActivatableThisOrThatServerImpl.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

import java.rmi.activation.*;
import java.rmi.MarshalledObject;
import java.rmi.RemoteException;
import java.io.IOException;

/**
 * ActivatableThisOrThatServerImpl: A remote implementation of ThisOrThatServer
 *     which allows for automated (re)activation of the server object.
 *
 * Example 3-5, Java Enterprise in a Nutshell, 1st ed.
 * Author: Jim Farley
 */

public class ActivatableThisOrThatServerImpl
  extends Activatable implements ThisOrThatServer {
  
  // Name for server
  private String myName = "";

  // "Regular" constructor used to create a "pre-activated" server
  public ActivatableThisOrThatServerImpl(String name, String src, int port)
      throws RemoteException, ActivationException, IOException {
    // Register and export object (on random open port)
    super(src, new MarshalledObject(name), false, port);
    // Save name
    myName = name;

    System.out.println("Initialization constructor called.");
  }

  // Constructor called by the activation runtime to (re)activate
  // and export the server
  protected ActivatableThisOrThatServerImpl(ActivationID id,
                                            MarshalledObject arg)
      throws RemoteException {
    // Export this object with the given activation id, on random port
    super(id, 0);
    System.out.println("Activating a server");
    // Check incoming data passed in with activation request
    try {
      Object oarg = arg.get();
      if (oarg instanceof String) {
        myName = (String)oarg;
      }
      else {
        System.out.println("Unknown argument received on activation: " + oarg);
      }
    }
    catch(Exception e) {
      System.out.println("Error retrieving argument to activation");
    }

    System.out.println("(Re)activation constructor called.");
  }

  // Remotely-accessible methods

  public String doThis(String todo) throws RemoteException {
    String result = doSomething("this", todo);
    return result;
  }

  public String doThat(String todo) throws RemoteException {
    String result = doSomething("that", todo);
    return result;
  }

  // Non-remote methods

  private String doSomething(String what, String todo) {
    String result = myName + ": " + what + " " + todo + " is done.";
    return result;
  }
}