[jboss-dev-forums] [Design of EJB 3.0] - Re: EJBTHREE-1396 MockServer must report startup / shutdown

jaikiran do-not-reply at jboss.com
Sun Nov 16 06:37:20 EST 2008


Here's how the code looks now. Let me know if you would want a "diff". 

RemoteAccessTestCase

package org.jboss.ejb3.test.proxy.remoteaccess.unit;
  | 
  | import static org.junit.Assert.assertEquals;
  | import static org.junit.Assert.assertTrue;
  | 
  | import javax.naming.Context;
  | import javax.naming.InitialContext;
  | 
  | import org.jboss.ejb3.test.proxy.common.ejb.sfsb.MyStatefulRemoteBusiness;
  | import org.jboss.ejb3.test.proxy.common.ejb.slsb.MyStatelessRemote;
  | import org.jboss.ejb3.test.proxy.remoteaccess.JndiPropertiesToJndiRemotePropertiesHackCl;
  | import org.jboss.ejb3.test.proxy.remoteaccess.MockServerController;
  | import org.jboss.logging.Logger;
  | import org.junit.AfterClass;
  | import org.junit.BeforeClass;
  | import org.junit.Test;
  | 
  | /**
  |  * RemoteAccessTestCase
  |  *
  |  * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
  |  * @version $Revision: $
  |  */
  | 
  | public class RemoteAccessTestCase
  | {
  |    // --------------------------------------------------------------------------------||
  |    // Class Members ------------------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    private static final Logger log = Logger.getLogger(RemoteAccessTestCase.class);
  | 
  |    private static final String JNDI_NAME_SLSB_LOCAL = "MyStatelessBean/local";
  | 
  |    private static final String JNDI_NAME_SLSB_REMOTE = "MyStatelessBean/remote";
  | 
  |    private static final String JNDI_NAME_SFSB_REMOTE = "MyStatefulBean/remote";
  | 
  |    private static MockServerController mockServerController;
  | 
  |    /**
  |     * The server host on which the MockServer will be available for requests
  |     */
  |    private static final String serverHost = "localhost";
  | 
  |    /**
  |     * The server port on which the MockServer will be available for requests
  |     */
  |    private static final int serverPort = 12345;
  | 
  |    private static Context context;
  | 
  |    // --------------------------------------------------------------------------------||
  |    // Instance Members ---------------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    // --------------------------------------------------------------------------------||
  |    // Tests --------------------------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    /**
  |     * Ensures that a SLSB Remote invocation succeeds
  |     */
  |    @Test
  |    public void testStatelessSessionRemoteInvocation() throws Throwable
  |    {
  |      ..... // removed while posting
  |    }
  | 
  |    /**
  |     * Ensures that more than one SLSB Remote invocations succeed
  |     */
  |    @Test
  |    public void testStatelessSessionDuplicateRemoteInvocations() throws Throwable
  |    {
  |      ..... // removed while posting
  |    }
  | 
  |    /**
  |     * Ensures that a SFSB Remote invocation succeeds
  |     */
  |    @Test
  |    public void testStatefulSessionRemoteInvocation() throws Throwable
  |    {
  |       ..... // removed while posting
  |       
  |    }
  | 
  |    /**
  |     * Ensures that more than one SFSB Remote invocations succeed
  |     */
  |    @Test
  |    public void testStatefulSessionDuplicateRemoteInvocation() throws Throwable
  |    {
  |       ..... // removed while posting
  |    }
  | 
  |    // --------------------------------------------------------------------------------||
  |    // Lifecycle Methods --------------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    /**
  |     * Starts the MockServer
  |     */
  |    @BeforeClass
  |    public static void beforeClass() throws Throwable
  |    {
  |       // Switch up to the hacky CL so that "jndi.properties" is not loaded, and uses instead "jndi-remote.properties"
  |       ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
  |       Thread.currentThread().setContextClassLoader(new JndiPropertiesToJndiRemotePropertiesHackCl());
  | 
  |       RemoteAccessTestCase.setContext(new InitialContext());
  | 
  |       // Replace the CL
  |       Thread.currentThread().setContextClassLoader(oldLoader);
  | 
  |       // create a controller for mockserver
  |       mockServerController = new MockServerController(serverHost, serverPort);
  | 
  |       // Start Server
  |       long start = System.currentTimeMillis();
  |       mockServerController.startServer(new String[]{RemoteAccessTestCase.class.getName()});
  |       long end = System.currentTimeMillis();
  |       log.info("MockServer started in " + (end - start) + " milli sec.");
  | 
  |    }
  | 
  |    /**
  |     * Stops the MockServer
  |     * 
  |     * @throws Throwable
  |     */
  |    @AfterClass
  |    public static void afterClass() throws Throwable
  |    {
  |       mockServerController.stopServer();
  | 
  |    }
  | 
  |    // --------------------------------------------------------------------------------||
  |    // Accessors / Mutators -----------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    public static Context getContext()
  |    {
  |       return context;
  |    }
  | 
  |    protected static void setContext(Context context)
  |    {
  |       RemoteAccessTestCase.context = context;
  |    }
  | 
  | }
  | 
  | 

