Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2008, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.web.tomcat.service.deployers;
 
 // $Id: JBossContextConfig.java 85945 2009-03-16 19:45:12Z dimitris@jboss.org $
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 
 @SuppressWarnings("unchecked")
 public class JBossContextConfig extends ContextConfig
 {
 
 
 
    public static ThreadLocal<KernelkernelLocal = new ThreadLocal<Kernel>();
   private static Logger log = Logger.getLogger(JBossContextConfig.class);
   private boolean runDestroy = false;

   

Creates a new instance of JBossContextConfig.

   public JBossContextConfig()
   {
      super();
      try
      {
         Map authMap = this.getAuthenticators();
         if (authMap.size() > 0)
             = authMap;
      }
      catch (Exception e)
      {
         .debug("Failed to load the customized authenticators"e);
      }
   }
   protected void applicationWebConfig()
   {
   }
   protected void defaultWebConfig()
   {
      ServletContext servletContext = .getServletContext();
      Kernel kernel = .get();
      DeploymentUnit unit = .get();
      .debug("Setting MC attributes, kernel: " + kernel + ", unit: " + unit);
      servletContext.setAttribute(.kernel);
      servletContext.setAttribute(DeploymentUnit.class.getName(), unit);
   }
   protected void processWebMetaData(JBossWebMetaData metaData)
   {
      if ( instanceof StandardContext)
      {
      }
      // Metadata complete
      // SetPublicId
      if (metaData.is25())
         .setPublicId("/javax/servlet/resources/web-app_2_5.dtd");
      else if (metaData.is24())
         .setPublicId("/javax/servlet/resources/web-app_2_4.dtd");
      else if (metaData.is23())
      else
      // processContextParameters() provides a merged view of the context params
      // Display name
      DescriptionGroupMetaData dg = metaData.getDescriptionGroup();
      if (dg != null)
      {
         String displayName = dg.getDisplayName();
         if (displayName != null)
         {
            .setDisplayName(displayName);
         }
      }
      // Distributable
      if (metaData.getDistributable() != null)
         .setDistributable(true);
      // Error pages
      List<ErrorPageMetaDataerrorPages = metaData.getErrorPages();
      if (errorPages != null)
      {
         for (ErrorPageMetaData value : errorPages)
         {
            org.apache.catalina.deploy.ErrorPage errorPage = new org.apache.catalina.deploy.ErrorPage();
            errorPage.setErrorCode(value.getErrorCode());
            errorPage.setExceptionType(value.getExceptionType());
            errorPage.setLocation(value.getLocation());
            .addErrorPage(errorPage);
         }
      }
      // Filter definitions
      FiltersMetaData filters = metaData.getFilters();
      if (filters != null)
      {
         for (FilterMetaData value : filters)
         {
            org.apache.catalina.deploy.FilterDef filterDef = new org.apache.catalina.deploy.FilterDef();
            filterDef.setFilterName(value.getName());
            filterDef.setFilterClass(value.getFilterClass());
            if (value.getInitParam() != null)
               for (ParamValueMetaData param : value.getInitParam())
               {
                  filterDef.addInitParameter(param.getParamName(), param.getParamValue());
               }
            .addFilterDef(filterDef);
         }
      }
      // Filter mappings
      List<FilterMappingMetaDatafiltersMappings = metaData.getFilterMappings();
      if (filtersMappings != null)
      {
         for (FilterMappingMetaData value : filtersMappings)
         {
            org.apache.catalina.deploy.FilterMap filterMap = new org.apache.catalina.deploy.FilterMap();
            filterMap.setFilterName(value.getFilterName());
            List<StringservletNames = value.getServletNames();
            if (servletNames != null)
            {
               for (String name : servletNames)
                  filterMap.addServletName(name);
            }
            List<StringurlPatterns = value.getUrlPatterns();
            if (urlPatterns != null)
            {
               for (String pattern : urlPatterns)
                  filterMap.addURLPattern(pattern);
            }
            List<DispatcherTypedispatchers = value.getDispatchers();
            if (dispatchers != null)
            {
               for (DispatcherType type : dispatchers)
                  filterMap.setDispatcher(type.name());
            }
            .addFilterMap(filterMap);
         }
      }
      // Listeners
      List<ListenerMetaDatalisteners = metaData.getListeners();
      if (listeners != null)
      {
         for (ListenerMetaData value : listeners)
         {
            .addApplicationListener(value.getListenerClass());
         }
      }
      // Login configuration
      LoginConfigMetaData loginConfig = metaData.getLoginConfig();
      if (loginConfig != null)
      {
         org.apache.catalina.deploy.LoginConfig loginConfig2 = new org.apache.catalina.deploy.LoginConfig();
         loginConfig2.setAuthMethod(loginConfig.getAuthMethod());
         loginConfig2.setRealmName(loginConfig.getRealmName());
         if (loginConfig.getFormLoginConfig() != null)
         {
            loginConfig2.setLoginPage(loginConfig.getFormLoginConfig().getLoginPage());
            loginConfig2.setErrorPage(loginConfig.getFormLoginConfig().getErrorPage());
         }
         .setLoginConfig(loginConfig2);
      }
      // MIME mappings
      List<MimeMappingMetaDatamimes = metaData.getMimeMappings();
      if (mimes != null)
      {
         for (MimeMappingMetaData value : mimes)
         {
            .addMimeMapping(value.getExtension(), value.getMimeType());
         }
      }
      // Security constraints
      List<SecurityConstraintMetaDatascs = metaData.getSecurityContraints();
      if (scs != null)
      {
         for (SecurityConstraintMetaData value : scs)
         {
            org.apache.catalina.deploy.SecurityConstraint constraint = new org.apache.catalina.deploy.SecurityConstraint();
            TransportGuaranteeType tg = value.getTransportGuarantee();
            constraint.setUserConstraint(tg.name());
            AuthConstraintMetaData acmd = value.getAuthConstraint();
            constraint.setAuthConstraint(acmd != null);
            if (acmd != null)
            {
               if (acmd.getRoleNames() != null)
                  for (String role : acmd.getRoleNames())
                  {
                     constraint.addAuthRole(role);
                  }
            }
            WebResourceCollectionsMetaData wrcs = value.getResourceCollections();
            if (wrcs != null)
            {
               for (WebResourceCollectionMetaData wrc : wrcs)
               {
                  org.apache.catalina.deploy.SecurityCollection collection2 = new org.apache.catalina.deploy.SecurityCollection();
                  collection2.setName(wrc.getName());
                  List<Stringmethods = wrc.getHttpMethods();
                  if (methods != null)
                  {
                     for (String method : wrc.getHttpMethods())
                     {
                        collection2.addMethod(method);
                     }
                  }
                  List<Stringpatterns = wrc.getUrlPatterns();
                  if (patterns != null)
                  {
                     for (String pattern : patterns)
                     {
                        collection2.addPattern(pattern);
                     }
                  }
                  constraint.addCollection(collection2);
               }
            }
            .addConstraint(constraint);
         }
      }
      // Security roles
      SecurityRolesMetaData roles = metaData.getSecurityRoles();
      if (roles != null)
      {
         for (SecurityRoleMetaData value : roles)
         {
            .addSecurityRole(value.getRoleName());
         }
      }
      // Servlet
      JBossServletsMetaData servlets = metaData.getServlets();
      if (servlets != null)
      {
         for (JBossServletMetaData value : servlets)
         {
            org.apache.catalina.Wrapper wrapper = .createWrapper();
            wrapper.setName(value.getName());
            wrapper.setServletClass(value.getServletClass());
            if (value.getJspFile() != null)
            {
               wrapper.setJspFile(value.getJspFile());
            }
            wrapper.setLoadOnStartup(value.getLoadOnStartup());
            if (value.getRunAs() != null)
            {
               wrapper.setRunAs(value.getRunAs().getRoleName());
            }
            List<ParamValueMetaDataparams = value.getInitParam();
            if (params != null)
            {
               for (ParamValueMetaData param : params)
               {
                  wrapper.addInitParameter(param.getParamName(), param.getParamValue());
               }
            }
            SecurityRoleRefsMetaData refs = value.getSecurityRoleRefs();
            if (refs != null)
            {
               for (SecurityRoleRefMetaData ref : refs)
               {
                  wrapper.addSecurityReference(ref.getRoleName(), ref.getRoleLink());
               }
            }
            .addChild(wrapper);
         }
      }
      // Servlet mapping
      List<ServletMappingMetaDatasmappings = metaData.getServletMappings();
      if (smappings != null)
      {
         for (ServletMappingMetaData value : smappings)
         {
            List<StringurlPatterns = value.getUrlPatterns();
            if (urlPatterns != null)
            {
               for (String pattern : urlPatterns)
                  .addServletMapping(patternvalue.getServletName());
            }
         }
      }
      // JSP mappings
      JspConfigMetaData config = metaData.getJspConfig();
      if (config != null)
      {
         List<JspPropertyGroupgroups = config.getPropertyGroups();
         if (groups != null)
         {
            for (JspPropertyGroup group : groups)
            {
               for (String pattern : group.getUrlPatterns())
               {
                  .addJspMapping(pattern);
               }
            }
         }
         // Taglib
         List<TaglibMetaDatataglibs = config.getTaglibs();
         if (taglibs != null)
         {
            for (TaglibMetaData taglib : taglibs)
            {
               .addTaglib(taglib.getTaglibUri(), taglib.getTaglibLocation());
            }
         }
      }
      // Locale encoding mapping
      LocaleEncodingsMetaData locales = metaData.getLocalEncodings();
      if (locales != null)
      {
         for (LocaleEncodingMetaData value : locales.getMappings())
         {
            .addLocaleEncodingMappingParameter(value.getLocale(), value.getEncoding());
         }
      }
      // Welcome files
      WelcomeFileListMetaData welcomeFiles = metaData.getWelcomeFileList();
      if (welcomeFiles != null)
      {
         for (String value : welcomeFiles.getWelcomeFiles())
            .addWelcomeFile(value);
      }
      // Session timeout
      SessionConfigMetaData scmd = metaData.getSessionConfig();
      if (scmd != null)
      {
      }
   }

   

Retrieves the map of authenticators according to the settings made available by TomcatService.

Returns:
a Map containing the authenticator that must be used for each authentication method.
Throws:
java.lang.Exception if an error occurs while getting the authenticators.
   protected Map getAuthenticators() throws Exception
   {
      Map authenticators = new HashMap();
      ClassLoader tcl = Thread.currentThread().getContextClassLoader();
      Properties authProps = this.getAuthenticatorsFromJndi();
      if (authProps != null)
      {
         Set keys = authProps.keySet();
         Iterator iter = keys != null ? keys.iterator() : null;
         while (iter != null && iter.hasNext())
         {
            String key = (String)iter.next();
            String authenticatorStr = (String)authProps.get(key);
            Class authClass = tcl.loadClass(authenticatorStr);
            authenticators.put(keyauthClass.newInstance());
         }
      }
      if (.isTraceEnabled())
         .trace("Authenticators plugged in::" + authenticators);
      return authenticators;
   }

   

Get the key-pair of authenticators from the JNDI.

Returns:
a Properties object containing the authenticator class name for each authentication method.
Throws:
javax.naming.NamingException if an error occurs while looking up the JNDI.
   {
      return (Properties)new InitialContext().lookup("TomcatAuthenticators");
   }

   
Process the context parameters. Let a user application override the sharedMetaData values.
   protected void processContextParameters()
   {
      JBossWebMetaData local = .get();
      JBossWebMetaData shared = .get();
      Map<StringStringoverrideParams = new HashMap<StringString>();
      List<ParamValueMetaDataparams = local.getContextParams();
      if (params != null)
      {
         for (ParamValueMetaData param : params)
         {
            overrideParams.put(param.getParamName(), param.getParamValue());
         }
      }
      params = shared.getContextParams();
      if (params != null)
      {
         for (ParamValueMetaData param : params)
         {
            if (overrideParams.get(param.getParamName()) == null)
            {
               overrideParams.put(param.getParamName(), param.getParamValue());
            }
         }
      }
      for (String key : overrideParams.keySet())
      {
         .addParameter(keyoverrideParams.get(key));
      }
   }

   
Process a "init" event for this Context.
   protected void init()
   {
      .setConfigured(false);
       = true;
      if (!.getOverride())
      {
         processContextConfig("context.xml"false);
      }
      // This should come from the deployment unit
   }
   protected void processContextConfig(String resourceNameboolean local)
   {
      ContextMetaData contextMetaData = null;
      try
      {
         SchemaBinding schema = JBossXBBuilder.build(ContextMetaData.class);
         Unmarshaller u = UnmarshallerFactory.newInstance().newUnmarshaller();
         u.setSchemaValidation(false);
         u.setValidation(false);
         u.setEntityResolver(new JBossEntityResolver());
         
         InputStream is = null;
         try
         {
            if (local)
            {
               DeploymentUnit localUnit = .get();
               if (localUnit instanceof VFSDeploymentUnit)
               {
                  VFSDeploymentUnit vfsUnit = (VFSDeploymentUnit)localUnit;
                  VirtualFile vf = vfsUnit.getFile(resourceName);
                  if (vf != null)
                     is = vf.openStream();
               }
            }
            if (is == null)
               is = getClass().getClassLoader().getResourceAsStream(resourceName);
            if (is != null)
               contextMetaData = ContextMetaData.class.cast(u.unmarshal(isschema));
         }
         finally
         {
            if (is != null)
            {
               try
               {
                  is.close();
               }
               catch (IOException e)
               {
                  // Ignore
               }
            }
         }
      }
      catch (Exception e)
      {
         .error("XML error parsing: " + resourceNamee);
          = false;
         return;
      }
      if (contextMetaData != null)
      {
         try
         {
            if (contextMetaData.getAttributes() != null)
            {
               Iterator<QNamenames = contextMetaData.getAttributes().keySet().iterator();
               while (names.hasNext())
               {
                  QName name = names.next();
                  String value = (String)contextMetaData.getAttributes().get(name);
                  // FIXME: This should be done by XB
                  value = StringPropertyReplacer.replaceProperties(value);
                  IntrospectionUtils.setProperty(name.getLocalPart(), value);
               }
            }
            TomcatService.addLifecycleListeners(contextMetaData.getListeners());
            // Context/Realm
            if (contextMetaData.getRealm() != null)
            {
               .setRealm((org.apache.catalina.Realm)TomcatService.getInstance(contextMetaData.getRealm(), null));
            }
            // Context/Valve
            TomcatService.addValves(contextMetaData.getValves());
            // Context/InstanceListener
            if (contextMetaData.getInstanceListeners() != null)
            {
               Iterator<Stringlisteners = contextMetaData.getInstanceListeners().iterator();
               while (listeners.hasNext())
               {
                  .addInstanceListener(listeners.next());
               }
            }
            // Context/Loader
            if (contextMetaData.getLoader() != null)
            {
               // This probably won't work very well in JBoss
               .setLoader((org.apache.catalina.Loader)TomcatService.getInstance(contextMetaData.getLoader(), "org.apache.catalina.loader.WebappLoader"));
            }
            // Context/Manager
            if (contextMetaData.getManager() != null)
            {
               .setManager((org.apache.catalina.Manager)TomcatService.getInstance(contextMetaData.getManager(),
                     "org.apache.catalina.session.StandardManager"));
            }
            // Context/Parameter
            if (contextMetaData.getParameters() != null)
            {
               Iterator<ParameterMetaDataparameterMetaDatas = contextMetaData.getParameters().iterator();
               while (parameterMetaDatas.hasNext())
               {
                  ParameterMetaData parameterMetaData = parameterMetaDatas.next();
                  .addApplicationParameter((org.apache.catalina.deploy.ApplicationParameter)TomcatService.getInstance(parameterMetaDatanull));
               }
            }
            // Context/Resources
            if (contextMetaData.getResources() != null)
            {
               .setResources((javax.naming.directory.DirContext)TomcatService.getInstance(contextMetaData.getResources(),
                     "org.apache.naming.resources.FileDirContext"));
            }
            // Context/SessionCookie
            if (contextMetaData.getSessionCookie() != null)
            {
               SessionCookie sessionCookie = new SessionCookie();
               sessionCookie.setComment(contextMetaData.getSessionCookie().getComment());
               sessionCookie.setDomain(contextMetaData.getSessionCookie().getDomain());
               sessionCookie.setHttpOnly(contextMetaData.getSessionCookie().getHttpOnly());
               sessionCookie.setPath(contextMetaData.getSessionCookie().getPath());
               sessionCookie.setSecure(contextMetaData.getSessionCookie().getSecure());
               .setSessionCookie(sessionCookie);
            }
         }
         catch (Exception e)
         {
            .error("Error processing: " + resourceNamee);
             = false;
         }
      }
   }
   protected void destroy()
   {
      if ()
      {
         super.destroy();
      }
   }
New to GrepCode? Check out our FAQ X