KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.MBeanServer JavaDoc;
12 import javax.management.ObjectName JavaDoc;
13 import javax.servlet.ServletContext JavaDoc;
14
15 import org.jboss.web.WebApplication;
16 import org.jboss.portal.server.servlet.CommandServlet;
17 import org.jboss.portal.server.servlet.CommandFilter;
18
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Array JavaDoc;
21
22 /**
23  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
24  * @version $Revision: 1.5 $
25  */

26 public class PortalWebTomcat5App extends PortalWebApp
27 {
28
29    private static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
30    private static final Class JavaDoc[] EMPTY_CLASS_ARRAY = new Class JavaDoc[0];
31
32    private final Object JavaDoc standardContext;
33    private String JavaDoc contextPath;
34    private ServletContext JavaDoc servletContext;
35    private ClassLoader JavaDoc loader;
36
37    public PortalWebTomcat5App(WebApplication webApp, MBeanServer JavaDoc server) throws CannotCreatePortletWebAppException
38    {
39       super(webApp);
40       try
41       {
42          ObjectName JavaDoc name = (ObjectName JavaDoc)webApp.getAppData();
43          standardContext = server.getAttribute(name, "managedResource");
44          contextPath = PortalWebTomcat4App.getContextPath(standardContext);
45          servletContext = PortalWebTomcat4App.getServletContext(standardContext);
46          loader = getClassLoader(server, name);
47       }
48       catch (Exception JavaDoc e)
49       {
50          throw new CannotCreatePortletWebAppException(e);
51       }
52    }
53
54    public void instrument() throws Exception JavaDoc
55    {
56       // Get class loader
57
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
58
59       // Get some class
60
Class JavaDoc containerItf = cl.loadClass("org.apache.catalina.Container");
61
62       // Inject the command servlet only one time, so we look if it exist already
63
Method JavaDoc findChildMethod = containerItf.getMethod("findChild", new Class JavaDoc[]{String JavaDoc.class});
64       Object JavaDoc commandServlet = findChildMethod.invoke(standardContext, new Object JavaDoc[]{"CommandServlet"});
65       if (commandServlet == null)
66       {
67          addFilterDef(cl);
68          addFilterMap(cl);
69          startFilter(cl);
70          addCommandServlet(cl);
71       }
72    }
73
74    public ClassLoader JavaDoc getClassLoader()
75    {
76       return loader;
77    }
78
79    public String JavaDoc getContextPath()
80    {
81       return contextPath;
82    }
83
84    public ServletContext JavaDoc getServletContext()
85    {
86       return servletContext;
87    }
88
89    private void startFilter(ClassLoader JavaDoc cl) throws Exception JavaDoc
90    {
91       // Get classes
92
Class JavaDoc standardContextClass = cl.loadClass("org.apache.catalina.core.StandardContext");
93
94       // Start filters
95
Method JavaDoc filterStartMethod = standardContextClass.getMethod("filterStart", new Class JavaDoc[0]);
96       filterStartMethod.invoke(standardContext, new Object JavaDoc[0]);
97    }
98
99    private void addFilterMap(ClassLoader JavaDoc cl) throws Exception JavaDoc
100    {
101       // Get classes
102
Class JavaDoc containerItf = cl.loadClass("org.apache.catalina.Container");
103       Class JavaDoc standardWrapperClass = cl.loadClass("org.apache.catalina.core.StandardWrapper");
104       Class JavaDoc filterMapClass = cl.loadClass("org.apache.catalina.deploy.FilterMap");
105       Class JavaDoc standardContextClass = cl.loadClass("org.apache.catalina.core.StandardContext");
106
107       // Get all the children
108
Method JavaDoc findChildrenMethod = containerItf.getMethod("findChildren", new Class JavaDoc[0]);
109       Object JavaDoc[] chilren = (Object JavaDoc[])findChildrenMethod.invoke(standardContext, new Object JavaDoc[0]);
110
111       // Iterate
112
for (int i = 0; i < chilren.length; i++)
113       {
114          Object JavaDoc child = chilren[i];
115          Class JavaDoc childClass = child.getClass();
116          if (childClass.equals(standardWrapperClass))
117          {
118             // If it is a wrapper we instrument
119
Object JavaDoc filterMap = filterMapClass.newInstance();
120
121             // Get servlet name
122
Method JavaDoc getServletNameMethod = standardWrapperClass.getMethod("getServletName", new Class JavaDoc[0]);
123             String JavaDoc servletName = (String JavaDoc)getServletNameMethod.invoke(child, new Object JavaDoc[0]);
124
125             // Configure filter map
126
Method JavaDoc setDispatcherMethod = filterMapClass.getMethod("setDispatcher", new Class JavaDoc[]{String JavaDoc.class});
127             setDispatcherMethod.invoke(filterMap, new Object JavaDoc[]{"INCLUDE"});
128             Method JavaDoc FilterMapSetFilterNameMethod = filterMapClass.getMethod("setFilterName", new Class JavaDoc[]{String JavaDoc.class});
129             FilterMapSetFilterNameMethod.invoke(filterMap, new Object JavaDoc[]{"CommandFilter"});
130             Method JavaDoc setServletNameMethod = filterMapClass.getMethod("setServletName", new Class JavaDoc[]{String JavaDoc.class});
131             setServletNameMethod.invoke(filterMap, new Object JavaDoc[]{servletName});
132             Method JavaDoc setURLPatternMethod = filterMapClass.getMethod("setURLPattern", new Class JavaDoc[]{String JavaDoc.class});
133             setURLPatternMethod.invoke(filterMap, new Object JavaDoc[]{null});
134
135             // Add the filter map
136
Method JavaDoc addFilterMapMethod = standardContextClass.getMethod("addFilterMap", new Class JavaDoc[]{filterMapClass});
137             addFilterMapMethod.invoke(standardContext, new Object JavaDoc[]{filterMap});
138          }
139       }
140    }
141
142    private void addCommandServlet(ClassLoader JavaDoc cl) throws Exception JavaDoc
143    {
144       //
145
Class JavaDoc containerItf = cl.loadClass("org.apache.catalina.Container");
146       Class JavaDoc standardContextClass = cl.loadClass("org.apache.catalina.core.StandardContext");
147       Class JavaDoc containerBaseClass = cl.loadClass("org.apache.catalina.core.ContainerBase");
148
149       // Ask the context to create a wrapper
150
Method JavaDoc createWrapperMethod = standardContextClass.getMethod("createWrapper", EMPTY_CLASS_ARRAY);
151       Object JavaDoc wrapper = createWrapperMethod.invoke(standardContext, EMPTY_OBJECT_ARRAY);
152       Class JavaDoc wrapperClass = wrapper.getClass();
153       Object JavaDoc[] wrapperArray = (Object JavaDoc[])Array.newInstance(containerItf, 1);
154       wrapperArray[0] = wrapper;
155
156       Method JavaDoc setServletNameMethod = wrapperClass.getMethod("setServletName", new Class JavaDoc[]{String JavaDoc.class});
157       setServletNameMethod.invoke(wrapper, new Object JavaDoc[]{"CommandServlet"});
158
159       Method JavaDoc setServletClassMethod = wrapperClass.getMethod("setServletClass", new Class JavaDoc[]{String JavaDoc.class});
160       setServletClassMethod.invoke(wrapper, new Object JavaDoc[]{CommandServlet.class.getName()});
161
162       Method JavaDoc setLoadOnStartupMethod = wrapperClass.getMethod("setLoadOnStartup", new Class JavaDoc[]{int.class});
163       setLoadOnStartupMethod.invoke(wrapper, new Object JavaDoc[]{new Integer JavaDoc(0)});
164
165       Method JavaDoc addChildMethod = containerBaseClass.getMethod("addChild", new Class JavaDoc[]{containerItf});
166       addChildMethod.invoke(standardContext, new Object JavaDoc[]{wrapper});
167
168       Method JavaDoc loadOnStartupMethod = standardContextClass.getMethod("loadOnStartup", new Class JavaDoc[]{wrapperArray.getClass()});
169       loadOnStartupMethod.invoke(standardContext, new Object JavaDoc[]{wrapperArray});
170    }
171
172    private void addFilterDef(ClassLoader JavaDoc cl) throws Exception JavaDoc
173    {
174       // Get class
175
Class JavaDoc standardContextClass = cl.loadClass("org.apache.catalina.core.StandardContext");
176       Class JavaDoc filterDefClass = cl.loadClass("org.apache.catalina.deploy.FilterDef");
177
178       // Create filter def
179
Object JavaDoc filterDef = filterDefClass.newInstance();
180
181       //
182
Method JavaDoc setDescriptionMethod = filterDefClass.getMethod("setDescription", new Class JavaDoc[]{String JavaDoc.class});
183       setDescriptionMethod.invoke(filterDef, new Object JavaDoc[]{""});
184
185       //
186
Method JavaDoc setDisplayNameMethod = filterDefClass.getMethod("setDisplayName", new Class JavaDoc[]{String JavaDoc.class});
187       setDisplayNameMethod.invoke(filterDef, new Object JavaDoc[]{""});
188
189       //
190
Method JavaDoc setFilterClassMethod = filterDefClass.getMethod("setFilterClass", new Class JavaDoc[]{String JavaDoc.class});
191       setFilterClassMethod.invoke(filterDef, new Object JavaDoc[]{CommandFilter.class.getName()});
192
193       //
194
Method JavaDoc FilterDefSetFilterNameMethod = filterDefClass.getMethod("setFilterName", new Class JavaDoc[]{String JavaDoc.class});
195       FilterDefSetFilterNameMethod.invoke(filterDef, new Object JavaDoc[]{"CommandFilter"});
196
197       //
198
Method JavaDoc setLargeIconMethod = filterDefClass.getMethod("setLargeIcon", new Class JavaDoc[]{String JavaDoc.class});
199       setLargeIconMethod.invoke(filterDef, new Object JavaDoc[]{""});
200
201       //
202
Method JavaDoc setSmallIconMethod = filterDefClass.getMethod("setSmallIcon", new Class JavaDoc[]{String JavaDoc.class});
203       setSmallIconMethod.invoke(filterDef, new Object JavaDoc[]{""});
204
205       // Finally add the filter def
206
Method JavaDoc addFilterDefMethod = standardContextClass.getMethod("addFilterDef", new Class JavaDoc[]{filterDefClass});
207       addFilterDefMethod.invoke(standardContext, new Object JavaDoc[]{filterDef});
208    }
209
210    private ClassLoader JavaDoc getClassLoader(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
211    {
212       Object JavaDoc loader = server.getAttribute(name, "loader");
213       Method JavaDoc getClassLoaderMethod = loader.getClass().getMethod("getClassLoader", new Class JavaDoc[0]);
214       ClassLoader JavaDoc classloader = (ClassLoader JavaDoc)getClassLoaderMethod.invoke(loader, new Object JavaDoc[0]);
215       return classloader;
216    }
217 }
218
Popular Tags