FileDocCategorySizeDatePackage
WebModule.javaAPI DocGlassfish v2 API30518Tue Jul 24 05:01:34 BST 2007com.sun.enterprise.web

WebModule

public class WebModule extends PwcWebModule
Class representing a web module for use by the Application Server.

Fields Summary
private static final Logger
logger
protected static final ResourceBundle
_rb
private static final String
ALTERNATE_FROM
private static final String
ALTERNATE_DOCBASE
private com.sun.enterprise.deployment.runtime.web.SunWebApp
iasBean
private com.sun.enterprise.deployment.runtime.web.LocaleCharsetMap[]
_lcMap
private boolean
hasBeenXmlConfigured
Is the default-web.xml parsed?
private WebContainer
webContainer
private final HashMap
adHocPaths
private boolean
hasAdHocPaths
private final HashMap
adHocSubtrees
private boolean
hasAdHocSubtrees
private org.apache.catalina.core.StandardPipeline
adHocPipeline
private String
fileEncoding
protected Object[]
cachedFinds
Cached findXXX results
private com.sun.enterprise.config.serverbeans.WebModule
bean
private com.sun.enterprise.deployment.WebBundleDescriptor
webBundleDescriptor
private boolean
started
Constructors Summary
public WebModule(WebContainer webContainer)
Constructor.

param
webContainer Web container on which this web module is deployed


    
                     
       

        this.webContainer = webContainer;

        this.adHocPaths = new HashMap<String,AdHocServletInfo>();
        this.adHocSubtrees = new HashMap<String,AdHocServletInfo>();

        this.adHocPipeline = new StandardPipeline(this);
        this.adHocPipeline.setBasic(new AdHocContextValve(this));

        notifyContainerListeners = false;
    
Methods Summary
voidaddAdHocPathAndSubtree(java.lang.String path, java.lang.String subtree, AdHocServletInfo servletInfo)


        if (path == null && subtree == null) {
            return;
        }

        Wrapper adHocWrapper = (Wrapper)
            findChild(servletInfo.getServletName());
        if (adHocWrapper == null) {
            adHocWrapper = createAdHocWrapper(servletInfo);
            addChild(adHocWrapper);
        }

        if (path != null) {
            adHocPaths.put(path, servletInfo);
            hasAdHocPaths = true;
        }

        if (subtree != null) {
            adHocSubtrees.put(subtree, servletInfo);
            hasAdHocSubtrees = true;
        }
    
voidaddAdHocPaths(java.util.HashMap newPaths)


        if (newPaths == null || newPaths.isEmpty()) {
            return;
        }

        Iterator<String> iter = newPaths.keySet().iterator();
        while (iter.hasNext()) {
            String adHocPath = iter.next();
            AdHocServletInfo servletInfo = (AdHocServletInfo)
                newPaths.get(adHocPath);
            Wrapper adHocWrapper = (Wrapper)
                findChild(servletInfo.getServletName());
            if (adHocWrapper == null) {
                adHocWrapper = createAdHocWrapper(servletInfo);
                addChild(adHocWrapper);
            }
            adHocPaths.put(adHocPath, servletInfo);
        }

        hasAdHocPaths = true;
    
voidaddAdHocSubtrees(java.util.HashMap newSubtrees)


        if (newSubtrees == null || newSubtrees.isEmpty()) {
            return;
        }

        Iterator<String> iter = newSubtrees.keySet().iterator();
        while (iter.hasNext()) {
            String adHocSubtree = iter.next();
            AdHocServletInfo servletInfo = (AdHocServletInfo)
                newSubtrees.get(adHocSubtree);
            Wrapper adHocWrapper = (Wrapper)
                findChild(servletInfo.getServletName());
            if (adHocWrapper == null) {
                adHocWrapper = createAdHocWrapper(servletInfo);
                addChild(adHocWrapper);
            }
            adHocSubtrees.put(adHocSubtree, servletInfo);
        }

        hasAdHocSubtrees = true;
    
public voidaddAdHocValve(org.apache.catalina.Valve valve)
Adds the given valve to this web module's ad-hoc pipeline.

param
valve The valve to add

        adHocPipeline.addValve(valve);
    
voidaddFilterMap(com.sun.enterprise.deployment.web.ServletFilterMapping sfm)
Configures this web module with the filter mappings specified in the deployment descriptor.

