001 /*
002 * Copyright (C) 2010 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.plugin;
021
022 import org.crsh.vfs.FS;
023 import org.crsh.vfs.spi.servlet.ServletContextDriver;
024
025 import javax.servlet.ServletContext;
026 import javax.servlet.ServletContextEvent;
027 import javax.servlet.ServletContextListener;
028 import java.util.HashMap;
029 import java.util.Map;
030
031 /**
032 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
033 * @version $Revision$
034 */
035 public class WebPluginLifeCycle extends PluginLifeCycle implements ServletContextListener {
036
037 /** . */
038 private static final Object lock = new Object();
039
040 /** . */
041 private static final Map<String, PluginContext> contextMap = new HashMap<String, PluginContext>();
042
043 /** . */
044 private boolean registered = false;
045
046 /**
047 * Returns a plugin context associated with the servlet context or null if such context does not exist.
048 *
049 * @param sc the servlet context
050 * @return the associated plugin context
051 * @throws NullPointerException if the servlet context argument is null
052 */
053 public static PluginContext getPluginContext(ServletContext sc) throws NullPointerException {
054 String contextPath = sc.getContextPath();
055 synchronized (lock) {
056 return contextMap.get(contextPath);
057 }
058 }
059
060 public void contextInitialized(ServletContextEvent sce) {
061 ServletContext sc = sce.getServletContext();
062 String contextPath = sc.getContextPath();
063
064 //
065 synchronized (lock) {
066 if (!contextMap.containsKey(contextPath)) {
067
068 //
069 FS fs = new FS().mount(new ServletContextDriver(sc), "/WEB-INF/crash/");
070 ClassLoader webAppLoader = Thread.currentThread().getContextClassLoader();
071
072 //
073 PluginContext context = new PluginContext(
074 new ServiceLoaderDiscovery(webAppLoader),
075 fs,
076 webAppLoader);
077
078 //
079 contextMap.put(contextPath, context);
080 registered = true;
081
082 //
083 start(context);
084 }
085 }
086 }
087
088 public void contextDestroyed(ServletContextEvent sce) {
089 if (registered) {
090
091 //
092 ServletContext sc = sce.getServletContext();
093 String contextPath = sc.getContextPath();
094
095 //
096 synchronized (lock) {
097
098 //
099 contextMap.remove(contextPath);
100 registered = false;
101
102 //
103 stop();
104 }
105 }
106 }
107 }