coliver 2003/03/03 22:08:37
Modified: src/java/org/apache/cocoon/components/flow/javascript JavaScriptInterpreter.java JSCocoon.java system.js ScriptableResult.java ScriptableConnection.java Log: Detached database stuff from Cocoon object Revision Changes Path 1.24 +0 -4 xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JavaScriptInterpreter.java Index: JavaScriptInterpreter.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JavaScriptInterpreter.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- JavaScriptInterpreter.java 3 Mar 2003 18:50:25 -0000 1.23 +++ JavaScriptInterpreter.java 4 Mar 2003 06:08:37 -0000 1.24 @@ -168,10 +168,6 @@ // Wrapper for WebContinuation ScriptableObject.defineClass(scope, JSWebContinuation.class); - // Wrapper for JDBC - ScriptableObject.defineClass(scope, ScriptableConnection.class); - ScriptableObject.defineClass(scope, ScriptableResult.class); - // Define some functions on the top level scope String[] names = { "print" }; try { 1.20 +9 -34 xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JSCocoon.java Index: JSCocoon.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JSCocoon.java,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- JSCocoon.java 3 Mar 2003 18:50:25 -0000 1.19 +++ JSCocoon.java 4 Mar 2003 06:08:37 -0000 1.20 @@ -77,7 +77,7 @@ import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.Component; -import org.apache.avalon.excalibur.datasource.DataSourceComponent; + /** @@ -152,8 +152,8 @@ public Request jsGet_request() { if (environment == null) { - // context has been invalidated - return null; + // context has been invalidated + return null; } Map objectModel = environment.getObjectModel(); return ObjectModelHelper.getRequest(objectModel); @@ -162,8 +162,8 @@ public Response jsGet_response() { if (environment == null) { - // context has been invalidated - return null; + // context has been invalidated + return null; } Map objectModel = environment.getObjectModel(); return ObjectModelHelper.getResponse(objectModel); @@ -172,8 +172,8 @@ public Session jsGet_session() { if (environment == null) { - // context has been invalidated - return null; + // context has been invalidated + return null; } return jsGet_request().getSession(); } @@ -181,8 +181,8 @@ public Context jsGet_context() { if (environment == null) { - // context has been invalidated - return null; + // context has been invalidated + return null; } Map objectModel = environment.getObjectModel(); return ObjectModelHelper.getContext(objectModel); @@ -193,31 +193,6 @@ return manager; } - /** - * Get a Scriptable JDBC Connection - * @param selector - * @return instance of ScriptableConnection - */ - - public Object jsFunction_getConnection(String selectorValue) - throws Exception { - ComponentSelector selector = - (ComponentSelector)manager.lookup(DataSourceComponent.ROLE + "Selector"); - try { - DataSourceComponent ds = - (DataSourceComponent)selector.select(selectorValue); - if (ds == null) { - return null; - } - ScriptableConnection conn = - new ScriptableConnection(getTopLevelScope(this), ds.getConnection()); - conn.setPrototype(getClassPrototype(this, conn.getClassName())); - conn.setParentScope(getTopLevelScope(this)); - return conn; - } finally { - manager.release(selector); - } - } /** 1.10 +17 -0 xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/system.js Index: system.js =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/system.js,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- system.js 28 Feb 2003 19:12:13 -0000 1.9 +++ system.js 4 Mar 2003 06:08:37 -0000 1.10 @@ -396,6 +396,23 @@ this[application](new XForm(id, validator_ns, validator_doc)); } +// +// Prototype Database API +// +// TBD: Move this Database stuff to its own library outside of flow +// +defineClass("org.apache.cocoon.components.flow.javascript.ScriptableConnection"); +defineClass("org.apache.cocoon.components.flow.javascript.ScriptableResult"); + +Database.getConnection = function(selectorValue) { + var selector = cocoon.componentManager.lookup(Packages.org.apache.avalon.excalibur.datasource.DataSourceComponent.ROLE + "Selector"); + try { + var ds = selector.select(selectorValue); + return new Database(ds.getConnection()); + } finally { + cocoon.componentManager.release(selector); + } +} 1.2 +29 -29 xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptableResult.java Index: ScriptableResult.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptableResult.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ScriptableResult.java 3 Mar 2003 18:50:25 -0000 1.1 +++ ScriptableResult.java 4 Mar 2003 06:08:37 -0000 1.2 @@ -50,7 +50,7 @@ public class ScriptableResult extends ScriptableObject { public String getClassName() { - return "Result"; + return "Result"; } public ScriptableResult() { @@ -58,36 +58,36 @@ public static class Row extends ScriptableObject { - public String getClassName() { - return "Row"; - } - - public Object get(String name, Scriptable start) { - return super.get(name.toUpperCase(), start); - } - - public void put(String name, Scriptable start, Object value) { - super.put(name.toUpperCase(), start, value); - } + public String getClassName() { + return "Row"; + } + + public Object get(String name, Scriptable start) { + return super.get(name.toUpperCase(), start); + } + + public void put(String name, Scriptable start, Object value) { + super.put(name.toUpperCase(), start, value); + } } ScriptableResult(Scriptable scope, - ResultSet rs, int startRow, int maxRows) - throws SQLException, PropertyException, NotAFunctionException, JavaScriptException { - Context cx = Context.getCurrentContext(); + ResultSet rs, int startRow, int maxRows) + throws SQLException, PropertyException, NotAFunctionException, JavaScriptException { + Context cx = Context.getCurrentContext(); Scriptable rowMap = cx.newObject(scope, "Array"); - put("rows", this, rowMap); + put("rows", this, rowMap); Scriptable rowByIndex = cx.newObject(scope, "Array"); - put("rowsByIndex", this, rowByIndex); + put("rowsByIndex", this, rowByIndex); ResultSetMetaData rsmd = rs.getMetaData(); int noOfColumns = rsmd.getColumnCount(); // Create the column name array Scriptable columnNames = cx.newObject(scope, - "Array", - new Object[] {new Integer(noOfColumns)}); - put("columnNames", this, columnNames); + "Array", + new Object[] {new Integer(noOfColumns)}); + put("columnNames", this, columnNames); for (int i = 1; i <= noOfColumns; i++) { columnNames.put(i-1, columnNames, rsmd.getColumnName(i)); } @@ -99,18 +99,18 @@ // Process the remaining rows upto maxRows int processedRows = 0; - int index = 0; - boolean isLimited = false; + int index = 0; + boolean isLimited = false; while (rs.next()) { if ((maxRows != -1) && (processedRows == maxRows)) { isLimited = true; break; } Scriptable columns = cx.newObject(scope, "Array", - new Object[] {new Integer(noOfColumns)}); - Scriptable columnMap = new Row(); - columnMap.setParentScope(columns.getParentScope()); - columnMap.setPrototype(getObjectPrototype(scope)); + new Object[] {new Integer(noOfColumns)}); + Scriptable columnMap = new Row(); + columnMap.setParentScope(columns.getParentScope()); + columnMap.setPrototype(getObjectPrototype(scope)); // JDBC uses 1 as the lowest index! for (int i = 1; i <= noOfColumns; i++) { @@ -124,10 +124,10 @@ rowMap.put(index, rowMap, columnMap); rowByIndex.put(index, rowByIndex, columns); processedRows++; - index++; + index++; } - put("rowCount", this, new Integer(index)); - put("isLimitedByMaxRows", this, new Boolean(isLimited)); + put("rowCount", this, new Integer(index)); + put("isLimitedByMaxRows", this, new Boolean(isLimited)); } } 1.3 +154 -153 xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptableConnection.java Index: ScriptableConnection.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptableConnection.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ScriptableConnection.java 3 Mar 2003 18:55:05 -0000 1.2 +++ ScriptableConnection.java 4 Mar 2003 06:08:37 -0000 1.3 @@ -76,100 +76,101 @@ Scriptable wrapper; static Object wrap(final Scriptable wrapper, - final Scriptable wrapped, - Object obj) { - if (obj instanceof Function) { - return wrap(wrapper, wrapped, (Function)obj); - } - return obj; + final Scriptable wrapped, + Object obj) { + if (obj instanceof Function) { + return wrap(wrapper, wrapped, (Function)obj); + } + return obj; } + static Function wrap(final Scriptable wrapper, - final Scriptable wrapped, - final Function fun) { - return new Function() { - public Object call(Context cx, Scriptable scope, Scriptable thisObj, - Object[] args) throws JavaScriptException { - if (thisObj == wrapper) { - thisObj = wrapped; - } - return fun.call(cx, scope, thisObj, args); - } - - public Scriptable construct(Context cx, Scriptable scope, - Object[] args) - throws JavaScriptException { - return fun.construct(cx, scope, args); - } - - public String getClassName() { - return fun.getClassName(); - } - - public Object get(String name, Scriptable start) { - return fun.get(name, fun); - } - - public Object get(int index, Scriptable start) { - return fun.get(index, fun); - } - - public boolean has(String name, Scriptable start) { - return fun.has(name, start); - } - - public boolean has(int index, Scriptable start) { - return fun.has(index, start); - } - - - public void put(String name, Scriptable start, Object value) { - fun.put(name, start, value); - } - - public void put(int index, Scriptable start, Object value) { - fun.put(index, start, value); - } - - public void delete(String name) { - fun.delete(name); - } - - public void delete(int index) { - fun.delete(index); - } - - public Scriptable getPrototype() { - return fun.getPrototype(); - } - - public void setPrototype(Scriptable prototype) { - } - - public Scriptable getParentScope() { - return fun.getParentScope(); - } - - public void setParentScope(Scriptable parent) { - } - - public Object[] getIds() { - return fun.getIds(); - } - - public Object getDefaultValue(Class hint) { - return fun.getDefaultValue(hint); - } - - public boolean hasInstance(Scriptable instance) { - return fun.hasInstance(instance); - } + final Scriptable wrapped, + final Function fun) { + return new Function() { + public Object call(Context cx, Scriptable scope, Scriptable thisObj, + Object[] args) throws JavaScriptException { + if (thisObj == wrapper) { + thisObj = wrapped; + } + return fun.call(cx, scope, thisObj, args); + } + + public Scriptable construct(Context cx, Scriptable scope, + Object[] args) + throws JavaScriptException { + return fun.construct(cx, scope, args); + } + + public String getClassName() { + return fun.getClassName(); + } + + public Object get(String name, Scriptable start) { + return fun.get(name, fun); + } + + public Object get(int index, Scriptable start) { + return fun.get(index, fun); + } + + public boolean has(String name, Scriptable start) { + return fun.has(name, start); + } + + public boolean has(int index, Scriptable start) { + return fun.has(index, start); + } + + + public void put(String name, Scriptable start, Object value) { + fun.put(name, start, value); + } + + public void put(int index, Scriptable start, Object value) { + fun.put(index, start, value); + } + + public void delete(String name) { + fun.delete(name); + } + + public void delete(int index) { + fun.delete(index); + } + + public Scriptable getPrototype() { + return fun.getPrototype(); + } + + public void setPrototype(Scriptable prototype) { + } + + public Scriptable getParentScope() { + return fun.getParentScope(); + } + + public void setParentScope(Scriptable parent) { + } + + public Object[] getIds() { + return fun.getIds(); + } + + public Object getDefaultValue(Class hint) { + return fun.getDefaultValue(hint); + } + + public boolean hasInstance(Scriptable instance) { + return fun.hasInstance(instance); + } - }; + }; } public String getClassName() { - return "Database"; + return "Database"; } public ScriptableConnection() { @@ -179,97 +180,97 @@ } public static Scriptable jsConstructor(Context cx, Object[] args, - Function ctorObj, - boolean inNewExpr) - throws Exception { - Connection conn = null; - if (args.length > 0) { - Object arg = args[0]; - if (arg instanceof Wrapper) { - arg = ((Wrapper)arg).unwrap(); - } - if (arg instanceof Connection) { - conn = (Connection)arg; - } - } - if (conn == null) { - throw new JavaScriptException("expected an instance of java.sql.Connection"); - } - ScriptableConnection result = new ScriptableConnection(ctorObj, conn); - return result; + Function ctorObj, + boolean inNewExpr) + throws Exception { + Connection conn = null; + if (args.length > 0) { + Object arg = args[0]; + if (arg instanceof Wrapper) { + arg = ((Wrapper)arg).unwrap(); + } + if (arg instanceof Connection) { + conn = (Connection)arg; + } + } + if (conn == null) { + throw new JavaScriptException("expected an instance of java.sql.Connection"); + } + ScriptableConnection result = new ScriptableConnection(ctorObj, conn); + return result; } public ScriptableConnection(Scriptable parent, Connection conn) { - this.connection = conn; - this.wrapper = Context.toObject(connection, parent); + this.connection = conn; + this.wrapper = Context.toObject(connection, parent); } public Object jsFunction_query(String sql, - int startRow, - int maxRows) throws Exception { - Statement stmt = connection.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - if (maxRows == 0) { - maxRows = -1; - } - ScriptableResult s = new ScriptableResult(this, rs, - startRow, maxRows); - s.setParentScope(getTopLevelScope(this)); - s.setPrototype(getClassPrototype(this, s.getClassName())); - return s; + int startRow, + int maxRows) throws Exception { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery(sql); + if (maxRows == 0) { + maxRows = -1; + } + ScriptableResult s = new ScriptableResult(this, rs, + startRow, maxRows); + s.setParentScope(getTopLevelScope(this)); + s.setPrototype(getClassPrototype(this, s.getClassName())); + return s; } public int jsFunction_update(String sql) throws Exception { - Statement stmt = connection.createStatement(); - stmt.execute(sql); - return stmt.getUpdateCount(); + Statement stmt = connection.createStatement(); + stmt.execute(sql); + return stmt.getUpdateCount(); } public Object get(String name, Scriptable start) { - if (wrapper != null) { - Object result = wrapper.get(name, wrapper); - if (result != NOT_FOUND) { - return wrap(this, wrapper, result); - } - } - return super.get(name, start); + if (wrapper != null) { + Object result = wrapper.get(name, wrapper); + if (result != NOT_FOUND) { + return wrap(this, wrapper, result); + } + } + return super.get(name, start); } public boolean has(String name, Scriptable start) { - if (wrapper != null) { - if (wrapper.has(name, wrapper)) { - return true; - } - } - return super.has(name, start); + if (wrapper != null) { + if (wrapper.has(name, wrapper)) { + return true; + } + } + return super.has(name, start); } public boolean has(int index, Scriptable start) { - if (wrapper != null) { - if (wrapper.has(index, start)) { - return true; - } - } - return super.has(index, start); + if (wrapper != null) { + if (wrapper.has(index, start)) { + return true; + } + } + return super.has(index, start); } public Object get(int index, Scriptable start) { - if (wrapper != null) { - Object result = wrapper.get(index, start); - if (result != NOT_FOUND) { - return wrap(this, wrapper, result); - } - } - return super.get(index, start); + if (wrapper != null) { + Object result = wrapper.get(index, start); + if (result != NOT_FOUND) { + return wrap(this, wrapper, result); + } + } + return super.get(index, start); } public void put(String name, Scriptable start, Object value) { - if (wrapper != null) { - wrapper.put(name, wrapper, value); - return; - } - super.put(name, start, value); + if (wrapper != null) { + wrapper.put(name, wrapper, value); + return; + } + super.put(name, start, value); } }