param
sfm The filter mappings of this web module as specified in the deployment descriptor


        FilterMaps filterMaps = new FilterMaps();

        filterMaps.setFilterName(sfm.getName());

        Set dispatchers = sfm.getDispatchers(); 
        if (dispatchers != null) {
            Iterator<String> iter = dispatchers.iterator();
            while (iter.hasNext()){
                filterMaps.setDispatcher(iter.next());
            }
        }
        
        List servletNames = sfm.getServletNames();
        if (servletNames != null) {
            Iterator<String> iter = servletNames.iterator();
            while (iter.hasNext()) {
                filterMaps.addServletName(iter.next());
	    }
        }

        List urlPatterns = sfm.getURLPatterns();
        if (urlPatterns != null) {
            Iterator<String> iter = urlPatterns.iterator();
            while (iter.hasNext()) {
                filterMaps.addURLPattern(iter.next());
	    }
        }

        addFilterMaps(filterMaps);
    
protected voidaddListener(java.lang.String listenerName)
Add a Catalina listener to a Container

param
listenerName the fully qualified class name of the listener.

        Object listener = loadInstance(listenerName);
        
        if ( listener == null ) return;

        if (listener instanceof ContainerListener) {
            addContainerListener((ContainerListener)listener);
        } else if (listener instanceof LifecycleListener ){
            addLifecycleListener((LifecycleListener)listener);
        } else if (listener instanceof InstanceListener){
            addInstanceListener(listenerName);            
        } else {
            logger.log(Level.SEVERE,"webcontainer.invalidListener"
                       + listenerName);
        }     
    
protected voidaddValve(java.lang.String valveName)
Add a Valve to a VirtualServer pipeline.

param
valveName the fully qualified class name of the Valve.

        Valve valve = (Valve)loadInstance(valveName);  
        
        if (valve == null) return;
        
        super.addValve(valve); 
    
protected voidconfigureCatalinaProperties()
Configure the WebModule

        String propName = null;
        String propValue = null;
        if (bean != null) {
            ElementProperty[] props = bean.getElementProperty();
            if (props != null) {
                for (int i=0; i< props.length; i++) {
                    propName = props[i].getName();
                    propValue = props[i].getValue();
                    configureCatalinaProperties(propName,propValue);
                }
            }
        }
        
        if (iasBean != null && iasBean.sizeWebProperty() > 0) {
            WebProperty[] wprops = iasBean.getWebProperty();
            for (int i = 0; i < wprops.length; i++) {
                propName = wprops[i].getAttributeValue("name");
                propValue = wprops[i].getAttributeValue("value");
                configureCatalinaProperties(propName,propValue);
            }
        }      
    
protected voidconfigureCatalinaProperties(java.lang.String propName, java.lang.String propValue)
Configure the WebModule
param
name the property name
param
value the property value

        if (propName == null || propValue == null) {
            logger.log(Level.WARNING,
                        "webcontainer.nullWebModuleProperty",
                        getName());
            return;
        }
        
        if (propName.startsWith("valve_")) {
            addValve(propValue);            
        } else if (propName.startsWith("listener_")) {
            addListener(propValue);   
        }           
    
private org.apache.catalina.WrappercreateAdHocWrapper(AdHocServletInfo servletInfo)
Creates an ad-hoc servlet wrapper from the given ad-hoc servlet info.

param
servletInfo Ad-hoc servlet info from which to generate ad-hoc servlet wrapper
return
The generated ad-hoc servlet wrapper


        Wrapper adHocWrapper = new StandardWrapper();
        adHocWrapper.setServletClass(servletInfo.getServletClass().getName());
        adHocWrapper.setName(servletInfo.getServletName());
        Map<String,String> initParams = servletInfo.getServletInitParams();
        if (initParams != null && !initParams.isEmpty()) {
            Iterator<String> iter = initParams.keySet().iterator();
            while (iter.hasNext()) {
                String paramName = iter.next();
                adHocWrapper.addInitParameter(
                    paramName,
                    initParams.get(paramName));
            }              
        }

        return adHocWrapper;
    
java.util.HashMapgetAdHocPaths()

        return adHocPaths;
    
public org.apache.catalina.PipelinegetAdHocPipeline()
Gets this web module's ad-hoc pipeline.

return
This web module's ad-hoc pipeline

        return adHocPipeline;
    
public java.lang.StringgetAdHocServletName(java.lang.String path)
Returns the name of the ad-hoc servlet responsible for servicing the given path.

