KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > engine > dm > ScriptManagementProcessor


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.server.engine.dm;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.security.Principal JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import java.util.logging.Level JavaDoc;
26
27 import sync4j.framework.core.dm.ddf.DevInfo;
28 import sync4j.framework.config.Configuration;
29 import sync4j.framework.config.ConfigClassLoader;
30 import sync4j.framework.logging.Sync4jLogger;
31 import sync4j.framework.logging.Sync4jLoggerName;
32 import sync4j.framework.engine.dm.DeviceDMState;
33 import sync4j.framework.engine.dm.ManagementProcessor;
34 import sync4j.framework.engine.dm.ManagementException;
35 import sync4j.framework.engine.dm.ManagementOperation;
36 import sync4j.framework.engine.dm.ManagementOperationResult;
37
38 import bsh.*;
39
40 /**
41  * This is a concrete implementation of the ManagementProcessor interface.
42  * It uses a scripting language to carry on the required management logic.
43  * <p>
44  * Currently supported scripting language is BeanShell. More scripting languages
45  * can be added in the future.
46  * <p>
47  * The interpreter is created once in the ManagementProcessor's beginSession()
48  * method and is initialized with the scripting variable listed below.
49  * In addition, the script specified in the scriptFile property is called.
50  * Scripts are located under the Sync4j config path. Details for the BeanShell
51  * engine are given later.
52  * <p>
53  * The script specified in scriptFile must have three entry points:
54  * init(), getNextOperations() and setOperationResults(). In order to keep the
55  * interaction between ScriptManagementProcessor and the the underlying
56  * scripting engine, input and output values are passed by variables and not as
57  * input parameters and return velues.
58  * <p>
59  * <g>Scripting Variables</g>
60  * The following scripting variables are set just after scripting engine
61  * creation:
62  * <ul>
63  * <li>processor - the ManagementProcessor instance reference</li>
64  * <li>principal - user principal who is going to be managed</li>
65  * <li>devInfo - device info of the device which is going to be managed</li>
66  * <li>managementType - value given by the device when starting the management
67  * session (such as server or client initiated management session)</li>
68  * <li>config - the Configuration object used to get server side beans</li>
69  * <li>sessionId - The current session identifier</li>
70  * <li>log - The Sync4jLogger to use for logging</li>
71  * <li>dmstate - The DeviceDMState object associated to the session</li>
72  *
73  * </ul>
74  * The following scripting variables are input/output variables that the
75  * management script and the management processor exchange:
76  * <ul>
77  * <li>operations (OUT) - ManagementOperation[] to be sent to the device
78  * management engine
79  * <li>results (IN) - ManagementResult[] returned by the device management
80  * engine</li>
81  * </ul>
82  *
83  * @author Stefano Fornari @ Funambol
84  *
85  * @version $Id: ScriptManagementProcessor.java,v 1.1 2005/05/16 17:32:56 nichele Exp $
86  */

