Author: mstruk
Date: 2011-05-19 10:56:12 -0400 (Thu, 19 May 2011)
New Revision: 6499
Added:
sandbox/as7_support/wci/jboss/jboss7/
sandbox/as7_support/wci/jboss/jboss7/pom.xml
sandbox/as7_support/wci/jboss/jboss7/src/
sandbox/as7_support/wci/jboss/jboss7/src/main/
sandbox/as7_support/wci/jboss/jboss7/src/main/java/
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7ContainerServlet.java
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7ServletContainerContext.java
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7WebAppContext.java
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7WebLifecycleListener.java
Log:
wci-jboss7
Added: sandbox/as7_support/wci/jboss/jboss7/pom.xml
===================================================================
--- sandbox/as7_support/wci/jboss/jboss7/pom.xml (rev 0)
+++ sandbox/as7_support/wci/jboss/jboss7/pom.xml 2011-05-19 14:56:12 UTC (rev 6499)
@@ -0,0 +1,29 @@
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.gatein.wci</groupId>
+ <artifactId>wci-parent</artifactId>
+ <version>2.1.0-Beta03-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>wci-jboss7</artifactId>
+ <packaging>jar</packaging>
+ <name>GateIn - WCI JBoss 7 compatibility component</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.gatein.wci</groupId>
+ <artifactId>wci-wci</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.web</groupId>
+ <artifactId>jbossweb</artifactId>
+ <version>3.0.0-CR1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.spec.javax.servlet</groupId>
+ <artifactId>jboss-servlet-api_3.0_spec</artifactId>
+ <version>1.0.0.Final</version>
+ </dependency>
+ </dependencies>
+
+</project>
Added:
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7ContainerServlet.java
===================================================================
---
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7ContainerServlet.java
(rev 0)
+++
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7ContainerServlet.java 2011-05-19
14:56:12 UTC (rev 6499)
@@ -0,0 +1,151 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2011, 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.gatein.wci.jboss;
+
+import org.apache.catalina.Container;
+import org.apache.catalina.ContainerServlet;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Wrapper;
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+
+/**
+ * @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
+ */
+public class JB7ContainerServlet extends HttpServlet implements ContainerServlet
+{
+ private static final Logger log = LoggerFactory.getLogger(JB7ContainerServlet.class);
+
+ /** Servlet context init parameter name that can be used to turn off cross-context
logout */
+ private static final String CROSS_CONTEXT_LOGOUT_KEY =
"org.gatein.wci.cross_context_logout";
+
+ /** . */
+ private Wrapper wrapper;
+
+ /** . */
+ private JB7ServletContainerContext containerContext;
+
+ /** . */
+ private boolean started;
+
+ public Wrapper getWrapper()
+ {
+ return wrapper;
+ }
+
+ public void setWrapper(Wrapper wrapper)
+ {
+ this.wrapper = wrapper;
+
+ //
+ if (wrapper != null)
+ {
+ attemptStart();
+ }
+ else
+ {
+ attemptStop();
+ }
+ }
+
+ public void init() throws ServletException
+ {
+ started = true;
+
+ //
+ attemptStart();
+ }
+
+ public void destroy()
+ {
+ started = false;
+
+ //
+ attemptStop();
+ }
+
+ private void attemptStart()
+ {
+ if (started && wrapper != null)
+ {
+ start();
+ }
+ }
+
+ private void attemptStop()
+ {
+ if (!started || wrapper == null)
+ {
+ stop();
+ }
+ }
+
+ private void start()
+ {
+ Container container = wrapper;
+ while (container.getParent() != null)
+ {
+ container = container.getParent();
+ if (container instanceof Engine)
+ {
+ Engine engine = (Engine) container;
+ containerContext = new JB7ServletContainerContext(engine);
+ containerContext.setCrossContextLogout(getCrossContextLogoutConfig());
+ containerContext.start();
+ break;
+ }
+ }
+ }
+
+ private void stop()
+ {
+ if (containerContext != null)
+ {
+ containerContext.stop();
+
+ //
+ containerContext = null;
+ }
+ }
+
+ private boolean getCrossContextLogoutConfig()
+ {
+ String val = getServletContext().getInitParameter(CROSS_CONTEXT_LOGOUT_KEY);
+ if (val == null || Boolean.valueOf(val))
+ {
+ return true;
+ }
+
+ if (!"false".equalsIgnoreCase(val))
+ {
+ log.warn("Context init param " + CROSS_CONTEXT_LOGOUT_KEY + "
value is invalid: " + val + " - falling back to: false");
+ }
+
+ log.info("Cross-context session invalidation on logout disabled");
+ return false;
+ }
+}
+
Added:
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7ServletContainerContext.java
===================================================================
---
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7ServletContainerContext.java
(rev 0)
+++
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7ServletContainerContext.java 2011-05-19
14:56:12 UTC (rev 6499)
@@ -0,0 +1,388 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2011, 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.gatein.wci.jboss;
+
+import org.apache.catalina.Container;
+import org.apache.catalina.ContainerEvent;
+import org.apache.catalina.ContainerListener;
+import org.apache.catalina.Context;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Host;
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.LifecycleEvent;
+import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.core.StandardContext;
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
+import org.gatein.wci.RequestDispatchCallback;
+import org.gatein.wci.ServletContainerVisitor;
+import org.gatein.wci.WebApp;
+import org.gatein.wci.authentication.GenericAuthentication;
+import org.gatein.wci.authentication.TicketService;
+import org.gatein.wci.command.CommandDispatcher;
+import org.gatein.wci.impl.DefaultServletContainerFactory;
+import org.gatein.wci.security.Credentials;
+import org.gatein.wci.spi.ServletContainerContext;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
+ */
+public class JB7ServletContainerContext implements ServletContainerContext,
ContainerListener, LifecycleListener
+{
+ private final static Logger log =
LoggerFactory.getLogger(JB7ServletContainerContext.class);
+
+ /** . */
+ //TODO: maybe we should rename this to /jbossgateinservlet? but older versions of
jboss
+ // use the tomcat ServletContainerContext.
+ private final CommandDispatcher dispatcher = new
CommandDispatcher("/tomcatgateinservlet");
+
+ /** The monitored hosts. */
+ private final Set<String> monitoredHosts = new HashSet<String>();
+
+ /** The monitored contexts. */
+ private final Set<String> monitoredContexts = new HashSet<String>();
+
+ /** . */
+ private final Engine engine;
+
+ /** . */
+ private Registration registration;
+
+ /** Perform cross-context session invalidation on logout, or not */
+ private boolean crossContextLogout = true;
+
+ /** Used to perform 2-pass init in case any container events occur during first pass
init */
+ private int initCount = 1;
+
+
+ public JB7ServletContainerContext(Engine engine)
+ {
+ this.engine = engine;
+ }
+
+ public Object include(ServletContext targetServletContext, HttpServletRequest request,
HttpServletResponse response,
+ RequestDispatchCallback callback, Object handback) throws ServletException,
IOException
+ {
+ return dispatcher.include(targetServletContext, request, response, callback,
handback);
+ }
+
+ public void setCallback(Registration registration)
+ {
+ this.registration = registration;
+ }
+
+ public void unsetCallback(Registration registration)
+ {
+ this.registration = null;
+ }
+
+ public void setCrossContextLogout(boolean val)
+ {
+ crossContextLogout = val;
+ }
+
+ public void login(HttpServletRequest request, HttpServletResponse response,
Credentials credentials, long validityMillis) throws ServletException, IOException
+ {
+ login(request, response, credentials, validityMillis, null);
+ }
+
+ public void login(HttpServletRequest request, HttpServletResponse response,
Credentials credentials, long validityMillis, String initialURI) throws ServletException,
IOException
+ {
+ if (initialURI == null)
+ {
+ initialURI = request.getRequestURI();
+ }
+ try
+ {
+ request.login(credentials.getUsername(), credentials.getPassword());
+ response.sendRedirect(response.encodeRedirectURL(initialURI));
+ }
+ catch (ServletException se)
+ {
+ se.printStackTrace();
+ try
+ {
+ String ticket = GenericAuthentication.TICKET_SERVICE.createTicket(new
Credentials(credentials.getUsername(), credentials.getUsername()),
+ TicketService.DEFAULT_VALIDITY);
+ String url = "j_security_check?j_username=" +
credentials.getUsername() + "&j_password=" + ticket +
"&initialURI=" + initialURI;
+ url = response.encodeRedirectURL(url);
+ response.sendRedirect(url);
+ response.flushBuffer();
+ }
+ catch (Exception ignore)
+ {
+ }
+ }
+ }
+
+
+ public void logout(HttpServletRequest request, HttpServletResponse response) throws
ServletException
+ {
+ HttpSession sess = request.getSession(false);
+ request.logout();
+
+ if (sess == null)
+ return;
+
+ if (!crossContextLogout)
+ return;
+
+ final String sessId = sess.getId();
+ DefaultServletContainerFactory.getInstance().getServletContainer().visit(new
ServletContainerVisitor()
+ {
+ public void accept(WebApp webApp)
+ {
+ webApp.invalidateSession(sessId);
+ }
+ });
+ }
+
+ public String getContainerInfo()
+ {
+ return "JBossAS/7.x";
+ }
+
+ public synchronized void containerEvent(ContainerEvent event)
+ {
+ if (initCount > 0)
+ {
+ initCount++;
+ return;
+ }
+
+ processContainerEvent(event);
+ }
+
+ private void processContainerEvent(ContainerEvent event)
+ {
+ if (event.getData() instanceof Host)
+ {
+ Host host = (Host) event.getData();
+
+ if (Container.ADD_CHILD_EVENT.equals(event.getType()))
+ {
+ registerHost(host);
+ }
+ else if (Container.REMOVE_CHILD_EVENT.equals(event.getType()))
+ {
+ unregisterHost(host);
+ }
+ }
+ else if (event.getData() instanceof StandardContext)
+ {
+ StandardContext context = (StandardContext) event.getData();
+
+ if (Container.ADD_CHILD_EVENT.equals(event.getType()))
+ {
+ registerContext(context);
+ }
+ else if (Container.REMOVE_CHILD_EVENT.equals(event.getType()))
+ {
+ unregisterContext(context);
+ }
+ }
+ }
+
+ public void lifecycleEvent(LifecycleEvent event)
+ {
+ if (event.getSource() instanceof Context)
+ {
+ Context context = (Context) event.getSource();
+
+ if (Lifecycle.AFTER_START_EVENT.equals(event.getType()))
+ {
+ start(context);
+ }
+ else if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType()))
+ {
+ stop(context);
+ }
+ }
+ }
+
+ void start()
+ {
+ DefaultServletContainerFactory.registerContext(this);
+ engine.addContainerListener(this);
+
+ boolean again = false;
+ do
+ {
+ Container[] childrenContainers = engine.findChildren();
+ for (Container childContainer : childrenContainers)
+ {
+ if (childContainer instanceof Host)
+ {
+ Host host = (Host) childContainer;
+ registerHost(host);
+ }
+ }
+
+ synchronized(this)
+ {
+ again = initCount > 1;
+ initCount = again ? 1 : 0;
+ }
+ } while (again);
+ }
+
+ void stop()
+ {
+ engine.removeContainerListener(this);
+
+ Container[] childrenContainers = engine.findChildren();
+ for (Container childContainer : childrenContainers)
+ {
+ if (childContainer instanceof Host)
+ {
+ Host host = (Host) childContainer;
+ unregisterHost(host);
+ }
+ }
+
+ registration.cancel();
+ registration = null;
+ }
+
+ /**
+ * Register an host for registration which means that we fire events for all the
contexts it contains and we
+ * subscribe for its life cycle events. If the host is already monitored nothing is
done.
+ *
+ * @param host the host to register for monitoring
+ */
+ private void registerHost(Host host)
+ {
+ if (!monitoredHosts.contains(host.getName()))
+ {
+ host.addContainerListener(this);
+ monitoredHosts.add(host.getName());
+ }
+
+ Container[] childrenContainers = host.findChildren();
+ for (Container childContainer : childrenContainers)
+ {
+ if (childContainer instanceof StandardContext)
+ {
+ StandardContext context = (StandardContext) childContainer;
+ registerContext(context);
+ }
+ }
+ }
+
+ private void unregisterHost(Host host)
+ {
+ if (monitoredHosts.contains(host.getName()))
+ {
+ monitoredHosts.remove(host.getName());
+ host.removeContainerListener(this);
+
+ Container[] childrenContainers = host.findChildren();
+ for (Container childContainer : childrenContainers)
+ {
+ if (childContainer instanceof StandardContext)
+ {
+ StandardContext context = (StandardContext) childContainer;
+ unregisterContext(context);
+ }
+ }
+ }
+ }
+
+ private void registerContext(StandardContext context)
+ {
+ if (!monitoredContexts.contains(context.getName()))
+ {
+ context.addLifecycleListener(this);
+
+ if (context.getState() == 1)
+ {
+ start(context);
+ }
+ monitoredContexts.add(context.getName());
+ }
+ }
+
+ private void unregisterContext(StandardContext context)
+ {
+ if (monitoredContexts.contains(context.getName()))
+ {
+ monitoredContexts.remove(context.getName());
+
+ if (context.getState() == 1)
+ {
+ stop(context);
+ }
+ context.removeLifecycleListener(this);
+ }
+ }
+
+ private void start(Context context)
+ {
+ try
+ {
+ log.debug("Context added " + context.getPath());
+
+ // we need to preemptively synchronize on context to prevent deadlocks from
+ // opposite order locking on jboss-as7
+ synchronized(context)
+ {
+ JB7WebAppContext webAppContext = new JB7WebAppContext(context);
+
+ //
+ if (registration != null)
+ {
+ registration.registerWebApp(webAppContext);
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ private void stop(Context context)
+ {
+ try
+ {
+ if (registration != null)
+ {
+ registration.unregisterWebApp(context.getPath());
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+}
+
Added:
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7WebAppContext.java
===================================================================
---
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7WebAppContext.java
(rev 0)
+++
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7WebAppContext.java 2011-05-19
14:56:12 UTC (rev 6499)
@@ -0,0 +1,153 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2011, 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.gatein.wci.jboss;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.Manager;
+import org.apache.catalina.Session;
+import org.apache.catalina.Wrapper;
+import org.gatein.wci.command.CommandServlet;
+import org.gatein.wci.spi.WebAppContext;
+import org.w3c.dom.Document;
+
+import javax.servlet.ServletContext;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
+ */
+public class JB7WebAppContext implements WebAppContext
+{
+ /** . */
+ private Document descriptor;
+
+ /** . */
+ private ServletContext servletContext;
+
+ /** . */
+ private ClassLoader loader;
+
+ /** . */
+ private String contextPath;
+
+ /** . */
+ private final Context context;
+
+ /** . */
+ private Wrapper commandServlet;
+
+ JB7WebAppContext(Context context) throws Exception
+ {
+ this.context = context;
+
+ //
+ servletContext = context.getServletContext();
+ loader = context.getLoader().getClassLoader();
+ contextPath = context.getPath();
+ }
+
+ public void start() throws Exception
+ {
+ try
+ {
+ commandServlet = context.createWrapper();
+ commandServlet.setName("TomcatGateInServlet");
+ commandServlet.setLoadOnStartup(0);
+ commandServlet.setServletClass(CommandServlet.class.getName());
+ context.addChild(commandServlet);
+ context.addServletMapping("/tomcatgateinservlet",
"TomcatGateInServlet");
+ }
+ catch (Exception e)
+ {
+ cleanup();
+ throw e;
+ }
+ }
+
+ public void stop()
+ {
+ cleanup();
+ }
+
+ private void cleanup()
+ {
+ // This code can cause deadlocks on .war undeploy when container performs multiple
such operations concurrently
+ // as for example when JBoss AS7 is shutting down.
+
+ if (commandServlet != null)
+ {
+ try
+ {
+ context.removeServletMapping("tomcatgateinservlet");
+ context.removeChild(commandServlet);
+ }
+ catch (Exception e)
+ {
+ }
+ }
+ }
+
+ public ServletContext getServletContext()
+ {
+ return servletContext;
+ }
+
+ 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;
+ }
+
+ public boolean invalidateSession(String sessId)
+ {
+ Manager mgr = context.getManager();
+ if (mgr != null)
+ {
+ try
+ {
+ Session sess = mgr.findSession(sessId);
+ if (sess != null)
+ {
+ sess.expire();
+ return true;
+ }
+ }
+ catch (IOException ignored)
+ {
+ }
+ }
+ return false;
+ }
+}
+
Added:
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7WebLifecycleListener.java
===================================================================
---
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7WebLifecycleListener.java
(rev 0)
+++
sandbox/as7_support/wci/jboss/jboss7/src/main/java/org/gatein/wci/jboss/JB7WebLifecycleListener.java 2011-05-19
14:56:12 UTC (rev 6499)
@@ -0,0 +1,104 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2011, 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.gatein.wci.jboss;
+
+import org.apache.catalina.Container;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.LifecycleEvent;
+import org.apache.catalina.LifecycleListener;
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
+
+/**
+ * @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
+ */
+public class JB7WebLifecycleListener implements LifecycleListener
+{
+ private static final Logger log =
LoggerFactory.getLogger(JB7WebLifecycleListener.class);
+
+ /** . */
+ private Container wrapper;
+
+ /** . */
+ private JB7ServletContainerContext containerContext;
+
+ /** . */
+ private boolean started;
+
+ private boolean crossContextLogout = true;
+
+
+ public void lifecycleEvent(LifecycleEvent lifecycleEvent)
+ {
+ log(lifecycleEvent);
+ if ("complete-config".equals(lifecycleEvent.getType()) &&
!started)
+ {
+ wrapper = (Container) lifecycleEvent.getSource();
+ start();
+ }
+ else if (Lifecycle.BEFORE_STOP_EVENT.equals(lifecycleEvent.getType()) &&
started)
+ {
+ wrapper = null;
+ stop();
+ }
+ }
+
+ private void start()
+ {
+ started = true;
+ Container container = wrapper;
+ while (container.getParent() != null)
+ {
+ container = container.getParent();
+ if (container instanceof Engine)
+ {
+ Engine engine = (Engine) container;
+ containerContext = new JB7ServletContainerContext(engine);
+ containerContext.setCrossContextLogout(crossContextLogout);
+ containerContext.start();
+ break;
+ }
+ }
+ }
+
+ private void stop()
+ {
+ started = false;
+ if (containerContext != null)
+ {
+ containerContext.stop();
+ containerContext = null;
+ }
+ }
+
+ public void setCrossContextLogout(String val)
+ {
+ crossContextLogout = Boolean.valueOf(val);
+ }
+
+ private void log(LifecycleEvent lifecycleEvent)
+ {
+ System.out.println(lifecycleEvent);
+ }
+}