Wednesday, 25 April 2012

Add MySQL support to javascript sandbox

Add MySQL support to javascript sandbox
This task add MySQL support to javascript sandbox.
Add MySQL support to javascript sandbox
  1. Create javascript sandbox with jsoup support
  2. Create com.paesia.schema.script.safe.mysql.SMySQL class as following
  3. Modify com.paesia.schema.script.Machine class as following
Modify com.paesia.schema.script.Machine class
1............
2
3import com.paesia.schema.script.safe.mysql.SMySQL;
4
5public class Machine {
6
7 private Handler handler;
8
9 public static void run(Machine env, String js, Map args) throws Exception {
10 try {
11 Context cx = Context.enter();
12 cx.setClassShutter(new ClassShutter() {
13 public boolean visibleToScripts(String className) {
14...........
15 return false;
16 }
17 });
18
19...........
20
21 } catch (Exception e) {
22 throw e;
23 } finally {
24 Context.exit();
25 }
26 }
27...........
28
29 public SMySQL newMySQL() {
30 return new SMySQL();
31 }
32
33...........
34}
............

import com.paesia.schema.script.safe.mysql.SMySQL;

public class Machine {

    private Handler handler;
 
    public static void run(Machine env, String js, Map args) throws Exception {
        try {
            Context cx = Context.enter();
            cx.setClassShutter(new ClassShutter() {
                public boolean visibleToScripts(String className) {  
...........
                    return false;
                }
            });   

...........

        } catch (Exception e) {
            throw e;
        } finally {
            Context.exit();   
        }
    }
...........

