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.Resource;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.net.URL;
029    import java.util.Properties;
030    
031    /**
032     * Controls the life cycle of a plugin manager.
033     *
034     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
035     * @version $Revision$
036     */
037    public abstract class PluginLifeCycle {
038    
039      /** . */
040      protected final Logger log = LoggerFactory.getLogger(getClass());
041    
042      /** . */
043      private PluginContext context;
044    
045      public PluginContext getContext() {
046        return context;
047      }
048    
049      protected final void start(PluginContext context) throws IllegalStateException {
050        if (this.context != null) {
051          throw new IllegalStateException("Already started");
052        }
053        
054        // Get properties from system properties
055        Properties props = new Properties();
056    
057        // Load properties from configuration file
058        Resource res = context.loadResource("crash.properties", ResourceKind.CONFIG);
059        if (res != null) {
060          try {
061            URL url = res.getURL();
062            InputStream in = url.openStream();
063            props.load(in);
064            log.debug("Loaded properties from " + url + " " + props);
065          } catch (IOException e) {
066            log.warn("Could not configure from crash.properties", e);
067          }
068        } else {
069          log.debug("Could not find crash.properties file");
070        }
071    
072        // Override default properties from System properties
073        props.putAll(System.getProperties());
074    
075        // Override default properties from command line
076        for (PropertyDescriptor<?> desc : PropertyDescriptor.ALL.values()) {
077          configureProperty(context, props, desc);
078        }
079    
080        // Override default properties from plugin defined properties.
081        for (final CRaSHPlugin<?> plugin : context.manager.getPlugins())
082        {
083          for (PropertyDescriptor<?> descriptor : plugin.getConfigurationCapabilities()) {
084            configureProperty(context, props, descriptor);
085          }
086        }
087    
088        //
089        context.start();
090    
091        //
092        this.context = context;
093      }
094    
095      public final void stop() throws IllegalStateException {
096        if (context == null) {
097          throw new IllegalStateException("Not started");
098        }
099        PluginContext context = this.context;
100        this.context = null;
101        context.stop();
102      }
103    
104      private void configureProperty(PluginContext context, Properties props, PropertyDescriptor<?> desc) {
105        String key = "crash." + desc.name;
106        String value = props.getProperty(key);
107        if (value != null) {
108          try {
109            log.info("Configuring property " + desc.name + "=" + value + " from properties");
110            context.setProperty(desc, value);
111          }
112          catch (IllegalArgumentException e) {
113            log.error("Could not configure property", e);
114          }
115        }
116      }
117    }