param
path The path whose associated ad-hoc servlet is needed
return
The name of the ad-hoc servlet responsible for servicing the given path, or null if the given path does not represent an ad-hoc path


        if (!hasAdHocPaths() && !hasAdHocSubtrees()) {
            return null;
        }

        AdHocServletInfo servletInfo = null;

        // Check if given path matches any of the ad-hoc paths (exact match)
        if (path == null) {
            servletInfo = adHocPaths.get("");
        } else {
            servletInfo = adHocPaths.get(path);
        }

        // Check if given path starts with any of the ad-hoc subtree paths
        if (servletInfo == null && path != null && hasAdHocSubtrees()) {
            Iterator<String> iter = adHocSubtrees.keySet().iterator();
            while (iter.hasNext()) {
                String adHocSubtree = iter.next();
                if (path.startsWith(adHocSubtree)) {
                    servletInfo = adHocSubtrees.get(adHocSubtree);
                    break;
                }
            }
        }

        if (servletInfo != null) {
            return servletInfo.getServletName();
        } else {
            return null;
        }
    
java.util.HashMapgetAdHocSubtrees()

        return adHocSubtrees;
    
public com.sun.enterprise.config.serverbeans.WebModulegetBean()

        return bean;
    
public java.lang.Object[]getCachedFindOperation()
Return the cached result of doing findXX on this object NOTE: this method MUST be used only when loading/using the content of default-web.xml

        return cachedFinds;
    
public java.lang.StringgetFileEncoding()
Gets the file encoding of all static resources of this web module.

return
The file encoding of static resources of this web module

        return fileEncoding;
    
public com.sun.enterprise.deployment.runtime.web.SunWebAppgetIasWebAppConfigBean()
gets the sun-web.xml config bean

       return iasBean; 
    
public com.sun.enterprise.deployment.runtime.web.LocaleCharsetMap[]getLocaleCharsetMap()
return locale-charset-map

        return _lcMap;
    
public booleanhasAdHocPaths()
Indicates whether this web module contains any ad-hoc paths. An ad-hoc path is a servlet path that is mapped to a servlet not declared in the web module's deployment descriptor. A web module all of whose mappings are for ad-hoc paths is called an ad-hoc web module.

return
true if this web module contains any ad-hoc paths, false otherwise

        return this.hasAdHocPaths;
    
public booleanhasAdHocSubtrees()
Indicates whether this web module contains any ad-hoc subtrees.

return
true if this web module contains any ad-hoc subtrees, false otherwise

        return this.hasAdHocSubtrees;
    
public booleanhasBeenXmlConfigured()
Return true if the default=web.xml has been read for this module.

        return hasBeenXmlConfigured;
    
public booleanhasLocaleToCharsetMapping()
Returns true if this web module specifies a locale-charset-map in its sun-web.xml, false otherwise.

return
true if this web module specifies a locale-charset-map in its sun-web.xml, false otherwise

        LocaleCharsetMap[] locCharsetMap = getLocaleCharsetMap();
        return (locCharsetMap != null && locCharsetMap.length > 0);
    
private java.lang.ObjectloadInstance(java.lang.String className)

        try{
            WebappClassLoader loader = (WebappClassLoader)
                                                getLoader().getClassLoader();
            Class clazz = loader.loadClass(className);
            return clazz.newInstance();
        } catch (Throwable ex){
            String msg = _rb.getString("webcontainer.unableToLoadExtension");
            msg = MessageFormat.format(msg, new Object[] { className,
                                                           getName() });
            logger.log(Level.SEVERE, msg, ex);
        } 
        return null;
    
public java.lang.StringmapLocalesToCharset(java.util.Enumeration locales)
Matches the given request locales against the charsets specified in the locale-charset-map of this web module's sun-web.xml, and returns the first matching charset.