MockServerController

package org.jboss.ejb3.test.proxy.remoteaccess;
  | 
  | import java.io.BufferedReader;
  | import java.io.File;
  | import java.io.FileReader;
  | import java.util.HashMap;
  | import java.util.Map;
  | 
  | import org.jboss.ejb3.common.thread.RedirectProcessOutputToSystemOutThread;
  | import org.jboss.ejb3.test.proxy.remoteaccess.MockServer.MockServerRequest;
  | import org.jboss.logging.Logger;
  | import org.jboss.remoting.Client;
  | import org.jboss.remoting.InvokerLocator;
  | 
  | /**
  |  * MockServerController
  |  * 
  |  * Controls the startup/shutdown of the {@link MockServer} <br/>
  |  * 
  |  * @author Jaikiran Pai
  |  * @version $Revision: $
  |  */
  | public class MockServerController
  | {
  | 
  |    /**
  |     * Instance of logger
  |     */
  |    private static Logger logger = Logger.getLogger(MockServerController.class);
  | 
  |    private static final String ENV_VAR_JAVAHOME = "JAVA_HOME";
  | 
  |    private static final String EXECUTABLE_JAVA = "bin" + File.separator + "java";
  | 
  |    private static final String LOCATION_BASEDIR = System.getProperty("basedir");
  | 
  |    private static final String LOCATION_TARGET = MockServerController.LOCATION_BASEDIR + File.separator + "target";
  | 
  |    private static final String LOCATION_TEST_CLASSES = MockServerController.LOCATION_TARGET + File.separator
  |          + "tests-classes";
  | 
  |    private static final String LOCATION_CLASSES = MockServerController.LOCATION_TARGET + File.separator + "classes";
  | 
  |    private static final String LOCATION_CONF = MockServerController.LOCATION_BASEDIR + File.separator + "conf";
  | 
  |    private static final String FILENAME_DEPENDENCY_CP = MockServerController.LOCATION_TARGET + File.separator
  |          + "cp.txt";
  | 
  |    /**
  |     * Timeout in milli sec. for server startup/shutdown
  |     */
  |    private static final int TIMEOUT = 120000;
  | 
  |    /**
  |     * The port number on which the {@link MockServer}
  |     * is available for requests
  |     */
  |    private int port;
  | 
  |    /**
  |     * The host on which the {@link MockServer}
  |     * is available for requests
  |     */
  |    private String serverHost;
  | 
  |    /**
  |     * Remote process in which the {@link MockServer} will run
  |     */
  |    private Process remoteProcess;
  | 
  |    /**
  |     * {@link Client} for sending requests to the {@link MockServer}
  |     */
  |    private Client mockServerClient;
  | 
  |    /**
  |     * Constructor <br>
  |     * Creates a {@link Client} to send requests to the remote {@link MockServer}
  |     *   
  |     * @param host The host on which the {@link MockServer} is available
  |     * @param port The port on which the {@link MockServer} is listening
  |     */
  |    public MockServerController(String host, int port)
  |    {
  |       this.serverHost = host;
  |       this.port = port;
  |       String uri = null;
  |       try
  |       {
  |          uri = "socket://" + this.serverHost + ":" + this.port;
  |          InvokerLocator invokerLocator = new InvokerLocator(uri);
  |          this.mockServerClient = new Client(invokerLocator);
  | 
  |       }
  |       catch (Exception e)
  |       {
  |          throw new RuntimeException("Could not create server controller: ", e);
  |       }
  | 
  |    }
  | 
  |    /**
  |     * Creates a remote process (JVM) to launch the {@link MockServer}
  |     * and then sends a {@link MockServerRequest#START} request to start the
  |     * server
  |     *   
  |     * @param arguments The arguments that will be passed to the {@link MockServer} 
  |     *       as JVM program arguments
  |     *       
  |     * @throws Throwable
  |     */
  |    public void startServer(String[] arguments) throws Throwable
  |    {
  |       // Along with the arguments that the client passes to the server,
  |       // append the the serverHost and port number on which the mockserver is
  |       // expected to listen
  |       int numberOfArgs = arguments.length;
  |       String[] processArgs = new String[numberOfArgs + 2];
  |       System.arraycopy(arguments, 0, processArgs, 0, numberOfArgs);
  |       // now append the server host and port
  |       processArgs[processArgs.length - 2] = this.serverHost;
  |       processArgs[processArgs.length - 1] = String.valueOf(this.port);
  |       
  |       createRemoteProcess(processArgs);
  | 
  |       sendStartRequestToServer();
  |    }
  | 
  |    /**
  |     * Sends a {@link MockServerRequest#STOP} request to the server
  |     * and also kills the process in whic the server was running
  |     * 
  |     * @see MockServerController#stopServer(boolean)
  |     * @throws Throwable
  |     */
  |    public void stopServer() throws Throwable
  |    {
  |       stopServer(true);
  |       logger.debug("Stopped the server and killed the remote process");
  | 
  |    }
  | 
  |    /**
  |     * Sends a {@link MockServerRequest#STOP} request to the server 
  |     * and if the <code>killProcess</code> is true then it also kills
  |     * the process in which the server was running.
  |     * 
  |     * @param killProcess If true, kills the process in which the {@link MockServer}
  |     *           was running. Else, just sends a {@link MockServerRequest#STOP} request
  |     *           to the server.
  |     * @throws Throwable
  |     */
  |    public void stopServer(boolean killProcess) throws Throwable
  |    {
  |       try
  |       {
  |          sendStopRequestToServer();
  |          logger.debug("Stopped server");
  |          // disconnect the client
  |          this.mockServerClient.disconnect();
  | 
  |       }
  |       finally
  |       {
  |          if (killProcess)
  |          {
  |             // destroy the remote process
  |             this.remoteProcess.destroy();
  |             logger.debug("Remote process killed");
  |          }
  |       }
  |    }
  | 
  |    /**
  |     * Sends a {@link MockServerRequest#STOP} to the server
  |     * 
  |     * @throws Throwable
  |     */
  |    protected void sendStopRequestToServer() throws Throwable
  |    {
  |       this.mockServerClient.connect();
  |       // set a timeout - The client will wait for this amount of time 
  |       // for the mockserver to shutdown
  |       Map configParams = new HashMap();
  |       configParams.put("timeout", String.valueOf(TIMEOUT));
  |       Object serverStatus = this.mockServerClient.invoke(MockServerRequest.STOP, configParams);
  |       logger.debug("Stop request returned Status = " + serverStatus);
  | 
  |    }
  | 
  |    /**
  |     * Sends a {@link MockServerRequest#START} to the server
  |     * @throws Throwable
  |     */
  |    protected void sendStartRequestToServer() throws Throwable
  |    {
  |       this.mockServerClient.connect();
  |       // set a timeout - The client will wait for this amount of time 
  |       // for the mockserver to shutdown
  |       Map configParams = new HashMap();
  |       configParams.put("timeout", String.valueOf(TIMEOUT));
  |       Object serverStatus = this.mockServerClient.invoke(MockServerRequest.START, configParams);
  |       logger.info("Server started. Status = " + serverStatus);
  | 
  |    }
  | 
  |    /**
  |     * Creates a new JVM process in which the {@link MockServer} will be active
  |     * 
  |     * 
  |     * @param arguments The arguments to the passed to the {@link MockServer}
  |     * 
  |     * @throws Throwable
  |     */
  |    private void createRemoteProcess(String arguments[]) throws Throwable
  |    {
  |       // Get the current System Properties and Environment Variables
  |       String javaHome = System.getenv(MockServerController.ENV_VAR_JAVAHOME);
  |       String conf = MockServerController.LOCATION_CONF;
  |       String testClasses = MockServerController.LOCATION_TEST_CLASSES;
  |       String classes = MockServerController.LOCATION_CLASSES;
  | 
  |       // Get the contents of the dependency classpath file
  |       String dependencyClasspathFilename = MockServerController.FILENAME_DEPENDENCY_CP;
  |       File dependencyClasspath = new File(dependencyClasspathFilename);
  |       assert dependencyClasspath.exists() : "File " + dependencyClasspathFilename
  |             + " is required to denote the dependency CP";
  |       BufferedReader reader = new BufferedReader(new FileReader(dependencyClasspath));
  |       StringBuffer contents = new StringBuffer();
  |       String line = null;
  |       while ((line = reader.readLine()) != null)
  |       {
  |          contents.append(line);
  |          contents.append(System.getProperty("line.separator"));
  |       }
  |       String depCp = contents.toString().trim();
  | 
  |       // Build the command
  |       StringBuffer command = new StringBuffer();
  |       command.append(javaHome); // JAVA_HOME
  |       command.append(File.separatorChar);
  |       command.append(MockServerController.EXECUTABLE_JAVA);
  |       command.append(" -cp "); // Classpath
  |       command.append("\"");
  | 
  |       command.append(classes);
  |       command.append(File.pathSeparatorChar);
  |       command.append(testClasses);
  |       command.append(File.pathSeparatorChar);
  |       command.append(conf);
  |       command.append(File.pathSeparatorChar);
  |       command.append(depCp); // Dependency CP
  |       command.append("\"");
  | 
  |       command.append(" -ea "); // Enable Assertions
  |       command.append(MockServer.class.getName());
  |       command.append(' ');
  |       for (int i = 0; i < arguments.length; i++)
  |       {
  |          command.append(arguments); // Argument
  |          command.append(' ');
  |       }
  | 
  |       // Create a Remote Launcher
  |       String cmd = command.toString();
  |       String[] cmds = cmd.split(" ");
  |       ProcessBuilder builder = new ProcessBuilder();
  |       builder.command(cmds);
  |       builder.redirectErrorStream(true);
  |       File pwd = new File(MockServerController.LOCATION_BASEDIR);
  |       assert pwd.exists() : "Present working directory for execution of remote process, " + pwd.getAbsolutePath()
  |             + ", could not be found.";
  |       logger.debug("Remote Process working directory: " + pwd.getAbsolutePath());
  |       builder.directory(pwd);
  |       logger.info("Launching in separate process: " + cmd);
  |       try
  |       {
  |          this.remoteProcess = builder.start();
  |          logger.info("Remote process = " + this.remoteProcess);
  |          // Redirect output from the separate process
  |          new RedirectProcessOutputToSystemOutThread(this.remoteProcess).start();
  | 
  |       }
  |       catch (Throwable t)
  |       {
  |          throw new RuntimeException("Could not execute remote process", t);
  |       }
  | 
  |    }
  | 
  | }
  | 
  | 
  | 

