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        // Use JVM properties as external config
065        setConfig(System.getProperties());
066    
067        //
068        synchronized (lock) {
069          if (!contextMap.containsKey(contextPath)) {
070    
071            //
072            FS binFS = new FS().mount(new ServletContextDriver(sc), "/WEB-INF/crash/commands/");
073            FS confFS = new FS().mount(new ServletContextDriver(sc), "/WEB-INF/crash/");
074            ClassLoader webAppLoader = Thread.currentThread().getContextClassLoader();
075    
076            //
077            PluginContext context = new PluginContext(
078                new ServiceLoaderDiscovery(webAppLoader),
079                binFS,
080                confFS,
081                webAppLoader);
082    
083            //
084            contextMap.put(contextPath, context);
085            registered = true;
086    
087            //
088            start(context);
089          }
090        }
091      }
092    
093      public void contextDestroyed(ServletContextEvent sce) {
094        if (registered) {
095    
096          //
097          ServletContext sc = sce.getServletContext();
098          String contextPath = sc.getContextPath();
099    
100          //
101          synchronized (lock) {
102    
103            //
104            contextMap.remove(contextPath);
105            registered = false;
106    
107            //
108            stop();
109          }
110        }
111      }
112    }