param
locales Request locales
return
First matching charset, or null if this web module does not specify any locale-charset-map in its sun-web.xml, or no match was found


        String encoding = null;

        LocaleCharsetMap[] locCharsetMap = getLocaleCharsetMap();
        if (locCharsetMap != null && locCharsetMap.length > 0) {
            /*
             * Check to see if there is a match between the request
             * locales (in preference order) and the locales in the
             * locale-charset-map.
             */
            boolean matchFound = false;
            while (locales.hasMoreElements() && !matchFound) {
                Locale reqLoc = (Locale) locales.nextElement();
                for (int i=0; i<locCharsetMap.length && !matchFound; i++) {
                    String language = locCharsetMap[i].getAttributeValue(
                                                LocaleCharsetMap.LOCALE);
                    if (language == null || language.equals("")) {
                        continue;
                    }
                    String country = null;
                    int index = language.indexOf('_");
                    if (index != -1) {
                        country = language.substring(index+1);
                        language = language.substring(0, index);
                    }
                    Locale mapLoc = null;
                    if (country != null) {
                        mapLoc = new Locale(language, country);
                    } else {
                        mapLoc = new Locale(language);
                    }
                    if (mapLoc.equals(reqLoc)) {
                        /*
                         * Match found. Get the charset to which the
                         * matched locale maps.
                         */
                        encoding = locCharsetMap[i].getAttributeValue(
                                                    LocaleCharsetMap.CHARSET);
                        matchFound = true;
                    }
                }
            }           
        }

        return encoding;
    
voidparseAlternateDocBase(java.lang.String propName, java.lang.String propValue)


        if (propName == null || propValue == null) {
            logger.log(Level.WARNING, "Null property name or value");
            return;
        }

        if (!propName.startsWith("alternatedocroot_")) {
            return;
        }
            
        /*
         * Validate the prop value
         */
        String urlPattern = null;
        String docBase = null;

        int fromIndex = propValue.indexOf(ALTERNATE_FROM);
        int dirIndex = propValue.indexOf(ALTERNATE_DOCBASE);

        if (fromIndex < 0 || dirIndex < 0) {
            logger.log(
                Level.WARNING,
                "webmodule.alternateDocBase.missingPathOrUrlPattern",
                propValue);
            return;
        }

        if (fromIndex > dirIndex) {
            urlPattern = propValue.substring(
                fromIndex + ALTERNATE_FROM.length());
            docBase = propValue.substring(
                dirIndex + ALTERNATE_DOCBASE.length(),
                fromIndex);
        } else {
            urlPattern = propValue.substring(
                fromIndex + ALTERNATE_FROM.length(),
                dirIndex);
            docBase = propValue.substring(
                dirIndex + ALTERNATE_DOCBASE.length());
        }

        urlPattern = urlPattern.trim();
        if (!validateURLPattern(urlPattern)) {
            logger.log(Level.WARNING,
                       "webmodule.alternateDocBase.illegalUrlPattern",
                       urlPattern);
            return;
        }

        docBase = docBase.trim();

        addAlternateDocBase(urlPattern, docBase);
    
voidremoveAdHocPath(java.lang.String path)


        if (path == null) {
            return;
        }

        adHocPaths.remove(path);
        if (adHocPaths.isEmpty()) {
            this.hasAdHocPaths = false;
        }
    
voidremoveAdHocSubtree(java.lang.String subtree)


        if (subtree == null) {
            return;
        }

        adHocSubtrees.remove(subtree);
        if (adHocSubtrees.isEmpty()) {
            this.hasAdHocSubtrees = false;
        }
    
public voidremoveAdHocValve(org.apache.catalina.Valve valve)
Removes the given valve from this web module's ad-hoc pipeline.

param
valve The valve to remove

        adHocPipeline.removeValve(valve);
    
voidsetAlternateDocBases(com.sun.enterprise.config.serverbeans.ElementProperty[] props)
Sets the alternate docroots of this web module from the given "alternatedocroot_" properties.


        if (props == null) {
            return;
        }

        for (int i=0; i<props.length; i++) {
            parseAlternateDocBase(props[i].getName(), props[i].getValue());
        }
    
public voidsetAttribute(java.lang.String name, java.lang.Object value)
Sets the context attribute with the given name and value.

param
name The context attribute name
param
value The context attribute value

        context.setAttribute(name, value);
    
public voidsetBean(com.sun.enterprise.config.serverbeans.WebModule bean)

        this.bean = bean;
    
public voidsetCachedFindOperation(java.lang.Object[] cachedFinds)
Cache the result of doing findXX on this object NOTE: this method MUST be used only when loading/using the content of default-web.xml

        this.cachedFinds = cachedFinds;
    
public voidsetFileEncoding(java.lang.String enc)
Sets the file encoding of all static resources of this web module.

param
enc The file encoding of static resources of this web module

        this.fileEncoding = enc;
    
public voidsetI18nInfo()
Sets the parameter encoding (i18n) info from sun-web.xml.


        if (iasBean == null) {
            return;
        }

        if (iasBean.isParameterEncoding()) {
            formHintField = (String) iasBean.getAttributeValue(
                                                SunWebApp.PARAMETER_ENCODING,
                                                SunWebApp.FORM_HINT_FIELD);
            defaultCharset = (String) iasBean.getAttributeValue(
                                                SunWebApp.PARAMETER_ENCODING,
                                                SunWebApp.DEFAULT_CHARSET);
        }

        LocaleCharsetInfo lcinfo = iasBean.getLocaleCharsetInfo();
        if (lcinfo != null) {
            if (lcinfo.getAttributeValue(
                            LocaleCharsetInfo.DEFAULT_LOCALE) != null) {
               logger.warning("webmodule.default_locale_deprecated");
            }
            /*
             * <parameter-encoding> subelem of <sun-web-app> takes precedence
             * over that of <locale-charset-info>
             */
            if (lcinfo.isParameterEncoding()
                    && !iasBean.isParameterEncoding()) {
                formHintField = (String) lcinfo.getAttributeValue(
                                        LocaleCharsetInfo.PARAMETER_ENCODING,
                                        LocaleCharsetInfo.FORM_HINT_FIELD);
                defaultCharset = (String) lcinfo.getAttributeValue(
                                        LocaleCharsetInfo.PARAMETER_ENCODING,
                                        LocaleCharsetInfo.DEFAULT_CHARSET);
            }
            _lcMap = lcinfo.getLocaleCharsetMap();
        }
    
public voidsetIasWebAppConfigBean(com.sun.enterprise.deployment.runtime.web.SunWebApp iasBean)
set the sun-web.xml config bean

       this.iasBean = iasBean; 
    
public voidsetParent(org.apache.catalina.Container container)
Sets the virtual server parent of this web module, and passes it on to this web module's realm adapter..

param
container The virtual server parent

        super.setParent(container);
        // The following assumes that the realm has been set on this WebModule
        // before the WebModule is added as a child to the virtual server on
        // which it is being deployed.
        RealmAdapter ra = (RealmAdapter) getRealm();
        if (ra != null) {
            ra.setVirtualServer(container);
        }
    
voidsetWebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor wbd)
Sets the WebBundleDescriptor (web.xml) for this WebModule.