MockServer

package org.jboss.ejb3.test.proxy.remoteaccess;
  | 
  | import java.net.URL;
  | 
  | import org.jboss.aop.AspectManager;
  | import org.jboss.aop.AspectXmlLoader;
  | import org.jboss.ejb3.common.registrar.plugin.mc.Ejb3McRegistrar;
  | import org.jboss.ejb3.common.registrar.spi.Ejb3RegistrarLocator;
  | import org.jboss.ejb3.test.mc.bootstrap.EmbeddedTestMcBootstrap;
  | import org.jboss.ejb3.test.proxy.common.Utils;
  | import org.jboss.ejb3.test.proxy.common.container.StatefulContainer;
  | import org.jboss.ejb3.test.proxy.common.container.StatelessContainer;
  | import org.jboss.ejb3.test.proxy.common.ejb.sfsb.MyStatefulBean;
  | import org.jboss.ejb3.test.proxy.common.ejb.slsb.MyStatelessBean;
  | import org.jboss.logging.Logger;
  | import org.jboss.remoting.InvokerLocator;
  | import org.jboss.remoting.ServerInvocationHandler;
  | import org.jboss.remoting.transport.Connector;
  | 
  | /**
  |  * MockServer
  |  * 
  |  * Launches a new MC Bootstrap, EJB Containers, and performs
  |  * all initialization to mock a remote server environment
  |  *
  |  * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
  |  * @version $Revision: $
  |  */
  | public class MockServer
  | {
  | 
  |    // --------------------------------------------------------------------------------||
  |    // Class Members ------------------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    private static final Logger log = Logger.getLogger(MockServer.class);
  |    
  |    /**
  |     * Invocation request to the MockServer will be handler by this
  |     * invocation handler
  |     */
  |    private ServerInvocationHandler mockServerInvocationHandler;
  |    
  |    private static final String FILENAME_EJB3_INTERCEPTORS_AOP = "ejb3-interceptors-aop.xml";
  |    
  |    /**
  |     * Various possible server status
  |     */
  |    public enum MockServerStatus
  |    {
  |       STARTED, STOPPED
  |    }
  |    
  |    /**
  |     * 
  |     * Various possible server requests
  |     */
  |    public enum MockServerRequest
  |    {
  |       START, STOP
  |    }
  | 
  |    // --------------------------------------------------------------------------------||
  |    // Instance Members ---------------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    private EmbeddedTestMcBootstrap bootstrap;
  |    
  |    /**
  |     * Accept requests from client using this {@link Connector} 
  |     */
  |    private Connector remoteConnector;
  |    
  |    /**
  |     * The current state of the server 
  |     */
  |    private MockServerStatus currentStatus = MockServerStatus.STOPPED;
  | 
  |    /**
  |     * The Test Class using this launcher
  |     */
  |    private Class<?> testClass;
  | 
  |    // --------------------------------------------------------------------------------||
  |    // Constructor --------------------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    /**
  |     * Constructor
  |     * Configures and creates a socket based {@link Connector} which will
  |     * accept (start/stop) requests from client 
  |     */
  |    public MockServer(Class<?> testClass, String serverHost, int port)
  |    {
  |       this.setTestClass(testClass);
  |       String uri = "socket://" + serverHost + ":" + port;
  |       try
  |       {
  |          InvokerLocator invokerLocator = new InvokerLocator(uri);
  |          
  |          this.remoteConnector = new Connector(invokerLocator);
  |          this.remoteConnector.create();
  |          this.mockServerInvocationHandler = new MockServerInvocationHandler(this);
  |          this.remoteConnector.addInvocationHandler("EJB3Test", this.mockServerInvocationHandler);
  |          
  |       }
  |       catch (Exception e)
  |       {
  |          throw new RuntimeException("Could not start server at " + uri, e);
  |       } 
  |       
  |    }
  | 
  |    // --------------------------------------------------------------------------------||
  |    // Main ---------------------------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    /**
  |     * Runtime Entry Point
  |     * 
  |     * @param args
  |     */
  |    public static void main(String... args)
  |    {
  | 
  |       // Assert test class passed in
  |       assert args.length > 2 : "Parameters requried (in that order): <Fully qualified test case name> <serverBindAddress> <serverPort> ";
  | 
  |       // Get Test Class
  |       String testClassname = args[0];
  |       Class<?> testClass = null;
  |       try
  |       {
  |          testClass = Class.forName(testClassname);
  |       }
  |       catch (ClassNotFoundException cnfe)
  |       {
  |          throw new RuntimeException("Specified Test Class, \"" + testClassname + "\" could not be found", cnfe);
  |       }
  | 
  |       // Create a new Launcher
  |       // the serverBindAddress and the port are always the last two arguments
  |       MockServer launcher = new MockServer(testClass,args[args.length - 2],Integer.parseInt(args[args.length -1]));
  |       try
  |       {
  |          // Ready to receive (start/stop) requests
  |          launcher.acceptRequests();
  |       }
  |       catch (Throwable e)
  |       {
  |          throw new RuntimeException("Exception while waiting for requests ",e);
  |       }
  |       
  |    }
  | 
  |    // --------------------------------------------------------------------------------||
  |    // Functional Methods -------------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    /**
  |     * Initializes the instance by starting up an MC Bootstrap, 
  |     * deploying relevant *-beans.xml, creating and installing EJB Containers
  |     */
  |    protected void initialize() throws Throwable
  |    {
  |       // Create and set a new MC Bootstrap 
  |       this.setBootstrap(EmbeddedTestMcBootstrap.createEmbeddedMcBootstrap());
  | 
  |       // Add a Shutdown Hook
  |       //Runtime.getRuntime().addShutdownHook(new ShutdownHook());
  | 
  |       // Bind the Ejb3Registrar
  |       Ejb3RegistrarLocator.bindRegistrar(new Ejb3McRegistrar(bootstrap.getKernel()));
  | 
  |       // Switch up to the hacky CL so that "jndi.properties" is not loaded
  |       ClassLoader olderLoader = Thread.currentThread().getContextClassLoader();
  |       Thread.currentThread().setContextClassLoader(new JndiPropertiesToJnpserverPropertiesHackCl());
  | 
  |       // Deploy *-beans.xml
  |       this.getBootstrap().deploy(this.getTestClass());
  |       
  |       // Load ejb3-interceptors-aop.xml into AspectManager
  |       ClassLoader cl = Thread.currentThread().getContextClassLoader();
  |       URL url = cl.getResource(FILENAME_EJB3_INTERCEPTORS_AOP);
  |       if (url == null)
  |       {
  |          throw new RuntimeException("Could not load " + AspectManager.class.getSimpleName()
  |                + " with definitions from XML as file " + FILENAME_EJB3_INTERCEPTORS_AOP + " could not be found");
  |       }
  |       AspectXmlLoader.deployXML(url);
  | 
  |       // Restore old CL
  |       Thread.currentThread().setContextClassLoader(olderLoader);
  | 
  |       // Create a SLSB Container
  |       StatelessContainer slsbContainer = Utils.createSlsb(MyStatelessBean.class);
  |       log.info("Created SLSB Container: " + slsbContainer.getName());
  | 
  |       // Create a SFSB Container
  |       StatefulContainer sfsbContainer = Utils.createSfsb(MyStatefulBean.class);
  |       log.info("Created SFSB Container: " + sfsbContainer.getName());
  | 
  |       // Install into MC
  |       this.getBootstrap().installInstance(slsbContainer.getName(), slsbContainer);
  |       this.getBootstrap().installInstance(sfsbContainer.getName(), sfsbContainer);
  |       
  |    }
  |    
  |    /**
  |     * Starts the server <br>
  |     * 
  |     * @throws IllegalStateException If the server is not in {@link MockServerStatus.STOPPED}
  |     *           state 
  |     * @throws Throwable
  |     */
  |    public void start() throws Throwable
  |    {
  |       // Server will be started only if current state is STOPPED
  |       if (!this.currentStatus.equals(MockServerStatus.STOPPED))
  |       {
  |          throw new IllegalStateException("Cannot start MockServer when its in " + getStatus() + " state");
  |       }
  |       initialize();
  |       this.currentStatus = MockServerStatus.STARTED;
  |       log.info("MockServer started");
  |    }
  |    
  |    /**
  |     * Stops the server <br>
  |     * 
  |     * @throws IllegalStateException If the server is not in {@link MockServerStatus.STARTED} 
  |     *           state
  |     */
  |    public void stop()
  |    {
  |       // Server will be stopped only if current state is STARTED
  |       if (!this.currentStatus.equals(MockServerStatus.STARTED))
  |       {
  |          throw new IllegalStateException("Cannot stop MockServer when its in " + getStatus() + " state");
  |       }
  |       this.bootstrap.shutdown();
  |       this.currentStatus = MockServerStatus.STOPPED;
  |       log.info("MockServer stopped");
  |       
  |       // Note: Do not stop the Connector which is waiting for clients to 
  |       // connect. Letting the Connector remain in waiting state will allow
  |       // clients to restart this MockServer by sending the MockServerRequest.START
  |       // request again.
  |    }
  |    
  |    /**
  |     * 
  |     * @return Returns the current status of the server
  |     */
  |    public MockServerStatus getStatus()
  |    {
  |       return this.currentStatus;
  |    }
  |    
  |    /**
  |     * Start accepting requests <br>
  |     * This is a blocking call and will wait for clients to connect
  |     * 
  |     * @see {@link Connector#start()}
  |     * @throws Throwable
  |     */
  |    protected void acceptRequests() throws Throwable
  |    {
  |       this.remoteConnector.start();
  |    }
  |    
  |    /**
  |     * 
  |     * @param serverInvocationHandler The {@link ServerInvocationHandler} to
  |     *   handle requests
  |     */
  |    protected void setInvocationHandler(ServerInvocationHandler serverInvocationHandler)
  |    {
  |       this.mockServerInvocationHandler = serverInvocationHandler;
  |       
  |    }
  |    
  |    // --------------------------------------------------------------------------------||
  |    // Accessors / Mutators -----------------------------------------------------------||
  |    // --------------------------------------------------------------------------------||
  | 
  |    public EmbeddedTestMcBootstrap getBootstrap()
  |    {
  |       return this.bootstrap;
  |    }
  | 
  |    public void setBootstrap(EmbeddedTestMcBootstrap bootstrap)
  |    {
  |       this.bootstrap = bootstrap;
  |    }
  | 
  |    public Class<?> getTestClass()
  |    {
  |       return testClass;
  |    }
  | 
  |    public void setTestClass(Class<?> testClass)
  |    {
  |       this.testClass = testClass;
  |    }
  |    
  | 
  | }
  | 
  | 

