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