我终于明白了。显然我想要的一切都内置在java中。 Java 能够将 java 对象传递给 javascript 调用,并且这些对象可以像本地 java 对象一样使用。我能够在 javascript 调用中设置全局 java.sql.Connection 并完全像在 java 中那样读取数据库。此外,我能够修改传入的对象并将它们返回给调用 java 代码。这是有效的代码。
Java 源文件。
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JSTest {
public static void main(String[] args) {
new JSTest().go();
}
public void go(){
Connection db = null;
try {
//Initialize all of the built in Java script parsing classes
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
db = login("127.0.0.1", "3306", "testdb", "user", "password");
//set the database connection as a global variable for the javascript object.
engine.put("g_connection",db);
engine.eval(new java.io.FileReader("/home/scripts/testScript.js"));
Invocable inv = (Invocable) engine;
//Put together some sample data for the script to manipulate
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(2014, 6, 20, 10, 30, 0);
Date apptTime = cal.getTime();
TripLeg tripLeg = new TripLeg();
tripLeg.setLegSequence("A");
tripLeg.setAppointmentTime(apptTime);
tripLeg.setProjectedMiles(5);
Trip trip = new Trip();
trip.setClientID(5);//This is the id of the client record. I need to look it up in the script.
tripLeg = (TripLeg) inv.invokeFunction("modifyTripLeg", trip, tripLeg);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (tripLeg.getScheduledPUTime() != null)
System.out.println(format.format(tripLeg.getScheduledPUTime()));
else
System.out.println("PU time not set.");
if (tripLeg.getScheduledDOTime() != null)
System.out.println(format.format(tripLeg.getScheduledDOTime()));
else
System.out.println("DO time not set.");
} catch (ScriptException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} finally {
try {
if (db != null)
db.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public Connection login (String server, String port, String database, String userName, String password)
{
Connection db = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = String.format("jdbc:mysql://%s:%s/%s", server, port, database);
db = DriverManager.getConnection(url, userName, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return db;
}
//Sample trip and trip leg classes stripped down to the bare minimum stuff that I need to access.
public class Trip{
private int clientID;
public int getClientID() {
return clientID;
}
public void setClientID(int clientID) {
this.clientID = clientID;
}
}
public class TripLeg{
private Date scheduledPUTime;
private Date scheduleDOTime;
private Date appointmentTime;
private String legSequence;
private double projectedMiles;
public Date getScheduledPUTime() {
return scheduledPUTime;
}
public void setScheduledPUTime(Date scheduledPUTime) {
this.scheduledPUTime = scheduledPUTime;
}
public Date getScheduledDOTime() {
return scheduleDOTime;
}
public void setScheduledDOTime(Date scheduleDOTime) {
this.scheduleDOTime = scheduleDOTime;
}
public Date getAppointmentTime() {
return appointmentTime;
}
public void setAppointmentTime(Date appointmentTime) {
this.appointmentTime = appointmentTime;
}
public String getLegSequence() {
return legSequence;
}
public void setLegSequence(String legSequence) {
this.legSequence = legSequence;
}
public double getProjectedMiles() {
return projectedMiles;
}
public void setProjectedMiles(double projectedMiles) {
this.projectedMiles = projectedMiles;
}
}
}
这里是 javascript 源文件。 (/home/scripts/testScript.js)
function modifyTrip(trip, tripLeg){
return trip;
};
function modifyTripLeg(trip, tripLeg){
var importFormat = getClientImportFormat(trip.getClientID());
var legSequence = tripLeg.getLegSequence();
var scheduledPUTime = tripLeg.getScheduledPUTime();
var scheduledDOTime = tripLeg.getScheduledDOTime();
var appointmentTime = tripLeg.getAppointmentTime();
var projectedMiles = tripLeg.getProjectedMiles();
if (importFormat == "TESTER_CLIENT"){
if (legSequence == "A"){
if (isEmptyDate(scheduledPUTime) && !isEmptyDate(appointmentTime) && !isEmptyDouble(projectedMiles))
scheduledPUTime = new Date(appointmentTime.getTime() - ((projectedMiles + 45) * 60 *1000));
if (isEmptyDate(scheduledDOTime) && !isEmptyDate(appointmentTime))
scheduledDOTime = new Date(appointmentTime.getTime() - (15 * 60 *1000));
}
if (legSequence == "B"){
if (scheduledPUTime == null && appointmentTime != null && (projectedMiles != null && projectedMiles > 0.00))
scheduledPUTime = new Date(appointmentTime.getTime() - ((projectedMiles + 45) * 60 *1000));
if (scheduledDOTime == null && appointmentTime != null)
scheduledPUTime = new Date(appointmentTime.getTime() - (15 * 60 *1000));
}
}
tripLeg.setScheduledPUTime(scheduledPUTime);
tripLeg.setScheduledDOTime(scheduledDOTime);
return tripLeg;
};
function getClientImportFormat(clientID){
var sql = "select ImportFormat from Client where id = ?";
var stmt = g_connection.prepareStatement(sql);
stmt.setInt(1, clientID);
var rs = stmt.executeQuery();
var importFormat = "";
if (rs.next()){
importFormat = rs.getString(1);
}
rs.close();
return importFormat;
}
function isEmptyDate(testDate){
if (!testDate)
return true;
if (testDate == null)
return true;
return false;
};
function isEmptyDouble(testDouble){
if (!testDouble)
return true;
if (testDouble == null)
return true;
if (testDouble <= 0.0)
return true;
return false;
}
让我明白的两页:
http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/api.html
http://javalandscape.blogspot.com/2008/12/scripting-in-jdk6-jsr-223-part-2.html
另外,我的公司仍在使用 java 6,所以我无法使用 nashorn 引擎。每当我在代码中请求引擎时,我都会使用“javascript”,而对于我需要做的有限的事情,它工作得很好。这一切都在 java servlet 中完美运行。
希望这可以帮助其他寻找类似东西的人。