87 public class ScriptManagementProcessor
88 implements ManagementProcessor, java.io.Serializable JavaDoc {
89
90     // --------------------------------------------------------------- Constants
91

92     public static final String JavaDoc VAR_PROCESSOR = "processor" ;
93     public static final String JavaDoc VAR_SESSIONID = "sessionId" ;
94     public static final String JavaDoc VAR_PRINCIPAL = "principal" ;
95     public static final String JavaDoc VAR_MANAGEMENT_TYPE = "managementType";
96     public static final String JavaDoc VAR_DEVINFO = "devInfo" ;
97     public static final String JavaDoc VAR_CONFIG = "config" ;
98     public static final String JavaDoc VAR_OPERATIONS = "operations" ;
99     public static final String JavaDoc VAR_RESULTS = "results" ;
100     public static final String JavaDoc VAR_LOG = "log" ;
101     public static final String JavaDoc VAR_DM_STATE = "dmstate" ;
102
103     public static final String JavaDoc SCRIPT_INIT = "init()" ;
104     public static final String JavaDoc SCRIPT_NEXTOPERATIONS = "getNextOperations()" ;
105     public static final String JavaDoc SCRIPT_SETRESULTS = "setOperationResults()";
106     public static final String JavaDoc SCRIPT_ENDSESSION = "endSession";
107
108
109
110     // ------------------------------------------------------------ Private data
111

112     private Interpreter interpreter;
113
114     private final Logger JavaDoc log = Sync4jLogger.getLogger(Sync4jLoggerName.HANDLER);
115
116     // -------------------------------------------------------------- Properties
117

118     /**
119      * Management processor name
120      */

121     private String JavaDoc name;
122
123     /**
124      * Sets management processor name
125      */

126     public void setName(String JavaDoc name) {
127         this.name = name;
128     }
129
130     /**
131      * @see ManagementProcesessor
132      */

133     public String JavaDoc getName() {
134         return name;
135     }
136
137     /**
138      * Script file. This path is relative to the config path
139      */

140     private String JavaDoc scriptFile;
141
142     /**
143      * Sets scriptFile
144      *
145      * @param scriptFile the new value
146      */

147     public void setScriptFile(String JavaDoc scriptFile) {
148         this.scriptFile = scriptFile;
149     }
150
151     /**
152      * Returns scriptFile
153      *
154      * @returns scriptFile property value
155      */

156     public String JavaDoc getScriptFile() {
157         return scriptFile;
158     }
159
160     // ------------------------------------------------------------ Constructors
161

162     /** Creates a new instance of ScriptMAnagementProcessor */
163     public ScriptManagementProcessor() {
164     }
165
166     // ----------------------------------------------------- ManagementProcessor
167
/**
168      * Creates and initializes a new BeanShell interpreter.
169      *
170      * @see ManagementProcesessor
171      */

172     public void beginSession(String JavaDoc sessionId,
173                              Principal JavaDoc p ,
174                              int type ,
175                              DevInfo devInfo ,
176                              DeviceDMState dmstate )
177     throws ManagementException {
178         if (log.isLoggable(Level.FINE)) {
179             log.fine("Starting a new management session");
180             log.fine("sessionId: " + sessionId );
181             log.fine("principal: " + p );
182             log.fine("type: " + type );
183             log.fine("deviceId: " + devInfo.getDevId() );
184         }
185         Configuration c = Configuration.getConfiguration();
186
187         ConfigClassLoader cl = (ConfigClassLoader)c.getClassLoader();
188
189         URL JavaDoc[] urls = cl.getURLs();
190
191         interpreter = new Interpreter();
192
193         //
194
// 1. We add all the configuration paths to the BeanShell path
195
//
196
if (urls != null) {
197             BshClassManager cm = interpreter.getClassManager();
198             for (int i=0; i<urls.length; ++i) {
199                 //
200
// NOTE: wrong path will be ignored
201
//
202
try {
203                     cm.addClassPath(urls[i]);
204                 } catch (Exception JavaDoc e) {
205                     if (log.isLoggable(Level.INFO)) {
206                         log.info( "Wrong path "
207                                 + urls[i]
208                                 + " will not be added to the interpreter classpath"
209                                 );
210                     }
211                 }
212             }
213         }
214
215         //
216
// 2. We set global objects and variables
217
//
218
try {
219             interpreter.set(VAR_PROCESSOR, this );
220             interpreter.set(VAR_SESSIONID, sessionId);
221             interpreter.set(VAR_PRINCIPAL, p );
222             interpreter.set(VAR_MANAGEMENT_TYPE, type );
223             interpreter.set(VAR_DEVINFO, devInfo );
224             interpreter.set(VAR_CONFIG, c );
225             interpreter.set(VAR_LOG, log );
226             interpreter.set(VAR_DM_STATE, dmstate );
227             interpreter.set(VAR_OPERATIONS, null );
228             interpreter.set(VAR_RESULTS, null );
229         } catch (Exception JavaDoc e) {
230             throw new ManagementException(e.getMessage(), e);
231         }
232
233         //
234
// 3. We evaluate the script file if any and run the init() function
235
//
236
if ((scriptFile != null) && (scriptFile.trim().length()>0)) {
237             InputStream JavaDoc is = cl.getResourceAsStream(scriptFile);
238
239             if (is == null) {
240                 if (log.isLoggable(Level.INFO)) {
241                     log.info( "Initializaion script file "
242                             + scriptFile
243                             + " not found in config path"
244                             );
245                 }
246             } else {
247                 try {
248                     interpreter.eval(new InputStreamReader JavaDoc(is));
249                     interpreter.eval(SCRIPT_INIT);
250                 } catch (Exception JavaDoc e) {
251                     throw new ManagementException(e.getMessage(), e);
252                 } finally {
253                     try {
254                         is.close();
255                     } catch (Exception JavaDoc e) {
256                     }
257                 }
258             }
259         }
260     }
261
262     /**
263      * @see ManagementProcessor
264      */

265     public void endSession(int completionCode) throws ManagementException {
266         String JavaDoc script = SCRIPT_ENDSESSION + "(" + completionCode + ")";
267         try {
268             interpreter.eval(script);
269         } catch (Exception JavaDoc e) {
270             throw new ManagementException(e.getMessage(), e);
271         }
272
273         interpreter = null;
274     }
275
276     /**
277      * @see ManagementProcessor
278      */

279     public ManagementOperation[] getNextOperations()
280     throws ManagementException {
281         try {
282             interpreter.eval(SCRIPT_NEXTOPERATIONS);
283
284             return (ManagementOperation[])interpreter.get(VAR_OPERATIONS);
285         } catch (Exception JavaDoc e) {
286             throw new ManagementException(e.getMessage(), e);
287         }
288     }
289
290     /**
291      * @see ManagementProcesessor
292      */

293     public void setOperationResults(ManagementOperationResult[] results)
294     throws ManagementException {
295         try {
296             interpreter.set(VAR_RESULTS, results);
297             interpreter.eval(SCRIPT_SETRESULTS);
298         } catch (Exception JavaDoc e) {
299             throw new ManagementException(e.getMessage(), e);
300         }
301     }
302
303     // ------------------------------------------------------------ Private data
304

305 }
Popular Tags