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