MockServerInvocationHandler

package org.jboss.ejb3.test.proxy.remoteaccess;
  | 
  | import javax.management.MBeanServer;
  | 
  | import org.jboss.ejb3.test.proxy.remoteaccess.MockServer.MockServerRequest;
  | import org.jboss.logging.Logger;
  | import org.jboss.remoting.InvocationRequest;
  | import org.jboss.remoting.ServerInvocationHandler;
  | import org.jboss.remoting.ServerInvoker;
  | import org.jboss.remoting.callback.InvokerCallbackHandler;
  | 
  | /**
  |  * MockServerInvocationHandler
  |  *
  |  * @author Jaikiran Pai
  |  * @version $Revision: $
  |  */
  | public class MockServerInvocationHandler implements ServerInvocationHandler
  | {
  | 
  |    private static Logger logger = Logger.getLogger(MockServerInvocationHandler.class);
  | 
  |    private MockServer mockServer;
  | 
  |    public MockServerInvocationHandler(MockServer mockServer)
  |    {
  |       this.mockServer = mockServer;
  |    }
  | 
  |    /**
  |     * @see org.jboss.remoting.ServerInvocationHandler#addListener(org.jboss.remoting.callback.InvokerCallbackHandler)
  |     */
  |    public void addListener(InvokerCallbackHandler callbackHandler)
  |    {
  |       // no asynchronous support required as of now. Implement later if required
  | 
  |    }
  | 
  |    /**
  |     * On receiving a {@link MockServerRequest} the invocation handler will
  |     * carry out appropriate operation on the {@link MockServer} <br>
  |     * 
  |     * Supported requests are <br/>
  |     * <li>
  |     *   <ul>
  |     *   {@link MockServerRequest.START} - On receiving this request, the invocation 
  |     *   handler will start the {@link MockServer}
  |     *   </ul>
  |     *   <ul>
  |     *   {@link MockServerRequest.STOP} - On receiving this request, the invocation 
  |     *   handler will stop the {@link MockServer}
  |     *   </ul>
  |     * </li>
  |     * @throws {@link IllegalArgumentException} If the <code>invocationRequest</code>
  |     *           is not supported
  |     * @see org.jboss.remoting.ServerInvocationHandler#invoke(org.jboss.remoting.InvocationRequest)
  |     */
  |    public Object invoke(InvocationRequest invocationRequest) throws Throwable
  |    {
  | 
  |       if (!(invocationRequest.getParameter() instanceof MockServerRequest))
  |       {
  |          throw new IllegalArgumentException("Unrecognized request type " + invocationRequest.getParameter());
  |       }
  |       MockServerRequest request = (MockServerRequest) invocationRequest.getParameter();
  |       logger.info("Received request: " + request);
  | 
  |       // The same invocation handler can be called by multiple threads.
  |       synchronized (this.mockServer)
  |       {
  |          if (request.equals(MockServerRequest.START))
  |          {
  |             this.mockServer.start();
  |          }
  |          else if (request.equals(MockServerRequest.STOP))
  |          {
  |             this.mockServer.stop();
  |          }
  |          else
  |          {
  |             throw new IllegalArgumentException("Unrecognized request " + invocationRequest.getParameter());
  |          }
  |       }
  | 
  |       logger.debug("Server in " + mockServer.getStatus() + " state");
  |       return mockServer.getStatus();
  |    }
  | 
  |    /**
  |     * @see org.jboss.remoting.ServerInvocationHandler#removeListener(org.jboss.remoting.callback.InvokerCallbackHandler)
  |     */
  |    public void removeListener(InvokerCallbackHandler callbackHandler)
  |    {
  |       // do nothing - Implement later if needed
  | 
  |    }
  | 
  |    /**
  |     * @see org.jboss.remoting.ServerInvocationHandler#setInvoker(org.jboss.remoting.ServerInvoker)
  |     */
  |    public void setInvoker(ServerInvoker invoker)
  |    {
  |       // do nothing - Implement later if needed
  | 
  |    }
  | 
  |    /**
  |     * @see org.jboss.remoting.ServerInvocationHandler#setMBeanServer(javax.management.MBeanServer)
  |     */
  |    public void setMBeanServer(MBeanServer server)
  |    {
  |       // do nothing - Implement later if needed
  | 
  |    }
  | 
  | }
  | 


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4189657#4189657

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4189657



More information about the jboss-dev-forums mailing list