1 18 package sync4j.server.engine.dm; 19 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.net.URL ; 23 import java.security.Principal ; 24 import java.util.logging.Logger ; 25 import java.util.logging.Level ; 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 87 public class ScriptManagementProcessor 88 implements ManagementProcessor, java.io.Serializable { 89 90 92 public static final String VAR_PROCESSOR = "processor" ; 93 public static final String VAR_SESSIONID = "sessionId" ; 94 public static final String VAR_PRINCIPAL = "principal" ; 95 public static final String VAR_MANAGEMENT_TYPE = "managementType"; 96 public static final String VAR_DEVINFO = "devInfo" ; 97 public static final String VAR_CONFIG = "config" ; 98 public static final String VAR_OPERATIONS = "operations" ; 99 public static final String VAR_RESULTS = "results" ; 100 public static final String VAR_LOG = "log" ; 101 public static final String VAR_DM_STATE = "dmstate" ; 102 103 public static final String SCRIPT_INIT = "init()" ; 104 public static final String SCRIPT_NEXTOPERATIONS = "getNextOperations()" ; 105 public static final String SCRIPT_SETRESULTS = "setOperationResults()"; 106 public static final String SCRIPT_ENDSESSION = "endSession"; 107 108 109 110 112 private Interpreter interpreter; 113 114 private final Logger log = Sync4jLogger.getLogger(Sync4jLoggerName.HANDLER); 115 116 118 121 private String name; 122 123 126 public void setName(String name) { 127 this.name = name; 128 } 129 130 133 public String getName() { 134 return name; 135 } 136 137 140 private String scriptFile; 141 142 147 public void setScriptFile(String scriptFile) { 148 this.scriptFile = scriptFile; 149 } 150 151 156 public String getScriptFile() { 157 return scriptFile; 158 } 159 160 162 163 public ScriptManagementProcessor() { 164 } 165 166 172 public void beginSession(String sessionId, 173 Principal 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 [] urls = cl.getURLs(); 190 191 interpreter = new Interpreter(); 192 193 if (urls != null) { 197 BshClassManager cm = interpreter.getClassManager(); 198 for (int i=0; i<urls.length; ++i) { 199 try { 203 cm.addClassPath(urls[i]); 204 } catch (Exception 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 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 e) { 230 throw new ManagementException(e.getMessage(), e); 231 } 232 233 if ((scriptFile != null) && (scriptFile.trim().length()>0)) { 237 InputStream 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 (is)); 249 interpreter.eval(SCRIPT_INIT); 250 } catch (Exception e) { 251 throw new ManagementException(e.getMessage(), e); 252 } finally { 253 try { 254 is.close(); 255 } catch (Exception e) { 256 } 257 } 258 } 259 } 260 } 261 262 265 public void endSession(int completionCode) throws ManagementException { 266 String script = SCRIPT_ENDSESSION + "(" + completionCode + ")"; 267 try { 268 interpreter.eval(script); 269 } catch (Exception e) { 270 throw new ManagementException(e.getMessage(), e); 271 } 272 273 interpreter = null; 274 } 275 276 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 e) { 286 throw new ManagementException(e.getMessage(), e); 287 } 288 } 289 290 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 e) { 299 throw new ManagementException(e.getMessage(), e); 300 } 301 } 302 303 305 } | Popular Tags |