FileDocCategorySizeDatePackage
SimpleAxisServer.javaAPI DocApache Axis 1.411764Sat Apr 22 18:57:28 BST 2006org.apache.axis.transport.http

SimpleAxisServer

public class SimpleAxisServer extends Object implements Runnable
This is a simple implementation of an HTTP server for processing SOAP requests via Apache's xml-axis. This is not intended for production use. Its intended uses are for demos, debugging, and performance profiling. Note this classes uses static objects to provide a thread pool, so you should not use multiple instances of this class in the same JVM/classloader unless you want bad things to happen at shutdown.
author
Sam Ruby (ruby@us.ibm.com)
author
Rob Jellinghaus (robj@unrealities.com)
author
Alireza Taherkordi (a_taherkordi@users.sourceforge.net)

Fields Summary
protected static Log
log
private Map
sessions
private int
maxSessions
public static final int
MAX_SESSIONS_DEFAULT
private static org.apache.axis.components.threadpool.ThreadPool
pool
pool of threads
private static boolean
doThreads
Are we doing threads?
private static boolean
doSessions
public static int
sessionIndex
private static org.apache.axis.server.AxisServer
myAxisServer
private org.apache.axis.EngineConfiguration
myConfig
private boolean
stopped
are we stopped? latch to true if stop() is called
private ServerSocket
serverSocket
per thread socket information
Constructors Summary
public SimpleAxisServer()
create a server with the default threads and sessions.


                  
      
        this(ThreadPool.DEFAULT_MAX_THREADS);
    
public SimpleAxisServer(int maxPoolSize)
Create a server with a configurable pool side; sessions set to the default limit

param
maxPoolSize maximum thread pool size

        this(maxPoolSize, MAX_SESSIONS_DEFAULT);
    
public SimpleAxisServer(int maxPoolSize, int maxSessions)
Constructor

param
maxPoolSize max number of threads
param
maxSessions maximum sessions

        this.maxSessions = maxSessions;
        sessions = new LRUMap(maxSessions);
        pool = new ThreadPool(maxPoolSize);
    
Methods Summary
protected org.apache.axis.session.SessioncreateSession(java.lang.String cooky)
demand create a session if there is not already one for the string

param
cooky
return
a session.


        // is there a session already?
        Session session = null;
        if (sessions.containsKey(cooky)) {
            session = (Session) sessions.get(cooky);
        } else {
            // no session for this cooky, bummer
            session = new SimpleSession();

            // ADD CLEANUP LOGIC HERE if needed
            sessions.put(cooky, session);
        }
        return session;
    
protected voidfinalize()
stop the server if not already told to.

throws
Throwable

        stop();
        super.finalize();
    
public synchronized org.apache.axis.server.AxisServergetAxisServer()
demand create an axis server; return an existing one if one exists. The configuration for the axis server is derived from #myConfig if not null, the default config otherwise.

return


                                       
        
        if (myAxisServer == null) {
            if (myConfig == null) {
                myConfig = EngineConfigurationFactoryFinder.newFactory().getServerEngineConfig();
            }
            myAxisServer = new AxisServer(myConfig);
            ServiceAdmin.setEngine(myAxisServer, NetworkUtils.getLocalHostname() + "@" + serverSocket.getLocalPort());
        }
        return myAxisServer;
    
private java.lang.StringgetCurrentDirectory()
Gets the current directory

return
current directory

        return System.getProperty("user.dir");
    
public booleangetDoThreads()

        return doThreads ;
    
public intgetMaxSessions()
get max session count

return

        return maxSessions;
    
public org.apache.axis.EngineConfigurationgetMyConfig()

        return myConfig;
    
public static org.apache.axis.components.threadpool.ThreadPoolgetPool()
get the thread pool

return


              
        
        return pool;
    
public java.net.ServerSocketgetServerSocket()
Obtain the serverSocket that that SimpleAxisServer is listening on.

        return serverSocket;
    
protected booleanisSessionUsed()

        return doSessions;
    
