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