FileDocCategorySizeDatePackage
StillActivatableServerImpl.javaAPI DocExample2262Wed Apr 05 11:25:42 BST 2000None

StillActivatableServerImpl.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;

public class StillActivatableServerImpl implements ThisOrThatServer {
  // Name for server
  private String myName = "";

  // "Regular" constructor used to create a "pre-activated" server
  public StillActivatableServerImpl(String name, String src, int port)
      throws RemoteException, ActivationException, IOException {
    // Register and export object
    Activatable.exportObject(this, src, new MarshalledObject(name),
			     false, port);
    // Save name
    myName = name;
  		//{{INIT_CONTROLS
		//}}
}

  // Constructor called by the activation runtime to both activate
  // and export the server
  public StillActivatableServerImpl(ActivationID id,
				    MarshalledObject arg)
      throws RemoteException {
    // Export this object with the given activation id, on random port
    Activatable.exportObject(this, id, 0);
    // 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");
    }
  }

  // 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;
  }
	//{{DECLARE_CONTROLS
	//}}
}