public static voidmain(java.lang.String[] args)
Server process.


        Options opts = null;
        try {
            opts = new Options(args);
        } catch (MalformedURLException e) {
            log.error(Messages.getMessage("malformedURLException00"), e);
            return;
        }

        String maxPoolSize = opts.isValueSet('t");
        if (maxPoolSize==null) maxPoolSize = ThreadPool.DEFAULT_MAX_THREADS + "";

        String maxSessions = opts.isValueSet('m");
        if (maxSessions==null) maxSessions = MAX_SESSIONS_DEFAULT + "";

        SimpleAxisServer sas = new SimpleAxisServer(Integer.parseInt(maxPoolSize),
                                                        Integer.parseInt(maxSessions));

        try {
            doThreads = (opts.isFlagSet('t") > 0);

            int port = opts.getPort();
            ServerSocket ss = null;
            // Try five times
            final int retries = 5;
            for (int i = 0; i < retries; i++) {
                try {
                    ss = new ServerSocket(port);
                    break;
                } catch (java.net.BindException be){
                    log.debug(Messages.getMessage("exception00"), be);
                    if (i < (retries-1)) {
                        // At 3 second intervals.
                        Thread.sleep(3000);
                    } else {
                        throw new Exception(Messages.getMessage("unableToStartServer00",
                                           Integer.toString(port)));
                    }
                }
            }
            sas.setServerSocket(ss);
            sas.start();
        } catch (Exception e) {
            log.error(Messages.getMessage("exception00"), e);
            return;
        }
    
public voidrun()
Accept requests from a given TCP port and send them through the Axis engine for processing.


                         
       
        log.info(Messages.getMessage("start01", "SimpleAxisServer",
                new Integer(getServerSocket().getLocalPort()).toString(),getCurrentDirectory()));

        // Accept and process requests from the socket
        while (!stopped) {
            Socket socket = null;
            try {
                socket = serverSocket.accept();
            } catch (java.io.InterruptedIOException iie) {
            } catch (Exception e) {
                log.debug(Messages.getMessage("exception00"), e);
                break;
            }
            if (socket != null) {
                SimpleAxisWorker worker = new SimpleAxisWorker(this, socket);
                if (doThreads) {
                    pool.addWorker(worker);
                } else {
                    worker.run();
                }
            }
        }
        log.info(Messages.getMessage("quit00", "SimpleAxisServer"));
    
public voidsetDoThreads(boolean value)
turn threading on or off. This sets a static value

param
value

        doThreads = value ;
    
public voidsetMaxSessions(int maxSessions)
Resize the session map

param
maxSessions maximum sessions

        this.maxSessions = maxSessions;
        ((LRUMap)sessions).setMaximumSize(maxSessions);
    
public voidsetMyConfig(org.apache.axis.EngineConfiguration myConfig)

        this.myConfig = myConfig;
    
public voidsetServerSocket(java.net.ServerSocket serverSocket)
Set the serverSocket this server should listen on. (note : changing this will not affect a running server, but if you stop() and then start() the server, the new socket will be used).

        this.serverSocket = serverSocket;
    
public voidstart(boolean daemon)
Start this server. Spawns a worker thread to listen for HTTP requests.

param
daemon a boolean indicating if the thread should be a daemon.

        stopped=false;
        if (doThreads) {
            Thread thread = new Thread(this);
            thread.setDaemon(daemon);
            thread.start();
        } else {
            run();
        }
    
public voidstart()
Start this server as a NON-daemon.

        start(false);
    
public voidstop()
Stop this server. Can be called safely if the system is already stopped, or if it was never started. This will interrupt any pending accept().

        //recognise use before we are live
        if(stopped ) {
            return;
        }
        /*
         * Close the server socket cleanly, but avoid fresh accepts while
         * the socket is closing.
         */
        stopped = true;

        try {
            if(serverSocket != null) {
                serverSocket.close();
            }
        } catch (IOException e) {
            log.info(Messages.getMessage("exception00"), e);
        } finally {
            serverSocket=null;
        }

        log.info(Messages.getMessage("quit00", "SimpleAxisServer"));

        //shut down the pool
        pool.shutdown();