User: salborini
Date: 00/11/02 10:02:47
Added: tomcat/src/main/org/jboss/tomcat/naming
JbossWebXmlReader.java JndiRequestInterceptor.java
StringMapWrapper.java
Log:
JNDI and security integration for EmbeddedTomcatService
Submitted by Kevin Lewis <[EMAIL PROTECTED]>
Revision Changes Path
1.1
contrib/tomcat/src/main/org/jboss/tomcat/naming/JbossWebXmlReader.java
Index: JbossWebXmlReader.java
===================================================================
package org.jboss.tomcat.naming;
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;
/**
* Reads the web.xml and jboss-web.xml file, doing JNDI associations.
* @author <a href="mailto:[EMAIL PROTECTED]">Kevin Lewis</a>
* @version $Revision: 1.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();
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();
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 {
// Bind val to name in ctx, and make sure that all intermediate contexts
exist
Name n = ctx.getNameParser("").parse(name);
while (n.size() > 1) {
String ctxName = n.get(0);
try {
ctx = (Context)ctx.lookup(ctxName);
}
catch (NameNotFoundException e) {
ctx = ctx.createSubcontext(ctxName);
}
n = n.getSuffix(1);
}
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/reference-descriptor/ejb-reference-description",
jbossMapper.methodSetter("put", 2, new String[]
{"java.lang.String", "java.lang.String"}));
jbossMapper.addRule("jboss-web/reference-descriptor/ejb-reference-description/ejb-ref-name",
jbossMapper.methodParam(0));
jbossMapper.addRule("jboss-web/reference-descriptor/ejb-reference-description/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/reference-descriptor/resource-description",
jbossMapper.methodSetter("put", 2, new String[]
{"java.lang.String", "java.lang.String"}));
jbossMapper.addRule("jboss-web/reference-descriptor/resource-description/res-ref-name",
jbossMapper.methodParam(0));
jbossMapper.addRule("jboss-web/reference-descriptor/resource-description/res-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();
}
}
1.1
contrib/tomcat/src/main/org/jboss/tomcat/naming/JndiRequestInterceptor.java
Index: JndiRequestInterceptor.java
===================================================================
package org.jboss.tomcat.naming;
import org.apache.tomcat.core.BaseInterceptor;
import org.apache.tomcat.core.Request;
import org.apache.tomcat.core.RequestInterceptor;
import org.apache.tomcat.core.Response;
import org.jboss.ejb.BeanClassLoader;
import javax.naming.NamingException;
/**
* Sets the appropriate context class loader, based on what class loader was
* used to initialize the application.
* @see JbossWebXmlReader
* @author <a href="mailto:[EMAIL PROTECTED]">Kevin Lewis</a>
* @version $Revision: 1.1 $
*/
public class JndiRequestInterceptor extends BaseInterceptor {
public int requestMap(Request request) {
Thread.currentThread().setContextClassLoader(
org.jboss.tomcat.naming.JbossWebXmlReader.getClassLoaderFor(
request.getContext().getDocBase()));
return 0;
}
}
1.1
contrib/tomcat/src/main/org/jboss/tomcat/naming/StringMapWrapper.java
Index: StringMapWrapper.java
===================================================================
package org.jboss.tomcat.naming;
import java.util.HashMap;
/**
* Wraps a hashmap with string argument types. Need this because Tomcat's
* XmlMapper only allows things with String or int argument types.
* @author <a href="mailto:[EMAIL PROTECTED]">Kevin Lewis</a>
* @version $Revision: 1.1 $
*/
public 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();
}