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