You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by mr...@apache.org on 2005/10/08 10:44:58 UTC
svn commit: r307274 - in /struts/flow/trunk: ./
src/java/org/apache/struts/flow/ src/java/org/apache/struts/flow/core/
src/java/org/apache/struts/flow/ibatis/
src/test/org/apache/struts/flow/ibatis/
Author: mrdon
Date: Sat Oct 8 01:44:42 2005
New Revision: 307274
URL: http://svn.apache.org/viewcvs?rev=307274&view=rev
Log:
== ibatis Integration ==
* Adding the automatic generation and registration of classes, one for each SqlMap namespace
* Adding a unit test to ensure classes discovered and generated correctly
* Adding ability to pass parameters to sql map as function parameter object, in addition to implicity through parent properties
* Adding Janino as dependency to compile the generated classes
== Misc ==
* Fixing the flow core to use sugar wrap factory at every Context enter
Added:
struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.template
struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/
struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/Foo.xml
struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/System.xml
struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/TestSqlMap.java
struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/sqlmap.conf
Modified:
struts/flow/trunk/build.xml
struts/flow/trunk/project.xml
struts/flow/trunk/src/java/org/apache/struts/flow/FlowPlugIn.java
struts/flow/trunk/src/java/org/apache/struts/flow/core/JavaScriptInterpreter.java
struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.java
Modified: struts/flow/trunk/build.xml
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/build.xml?rev=307274&r1=307273&r2=307274&view=diff
==============================================================================
--- struts/flow/trunk/build.xml (original)
+++ struts/flow/trunk/build.xml Sat Oct 8 01:44:42 2005
@@ -83,15 +83,17 @@
<get dest="${lib.build}/xjavadoc-1.0.3.jar" usetimestamp="true"
ignoreerrors="true"
src="http://www.ibiblio.org/maven/xdoclet/jars/xjavadoc-1.0.3.jar"/>
+ <!--
<get dest="${lib.core}/commons-collections-2.1.1.jar" usetimestamp="true"
ignoreerrors="true"
- src="http://www.ibiblio.org/maven/commons-collections/jars/commons-collections-2.1.1.jar"/>
+ src="http://www.ibiblio.org/maven/commons-collections/jars/commons-collections-2.1.1.jar"/>
+ -->
<mkdir dir="${lib.core}" />
- <get dest="${lib.core}/struts-1.1.jar" usetimestamp="true"
+ <get dest="${lib.core}/struts-1.2.7.jar" usetimestamp="true"
ignoreerrors="true"
- src="http://www.ibiblio.org/maven/struts/jars/struts-1.1.jar"/>
+ src="http://www.ibiblio.org/maven/struts/jars/struts-1.2.7.jar"/>
<get dest="${lib.core}/js-1.6R1.jar" usetimestamp="true"
ignoreerrors="true"
@@ -118,14 +120,17 @@
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"/>
+ src="http://www.ibiblio.org/maven/com.ibatis/jars/ibatis2-sqlmap-2.1.5.582.jar"/>
+ <get dest="${lib.core}/janino.jar" usetimestamp="true"
+ ignoreerrors="true"
+ src="http://www.ibiblio.org/maven/janino/jars/janino-2.3.8.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"/>
<get dest="${lib.core}/commons-digester.jar" usetimestamp="true"
ignoreerrors="true"
- src="http://www.ibiblio.org/maven/commons-digester/jars/commons-digester-1.6.jar"/>
+ src="http://www.ibiblio.org/maven/commons-digester/jars/commons-digester-1.7.jar"/>
</target>
@@ -238,7 +243,9 @@
destdir="${build.target}"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
- optimize="${compile.optimize}">
+ optimize="${compile.optimize}"
+ source="1.4"
+ target="1.4">
<classpath refid="base.classpath" />
</javac>
<copy todir="${build.target}">
@@ -257,20 +264,24 @@
<classpath refid="base.classpath" />
</javac>
- <java classpathref="base.classpath" classname="org.apache.struts.flow.sugar.JavascriptSugarTest">
+ <copy todir="${build.test}">
+ <fileset dir="${src.test}" excludes="**/*.java" />
+ </copy>
+
+ <junit printsummary="on" fork="on">
+ <formatter type="xml" />
+ <classpath refid="base.classpath" />
+
+ <batchtest todir="${build.test}">
+ <fileset dir="${src.test}">
+ <include name="**/Test*.java" />
+ </fileset>
+ </batchtest>
+ </junit>
+
+ <java classpathref="base.classpath" classname="org.apache.struts.flow.sugar.JavascriptSugarTest">
<arg path="${src.test}" />
</java>
- <!--
- <junit printsummary="on" fork="on">
- <formatter type="xml" />
- <classpath refid="base.classpath" />
-
- <batchtest todir="${test.results}">
- <fileset dir="${test.src}">
- <include name="**/*Test.java" />
- </fileset>
- </batchtest>
- </junit> -->
</target>
<!--
The 'javadoc' target creates the API documentation.
Modified: struts/flow/trunk/project.xml
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/project.xml?rev=307274&r1=307273&r2=307274&view=diff
==============================================================================
--- struts/flow/trunk/project.xml (original)
+++ struts/flow/trunk/project.xml Sat Oct 8 01:44:42 2005
@@ -45,7 +45,7 @@
<dependency>
<groupId>struts</groupId>
<artifactId>struts-core</artifactId>
- <version>1.3.0-dev</version>
+ <version>1.2.7</version>
<properties>
<war.bundle>true</war.bundle>
</properties>
@@ -92,7 +92,7 @@
<url>http://ibatis.apache.org/</url>
</dependency>
- <dependency>
+ <dependency>
<groupId>com.ibatis</groupId>
<artifactId>ibatis2-sqlmap</artifactId>
<version>2.1.5.582</version>
@@ -102,6 +102,17 @@
<url>http://ibatis.apache.org/</url>
</dependency>
+ <dependency>
+ <groupId>janino</groupId>
+ <artifactId>janino</artifactId>
+ <version>2.3.8</version>
+ <properties>
+ <war.bundle>true</war.bundle>
+ </properties>
+ <url>http://www.janino.net/</url>
+ </dependency>
+
+
</dependencies>
<build>
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=307274&r1=307273&r2=307274&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 Sat Oct 8 01:44:42 2005
@@ -165,23 +165,13 @@
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
- );
+ Map classes = SqlMap.buildNamespaceClasses();
+
+ for (Iterator i = classes.values().iterator(); i.hasNext(); ) {
+ Class cls = (Class) i.next();
+ System.out.println("Registring "+cls.getName());
+ classesToRegister.add(cls);
}
- */
} catch (Exception ex) {
ex.printStackTrace();
}
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=307274&r1=307273&r2=307274&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 Sat Oct 8 01:44:42 2005
@@ -500,6 +500,9 @@
context.setOptimizationLevel(OPTIMIZATION_LEVEL);
context.setGeneratingDebug(true);
context.setCompileFunctionsWithDynamicScope(true);
+ if (wrapFactory != null) {
+ context.setWrapFactory(wrapFactory);
+ }
// Obtain the JS continuation object from it, and setup the
// JSFlow object associated in the dynamic scope of the saved
@@ -580,6 +583,9 @@
context.setGeneratingDebug(true);
context.setCompileFunctionsWithDynamicScope(true);
context.setErrorReporter(errorReporter);
+ if (wrapFactory != null) {
+ context.setWrapFactory(wrapFactory);
+ }
Scriptable thrScope = null;
// The Cocoon object exported to JavaScript needs to be setup here
Modified: 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=307274&r1=307273&r2=307274&view=diff
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.java (original)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.java Sat Oct 8 01:44:42 2005
@@ -27,6 +27,8 @@
import java.io.*;
import java.sql.SQLException;
+import org.codehaus.janino.SimpleCompiler;
+
/**
* JavaScript interface to the log facility.
*
@@ -44,6 +46,46 @@
public static void setSqlMapClient(SqlMapClient c) {
client = c;
}
+
+ public static Map buildNamespaceClasses() {
+ InputStream in = SqlMap.class.getResourceAsStream("SqlMap.template");
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+ StringBuffer sb = new StringBuffer();
+ String line;
+ try {
+ while ((line = reader.readLine()) != null) {
+ sb.append(line);
+ }
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ return null;
+ }
+
+ String templateText = sb.toString();
+
+ Map classes = new HashMap();
+ Set namespaces = getNamespaces();
+ String name, source;
+ for (Iterator i = namespaces.iterator(); i.hasNext(); ) {
+ name = (String) i.next();
+ source = templateText.replaceAll("%ns", name);
+ try {
+ SimpleCompiler compiler = new SimpleCompiler(name+".java", new StringReader(source));
+ ClassLoader cl = compiler.getClassLoader();
+
+ Class cls = cl.loadClass("org.apache.struts.flow.ibatis."+name);
+ if (cls != null) {
+ classes.put(name, cls);
+ } else {
+ System.err.println("Unable to compile class for namespace "+name);
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ return classes;
+ }
public Object jsFunction_setNamespace(String ns) throws JavaScriptException {
setNamespace(ns);
@@ -67,7 +109,7 @@
}
}
- public Set getNamespaces() {
+ public static Set getNamespaces() {
Set result = new HashSet();
SqlMapClientImpl cimpl = (SqlMapClientImpl)client;
SqlMapExecutorDelegate del = cimpl.getDelegate();
@@ -103,8 +145,13 @@
return new ExtensionFunction() {
public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args)
throws IOException {
-
- Map params = JSFlow.jsobjectToMap(thisObj);
+
+ Map params = null;
+ if (args.length == 1 && args[0] instanceof Scriptable) {
+ params = JSFlow.jsobjectToMap((Scriptable)args[0]);
+ } else {
+ params = JSFlow.jsobjectToMap(thisObj);
+ }
String stmName = namespace+"."+name;
Object result = null;
StatementType type = ms.getStatementType();
Added: struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.template
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.template?rev=307274&view=auto
==============================================================================
--- struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.template (added)
+++ struts/flow/trunk/src/java/org/apache/struts/flow/ibatis/SqlMap.template Sat Oct 8 01:44:42 2005
@@ -0,0 +1,12 @@
+package org.apache.struts.flow.ibatis;
+
+public class %ns extends SqlMap {
+
+ public %ns() {
+ setNamespace("%ns");
+ }
+
+ public String getClassName() {
+ return "%ns";
+ }
+}
Added: struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/Foo.xml
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/Foo.xml?rev=307274&view=auto
==============================================================================
--- struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/Foo.xml (added)
+++ struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/Foo.xml Sat Oct 8 01:44:42 2005
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE sqlMap
+ PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
+ "http://www.ibatis.com/dtd/sql-map-2.dtd">
+<sqlMap namespace="Foo">
+
+ <resultMap id="result1" class="java.util.HashMap">
+ <result property="id" column="systemid" />
+ <result property="name" column="systemname" />
+ </resultMap>
+
+ <select id="getAll" parameterClass="java.util.Map" resultMap="result1">
+ select systemid, systemname
+ from
+ system
+ </select>
+</sqlMap>
Added: struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/System.xml
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/System.xml?rev=307274&view=auto
==============================================================================
--- struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/System.xml (added)
+++ struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/System.xml Sat Oct 8 01:44:42 2005
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE sqlMap
+ PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
+ "http://www.ibatis.com/dtd/sql-map-2.dtd">
+<sqlMap namespace="System">
+
+ <resultMap id="result1" class="java.util.HashMap">
+ <result property="id" column="systemid" />
+ <result property="name" column="systemname" />
+ </resultMap>
+
+ <select id="getAll" parameterClass="java.util.Map" resultMap="result1">
+ select systemid, systemname
+ from
+ system
+ </select>
+</sqlMap>
Added: struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/TestSqlMap.java
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/TestSqlMap.java?rev=307274&view=auto
==============================================================================
--- struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/TestSqlMap.java (added)
+++ struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/TestSqlMap.java Sat Oct 8 01:44:42 2005
@@ -0,0 +1,45 @@
+package org.apache.struts.flow.ibatis;
+
+import junit.framework.TestCase;
+
+import java.io.*;
+import java.util.*;
+import com.ibatis.sqlmap.engine.mapping.statement.*;
+import com.ibatis.sqlmap.engine.impl.*;
+import com.ibatis.sqlmap.client.*;
+import com.ibatis.common.resources.*;
+
+/* JUnitTest case for class: org.apache.struts.chain.commands.servlet.SqlMap */
+public class TestSqlMap extends TestCase {
+
+
+ public TestSqlMap(String _name) {
+ super(_name);
+ }
+
+
+ public void testLoadNamespaceClasses() throws Exception {
+
+ Reader reader = Resources.getResourceAsReader("org/apache/struts/flow/ibatis/sqlmap.conf");
+ SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient(reader);
+
+ SqlMap.setSqlMapClient(client);
+ Map classes = SqlMap.buildNamespaceClasses();
+ assertNotNull(classes);
+ assertTrue("Size wrong, "+classes.size(), classes.size() == 2);
+
+ Class cls = (Class) classes.get("System");
+ assertNotNull(cls);
+
+ assertTrue("org.apache.struts.flow.ibatis.System".equals(cls.getName()));
+
+ SqlMap map = (SqlMap) cls.newInstance();
+ assertTrue("class name wrong ", "System".equals(map.getClassName()));
+ }
+
+ /* Executes the test case */
+ public static void main(String[] argv) {
+ String[] testCaseList = {TestSqlMap.class.getName()};
+ junit.textui.TestRunner.main(testCaseList);
+ }
+}
Added: struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/sqlmap.conf
URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/sqlmap.conf?rev=307274&view=auto
==============================================================================
--- struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/sqlmap.conf (added)
+++ struts/flow/trunk/src/test/org/apache/struts/flow/ibatis/sqlmap.conf Sat Oct 8 01:44:42 2005
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE sqlMapConfig
+ PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
+ "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
+
+<!-- Be sure to always use the appropriate header as above! Note the 2.0 DTDs. -->
+
+<sqlMapConfig>
+
+ <settings useStatementNamespaces="true" />
+
+ <sqlMap resource="org/apache/struts/flow/ibatis/System.xml" />
+ <sqlMap resource="org/apache/struts/flow/ibatis/Foo.xml" />
+
+
+</sqlMapConfig>
+
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org