User: starksm 
  Date: 01/05/24 08:36:40

  Modified:    tomcat/src/main/org/jboss/tomcat/naming Tag: Branch_2_2
                        JbossWebXmlReader.java
  Log:
  Merge 2.3 changes into 2.2 branch
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.1   +43 -199   
contrib/tomcat/src/main/org/jboss/tomcat/naming/JbossWebXmlReader.java
  
  Index: JbossWebXmlReader.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/contrib/tomcat/src/main/org/jboss/tomcat/naming/JbossWebXmlReader.java,v
  retrieving revision 1.3
  retrieving revision 1.3.2.1
  diff -u -r1.3 -r1.3.2.1
  --- JbossWebXmlReader.java    2000/12/21 16:30:36     1.3
  +++ JbossWebXmlReader.java    2001/05/24 15:36:40     1.3.2.1
  @@ -6,221 +6,65 @@
    */
   package org.jboss.tomcat.naming;
   
  +import java.io.File;
  +import java.io.IOException;
  +import javax.xml.parsers.DocumentBuilderFactory;
  +import javax.xml.parsers.DocumentBuilder;
  +
  +import org.w3c.dom.Document;
  +import org.w3c.dom.Element;
  +
   import org.apache.tomcat.core.BaseInterceptor;
   import org.apache.tomcat.core.ContextManager;
   import org.apache.tomcat.core.TomcatException;
  -import org.apache.tomcat.util.StringManager;
  -import org.apache.tomcat.util.xml.XmlMapper;
  -
  -import org.jnp.interfaces.Naming;
  -
  -import javax.naming.Context;
  -import javax.naming.InitialContext;
  -import javax.naming.LinkRef;
  -import javax.naming.NamingException;
  -import javax.naming.Name;
  -import javax.naming.NameNotFoundException;
  -
  -import java.io.File;
  -import java.io.IOException;
   
  -import java.util.HashMap;
  -import java.util.Iterator;
  +import org.jboss.web.AbstractWebContainer.WebDescriptorParser;
   
  -/**
  - * Reads the web.xml and jboss-web.xml file, doing JNDI associations.
  - * @author <a href="mailto:[EMAIL PROTECTED]";>Kevin Lewis</a>
  - * @version $Revision: 1.3 $
  - */
  +/** A ContextInterceptor that hooks into the context initialization sequence
  +to delegate the JNDI env setup to the JBoss AbstractWebContainer. This must
  +be configured AFTER the org.apache.tomcat.context.LoaderInterceptor so that
  +the servlet class loader has been set on the Context.
  +
  +@author <a href="mailto:[EMAIL PROTECTED]";>Kevin Lewis</a>
  +@author [EMAIL PROTECTED]
  +@version $Revision: 1.3.2.1 $
  +*/
   public class JbossWebXmlReader 
      extends BaseInterceptor 
   {
  -   private static HashMap loaderMap = new HashMap();
  -
  -   public static ClassLoader getClassLoaderFor(String docBase)
  -   {
  -      return (ClassLoader)loaderMap.get(docBase);
  -   }
  -
      public void contextInit(org.apache.tomcat.core.Context context) 
         throws TomcatException
      {
         try
         {
  -         ContextManager contextManager = context.getContextManager();
  -         
Thread.currentThread().setContextClassLoader(((ClassLoader)context.getServletLoader().getClassLoader()));
  -         File webXml = contextManager.getAbsolute(new File(context.getDocBase() + 
"/WEB-INF/web.xml"));
  -         File jbossWebXml = contextManager.getAbsolute(new 
File(context.getDocBase() + "/WEB-INF/jboss-web.xml"));
  -
  -         if(jbossWebXml.exists() && webXml.exists())
  -         {
  -            loaderMap.put(context.getDocBase(), 
Thread.currentThread().getContextClassLoader());
  -            processJbossWebXmlFile(jbossWebXml, webXml);
  -         }
  -      }
  -      catch(Exception exception)
  -      {
  -         exception.printStackTrace(System.out);
  -      }
  -   }
  -
  -   private void processJbossWebXmlFile(File jbossWebXml, File webXml)
  -      throws javax.naming.NamingException
  -   {
  -      try
  -      {
  -         Context context = (Context)(new InitialContext().lookup("java:comp"));
  -         context = context.createSubcontext("env");
  -
  -         addToJndiContext(context, processJbossEnvEntries(webXml));
  -         addLinkRefsToJndiContext(context, processJbossEjbRefs(jbossWebXml));
  -         addLinkRefsToJndiContext(context, processJbossResourceRefs(jbossWebXml));
  -      }
  -      catch(Exception exception)
  -      {
  -         exception.printStackTrace(System.out);
  -      }
  -   }
  -
  -   private void addLinkRefsToJndiContext(Context context, HashMap contextMap)
  -      throws Exception
  -   {
  -      Iterator names = contextMap.keySet().iterator();
  -      while(names.hasNext())
  -      {
  -         Object key = names.next();
  -         System.out.println("Binding "+key+" to "+contextMap.get(key));
  -         bind(context, (String)key, new LinkRef((String)contextMap.get(key)));
  -      }
  -   }
  -
  -   private void addToJndiContext(Context context, HashMap contextMap) 
  -      throws Exception
  -   {
  -      Iterator names = contextMap.keySet().iterator();
  -      while(names.hasNext())
  -      {
  -         Object key = names.next();
  -         bind(context, (String)key, contextMap.get(key));
  -      }
  -   }
  -
  -   private void bind(Context ctx, String name, Object val) 
  -      throws NamingException
  -   {
  +        WebDescriptorParser webAppParser = (WebDescriptorParser) 
context.getAttribute("org.jboss.web.AbstractWebContainer.WebDescriptorParser");
  +        if( webAppParser == null )
  +            return;
  +
  +        ContextManager contextManager = context.getContextManager();
  +        // Get the servlet class loader
  +        ClassLoader scl = (ClassLoader) context.getServletLoader().getClassLoader();
  +        // Get the web-app.xml and jboss-web.xml deployment descriptors 
  +        File webXml = contextManager.getAbsolute(new File(context.getDocBase() + 
"/WEB-INF/web.xml"));
  +        File jbossWebXml = contextManager.getAbsolute(new File(context.getDocBase() 
+ "/WEB-INF/jboss-web.xml"));
  +        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  +        DocumentBuilder parser = factory.newDocumentBuilder();
  +        Document webDoc = parser.parse(webXml);
  +        Element web = webDoc.getDocumentElement();
  +        Element jbossWeb = null;
  +        if( jbossWebXml.exists() == true )
  +        {
  +            Document jbossWebDoc = parser.parse(jbossWebXml);
  +            jbossWeb = jbossWebDoc.getDocumentElement();
  +        }
  +        // Setup the wep app JNDI java:comp/env namespace
  +        webAppParser.parseWebAppDescriptors(scl, web, jbossWeb);
   
  -      // Bind val to name in ctx, and make sure that all intermediate contexts exist
  -      Name n = ctx.getNameParser("").parse(name);
  -      while(n.size() > 1)
  +     }
  +      catch(Exception e)
         {
  -         String ctxName = n.get(0);
  -         try
  -         {
  -            ctx = (Context)ctx.lookup(ctxName);
  -         }
  -         catch(NameNotFoundException e)
  -         {
  -            ctx = ctx.createSubcontext(ctxName);
  -         }
  -
  -         n = n.getSuffix(1);
  +         e.printStackTrace();
         }
  -
  -      ctx.bind(n.get(0), val);
  -   }
  -
  -   private HashMap processJbossEjbRefs(File jbossXml) 
  -      throws Exception
  -   {
  -
  -      XmlMapper jbossMapper = new XmlMapper();
  -      jbossMapper.setValidating(true);
  -
  -      jbossMapper.registerDTDRes("-//jBoss//DTD Web Application 2.2//EN",
  -         "jboss-web.dtd");
  -
  -      jbossMapper.addRule("jboss-web/ejb-ref",
  -         jbossMapper.methodSetter("put", 2, new String[]
  -         {"java.lang.String", "java.lang.String"
  -         }));
  -         jbossMapper.addRule("jboss-web/ejb-ref/ejb-ref-name",
  -         jbossMapper.methodParam(0));
  -      jbossMapper.addRule("jboss-web/ejb-ref/jndi-name",
  -         jbossMapper.methodParam(1));
  -
  -      StringMapWrapper ejbRefs = new StringMapWrapper();
  -      jbossMapper.readXml(jbossXml, ejbRefs);
  -      return ejbRefs.getMap();
      }
   
  -   private HashMap processJbossResourceRefs(File jbossXml) 
  -      throws Exception
  -   {
  -
  -      XmlMapper jbossMapper = new XmlMapper();
  -      jbossMapper.setValidating(true);
  -
  -      jbossMapper.registerDTDRes("-//jBoss//DTD Web Application 2.2//EN",
  -         "jboss-web.dtd");
  -
  -      jbossMapper.addRule("jboss-web/resource-ref",
  -         jbossMapper.methodSetter("put", 2, new String[]
  -         {"java.lang.String", "java.lang.String"
  -         }));
  -         jbossMapper.addRule("jboss-web/resource-ref/res-ref-name",
  -         jbossMapper.methodParam(0));
  -      jbossMapper.addRule("jboss-web/resource-ref/jndi-name",
  -         jbossMapper.methodParam(1));
  -
  -      StringMapWrapper resourceRefs = new StringMapWrapper();
  -      jbossMapper.readXml(jbossXml, resourceRefs);
  -      return resourceRefs.getMap();
  -   }
  -
  -   private HashMap processJbossEnvEntries(File webXml) 
  -      throws Exception
  -   {
  -
  -      XmlMapper webMapper = new XmlMapper();
  -      webMapper.setValidating(true);
  -
  -      webMapper.registerDTDRes("-//Sun Microsystems, Inc.//DTD Web Application 
2.2//EN",
  -         "/org/apache/tomcat/resources/web.dtd");
  -
  -      webMapper.addRule("web-app/env-entry",
  -         webMapper.methodSetter("put", 2, new String[]
  -         {"java.lang.String", "java.lang.String"
  -         }));
  -         webMapper.addRule("web-app/env-entry/env-entry-name", 
webMapper.methodParam(0));
  -      webMapper.addRule("web-app/env-entry/env-entry-value", 
webMapper.methodParam(1));
  -
  -      StringMapWrapper envEntries = new StringMapWrapper();
  -      webMapper.readXml(webXml, envEntries);
  -      return envEntries.getMap();
  -   }
  -
  -   /**
  -    * Wraps a hashmap with string argument types.  Need this because Tomcat's
  -    * XmlMapper only allows things with String or int argument types.
  -    */
  -   public static class StringMapWrapper
  -   {
  -
  -      public void put(String key, String value)
  -      {
  -         map.put(key, value);
  -      }
  -
  -      public String get(String key)
  -      {
  -         return (String)map.get(key);
  -      }
  -
  -      public HashMap getMap()
  -      { return map; 
  -      }
  -
  -      private HashMap map = new HashMap();
  -   }
   }
  -
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to