001: // jdbcCommand.java
002: // $Id: jdbcCommand.java,v 1.9 2000/08/16 21:37:47 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.ssi.jdbc;
007:
008: import java.util.Dictionary;
009:
010: import java.sql.Connection;
011: import java.sql.DriverManager;
012: import java.sql.ResultSet;
013: import java.sql.Statement;
014:
015: import org.w3c.www.http.HTTP;
016:
017: import org.w3c.jigsaw.http.Reply;
018: import org.w3c.jigsaw.http.Request;
019:
020: import org.w3c.util.ArrayDictionary;
021:
022: import org.w3c.jigsaw.ssi.SSIFrame;
023:
024: import org.w3c.jigsaw.ssi.commands.Command;
025:
026: /**
027: * Implementation of the SSI <code>jdbc</code> command.
028: */
029: public class jdbcCommand implements Command {
030: private final static String NAME = "jdbc";
031: private final static boolean debug = true;
032:
033: private static final String keys[] = { "select", "url", "driver",
034: "user", "password", "name", "column", "next" };
035:
036: /**
037: * return true if reply can be cached.
038: * @return a boolean.
039: */
040: public boolean acceptCaching() {
041: return true;
042: }
043:
044: protected Connection getConnection(String driver, String url,
045: String user, String password) {
046: try {
047: Class.forName(driver);
048: return DriverManager.getConnection(url, user, password);
049: } catch (Exception ex) {
050: ex.printStackTrace();
051: }
052: return null;
053: }
054:
055: protected ResultSet performSelect(Connection conn, String cmd) {
056: try {
057: Statement smt = conn.createStatement();
058: ResultSet set = smt.executeQuery(cmd);
059: return set;
060: } catch (Exception ex) {
061: ex.printStackTrace();
062: }
063: return null;
064: }
065:
066: protected void addSet(Dictionary d, String name, Request request,
067: ResultSet set) {
068: d.put(request.toString() + "." + name, set);
069: }
070:
071: protected ResultSet getSet(Dictionary d, String name,
072: Request request) {
073: return (ResultSet) d.get(request.toString() + "." + name);
074: }
075:
076: public String getName() {
077: return NAME;
078: }
079:
080: public String getValue(Dictionary variables, String var,
081: Request request) {
082: ResultSet set = getSet(variables, var, request);
083: if (!hasMoreValue(variables, var, request))
084: return "empty";
085: else
086: return "not-empty";
087: }
088:
089: protected void sethasMoreValueFlag(Dictionary d, String name,
090: Request request, boolean flag) {
091: d.put(request.toString() + "." + name + ".flag", new Boolean(
092: flag));
093: }
094:
095: protected boolean hasMoreValue(Dictionary d, String name,
096: Request request) {
097: Boolean flag = (Boolean) d.get(request.toString() + "." + name
098: + ".flag");
099: return (flag == null) ? true : flag.booleanValue();
100: }
101:
102: public synchronized Reply execute(SSIFrame ssiframe,
103: Request request, ArrayDictionary parameters,
104: Dictionary variables) {
105: Object values[] = parameters.getMany(keys);
106: String select = (String) values[0];
107: String url = (String) values[1];
108: String driver = (String) values[2];
109: String user = (String) values[3];
110: String password = (String) values[4];
111: String name = (String) values[5];
112: String column = (String) values[6];
113: String next = (String) values[7];
114: String text = null;
115: if (select != null) {
116: // user and password are optionnals, they can be null.
117: Connection conn = getConnection(driver, url, user, password);
118: if (conn != null) {
119: addSet(variables, name, request, performSelect(conn,
120: select));
121: sethasMoreValueFlag(variables, name, request, true);
122: }
123: } else if (column != null) {
124: ResultSet set = getSet(variables, name, request);
125: try {
126: if (set != null)
127: text = set.getObject(Integer.parseInt(column))
128: .toString();
129: } catch (Exception ex) {
130: ex.printStackTrace();
131: }
132: } else if (next != null) {
133: ResultSet set = getSet(variables, name, request);
134: if (set != null) {
135: try {
136: sethasMoreValueFlag(variables, name, request, set
137: .next());
138: } catch (Exception ex) {
139: ex.printStackTrace();
140: }
141: }
142: }
143:
144: // We are NOT doing notMod hack here (tricky and useless ?)
145: Reply reply = ssiframe.createCommandReply(request, HTTP.OK);
146: if (text != null)
147: reply.setContent(text);
148: return reply;
149:
150: }
151: }
|