Author: mrdon Date: Wed Oct 5 16:54:04 2005 New Revision: 295142 URL: http://svn.apache.org/viewcvs?rev=295142&view=rev Log: * Changing examples to auto-reload flow scripts * Adding experimental ibatis integration * Adding the ability to define classes outside core
Added: struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/ struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.java (with props) Modified: struts/flow/trunk/build.xml struts/flow/trunk/project.xml struts/flow/trunk/src/examples/WEB-INF/guess/struts-config.xml struts/flow/trunk/src/examples/WEB-INF/remote/struts-config.xml struts/flow/trunk/src/examples/WEB-INF/templates/struts-config.xml struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java Modified: struts/flow/trunk/build.xml URL: http://svn.apache.org/viewcvs/struts/flow/trunk/build.xml?rev=295142&r1=295141&r2=295142&view=diff ============================================================================== --- struts/flow/trunk/build.xml (original) +++ struts/flow/trunk/build.xml Wed Oct 5 16:54:04 2005 @@ -113,6 +113,13 @@ ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-beanutils/jars/commons-beanutils-1.7.0.jar"/> + <get dest="${lib.core}/ibatis-common-2.jar" usetimestamp="true" + ignoreerrors="true" + src="http://www.ibiblio.org/maven/com.ibatis/jars/ibatis2-common-2.1.5.582.jar"/> + <get dest="${lib.core}/ibatis-sqlmap-2.jar" usetimestamp="true" + ignoreerrors="true" + src="http://www.ibiblio.org/maven/com.ibatis/jars/ibatis2-sqlmap-2.1.5.582.jar"/> + <get dest="${lib.core}/commons-validator.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-validator/jars/commons-validator-1.1.3.jar"/> Modified: struts/flow/trunk/project.xml URL: http://svn.apache.org/viewcvs/struts/flow/trunk/project.xml?rev=295142&r1=295141&r2=295142&view=diff ============================================================================== --- struts/flow/trunk/project.xml (original) +++ struts/flow/trunk/project.xml Wed Oct 5 16:54:04 2005 @@ -81,7 +81,27 @@ </properties> <url>http://xdoclet.sourceforge.net/xjavadoc/</url> </dependency> + + <dependency> + <groupId>com.ibatis</groupId> + <artifactId>ibatis2-common</artifactId> + <version>2.1.5.582</version> + <properties> + <war.bundle>true</war.bundle> + </properties> + <url>http://ibatis.apache.org/</url> + </dependency> + <dependency> + <groupId>com.ibatis</groupId> + <artifactId>ibatis2-sqlmap</artifactId> + <version>2.1.5.582</version> + <properties> + <war.bundle>true</war.bundle> + </properties> + <url>http://ibatis.apache.org/</url> + </dependency> + </dependencies> <build> Modified: struts/flow/trunk/src/examples/WEB-INF/guess/struts-config.xml URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/examples/WEB-INF/guess/struts-config.xml?rev=295142&r1=295141&r2=295142&view=diff ============================================================================== --- struts/flow/trunk/src/examples/WEB-INF/guess/struts-config.xml (original) +++ struts/flow/trunk/src/examples/WEB-INF/guess/struts-config.xml Wed Oct 5 16:54:04 2005 @@ -25,6 +25,7 @@ <plug-in className="org.apache.struts.flow.FlowPlugIn"> <set-property property="scripts" value="/WEB-INF/guess/numberguess.js" /> <set-property property="debugger" value="false" /> + <set-property property="reloadScripts" value="true" /> <set-property property="timeToLive" value="600000" /> </plug-in> Modified: struts/flow/trunk/src/examples/WEB-INF/remote/struts-config.xml URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/examples/WEB-INF/remote/struts-config.xml?rev=295142&r1=295141&r2=295142&view=diff ============================================================================== --- struts/flow/trunk/src/examples/WEB-INF/remote/struts-config.xml (original) +++ struts/flow/trunk/src/examples/WEB-INF/remote/struts-config.xml Wed Oct 5 16:54:04 2005 @@ -24,6 +24,7 @@ <plug-in className="org.apache.struts.flow.FlowPlugIn"> <set-property property="scripts" value="/WEB-INF/remote/numberguess.js" /> + <set-property property="reloadScripts" value="true" /> <set-property property="debugger" value="false" /> <set-property property="timeToLive" value="600000" /> </plug-in> Modified: struts/flow/trunk/src/examples/WEB-INF/templates/struts-config.xml URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/examples/WEB-INF/templates/struts-config.xml?rev=295142&r1=295141&r2=295142&view=diff ============================================================================== --- struts/flow/trunk/src/examples/WEB-INF/templates/struts-config.xml (original) +++ struts/flow/trunk/src/examples/WEB-INF/templates/struts-config.xml Wed Oct 5 16:54:04 2005 @@ -21,6 +21,7 @@ <plug-in className="org.apache.struts.flow.FlowPlugIn"> <set-property property="scripts" value="/WEB-INF/templates/numberguess.js" /> + <set-property property="reloadScripts" value="true" /> <set-property property="debugger" value="false" /> <set-property property="timeToLive" value="600000" /> </plug-in> Modified: struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java?rev=295142&r1=295141&r2=295142&view=diff ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java Wed Oct 5 16:54:04 2005 @@ -15,8 +15,8 @@ */ package org.apache.struts.flow; -import java.util.HashMap; -import java.util.Map; +import java.util.*; +import java.io.*; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -30,6 +30,10 @@ import org.apache.struts.flow.core.JavaScriptInterpreter; import org.apache.struts.flow.core.DefaultCallVariableRegistrar; import org.apache.struts.flow.sugar.SugarWrapFactory; +import org.apache.struts.flow.ibatis.SqlMap; + +import com.ibatis.sqlmap.client.*; +import com.ibatis.common.resources.*; /** * Initializes the Flow interpreter and loads system and user scripts. There @@ -76,6 +80,7 @@ private ServletContext context; private String scripts = null; private String scriptBase = null; + private List classesToRegister = new ArrayList(); private boolean debugger = false; private boolean reloadScripts; @@ -153,6 +158,35 @@ this.ttl = ttl; } + public void setSqlMapConfig(String path) { + try { + Reader reader = Resources.getResourceAsReader(path); + SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient(reader); + SqlMap.setSqlMapClient(client); + classesToRegister.add(SqlMap.class); + + /* + Set namespaces = SqlMap.getNamespaces(); + for (Iterator i = namespaces.iterator(); i.hasNext(); ) { + final String ns = (String)i.next(); + classesToRegister.add( + new SqlMap() { + //public SqlMap() { + // setNamespace(ns); + //} + + public String getClassName() { + return ns; + } + }s + ); + } + */ + } catch (Exception ex) { + ex.printStackTrace(); + } + } + /** * Initialize the flow interpreter @@ -227,9 +261,10 @@ interp.setCheckTime(checkTime); interp.setReloadScripts(reloadScripts); interp.setWrapFactory(new SugarWrapFactory()); - interp.initialize(); + interp.initialize(classesToRegister); interp.register("/system.js"); interp.addVariableRegistrar(new DefaultCallVariableRegistrar(Struts.class, "struts")); + interp.addVariableRegistrar(new DefaultCallVariableRegistrar(SqlMap.class, "sqlMap")); return interp; } Modified: struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java?rev=295142&r1=295141&r2=295142&view=diff ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java Wed Oct 5 16:54:04 2005 @@ -222,6 +222,16 @@ [EMAIL PROTECTED] FlowException If anything goes wrong */ public void initialize() throws FlowException { + initialize(null); + } + + + /** + * Initialize the global scope + * + [EMAIL PROTECTED] FlowException If anything goes wrong + */ + public void initialize(List classes) throws FlowException { if (enableDebugger) { if (getLogger().isDebugEnabled()) { getLogger().debug("Flow debugger enabled, creating"); @@ -241,6 +251,13 @@ // Register some handy classes with JavaScript, so we can make // use of them from the flow layer. initScope(context, scope); + + // Register any custom classes + if (classes != null) { + for (Iterator i = classes.iterator(); i.hasNext(); ) { + ScriptableObject.defineClass(scope, (Class)i.next()); + } + } errorReporter = new JSErrorReporter(getLogger()); } catch (Exception e) { Added: struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.java?rev=295142&view=auto ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.java (added) +++ struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.java Wed Oct 5 16:54:04 2005 @@ -0,0 +1,134 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts.flow.ibatis; + +import org.mozilla.javascript.*; +import com.ibatis.sqlmap.engine.mapping.statement.*; +import com.ibatis.sqlmap.engine.impl.*; +import com.ibatis.sqlmap.client.*; +import org.apache.struts.flow.core.*; +import org.apache.struts.flow.sugar.*; + +import org.mozilla.javascript.JavaScriptException; +import java.util.*; +import java.io.*; +import java.sql.SQLException; + +/** + * JavaScript interface to the log facility. + * + [EMAIL PROTECTED] log + */ +public class SqlMap extends ScriptableObject { + protected static SqlMapClient client; + protected String namespace; + protected Map statements = new HashMap(); + + + /** Constructor for the JSLog object */ + public SqlMap() { } + + public static void setSqlMapClient(SqlMapClient c) { + client = c; + } + + public Object jsFunction_setNamespace(String ns) throws JavaScriptException { + setNamespace(ns); + return null; + } + + public void setNamespace(String ns) { + System.out.println("setting namespace"); + this.namespace = ns; + statements.clear(); + SqlMapClientImpl cimpl = (SqlMapClientImpl)client; + SqlMapExecutorDelegate del = cimpl.getDelegate(); + String name; + for (Iterator i = del.getMappedStatementNames(); i.hasNext(); ) { + name = (String) i.next(); + System.out.println("looking at statement "+name); + if (name.startsWith(namespace+".")) { + System.out.println("adding statement "+name); + statements.put(name.substring(namespace.length() + 1), del.getMappedStatement(name)); + } + } + } + + public Set getNamespaces() { + Set result = new HashSet(); + SqlMapClientImpl cimpl = (SqlMapClientImpl)client; + SqlMapExecutorDelegate del = cimpl.getDelegate(); + String name, ns; + int pos; + for (Iterator i = del.getMappedStatementNames(); i.hasNext(); ) { + name = (String) i.next(); + pos = name.indexOf('.'); + if (pos > -1) { + ns = name.substring(0, pos); + result.add(ns); + } + } + return result; + } + + + /** + * Gets the class name + * + [EMAIL PROTECTED] The className value + */ + public String getClassName() { + return (this.namespace == null ? "SqlMap" : this.namespace); + } + + public Object get(final String name, Scriptable start) { + + System.out.println("getting property "+name); + final MappedStatement ms = (MappedStatement) statements.get(name); + if (ms != null) { + System.out.println("found as a function"); + return new ExtensionFunction() { + public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) + throws IOException { + + Map params = JSFlow.jsobjectToMap(thisObj); + String stmName = namespace+"."+name; + Object result = null; + StatementType type = ms.getStatementType(); + try { + if (type == StatementType.INSERT) { + result = client.insert(stmName, params); + } else if (type == StatementType.DELETE) { + result = new Integer(client.delete(stmName, params)); + } else if (type == StatementType.UPDATE) { + result = new Integer(client.update(stmName, params)); + } else { + result = client.queryForList(stmName, params); + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return result; + } + }; + } else { + return super.get(name, start); + } + } + + +} + Propchange: struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.java ------------------------------------------------------------------------------ svn:executable = * --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]