KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > deployment > PortalWebTomcat4App


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.deployment;
10
11 import java.lang.reflect.Array JavaDoc;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14
15 import javax.servlet.ServletContext JavaDoc;
16
17 import org.jboss.portal.server.servlet.CommandServlet;
18 import org.jboss.web.WebApplication;
19
20 /**
21  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
22  * @version $Revision: 1.4 $
23  */

24 public class PortalWebTomcat4App extends PortalWebApp
25 {
26
27    private static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
28    private static final Class JavaDoc[] EMPTY_CLASS_ARRAY = new Class JavaDoc[0];
29
30    private String JavaDoc contextPath;
31    private ServletContext JavaDoc servletContext;
32
33    public PortalWebTomcat4App(WebApplication webApp) throws CannotCreatePortletWebAppException
34    {
35       super(webApp);
36       try
37       {
38          Object JavaDoc ctx = webApp.getAppData();
39          contextPath = getContextPath(ctx);
40          servletContext = getServletContext(ctx);
41       }
42       catch (Exception JavaDoc e)
43       {
44          throw new CannotCreatePortletWebAppException(e);
45       }
46    }
47
48    public void instrument() throws Exception JavaDoc
49    {
50       Object JavaDoc ctx = webApp.getAppData();
51       inject(ctx);
52    }
53
54    public ClassLoader JavaDoc getClassLoader()
55    {
56       throw new UnsupportedOperationException JavaDoc("Not supported yet");
57    }
58
59    public String JavaDoc getContextPath()
60    {
61       return contextPath;
62    }
63
64    public ServletContext JavaDoc getServletContext()
65    {
66       return servletContext;
67    }
68
69    public static void inject(Object JavaDoc standardContext) throws Exception JavaDoc
70    {
71       try
72       {
73          ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
74
75          Class JavaDoc standardContextClass = standardContext.getClass();
76
77          Class JavaDoc containerBaseClass = standardContextClass.getSuperclass();
78          Class JavaDoc containerItf = cl.loadClass("org.apache.catalina.Container");
79
80          // Inject the command servlet only one time, so we look if it exist already
81
Method JavaDoc findChildMethod = containerItf.getMethod("findChild", new Class JavaDoc[]{String JavaDoc.class});
82          Object JavaDoc commandServlet = findChildMethod.invoke(standardContext, new Object JavaDoc[]{"CommandServlet"});
83          if (commandServlet == null)
84          {
85             Method JavaDoc createWrapperMethod = standardContextClass.getMethod("createWrapper", EMPTY_CLASS_ARRAY);
86             Object JavaDoc wrapper = createWrapperMethod.invoke(standardContext, EMPTY_OBJECT_ARRAY);
87             Class JavaDoc wrapperClass = wrapper.getClass();
88             Object JavaDoc[] wrapperArray = (Object JavaDoc[])Array.newInstance(containerItf, 1);
89             wrapperArray[0] = wrapper;
90
91             Method JavaDoc setServletNameMethod = wrapperClass.getMethod("setServletName", new Class JavaDoc[]{String JavaDoc.class});
92             setServletNameMethod.invoke(wrapper, new Object JavaDoc[]{"CommandServlet"});
93
94             Method JavaDoc setServletClassMethod = wrapperClass.getMethod("setServletClass", new Class JavaDoc[]{String JavaDoc.class});
95             setServletClassMethod.invoke(wrapper, new Object JavaDoc[]{CommandServlet.class.getName()});
96
97             Method JavaDoc setLoadOnStartupMethod = wrapperClass.getMethod("setLoadOnStartup", new Class JavaDoc[]{int.class});
98             setLoadOnStartupMethod.invoke(wrapper, new Object JavaDoc[]{new Integer JavaDoc(0)});
99
100             Method JavaDoc addChildMethod = containerBaseClass.getMethod("addChild", new Class JavaDoc[]{containerItf});
101             addChildMethod.invoke(standardContext, new Object JavaDoc[]{wrapper});
102
103             Method JavaDoc loadOnStartupMethod = standardContextClass.getMethod("loadOnStartup", new Class JavaDoc[]{wrapperArray.getClass()});
104             loadOnStartupMethod.invoke(standardContext, new Object JavaDoc[]{wrapperArray});
105          }
106       }
107       catch (InvocationTargetException JavaDoc e)
108       {
109          e.getTargetException().printStackTrace();
110       }
111    }
112
113    public static ServletContext JavaDoc getServletContext(Object JavaDoc standardContext) throws Exception JavaDoc
114    {
115       Class JavaDoc standardContextClass = standardContext.getClass();
116       Method JavaDoc getServletContextMethod = standardContextClass.getMethod("getServletContext", EMPTY_CLASS_ARRAY);
117       return (ServletContext JavaDoc)getServletContextMethod.invoke(standardContext, EMPTY_OBJECT_ARRAY);
118    }
119
120    public static String JavaDoc getContextPath(Object JavaDoc standardContext) throws Exception JavaDoc
121    {
122       Class JavaDoc standardContextClass = standardContext.getClass();
123       Method JavaDoc getServletContextMethod = standardContextClass.getMethod("getPath", EMPTY_CLASS_ARRAY);
124       return (String JavaDoc)getServletContextMethod.invoke(standardContext, EMPTY_OBJECT_ARRAY);
125    }
126 }
127
Popular Tags