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