001: /*
002: The contents of this file are subject to the Mozilla Public License Version 1.1
003: (the "License"); you may not use this file except in compliance with the
004: License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005:
006: Software distributed under the License is distributed on an "AS IS" basis,
007: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: for the specific language governing rights and
009: limitations under the License.
010:
011: The Original Code is "The Columba Project"
012:
013: The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
014: Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
015:
016: All Rights Reserved.
017: */
018: package org.columba.core.scripting;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Observable;
023: import java.util.Observer;
024: import java.util.logging.Logger;
025:
026: import org.columba.core.io.DiskIO;
027: import org.columba.core.scripting.config.BeanshellConfig;
028: import org.columba.core.scripting.service.api.IColumbaService;
029:
030: /**
031: This class represents the Beanshell Service.<br>
032: The Beanshell Service enables the use of scriptable plugins, meaning a
033: 3rd-party developer can created plugins based on beanshell scripts. <br>
034: To create a Beanshell plugin, the 3rd party must create one script with a
035: .bsh extension and copy the file to the ~/.columba/scripts directory. <br>
036: <br>
037: The service will then automaticaly pick up the script and execute it. <br>
038: If a plugin depends on more than one script, then only the entry point should
039: have the .bsh extension, for example:<br> - my_plugin.bsh<br> -
040: my_plugin.file_2<br> - my_plugin.file_3<br> - ...<br>
041: <br>
042: <br>
043: <strong>This is still alpha software so expect things to change.</strong>
044: <br>
045:
046: @author Celso Pinto (cpinto@yimports.com)
047: */
048: public class BeanshellService implements IColumbaService, Observer {
049:
050: private static final Logger LOG = Logger
051: .getLogger(BeanshellService.class.getName());
052:
053: private BeanshellConfig config = BeanshellConfig.getInstance();
054:
055: private Map beanshellScripts = new HashMap();
056: private ScriptLogger logger = ScriptLogger.getInstance();
057:
058: public BeanshellService() {
059: logger.addObserver(this );
060: }
061:
062: /**
063: @see org.columba.core.scripting.service.api.IColumbaService#initService()
064: */
065: public boolean initService() {
066:
067: /* check if script directory exists */
068: if (!DiskIO.ensureDirectory(config.getPath()))
069: return false;
070:
071: /*
072: * initialize file observer thread with a reference to our
073: * beanshellScripts map
074: */
075: FileObserverThread.getInstance()
076: .setScriptList(beanshellScripts);
077:
078: LOG.info("BeanshellService initialized...");
079: return true;
080:
081: }
082:
083: /**
084: @see org.columba.core.scripting.service.api.IColumbaService#disposeService()
085: */
086: public void disposeService() {
087: /* nothing to dispose, yet... */
088: }
089:
090: /**
091: @see org.columba.core.scripting.service.api.IColumbaService#startService()
092: */
093: public void startService() {
094: /* start pooling thread */
095: logger.append("Starting " + getClass().getName());
096: logger.append("Starting FileObserverThread...");
097: logger.addObserver(this ); /*in case of a stop-start */
098: FileObserverThread.getInstance().start();
099: }
100:
101: /**
102: @see org.columba.core.scripting.service.api.IColumbaService#stopService()
103: */
104: public void stopService() {
105:
106: logger.append("Stoping " + getClass().getName());
107: logger.append("Stopping FileObserverThread...");
108:
109: logger.deleteObserver(this );
110:
111: FileObserverThread.getInstance().finish();
112:
113: }
114:
115: public Map getBeanshellScripts() {
116: return beanshellScripts;
117: }
118:
119: public ScriptLogger getLogger() {
120: return logger;
121: }
122:
123: public void update(Observable o, Object arg) {
124: ScriptLogger.LogEntry log = (ScriptLogger.LogEntry) arg;
125: LOG.finest(log.getMessage());
126: LOG.finest(log.getDetails());
127: }
128: }
|