    public SMySQL newMySQL() {
        return new SMySQL();
    }

...........
}
com.paesia.schema.script.safe.mysql.SMySQL class
1package com.paesia.schema.script.safe.mysql;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.ResultSetMetaData;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12
13public class SMySQL {
14
15 private Connection conn;
16
17 public void open(String server, String database, String username, String password) throws Exception {
18 close();
19 Class.forName("com.mysql.jdbc.Driver");
20 conn = DriverManager.getConnection("jdbc:mysql://" + server + "/" + database + "?"+ "user=" + username + "&password=" + password);
21 }
22
23 public void open(Map info) throws Exception {
24 String server = info.get("server") + "";
25 String database = info.get("database") + "";
26 String username = info.get("username") + "";
27 String password = info.get("password") + "";
28 open(server, database, username, password);
29 }
30
31 public void close() {
32 if (conn != null) {
33 try {
34 conn.close();
35 } catch (Exception e) {
36 }
37 conn = null;
38 }
39 }
40
41 public boolean execute(String sql, List params) throws Exception {
42 PreparedStatement prep = buildStatement(sql, params);
43 boolean tag = prep.execute();
44 prep.close();
45 return tag;
46 }
47
48 public boolean execute(String sql, Map... params) throws Exception {
49 PreparedStatement prep = buildStatement(sql, params);
50 boolean tag = prep.execute();
51 prep.close();
52 return tag;
53 }
54
55 public List query(String sql, List params) throws Exception {
56 PreparedStatement prep = buildStatement(sql, params);
57 ResultSet rs = prep.executeQuery();
58 List tag = parseResult(rs);
59 rs.close();
60 prep.close();
61 return tag;
62 }
63
64 public List query(String sql, Map... params) throws Exception {
65 PreparedStatement prep = buildStatement(sql, params);
66 ResultSet rs = prep.executeQuery();
67 List tag = parseResult(rs);
68 rs.close();
69 prep.close();
70 return tag;
71 }
72
73 public Map paramString(String value) {
74 Map tag = new HashMap();
75 tag.put("kind", "String");
76 tag.put("value", value);
77 return tag;
78 }
79
80 public Map paramBoolean(Boolean value) {
81 Map tag = new HashMap();
82 tag.put("kind", "Boolean");
83 tag.put("value", value);
84 return tag;
85 }
86
87 public Map paramInteger(Integer value) {
88 Map tag = new HashMap();
89 tag.put("kind", "Integer");
90 tag.put("value", value);
91 return tag;
92 }
93
94 public Map paramLong(Long value) {
95 Map tag = new HashMap();
96 tag.put("kind", "Long");
97 tag.put("value", value);
98 return tag;
99 }
100
101 public Map paramFloat(Float value) {
102 Map tag = new HashMap();
103 tag.put("kind", "Float");
104 tag.put("value", value);
105 return tag;
106 }
107
108 public Map paramDouble(Double value) {
109 Map tag = new HashMap();
110 tag.put("kind", "Double");
111 tag.put("value", value);
112 return tag;
113 }
114
115 protected PreparedStatement buildStatement(String sql, Map... params) throws Exception {
116 List args = new ArrayList();
117 if (params != null) {
118 for (int i = 0; i < params.length; i++) {
119 args.add(params[i]);
120 }
121 }
122 return buildStatement(sql, args);
123 }
124
125 protected PreparedStatement buildStatement(String sql, List params) throws Exception {
126 PreparedStatement prep = conn.prepareStatement(sql);
127 for (int i = 0; i < params.size(); i++) {
128 Map item = (Map)params.get(i);
129 String kind = item.get("kind") + "";
130 if ("String".equals(kind)) {
131 prep.setString(i + 1, (String)item.get("value"));
132 } else if ("Boolean".equals(kind)) {
133 prep.setBoolean(i + 1, (Boolean)item.get("value"));
134 } else if ("Integer".equals(kind)) {
135 prep.setInt(i + 1, (Integer)item.get("value"));
136 } else if ("Long".equals(kind)) {
137 prep.setLong(i + 1, (Long)item.get("value"));
138 } else if ("Float".equals(kind)) {
139 prep.setFloat(i + 1, (Float)item.get("value"));
140 } else if ("Double".equals(kind)) {
141 prep.setDouble(i + 1, (Double)item.get("value"));
142 } else {
143 prep.setString(i + 1, item.get("value") + "");
144 }
145 }
146 return prep;
147 }
148
149 protected List parseResult(ResultSet rs) throws Exception {
150 List tag = new ArrayList();
151 ResultSetMetaData md = rs.getMetaData();
152 while (rs.next()) {
153 Map item = new HashMap();
154 for (int i = 1; i <= md.getColumnCount(); i++) {
155 String name = md.getColumnName(i);
156 int type = md.getColumnType(i);
157 if (type == java.sql.Types.BIGINT) {
158 item.put(name, rs.getLong(name));
159 } else if (type == java.sql.Types.INTEGER || type == java.sql.Types.SMALLINT || type == java.sql.Types.TINYINT) {
160 item.put(name, rs.getInt(name));
161 } else if (type == java.sql.Types.DECIMAL || type == java.sql.Types.DOUBLE || type == java.sql.Types.NUMERIC || type == java.sql.Types.REAL) {
162 item.put(name, rs.getDouble(name));
163 } else if (type == java.sql.Types.FLOAT) {
164 item.put(name, rs.getFloat(name));
165 } else if (type == java.sql.Types.BIT || type == java.sql.Types.BOOLEAN) {
166 item.put(name, rs.getBoolean(name));
167 } else {
168 item.put(name, rs.getString(name));
169 }
170 }
171 tag.add(item);
172 }
173 return tag;
174 }
175
176}
package com.paesia.schema.script.safe.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SMySQL {

    private Connection conn;
 
    public void open(String server, String database, String username, String password) throws Exception {
        close();
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://" + server + "/" + database + "?"+ "user=" + username + "&password=" + password);
    }
 
    public void open(Map info) throws Exception {
        String server = info.get("server") + "";
        String database = info.get("database") + "";
        String username = info.get("username") + "";
        String password = info.get("password") + "";
        open(server, database, username, password);
    }
 
    public void close() {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
            conn = null;
        }
    }
 
    public boolean execute(String sql, List params) throws Exception {
        PreparedStatement prep = buildStatement(sql, params);
        boolean tag = prep.execute();
        prep.close();
        return tag;
    }

    public boolean execute(String sql, Map... params) throws Exception {
        PreparedStatement prep = buildStatement(sql, params);
        boolean tag = prep.execute();
        prep.close();
        return tag;
    }

    public List query(String sql, List params) throws Exception {
        PreparedStatement prep = buildStatement(sql, params);
        ResultSet rs = prep.executeQuery();
        List tag = parseResult(rs);
        rs.close();
        prep.close();
        return tag;
    }

    public List query(String sql, Map... params) throws Exception {
        PreparedStatement prep = buildStatement(sql, params);
        ResultSet rs = prep.executeQuery();
        List tag = parseResult(rs);
        rs.close();
        prep.close();
        return tag;
    }
 
    public Map paramString(String value) {
        Map tag = new HashMap();
        tag.put("kind", "String");
        tag.put("value", value);
        return tag;
    }

    public Map paramBoolean(Boolean value) {
        Map tag = new HashMap();
        tag.put("kind", "Boolean");
        tag.put("value", value);
        return tag;
    }

    public Map paramInteger(Integer value) {
        Map tag = new HashMap();
        tag.put("kind", "Integer");
        tag.put("value", value);
        return tag;
    }

    public Map paramLong(Long value) {
        Map tag = new HashMap();
        tag.put("kind", "Long");
        tag.put("value", value);
        return tag;
    }
 
    public Map paramFloat(Float value) {
        Map tag = new HashMap();
        tag.put("kind", "Float");
        tag.put("value", value);
        return tag;
    }
 
    public Map paramDouble(Double value) {
        Map tag = new HashMap();
        tag.put("kind", "Double");
        tag.put("value", value);
        return tag;
    }

    protected PreparedStatement buildStatement(String sql, Map... params) throws Exception {
        List args = new ArrayList();
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                args.add(params[i]);
            }
        }
        return buildStatement(sql, args);
    }
 
    protected PreparedStatement buildStatement(String sql, List params) throws Exception {
        PreparedStatement prep = conn.prepareStatement(sql);
        for (int i = 0; i < params.size(); i++) {
            Map item = (Map)params.get(i);
            String kind = item.get("kind") + "";
            if ("String".equals(kind)) {
                prep.setString(i + 1, (String)item.get("value"));
            } else if ("Boolean".equals(kind)) {
                prep.setBoolean(i + 1, (Boolean)item.get("value"));
            } else if ("Integer".equals(kind)) {
                prep.setInt(i + 1, (Integer)item.get("value"));
            } else if ("Long".equals(kind)) {
                prep.setLong(i + 1, (Long)item.get("value"));
            } else if ("Float".equals(kind)) {
                prep.setFloat(i + 1, (Float)item.get("value"));
            } else if ("Double".equals(kind)) {
                prep.setDouble(i + 1, (Double)item.get("value"));
            } else {
                prep.setString(i + 1, item.get("value") + "");
            }
        }
        return prep;
    }

    protected List parseResult(ResultSet rs) throws Exception {
        List tag = new ArrayList();
        ResultSetMetaData md = rs.getMetaData();
        while (rs.next()) {
            Map item = new HashMap();
            for (int i = 1; i <= md.getColumnCount(); i++) {
                String name = md.getColumnName(i);
                int type = md.getColumnType(i);
                if (type == java.sql.Types.BIGINT) {
                    item.put(name, rs.getLong(name));
                } else if (type == java.sql.Types.INTEGER || type == java.sql.Types.SMALLINT || type == java.sql.Types.TINYINT) {
                    item.put(name, rs.getInt(name));
                } else if (type == java.sql.Types.DECIMAL || type == java.sql.Types.DOUBLE || type == java.sql.Types.NUMERIC || type == java.sql.Types.REAL) {
                    item.put(name, rs.getDouble(name));
                } else if (type == java.sql.Types.FLOAT) {
                    item.put(name, rs.getFloat(name));
                } else if (type == java.sql.Types.BIT || type == java.sql.Types.BOOLEAN) {
                    item.put(name, rs.getBoolean(name));
                } else {
                    item.put(name, rs.getString(name));
                }
            }
            tag.add(item);
        }
        return tag;
    }
 
}

  Protected by Copyscape Online Copyright Protection

No comments:

Post a Comment