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