70 |
brianR |
1 |
/*
|
|
|
2 |
* Copyright 2011 Brian Rosenberger (Brutex Network)
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package net.brutex.xservices.util;
|
|
|
18 |
|
|
|
19 |
import java.io.Serializable;
|
|
|
20 |
import java.util.Date;
|
|
|
21 |
|
|
|
22 |
import org.mozilla.javascript.Context;
|
|
|
23 |
import org.mozilla.javascript.Scriptable;
|
|
|
24 |
import org.quartz.Job;
|
|
|
25 |
import org.quartz.JobDataMap;
|
|
|
26 |
import org.quartz.JobExecutionContext;
|
|
|
27 |
import org.quartz.JobExecutionException;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Wrapper for jobs that can be executed through quartz scheduler.
|
|
|
31 |
*
|
|
|
32 |
* @author Brian Rosenberger, bru@brutex.de
|
|
|
33 |
* @since 0.5.0
|
|
|
34 |
*
|
|
|
35 |
*/
|
|
|
36 |
public class JobWrapper implements Job, Serializable {
|
|
|
37 |
|
|
|
38 |
public void execute(JobExecutionContext jcontext)
|
|
|
39 |
|
|
|
40 |
throws JobExecutionException {
|
|
|
41 |
try {
|
|
|
42 |
|
|
|
43 |
System.out.println("Executing scheduled job '"+jcontext.getJobDetail().getKey().getName()+"' at " + new Date());
|
|
|
44 |
|
|
|
45 |
JobDataMap jdMap = jcontext.getJobDetail().getJobDataMap();
|
|
|
46 |
String script = jdMap.getString("script");
|
|
|
47 |
|
|
|
48 |
// Create and enter a Context. A Context stores information about
|
|
|
49 |
// the execution environment of a script.
|
|
|
50 |
Context cx = Context.enter();
|
|
|
51 |
cx.setOptimizationLevel(0);
|
|
|
52 |
cx.setLanguageVersion(Context.VERSION_1_7);
|
|
|
53 |
// cx is the Context instance you're using to run scripts
|
|
|
54 |
/*
|
|
|
55 |
* cx.setClassShutter(new ClassShutter() { public boolean
|
|
|
56 |
* visibleToScripts(String className) {
|
|
|
57 |
* if(className.startsWith("adapter")) return true;
|
|
|
58 |
* if(className.startsWith("java.lang.System") ||
|
|
|
59 |
* className.startsWith
|
|
|
60 |
* ("org.apache.tomcat.util.log.SystemLogHandler")) return true;
|
|
|
61 |
* System.out.println(className + " is blocked."); return false; }
|
|
|
62 |
* });
|
|
|
63 |
*/
|
|
|
64 |
|
|
|
65 |
// Initialise the standard objects (Object, Function, etc.). This
|
|
|
66 |
// must be done before scripts can be
|
|
|
67 |
// executed. The null parameter tells initStandardObjects
|
|
|
68 |
// to create and return a scope object that we use
|
|
|
69 |
// in later calls.
|
|
|
70 |
Scriptable scope = cx.initStandardObjects();
|
|
|
71 |
//Object wrappedOut = Context.javaToJS(System.out, scope);
|
|
|
72 |
//Object wrappedOut2 = Context.javaToJS(this, scope);
|
|
|
73 |
//scope.put("out", scope, wrappedOut);
|
|
|
74 |
//scope.put("exe", scope, wrappedOut2);
|
|
|
75 |
|
|
|
76 |
// Execute the script
|
|
|
77 |
// cx.evaluateString(scope, "importClass('java.lang.System');\n",
|
|
|
78 |
// "head", 1, null);
|
|
|
79 |
// cx.evaluateString(scope, "importPackage('java.util');\n", "head",
|
|
|
80 |
// 2, null);
|
|
|
81 |
Object obj = cx
|
|
|
82 |
.evaluateString(scope, script, "TestScript", 1, null);
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
} catch (Exception e) {
|
|
|
86 |
System.out.println(e.getMessage());
|
|
|
87 |
} finally {
|
|
|
88 |
// Exit the Context. This removes the association between the
|
|
|
89 |
// Context and the current thread and is an
|
|
|
90 |
// essential cleanup action. There should be a call to exit for
|
|
|
91 |
// every call to enter.
|
|
|
92 |
Context.exit();
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
}
|
|
|
96 |
}
|