param
wbd The WebBundleDescriptor

        this.webBundleDescriptor = wbd;
    
public voidsetXmlConfigured(boolean hasBeenXmlConfigured)
Set to true when the default-web.xml has been read for this module.

        this.hasBeenXmlConfigured = hasBeenXmlConfigured;
    
public synchronized voidstart()
Starts this web module.

        // Start and register Tomcat mbeans
        super.start();
        configureCatalinaProperties();
        started = true;
        // Register monitoring mbeans, which delegate to the Tomcat mbeans
        webContainer.enableMonitoring(this,
                                      ((VirtualServer) getParent()).getID());
    
public voidstop()
Stops this web module.

        // Unregister monitoring mbeans only if this web module was
        // successfully started, because if stop() is called during an
        // aborted start(), no monitoring mbeans will have been registered
        if (started) {
            webContainer.disableMonitoring(
                this, ((VirtualServer) getParent()).getID());
            started = false;
        }

        if (webBundleDescriptor != null && webBundleDescriptor.getServiceReferenceDescriptors() != null) {
            for (Object obj: webBundleDescriptor.getServiceReferenceDescriptors()) {
                ClientPipeCloser.getInstance().cleanupClientPipe((ServiceReferenceDescriptor)obj);
            }
        }
       
        // Stop and unregister Tomcat mbeans
        super.stop();
    
private booleanvalidateURLPattern(java.lang.String urlPattern)


        if (urlPattern == null) {
            return (false);
	}

        if (urlPattern.indexOf('\n") >= 0 || urlPattern.indexOf('\r") >= 0) {
            logger.log(Level.WARNING,
                       "webmodule.alternateDocBase.crlfInUrlPattern",
                       urlPattern);
            return false;
        }

        if (urlPattern.startsWith("*.")) {
            if (urlPattern.indexOf('/") < 0) {
                return (true);
            } else {
                return (false);
            }
        }
        if ( (urlPattern.startsWith("/")) &&
	     (urlPattern.indexOf("*.") < 0)) {
            return (true);
        } else {
            return (false);
        }