Author: julien(a)jboss.com
Date: 2007-08-19 17:19:12 -0400 (Sun, 19 Aug 2007)
New Revision: 7995
Added:
modules/web/trunk/web/src/main/org/jboss/portal/web/ServletContainer.java
modules/web/trunk/web/src/main/org/jboss/portal/web/WebApp.java
modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppEvent.java
modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppLifeCycleEvent.java
modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppListener.java
modules/web/trunk/web/src/main/org/jboss/portal/web/command/
modules/web/trunk/web/src/main/org/jboss/portal/web/command/CommandDispatcher.java
modules/web/trunk/web/src/main/org/jboss/portal/web/command/CommandServlet.java
modules/web/trunk/web/src/main/org/jboss/portal/web/command/ServletCommand.java
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6ServletContainerContext.java
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6WebApp.java
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6WebAppIdFactory.java
Modified:
modules/web/trunk/build/build-thirdparty.xml
modules/web/trunk/build/ide/intellij/idea60/modules/web/web.iml
modules/web/trunk/web/build.xml
modules/web/trunk/web/src/main/org/jboss/portal/web/spi/ServletContainerContext.java
Log:
started to improve and complete the web module by defining more precisely the contract
with the servlet container
Modified: modules/web/trunk/build/build-thirdparty.xml
===================================================================
--- modules/web/trunk/build/build-thirdparty.xml 2007-08-19 21:08:39 UTC (rev 7994)
+++ modules/web/trunk/build/build-thirdparty.xml 2007-08-19 21:19:12 UTC (rev 7995)
@@ -51,6 +51,7 @@
<componentref name="apache-log4j" version="1.2.8"/>
<componentref name="junit" version="3.8.1"/>
<componentref name="sun-servlet" version="2.4"/>
+ <componentref name="apache-tomcat" version="5.0.30"/>
</build>
<synchronizeinfo/>
Modified: modules/web/trunk/build/ide/intellij/idea60/modules/web/web.iml
===================================================================
--- modules/web/trunk/build/ide/intellij/idea60/modules/web/web.iml 2007-08-19 21:08:39
UTC (rev 7994)
+++ modules/web/trunk/build/ide/intellij/idea60/modules/web/web.iml 2007-08-19 21:19:12
UTC (rev 7995)
@@ -17,8 +17,30 @@
<SOURCES />
</library>
</orderEntry>
+ <orderEntry type="module-library">
+ <library>
+ <CLASSES>
+ <root
url="jar://$MODULE_DIR$/../../../../../../../jboss-portal-test/thirdparty/apache-log4j/lib/log4j.jar!/"
/>
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library">
+ <library>
+ <CLASSES>
+ <root
url="jar://$MODULE_DIR$/../../../../../../thirdparty/apache-tomcat/lib/catalina.jar!/"
/>
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
<orderEntryProperties />
</component>
+ <component name="VcsManagerConfiguration">
+ <option name="ACTIVE_VCS_NAME" value="svn" />
+ <option name="USE_PROJECT_VCS" value="false" />
+ </component>
<component name="copyright">
<Base>
<setting name="state" value="1" />
Modified: modules/web/trunk/web/build.xml
===================================================================
--- modules/web/trunk/web/build.xml 2007-08-19 21:08:39 UTC (rev 7994)
+++ modules/web/trunk/web/build.xml 2007-08-19 21:19:12 UTC (rev 7995)
@@ -92,6 +92,7 @@
<path refid="sun.servlet.classpath"/>
<path refid="apache.log4j.classpath"/>
<path refid="junit.junit.classpath"/>
+ <path refid="apache.tomcat.classpath"/>
</path>
<!-- Configure modules -->
@@ -149,6 +150,15 @@
<fileset dir="${build.classes}"/>
</jar>
+ <!-- portal-web.jar -->
+ <copy todir="${build.resources}/test/tomcat-war/WEB-INF/lib">
+ <fileset dir="${build.lib}"
includes="portal-web-lib.jar"/>
+ <fileset dir="${jboss.portal/modules/common.lib}"
includes="portal-common-lib.jar"/>
+ </copy>
+ <jar jarfile="${build.lib}/tomcat.war">
+ <fileset dir="${build.resources}/test/tomcat-war"/>
+ </jar>
+
</target>
<!-- ================================================================== -->
Added: modules/web/trunk/web/src/main/org/jboss/portal/web/ServletContainer.java
===================================================================
--- modules/web/trunk/web/src/main/org/jboss/portal/web/ServletContainer.java
(rev 0)
+++ modules/web/trunk/web/src/main/org/jboss/portal/web/ServletContainer.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,73 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web;
+
+import org.jboss.portal.web.spi.ServletContainerContext;
+
+/**
+ * A static registry for the servlet container context.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class ServletContainer
+{
+
+ /** . */
+ private static ServletContainerContext _context;
+
+ /**
+ * Register a servlet container context. The registration is considered as successful
if no existing context
+ * is already registered.
+ *
+ * @param context the servlet container context to register
+ * @return true if the registration is successful
+ * @throws IllegalArgumentException if the context is null
+ */
+ public synchronized static boolean registerContext(ServletContainerContext context)
throws IllegalArgumentException
+ {
+ if (context == null)
+ {
+ throw new IllegalArgumentException("No null context accepted");
+ }
+ if (_context == null)
+ {
+ _context = context;
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ /**
+ * Returns the existing servlet container context or null if none is registered.
+ *
+ * @return the registered servlet container context
+ */
+ public synchronized static ServletContainerContext getContext()
+ {
+ return _context;
+ }
+}
Added: modules/web/trunk/web/src/main/org/jboss/portal/web/WebApp.java
===================================================================
--- modules/web/trunk/web/src/main/org/jboss/portal/web/WebApp.java
(rev 0)
+++ modules/web/trunk/web/src/main/org/jboss/portal/web/WebApp.java 2007-08-19 21:19:12
UTC (rev 7995)
@@ -0,0 +1,82 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web;
+
+import java.net.URL;
+import java.io.InputStream;
+import java.io.IOException;
+
+/**
+ * Abstraction of a web application.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public interface WebApp
+{
+
+ /**
+ * Returns the URL of the WEB-INF directory of the web application.
+ *
+ * @return the URL of the WEB-INF directory
+ */
+ URL getWEBINFURL();
+
+ /**
+ * Returns an identifier for the web application valid in the scope of all the web
applications deployed.
+ *
+ * @return the web application identifier
+ */
+ String getId();
+
+ /**
+ * Returns the class loader of the web application.
+ *
+ * @return the web application class loader
+ */
+ ClassLoader getClassLoader();
+
+ /**
+ * Returns the context path of the web application.
+ *
+ * @return the web application context path
+ */
+ String getContextPath();
+
+ /**
+ * Import a file in the war file. The file could not be created for some reasons which
are :
+ * <ul>
+ * <li>The parent dir exists and is a file</li>
+ * <li>The parent dir does not exist and its creation failed</li>
+ * <li>An underlying exception occurs when writing bytes from the source
<code>Inputstream</code> to the target
<code>OutputStream</code></li>
+ * </ul>
+ *
+ * @param parentDirRelativePath the parent relative path in the web app starting from
the app root
+ * @param name the name the created file should have
+ * @param source the data of the target file
+ * @param overwrite if false and the file already exists nothing is done
+ * @return true if the file has been created
+ * @throws IOException if the file cannot be created
+ */
+ boolean importFile(String parentDirRelativePath, String name, InputStream source,
boolean overwrite) throws IOException;
+}
Added: modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppEvent.java
===================================================================
--- modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppEvent.java
(rev 0)
+++ modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppEvent.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,61 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web;
+
+/**
+ * Base class for web application events.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class WebAppEvent
+{
+
+ /** The web application. */
+ private final WebApp webApp;
+
+ /**
+ * Construct a new web application event.
+ *
+ * @param webApp the web application
+ * @throws IllegalArgumentException if the provided web application is null
+ */
+ public WebAppEvent(WebApp webApp) throws IllegalArgumentException
+ {
+ if (webApp == null)
+ {
+ throw new IllegalArgumentException("No null web application
accepted");
+ }
+ this.webApp = webApp;
+ }
+
+ /**
+ * Returns the web application.
+ *
+ * @return the web application
+ */
+ public WebApp getWebApp()
+ {
+ return webApp;
+ }
+}
Added: modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppLifeCycleEvent.java
===================================================================
--- modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppLifeCycleEvent.java
(rev 0)
+++
modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppLifeCycleEvent.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,72 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class WebAppLifeCycleEvent extends WebAppEvent
+{
+
+ /** Application is removed. */
+ public final static int REMOVED = 0;
+
+ /** Application is added. */
+ public final static int ADDED = 1;
+
+ /** The type of the life cycle which can be <code>ADDED</code> or
<code>REMOVED</code> . */
+ private final int type;
+
+ /**
+ * Creates a new web application life cycle event.
+ *
+ * @param webApp the web application
+ * @param type the life cycle type
+ * @throws IllegalArgumentException if the web application is null or the type value
is not valid
+ */
+ public WebAppLifeCycleEvent(WebApp webApp, int type)
+ throws IllegalArgumentException
+ {
+ super(webApp);
+
+ //
+ if (type < REMOVED || type > ADDED)
+ {
+ throw new IllegalArgumentException("Type " + type + " not
accepted");
+ }
+
+ //
+ this.type = type;
+ }
+
+ /**
+ * Returns the life cycle type.
+ *
+ * @return the life cycle type
+ */
+ public int getType()
+ {
+ return type;
+ }
+}
Added: modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppListener.java
===================================================================
--- modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppListener.java
(rev 0)
+++ modules/web/trunk/web/src/main/org/jboss/portal/web/WebAppListener.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,40 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web;
+
+/**
+ * Defines the contract for listening web application events.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public interface WebAppListener
+{
+
+ /**
+ * Signal a web application event to the listener.
+ *
+ * @param event the web application event
+ */
+ void onEvent(WebAppEvent event);
+}
Added: modules/web/trunk/web/src/main/org/jboss/portal/web/command/CommandDispatcher.java
===================================================================
--- modules/web/trunk/web/src/main/org/jboss/portal/web/command/CommandDispatcher.java
(rev 0)
+++
modules/web/trunk/web/src/main/org/jboss/portal/web/command/CommandDispatcher.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,139 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web.command;
+
+import org.jboss.portal.web.RequestDispatchCallback;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class CommandDispatcher
+{
+
+ /** . */
+ private static final ThreadLocal localResponse = new ThreadLocal();
+
+ public Object include(
+ ServletContext targetServletContext,
+ HttpServletRequest req,
+ HttpServletResponse resp,
+ RequestDispatchCallback callback,
+ Object handback) throws Exception
+ {
+ try
+ {
+ //
+ ServletCommand cmd = new InvokeNextCommand(targetServletContext, callback,
handback);
+
+ //
+ CommandServlet.include(req, resp, cmd, targetServletContext);
+
+ // Rethrow any InvocationException
+ Object response = localResponse.get();
+
+ //
+ if (response instanceof Exception)
+ {
+ throw (Exception)response;
+ }
+ else
+ {
+ return response;
+ }
+ }
+ catch (IOException e)
+ {
+ throw e;
+ }
+ catch (ServletException e)
+ {
+ // Unwrap underlying exception if any
+ Throwable t = e.getRootCause();
+
+ //
+ if (t instanceof Error)
+ {
+ // Rethrow any error
+ throw(Error)t;
+ }
+ else if (t instanceof RuntimeException)
+ {
+ throw (RuntimeException)t;
+ }
+ else if (t instanceof Exception)
+ {
+ throw (Exception)t;
+ }
+ else
+ {
+ throw e;
+ }
+ }
+ finally
+ {
+ localResponse.set(null);
+ }
+ }
+
+ public static class InvokeNextCommand implements ServletCommand
+ {
+
+ /** . */
+ private final ServletContext servletContext;
+
+ /** . */
+ private final RequestDispatchCallback invocation;
+
+ /** . */
+ private final Object handback;
+
+ public InvokeNextCommand(ServletContext servletContext, RequestDispatchCallback
invocation, Object handback)
+ {
+ this.servletContext = servletContext;
+ this.invocation = invocation;
+ this.handback = handback;
+ }
+
+ public void execute(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
+ {
+ try
+ {
+ Object response = invocation.doCallback(servletContext, req, resp,
handback);
+
+ //
+ localResponse.set(response);
+ }
+ catch (Exception e)
+ {
+ localResponse.set(e);
+ }
+ }
+ }
+}
Added: modules/web/trunk/web/src/main/org/jboss/portal/web/command/CommandServlet.java
===================================================================
--- modules/web/trunk/web/src/main/org/jboss/portal/web/command/CommandServlet.java
(rev 0)
+++
modules/web/trunk/web/src/main/org/jboss/portal/web/command/CommandServlet.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,149 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web.command;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.ServletContext;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * This servlet is used to execute command coming from another context through a
dispatching request.
+ * <p/>
+ * The command invocation is detyped to allow it working on redeploy and avoid class cast
exception.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class CommandServlet extends HttpServlet
+{
+
+ /** . */
+// private static final Logger log = Logger.getLogger(CommandServlet.class);
+
+ /** . */
+ private static final ThreadLocal localCmd = new ThreadLocal();
+
+ /**
+ * Execute a command after having performed a request dispatch in the target servlet
context.
+ *
+ * @param cmd the command to execute after the inclusion is done
+ * @param ctx the target servlet context
+ * @throws IOException
+ * @throws ServletException
+ */
+ public static void include(
+ HttpServletRequest req,
+ HttpServletResponse resp,
+ ServletCommand cmd,
+ ServletContext ctx) throws IOException, ServletException
+ {
+ try
+ {
+ localCmd.set(cmd);
+ RequestDispatcher switcher =
ctx.getRequestDispatcher("/jbossportlet");
+ switcher.include(req, resp);
+ }
+ finally
+ {
+ localCmd.set(null);
+ }
+ }
+
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
+ {
+ Object cmd = localCmd.get();
+ if (cmd != null)
+ {
+ try
+ {
+ Method methods = cmd.getClass().getMethod(
+ "execute",
+ new Class[]{
+ HttpServletRequest.class,
+ HttpServletResponse.class});
+ methods.invoke(cmd, new Object[]{req, resp});
+ }
+ catch (NoSuchMethodException e)
+ {
+ throw new ServletException("No execute method found on the
command", e);
+ }
+ catch (InvocationTargetException e)
+ {
+ // Log the wrappee
+ Throwable wrappee = e.getTargetException();
+ // log.error("Exception in command invocation", wrappee);
+
+ // Rethrow the checked ServletException
+ if (wrappee instanceof ServletException)
+ {
+ ServletException se = (ServletException)wrappee;
+ if (se.getCause() == null && se.getRootCause() != null)
+ {
+ se.initCause(se.getRootCause());
+ }
+ throw(ServletException)wrappee;
+ }
+ // Rethrow the checked IOException
+ if (wrappee instanceof IOException)
+ {
+ throw(IOException)wrappee;
+ }
+ // Rethrow RuntimeException
+ if (wrappee instanceof RuntimeException)
+ {
+ throw(RuntimeException)wrappee;
+ }
+ // Rethrow Error
+ if (wrappee instanceof Error)
+ {
+ throw(Error)wrappee;
+ }
+ // Here we wrap it and rethrow
+ ServletException se = new ServletException("The invoked command threw an
exception", wrappee);
+ se.initCause(wrappee);
+ throw se;
+ }
+ catch (IllegalAccessException e)
+ {
+ ServletException se = new ServletException("Unexpected
IllegalAccessException during command invocation", e);
+ se.initCause(e);
+ throw se;
+ }
+ }
+ else
+ {
+ throw new ServletException("No command found");
+ }
+ }
+
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
+ {
+ doGet(req, resp);
+ }
+}
Added: modules/web/trunk/web/src/main/org/jboss/portal/web/command/ServletCommand.java
===================================================================
--- modules/web/trunk/web/src/main/org/jboss/portal/web/command/ServletCommand.java
(rev 0)
+++
modules/web/trunk/web/src/main/org/jboss/portal/web/command/ServletCommand.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,39 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web.command;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.ServletException;
+import java.io.IOException;
+
+/**
+ * Command to be executed in the command servlet.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public interface ServletCommand
+{
+ void execute(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException;
+}
Added:
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6ServletContainerContext.java
===================================================================
---
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6ServletContainerContext.java
(rev 0)
+++
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6ServletContainerContext.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,253 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web.impl.tomcat;
+
+import org.jboss.portal.web.WebAppListener;
+import org.jboss.portal.web.RequestDispatchCallback;
+import org.jboss.portal.web.WebAppEvent;
+import org.jboss.portal.web.WebAppLifeCycleEvent;
+import org.jboss.portal.web.command.CommandDispatcher;
+import org.jboss.portal.web.spi.ServletContainerContext;
+import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.ContainerListener;
+import org.apache.catalina.LifecycleEvent;
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.Server;
+import org.apache.catalina.Service;
+import org.apache.catalina.Container;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Host;
+import org.apache.catalina.ContainerEvent;
+import org.apache.catalina.Context;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ * An implementation of the <code>ServletContainerContext</code> for Tomcat.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class TC6ServletContainerContext implements ServletContainerContext,
LifecycleListener, ContainerListener
+{
+
+ /** The monitored hosts. */
+ private final Set monitoredHosts = new HashSet();
+
+ /** The event listeners. */
+ private final ArrayList listeners = new ArrayList();
+
+ /** The web applications. */
+ private final Map webAppMap = new HashMap();
+
+ /** . */
+ private final CommandDispatcher dispatcher = new CommandDispatcher();
+
+ public Object include(
+ ServletContext targetServletContext,
+ HttpServletRequest request,
+ HttpServletResponse response,
+ RequestDispatchCallback callback,
+ Object handback) throws Exception
+ {
+ return dispatcher.include(targetServletContext, request, response, callback,
handback);
+ }
+
+ public synchronized boolean addWebAppListener(WebAppListener listener)
+ {
+ if (listener == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (listeners.contains(listener))
+ {
+ return false;
+ }
+ listeners.add(listener);
+ for (Iterator i = webAppMap.values().iterator(); i.hasNext();)
+ {
+ TC6WebApp webApp = (TC6WebApp)i.next();
+ WebAppLifeCycleEvent event = new WebAppLifeCycleEvent(webApp,
WebAppLifeCycleEvent.ADDED);
+ fireEvent(listener, event);
+ }
+ return true;
+ }
+
+ public synchronized boolean removeWebAppListener(WebAppListener listener)
+ {
+ if (listener == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (listeners.remove(listener))
+ {
+ for (Iterator i = webAppMap.values().iterator(); i.hasNext();)
+ {
+ TC6WebApp webApp = (TC6WebApp)i.next();
+ WebAppLifeCycleEvent event = new WebAppLifeCycleEvent(webApp,
WebAppLifeCycleEvent.REMOVED);
+ fireEvent(listener, event);
+ }
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ public synchronized void lifecycleEvent(LifecycleEvent lifecycleEvent)
+ {
+ Lifecycle lifecycle = lifecycleEvent.getLifecycle();
+ System.out.println("lifecycle = " + lifecycle);
+ if (lifecycle instanceof Server)
+ {
+ Server server = (Server)lifecycle;
+ Service[] services = server.findServices();
+ for (int i = 0; i < services.length; i++)
+ {
+ Service service = services[i];
+ handleContainer(service.getContainer());
+ }
+ }
+ }
+
+ public synchronized void containerEvent(ContainerEvent containerEvent)
+ {
+ if (containerEvent.getData() instanceof Context)
+ {
+ Context context = (Context)containerEvent.getData();
+ String webAppId = TC6WebAppIdFactory.createId(context);
+
+ if ("addChild".equals(containerEvent.getType()))
+ {
+ System.out.println("Context added " + context.getPath());
+ try
+ {
+ if (webAppMap.containsKey(webAppId))
+ {
+ System.out.println("Web application " + webAppId + " is
already registered");
+ }
+ else
+ {
+ TC6WebApp webApp = new TC6WebApp(context);
+ System.out.println("Web application " + webAppId + "
registration");
+ webApp.instrument();
+ webAppMap.put(webApp.getId(), webApp);
+ fireEvent(new WebAppLifeCycleEvent(webApp,
WebAppLifeCycleEvent.ADDED));
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ else if ("removeChild".equals(containerEvent.getType()))
+ {
+ try
+ {
+ TC6WebApp webApp = (TC6WebApp)webAppMap.remove(webAppId);
+ if (webApp != null)
+ {
+ System.out.println("Web application " + webAppId + "
cleanup");
+ fireEvent(new WebAppLifeCycleEvent(webApp,
WebAppLifeCycleEvent.REMOVED));
+ webApp.cleanup();
+ }
+ else
+ {
+ System.out.println("Web application " + webAppId + " is
already registered");
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+// synchronized(webAppMap)
+// {
+// for (Iterator i = webAppMap.values().iterator();i.hasNext();)
+// {
+// TC6WebApp webApp = (TC6WebApp)i.next();
+// System.out.println("Web application " + webApp.getId() + "
unregistration");
+// webApp.cleanup();
+// i.remove();
+// }
+// }
+
+ private void handleContainer(Container container)
+ {
+ if (container instanceof Engine)
+ {
+ Engine engine = (Engine)container;
+ Container[] childrenContainers = engine.findChildren();
+ for (int i = 0; i < childrenContainers.length; i++)
+ {
+ Container childContainer = childrenContainers[i];
+ handleContainer(childContainer);
+ }
+ }
+ else if (container instanceof Host)
+ {
+ Host host = (Host)container;
+ if (!monitoredHosts.contains(host.getName()))
+ {
+ host.addContainerListener(this);
+ monitoredHosts.add(host.getName());
+ }
+ }
+ else
+ {
+ System.out.println("Container not handled " + container);
+ }
+ }
+
+ private void fireEvent(WebAppListener listener, WebAppEvent event)
+ {
+ try
+ {
+ listener.onEvent(event);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ private void fireEvent(WebAppEvent event)
+ {
+ for (Iterator i = listeners.iterator();i.hasNext();)
+ {
+ fireEvent((WebAppListener)i.next(), event);
+ }
+ }
+}
Added: modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6WebApp.java
===================================================================
--- modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6WebApp.java
(rev 0)
+++
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6WebApp.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,139 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web.impl.tomcat;
+
+import org.w3c.dom.Document;
+import org.apache.catalina.Context;
+import org.apache.catalina.Wrapper;
+import org.jboss.portal.web.command.CommandServlet;
+import org.jboss.portal.web.WebApp;
+
+import javax.servlet.ServletContext;
+import java.net.URL;
+import java.io.InputStream;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class TC6WebApp implements WebApp
+{
+
+ /** The logger. */
+// protected final Logger log = Logger.getLogger(getClass());
+
+ /** . */
+ private Document descriptor;
+
+ /** . */
+ private ServletContext servletContext;
+
+ /** . */
+ private ClassLoader loader;
+
+ /** . */
+ private URL webinfurl;
+
+ /** . */
+ private String id;
+
+ /** . */
+ private String contextPath;
+
+ /** . */
+ private final Context context;
+
+ /** . */
+ private Wrapper commandServlet;
+
+ TC6WebApp(Context context) throws Exception
+ {
+ this.context = context;
+
+ //
+ servletContext = context.getServletContext();
+ loader = context.getLoader().getClassLoader();
+ id = context.getPath();
+ contextPath = context.getPath();
+ webinfurl = servletContext.getResource("/WEB-INF");
+ }
+
+ void instrument() throws Exception
+ {
+ try
+ {
+ commandServlet = context.createWrapper();
+ commandServlet.setName("JBossServlet");
+ commandServlet.setLoadOnStartup(0);
+ commandServlet.setServletClass(CommandServlet.class.getName());
+ context.addChild(commandServlet);
+ context.addServletMapping("/jbossportlet", "JBossServlet");
+ }
+ catch (Exception e)
+ {
+ cleanup();
+ throw e;
+ }
+ }
+
+ void cleanup()
+ {
+ if (commandServlet != null)
+ {
+ try
+ {
+ context.removeServletMapping("jbossportlet");
+ context.removeChild(commandServlet);
+ }
+ catch (Exception e)
+ {
+ }
+ }
+ }
+
+ public URL getWEBINFURL()
+ {
+ return webinfurl;
+ }
+
+ public String getId()
+ {
+ return id;
+ }
+
+ public ClassLoader getClassLoader()
+ {
+ return loader;
+ }
+
+ public String getContextPath()
+ {
+ return contextPath;
+ }
+
+ public boolean importFile(String parentDirRelativePath, String name, InputStream
source, boolean overwrite) throws IOException
+ {
+ return false;
+ }
+}
Added:
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6WebAppIdFactory.java
===================================================================
---
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6WebAppIdFactory.java
(rev 0)
+++
modules/web/trunk/web/src/main/org/jboss/portal/web/impl/tomcat/TC6WebAppIdFactory.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -0,0 +1,37 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.web.impl.tomcat;
+
+import org.apache.catalina.Context;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class TC6WebAppIdFactory
+{
+ public static String createId(Context context)
+ {
+ return context.getPath();
+ }
+}
Modified:
modules/web/trunk/web/src/main/org/jboss/portal/web/spi/ServletContainerContext.java
===================================================================
---
modules/web/trunk/web/src/main/org/jboss/portal/web/spi/ServletContainerContext.java 2007-08-19
21:08:39 UTC (rev 7994)
+++
modules/web/trunk/web/src/main/org/jboss/portal/web/spi/ServletContainerContext.java 2007-08-19
21:19:12 UTC (rev 7995)
@@ -23,13 +23,15 @@
package org.jboss.portal.web.spi;
import org.jboss.portal.web.RequestDispatchCallback;
+import org.jboss.portal.web.WebAppListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
/**
- * Defines the service provider interface that a servlet container offers.
+ * Defines the service provider interface for a servlet container. It is an attempt to
abstract the non
+ * portable services required by a portal with respect to the web container layer.
*
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
@@ -52,4 +54,30 @@
HttpServletResponse response,
RequestDispatchCallback callback,
Object handback) throws Exception;
+
+ /**
+ * Try to add a web application listener on the servlet container context. If the
listener is already registered then it
+ * has no effect and the method call returns false, otherwise it adds the listener and
returns true. When a listener
+ * is added it will receive add notifications for all web applications already
registered. When a new web application
+ * is added in the context, all listeners will receive a notification.
+ *
+ * @param listener the listener
+ * @return true if the listener has been added
+ * @throws IllegalArgumentException if the listener argument is null
+ */
+ boolean addWebAppListener(WebAppListener listener) throws IllegalArgumentException;
+
+ /**
+ * Try to remove a web application listener on the servlet container context. If the
listener is not registered then
+ * it has no effect and the method call returns false, otherwise it removes the
listener and returns true. When a
+ * listener is removed it will receive remove notifications for all the web
applications that are still registered.
+ * When a registered web application is removes from the context, all listeners will
receive a notification.
+ *
+ * @param listener
+ * @return
+ * @throws IllegalArgumentException
+ */
+ boolean removeWebAppListener(WebAppListener listener) throws
IllegalArgumentException;
+
+
}