FileDocCategorySizeDatePackage
AxisServletBase.javaAPI DocApache Axis 1.413524Sat Apr 22 18:57:26 BST 2006org.apache.axis.transport.http

AxisServletBase

public class AxisServletBase extends HttpServlet
Base class for servlets used in axis, has common methods to get and save the engine to a common location, currently the webapp's context, though some alternate persistence mechanism is always possible. Also has a load counter shared by all servlets; tracks the # of active http requests open to any of the subclasses.
author
Steve Loughran

Fields Summary
protected org.apache.axis.server.AxisServer
axisServer
per-instance cache of the axis server
private static Log
log
private static boolean
isDebug
private static int
loadCounter
count number of service requests in progress
private static Object
loadCounterLock
and a lock
protected static final String
ATTR_AXIS_ENGINE
name of the axis engine to use in the servlet context
private String
webInfPath
Cached path to our WEB-INF directory
private String
homeDir
Cached path to our "root" dir
private boolean
isDevelopment
flag set to true for a 'production' server
private static final String
INIT_PROPERTY_DEVELOPMENT_SYSTEM
property name for a production server
Constructors Summary
Methods Summary
protected static voiddecLockCounter()
thread safe lock counter decrement

        synchronized(loadCounterLock) {
            loadCounter--;
        }
    
public voiddestroy()
Destroy method is called when the servlet is going away. Pass this down to the AxisEngine to let it clean up... But don't create the engine if it hasn't already been created.

