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