KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > scripting > BeanshellService


1 /*
2   The contents of this file are subject to the Mozilla Public License Version 1.1
3   (the "License"); you may not use this file except in compliance with the
4   License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5   
6   Software distributed under the License is distributed on an "AS IS" basis,
7   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8   for the specific language governing rights and
9   limitations under the License.
10
11   The Original Code is "The Columba Project"
12   
13   The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
14   Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15   
16   All Rights Reserved.
17 */

18 package org.columba.core.scripting;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Observable JavaDoc;
23 import java.util.Observer JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 import org.columba.core.io.DiskIO;
27 import org.columba.core.scripting.config.BeanshellConfig;
28 import org.columba.core.scripting.service.api.IColumbaService;
29
30 /**
31  This class represents the Beanshell Service.<br>
32  The Beanshell Service enables the use of scriptable plugins, meaning a
33  3rd-party developer can created plugins based on beanshell scripts. <br>
34  To create a Beanshell plugin, the 3rd party must create one script with a
35  .bsh extension and copy the file to the ~/.columba/scripts directory. <br>
36  <br>
37  The service will then automaticaly pick up the script and execute it. <br>
38  If a plugin depends on more than one script, then only the entry point should
39  have the .bsh extension, for example:<br> - my_plugin.bsh<br> -
40  my_plugin.file_2<br> - my_plugin.file_3<br> - ...<br>
41  <br>
42  <br>
43  <strong>This is still alpha software so expect things to change.</strong>
44  <br>
45
46  @author Celso Pinto (cpinto@yimports.com)
47 */

48 public class BeanshellService
49     implements IColumbaService,
50                 Observer JavaDoc
51 {
52
53     private static final Logger JavaDoc LOG = Logger.getLogger(BeanshellService.class.getName());
54
55     private BeanshellConfig config = BeanshellConfig.getInstance();
56
57     private Map JavaDoc beanshellScripts = new HashMap JavaDoc();
58     private ScriptLogger logger = ScriptLogger.getInstance();
59
60     public BeanshellService()
61     {
62         logger.addObserver(this);
63     }
64
65     /**
66      @see org.columba.core.scripting.service.api.IColumbaService#initService()
67      */

68     public boolean initService()
69     {
70
71         /* check if script directory exists */
72         if (!DiskIO.ensureDirectory(config.getPath())) return false;
73
74         /*
75            * initialize file observer thread with a reference to our
76            * beanshellScripts map
77            */

78         FileObserverThread.getInstance().setScriptList(beanshellScripts);
79
80         LOG.info("BeanshellService initialized...");
81         return true;
82
83     }
84
85     /**
86      @see org.columba.core.scripting.service.api.IColumbaService#disposeService()
87      */

88     public void disposeService()
89     {
90         /* nothing to dispose, yet... */
91     }
92
93     /**
94      @see org.columba.core.scripting.service.api.IColumbaService#startService()
95      */

96     public void startService()
97     {
98         /* start pooling thread */
99         logger.append("Starting " + getClass().getName());
100         logger.append("Starting FileObserverThread...");
101         logger.addObserver(this); /*in case of a stop-start */
102         FileObserverThread.getInstance().start();
103     }
104
105     /**
106      @see org.columba.core.scripting.service.api.IColumbaService#stopService()
107      */

108     public void stopService()
109     {
110
111         logger.append("Stoping " + getClass().getName());
112         logger.append("Stopping FileObserverThread...");
113
114         logger.deleteObserver(this);
115
116         FileObserverThread.getInstance().finish();
117
118     }
119
120     public Map JavaDoc getBeanshellScripts()
121     {
122         return beanshellScripts;
123     }
124
125     public ScriptLogger getLogger()
126     {
127         return logger;
128     }
129
130     public void update(Observable JavaDoc o, Object JavaDoc arg)
131     {
132         ScriptLogger.LogEntry log = (ScriptLogger.LogEntry) arg;
133         LOG.finest(log.getMessage());
134         LOG.finest(log.getDetails());
135     }
136 }
137
Popular Tags