1 9 package org.jboss.portal.server.deployment; 10 11 import javax.management.MBeanServer ; 12 import javax.management.ObjectName ; 13 import javax.servlet.ServletContext ; 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 ; 20 import java.lang.reflect.Array ; 21 22 26 public class PortalWebTomcat5App extends PortalWebApp 27 { 28 29 private static final Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 30 private static final Class [] EMPTY_CLASS_ARRAY = new Class [0]; 31 32 private final Object standardContext; 33 private String contextPath; 34 private ServletContext servletContext; 35 private ClassLoader loader; 36 37 public PortalWebTomcat5App(WebApplication webApp, MBeanServer server) throws CannotCreatePortletWebAppException 38 { 39 super(webApp); 40 try 41 { 42 ObjectName name = (ObjectName )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 e) 49 { 50 throw new CannotCreatePortletWebAppException(e); 51 } 52 } 53 54 public void instrument() throws Exception 55 { 56 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 58 59 Class containerItf = cl.loadClass("org.apache.catalina.Container"); 61 62 Method findChildMethod = containerItf.getMethod("findChild", new Class []{String .class}); 64 Object commandServlet = findChildMethod.invoke(standardContext, new Object []{"CommandServlet"}); 65 if (commandServlet == null) 66 { 67 addFilterDef(cl); 68 addFilterMap(cl); 69 startFilter(cl); 70 addCommandServlet(cl); 71 } 72 } 73 74 public ClassLoader getClassLoader() 75 { 76 return loader; 77 } 78 79 public String getContextPath() 80 { 81 return contextPath; 82 } 83 84 public ServletContext getServletContext() 85 { 86 return servletContext; 87 } 88 89 private void startFilter(ClassLoader cl) throws Exception 90 { 91 Class standardContextClass = cl.loadClass("org.apache.catalina.core.StandardContext"); 93 94 Method filterStartMethod = standardContextClass.getMethod("filterStart", new Class [0]); 96 filterStartMethod.invoke(standardContext, new Object [0]); 97 } 98 99 private void addFilterMap(ClassLoader cl) throws Exception 100 { 101 Class containerItf = cl.loadClass("org.apache.catalina.Container"); 103 Class standardWrapperClass = cl.loadClass("org.apache.catalina.core.StandardWrapper"); 104 Class filterMapClass = cl.loadClass("org.apache.catalina.deploy.FilterMap"); 105 Class standardContextClass = cl.loadClass("org.apache.catalina.core.StandardContext"); 106 107 Method findChildrenMethod = containerItf.getMethod("findChildren", new Class [0]); 109 Object [] chilren = (Object [])findChildrenMethod.invoke(standardContext, new Object [0]); 110 111 for (int i = 0; i < chilren.length; i++) 113 { 114 Object child = chilren[i]; 115 Class childClass = child.getClass(); 116 if (childClass.equals(standardWrapperClass)) 117 { 118 Object filterMap = filterMapClass.newInstance(); 120 121 Method getServletNameMethod = standardWrapperClass.getMethod("getServletName", new Class [0]); 123 String servletName = (String )getServletNameMethod.invoke(child, new Object [0]); 124 125 Method setDispatcherMethod = filterMapClass.getMethod("setDispatcher", new Class []{String .class}); 127 setDispatcherMethod.invoke(filterMap, new Object []{"INCLUDE"}); 128 Method FilterMapSetFilterNameMethod = filterMapClass.getMethod("setFilterName", new Class []{String .class}); 129 FilterMapSetFilterNameMethod.invoke(filterMap, new Object []{"CommandFilter"}); 130 Method setServletNameMethod = filterMapClass.getMethod("setServletName", new Class []{String .class}); 131 setServletNameMethod.invoke(filterMap, new Object []{servletName}); 132 Method setURLPatternMethod = filterMapClass.getMethod("setURLPattern", new Class []{String .class}); 133 setURLPatternMethod.invoke(filterMap, new Object []{null}); 134 135 Method addFilterMapMethod = standardContextClass.getMethod("addFilterMap", new Class []{filterMapClass}); 137 addFilterMapMethod.invoke(standardContext, new Object []{filterMap}); 138 } 139 } 140 } 141 142 private void addCommandServlet(ClassLoader cl) throws Exception 143 { 144 Class containerItf = cl.loadClass("org.apache.catalina.Container"); 146 Class standardContextClass = cl.loadClass("org.apache.catalina.core.StandardContext"); 147 Class containerBaseClass = cl.loadClass("org.apache.catalina.core.ContainerBase"); 148 149 Method createWrapperMethod = standardContextClass.getMethod("createWrapper", EMPTY_CLASS_ARRAY); 151 Object wrapper = createWrapperMethod.invoke(standardContext, EMPTY_OBJECT_ARRAY); 152 Class wrapperClass = wrapper.getClass(); 153 Object [] wrapperArray = (Object [])Array.newInstance(containerItf, 1); 154 wrapperArray[0] = wrapper; 155 156 Method setServletNameMethod = wrapperClass.getMethod("setServletName", new Class []{String .class}); 157 setServletNameMethod.invoke(wrapper, new Object []{"CommandServlet"}); 158 159 Method setServletClassMethod = wrapperClass.getMethod("setServletClass", new Class []{String .class}); 160 setServletClassMethod.invoke(wrapper, new Object []{CommandServlet.class.getName()}); 161 162 Method setLoadOnStartupMethod = wrapperClass.getMethod("setLoadOnStartup", new Class []{int.class}); 163 setLoadOnStartupMethod.invoke(wrapper, new Object []{new Integer (0)}); 164 165 Method addChildMethod = containerBaseClass.getMethod("addChild", new Class []{containerItf}); 166 addChildMethod.invoke(standardContext, new Object []{wrapper}); 167 168 Method loadOnStartupMethod = standardContextClass.getMethod("loadOnStartup", new Class []{wrapperArray.getClass()}); 169 loadOnStartupMethod.invoke(standardContext, new Object []{wrapperArray}); 170 } 171 172 private void addFilterDef(ClassLoader cl) throws Exception 173 { 174 Class standardContextClass = cl.loadClass("org.apache.catalina.core.StandardContext"); 176 Class filterDefClass = cl.loadClass("org.apache.catalina.deploy.FilterDef"); 177 178 Object filterDef = filterDefClass.newInstance(); 180 181 Method setDescriptionMethod = filterDefClass.getMethod("setDescription", new Class []{String .class}); 183 setDescriptionMethod.invoke(filterDef, new Object []{""}); 184 185 Method setDisplayNameMethod = filterDefClass.getMethod("setDisplayName", new Class []{String .class}); 187 setDisplayNameMethod.invoke(filterDef, new Object []{""}); 188 189 Method setFilterClassMethod = filterDefClass.getMethod("setFilterClass", new Class []{String .class}); 191 setFilterClassMethod.invoke(filterDef, new Object []{CommandFilter.class.getName()}); 192 193 Method FilterDefSetFilterNameMethod = filterDefClass.getMethod("setFilterName", new Class []{String .class}); 195 FilterDefSetFilterNameMethod.invoke(filterDef, new Object []{"CommandFilter"}); 196 197 Method setLargeIconMethod = filterDefClass.getMethod("setLargeIcon", new Class []{String .class}); 199 setLargeIconMethod.invoke(filterDef, new Object []{""}); 200 201 Method setSmallIconMethod = filterDefClass.getMethod("setSmallIcon", new Class []{String .class}); 203 setSmallIconMethod.invoke(filterDef, new Object []{""}); 204 205 Method addFilterDefMethod = standardContextClass.getMethod("addFilterDef", new Class []{filterDefClass}); 207 addFilterDefMethod.invoke(standardContext, new Object []{filterDef}); 208 } 209 210 private ClassLoader getClassLoader(MBeanServer server, ObjectName name) throws Exception 211 { 212 Object loader = server.getAttribute(name, "loader"); 213 Method getClassLoaderMethod = loader.getClass().getMethod("getClassLoader", new Class [0]); 214 ClassLoader classloader = (ClassLoader )getClassLoaderMethod.invoke(loader, new Object [0]); 215 return classloader; 216 } 217 } 218 | Popular Tags |