todo
Fixme for multiple servlets. This has always been slightly broken (the context's copy stayed around), but now we have extracted it into a superclass it is blatantly broken.

        super.destroy();

        //if we have had anything to do with creating an axis server
        if (axisServer != null) {
            //then we lock it
            synchronized(axisServer) {
                if (axisServer != null) {
                    //clean it up
                    axisServer.cleanup();
                    //and erase our history of it
                    axisServer =null;
                    storeEngine(this,null);
                }
            }
        }
    
public org.apache.axis.server.AxisServergetEngine()
get the engine for this servlet from cache or context

return
throws
AxisFault

        if (axisServer == null)
            axisServer = getEngine(this);
        return axisServer;
    
public static org.apache.axis.server.AxisServergetEngine(javax.servlet.http.HttpServlet servlet)
This is a uniform method of initializing AxisServer in a servlet context.

todo
add catch for not being able to cast the context attr to an engine and reinit the engine if so.

        AxisServer engine = null;
        if (isDebug)
            log.debug("Enter: getEngine()");

        ServletContext context = servlet.getServletContext();
        synchronized (context) {
            engine = retrieveEngine(servlet);
            if (engine == null) {
                Map environment = getEngineEnvironment(servlet);

                // Obtain an AxisServer by using whatever AxisServerFactory is
                // registered.  The default one will just use the provider we
                // passed in, and presumably JNDI ones will use the ServletContext
                // to figure out a JNDI name to look up.
                //
                // The point of doing this rather than just creating the server
                // manually with the provider above is that we will then support
                // configurations where the server instance is managed by the
                // container, and pre-registered in JNDI at deployment time.  It
                // also means we put the standard configuration pattern in one
                // place.
                engine = AxisServer.getServer(environment);
//              attach the AxisServer with the current Servlet
                engine.setName(servlet.getServletName());
                storeEngine(servlet, engine);
            }
        }

        if (isDebug)
            log.debug("Exit: getEngine()");

        return engine;
    
protected static java.util.MapgetEngineEnvironment(javax.servlet.http.HttpServlet servlet)
extract information from the servlet configuration files

param
servlet
return

        Map environment = new HashMap();

        String attdir= servlet.getInitParameter(AxisEngine.ENV_ATTACHMENT_DIR);
        if (attdir != null)
            environment.put(AxisEngine.ENV_ATTACHMENT_DIR, attdir);

        ServletContext context = servlet.getServletContext();
        environment.put(AxisEngine.ENV_SERVLET_CONTEXT, context);

        String webInfPath = context.getRealPath("/WEB-INF");
        if (webInfPath != null)
            environment.put(AxisEngine.ENV_SERVLET_REALPATH,
                            webInfPath + File.separator + "attachments");

        EngineConfiguration config =
            EngineConfigurationFactoryFinder.newFactory(servlet)
                    .getServerEngineConfig();

        if (config != null) {
            environment.put(EngineConfiguration.PROPERTY_NAME, config);
        }

        return environment;
    
protected java.lang.StringgetHomeDir()
what is the root dir of the applet?

return
path of root dir

        return homeDir;
    
public static intgetLoadCounter()
get a count of the # of services running. This is only ever an approximate number in a busy system

return
The TotalServiceCount value

            return loadCounter;
    
protected java.lang.StringgetOption(javax.servlet.ServletContext context, java.lang.String param, java.lang.String dephault)
Retrieve option, in order of precedence: (Managed) System property (see discovery.ManagedProperty), servlet init param, context init param. Use of system properties is discouraged in production environments, as it overrides everything else.

        String value = AxisProperties.getProperty(param);

        if (value == null)
            value = getInitParameter(param);

        if (value == null)
            value = context.getInitParameter(param);
        try {
            AxisServer engine = getEngine(this);
            if (value == null && engine != null)
                value = (String) engine.getOption(param);
        } catch (AxisFault axisFault) {
        }

        return (value != null) ? value : dephault;
    
public javax.servlet.ServletContextgetServletContext()
what is the servlet context

return
get the context from the servlet config

        return getServletConfig().getServletContext();
    
protected java.lang.StringgetWebInfPath()
accessor to webinf

return
path to WEB-INF/ in the local filesystem

        return webInfPath;
    
protected java.lang.StringgetWebappBase(javax.servlet.http.HttpServletRequest request)
extract the base of our webapp from an inbound request

param
request request containing http://foobar/axis/services/something
return
some URL like http://foobar:8080/axis/

        StringBuffer baseURL=new StringBuffer(128);
        baseURL.append(request.getScheme());
        baseURL.append("://");
        baseURL.append(request.getServerName());
        if(request.getServerPort()!=80) {
            baseURL.append(":");
            baseURL.append(request.getServerPort());
        }
        baseURL.append(request.getContextPath());
        return baseURL.toString();
    
protected static voidincLockCounter()
thread safe lock counter increment

        synchronized(loadCounterLock) {
            loadCounter++;
        }
    
public voidinit()
our initialize routine; subclasses should call this if they override it



                    
         
        ServletContext context = getServletConfig().getServletContext();

        webInfPath = context.getRealPath("/WEB-INF");
        homeDir = context.getRealPath("/");

        isDebug = log.isDebugEnabled();
        if(log.isDebugEnabled()) log.debug("In AxisServletBase init");
        isDevelopment= JavaUtils.isTrueExplicitly(getOption(context,
                        INIT_PROPERTY_DEVELOPMENT_SYSTEM, null));

    
public booleanisDevelopment()
probe for the system being 'production'

return
true for a dev system.

        return isDevelopment;
    
private static org.apache.axis.server.AxisServerretrieveEngine(javax.servlet.http.HttpServlet servlet)
Get an engine from the servlet context; robust againt serialization issues of hot-updated webapps. Remember than if a webapp is marked as distributed, there is more than 1 servlet context, hence more than one AxisEngine instance

param
servlet
return
the engine or null if either the engine couldnt be found or the attribute wasnt of the right type

        Object contextObject = servlet.getServletContext().getAttribute(servlet.getServletName() + ATTR_AXIS_ENGINE);
        if (contextObject == null) {
            // if AxisServer not found :
            // fall back to the "default" AxisEngine
            contextObject = servlet.getServletContext().getAttribute(ATTR_AXIS_ENGINE);
        }
        if (contextObject instanceof AxisServer) {
            AxisServer server = (AxisServer) contextObject;
            // if this is "our" Engine
            if (server != null && servlet.getServletName().equals(server.getName())) {
                return server;
            }
            return null;
        } else {
            return null;
        }
     
protected voidservice(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
subclass of service method that tracks entry count; calls the parent's implementation to have the http method cracked and delegated to the doGet, doPost method.

param
req request
param
resp response
throws
ServletException something went wrong
throws
IOException something different went wrong

        incLockCounter();
        try {
            super.service(req, resp);
        }
        finally {
            decLockCounter();
        }
    
private static voidstoreEngine(javax.servlet.http.HttpServlet servlet, org.apache.axis.server.AxisServer engine)
put the engine back in to the context.

param
context servlet context to use
param
engine reference to the engine. If null, the engine is removed

        ServletContext context = servlet.getServletContext();
        String axisServletName = servlet.getServletName();
        if (engine == null) {
            context.removeAttribute(axisServletName + ATTR_AXIS_ENGINE);
            // find if there is other AxisEngine in Context
            AxisServer server = (AxisServer) context.getAttribute(ATTR_AXIS_ENGINE);

            // no other AxisEngine in ServletContext
            if (server != null && servlet.getServletName().equals(server.getName())) {
                context.removeAttribute(ATTR_AXIS_ENGINE);
            }
        } else {
            if (context.getAttribute(ATTR_AXIS_ENGINE) == null) {
                // first Axis servlet to store its AxisEngine
                // use default name
                context.setAttribute(ATTR_AXIS_ENGINE, engine);
            }
            context.setAttribute(axisServletName + ATTR_AXIS_ENGINE, engine);
        }