You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2006/07/29 18:03:29 UTC
svn commit: r426823 - in /tapestry/tapestry5/tapestry-core/trunk/src:
main/java/org/apache/tapestry/internal/ioc/services/
main/java/org/apache/tapestry/ioc/services/
test/java/org/apache/tapestry/ioc/services/
Author: hlship
Date: Sat Jul 29 09:03:29 2006
New Revision: 426823
URL: http://svn.apache.org/viewvc?rev=426823&view=rev
Log:
Partial check in of chain of command builder.
Added:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ChainBuilder.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/services/ClassFabUtilsTest.java
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ClassFabUtils.java
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java?rev=426823&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java Sat Jul 29 09:03:29 2006
@@ -0,0 +1,220 @@
+package org.apache.tapestry.internal.ioc.services;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Modifier;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.tapestry.internal.annotations.Synchronized;
+import org.apache.tapestry.ioc.services.ChainBuilder;
+import org.apache.tapestry.ioc.services.ClassFab;
+import org.apache.tapestry.ioc.services.ClassFabUtils;
+import org.apache.tapestry.ioc.services.ClassFactory;
+import org.apache.tapestry.ioc.services.MethodIterator;
+import org.apache.tapestry.ioc.services.MethodSignature;
+import org.apache.tapestry.util.BodyBuilder;
+import org.apache.tapestry.util.CollectionFactory;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+@Synchronized
+public class ChainBuilderImpl implements ChainBuilder
+{
+ private final ClassFactory _classFactory;
+
+ public ChainBuilderImpl(ClassFactory classFactory)
+ {
+ _classFactory = classFactory;
+ }
+
+ public <T> T buildImplementation(Class<T> commandInterface, List<T> commands)
+ {
+ return null;
+ }
+
+ /**
+ * Map, keyed on service interface, of implementation Class.
+ */
+
+ private Map<Class, Class> _implementations = CollectionFactory.newMap();
+
+ // public Object buildImplementation(Class commandInterface, List commands, String toString)
+ // {
+ // Defense.notNull(commandInterface, "commandInterface");
+ // Defense.notNull(commands, "commands");
+ // Defense.notNull(toString, "toString");
+ //
+ // Class instanceClass = findImplementationClass(commandInterface);
+ //
+ // return createInstance(instanceClass, commands, toString);
+ // }
+
+ @Synchronized.Read
+ private Class findImplementationClass(Class commandInterface)
+ {
+ Class result = (Class) _implementations.get(commandInterface);
+
+ if (result == null)
+ result = constructImplementationClass(commandInterface);
+
+ return result;
+ }
+
+ @Synchronized.Write
+ private Class constructImplementationClass(Class commandInterface)
+ {
+ String name = ClassFabUtils.generateClassName(commandInterface);
+
+ ClassFab cf = _classFactory.newClass(name, Object.class);
+
+ addInfrastructure(cf, commandInterface);
+
+ addMethods(cf, commandInterface);
+
+ Class result = cf.createClass();
+
+ // This is the part that really needs synchronization:
+
+ _implementations.put(commandInterface, result);
+
+ return result;
+
+ // But making the whole method exclusive ensures that redundant work
+ // doesn't take place (if multiple threads try to build chains of the
+ // same command interface simultaneously).
+ }
+
+ void addInfrastructure(ClassFab cf, Class commandInterface)
+ {
+ String arrayClassName = commandInterface.getCanonicalName() + "[]";
+ String jvmName = org.apache.tapestry.ioc.services.ClassFabUtils
+ .getJVMClassName(arrayClassName);
+
+ Class array = null;
+
+ try
+ {
+ array = Class.forName(jvmName, true, commandInterface.getClass().getClassLoader());
+ }
+ catch (Exception ex)
+ {
+ throw new RuntimeException(ex);
+ }
+
+ cf.addInterface(commandInterface);
+ cf.addField("_commands", array);
+
+ BodyBuilder builder = new BodyBuilder();
+
+ builder.addln("_commands = (%s[]) $1.toArray(new %<s[0]);", commandInterface.getName());
+
+ cf.addConstructor(new Class[]
+ { List.class, String.class }, null, builder.toString());
+ }
+
+ private <T> T createInstance(Class<T> instanceClass, List<T> commands)
+ {
+ try
+ {
+ Constructor<T> ctor = instanceClass.getConstructor(new Class[]
+ { List.class });
+
+ return instanceClass.cast(ctor.newInstance(commands));
+ }
+ catch (Exception ex)
+ {
+ // This should not be reachable!
+ throw new RuntimeException(ex);
+ }
+
+ }
+
+ void addMethods(ClassFab cf, Class commandInterface)
+ {
+ MethodIterator mi = new MethodIterator(commandInterface);
+
+ while (mi.hasNext())
+ {
+ MethodSignature sig = mi.next();
+
+ addMethod(cf, commandInterface, sig);
+ }
+
+ if (!mi.getToString())
+ addToString(cf);
+ }
+
+ void addMethod(ClassFab cf, Class commandInterface, MethodSignature sig)
+ {
+ Class returnType = sig.getReturnType();
+
+ if (returnType.equals(void.class))
+ {
+ addVoidMethod(cf, commandInterface, sig);
+ return;
+ }
+
+ String defaultValue = defaultForReturnType(returnType);
+
+ BodyBuilder builder = new BodyBuilder();
+ builder.begin();
+
+ builder
+ .addln(
+ "{0} result = {1};",
+ ClassFabUtils.getJavaClassName(returnType),
+ defaultValue);
+ builder.addln("for (int i = 0; i < _commands.length; i++)");
+
+ builder.begin();
+ builder.addln("result = _commands[i].{0}($$);", sig.getName());
+
+ builder.addln("if (result != {0}) break;", defaultValue);
+
+ builder.end();
+
+ builder.addln("return result;");
+ builder.end();
+
+ cf.addMethod(Modifier.PUBLIC, sig, builder.toString());
+ }
+
+ String defaultForReturnType(Class returnType)
+ {
+ // For all object and array types.
+
+ if (!returnType.isPrimitive())
+ return "null";
+
+ if (returnType.equals(boolean.class))
+ return "false";
+
+ // Assume, then, that it is a numeric type (this method
+ // isn't called for void).
+
+ return "0";
+ }
+
+ private void addVoidMethod(ClassFab cf, Class commandInterface, MethodSignature sig)
+ {
+ BodyBuilder builder = new BodyBuilder();
+
+ builder.begin();
+
+ builder.addln("for (int i = 0; i < _commands.length; i++)");
+ builder.addln("_commands[i].{0}($$);", sig.getName());
+
+ builder.end();
+
+ cf.addMethod(Modifier.PUBLIC, sig, builder.toString());
+ }
+
+ void addToString(ClassFab cf)
+ {
+ MethodSignature sig = new MethodSignature(String.class, "toString", null, null);
+
+ cf.addMethod(Modifier.PUBLIC, sig, "return _toString;");
+ }
+
+}
Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ChainBuilder.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ChainBuilder.java?rev=426823&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ChainBuilder.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ChainBuilder.java Sat Jul 29 09:03:29 2006
@@ -0,0 +1,40 @@
+// Copyright 2006 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.tapestry.ioc.services;
+
+import java.util.List;
+
+/**
+ * A service which can assemble an implementation based on a command interface, and an ordered list
+ * of objects implementing that interface (the "commands"). This is an implementation of the Gang of
+ * Four Chain Of Command pattern.
+ * <p>
+ * For each method in the interface, the chain implementation will call the corresponding method on
+ * each command object in turn. If any of the command objects return true, then the chain of command
+ * stops and the initial method invocation returns true. Otherwise, the chain of command continues
+ * to the next command (and will return false if none of the commands returns true).
+ * <p>
+ * For methods whose return type is not boolean, the chain stops with the first non-null (for object
+ * types), or non-zero (for numeric types). The chain returns the value that was returned by the
+ * command. If the method return type is void, all commands will be invoked.
+ * <p>
+ * Method invocations will also be terminated if an exception is thrown.
+ *
+ * @author Howard M. Lewis Ship
+ */
+public interface ChainBuilder
+{
+ <T> T buildImplementation(Class<T> commandInterface, List<T> commands);
+}
Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ClassFabUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ClassFabUtils.java?rev=426823&r1=426822&r2=426823&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ClassFabUtils.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/ClassFabUtils.java Sat Jul 29 09:03:29 2006
@@ -15,9 +15,12 @@
package org.apache.tapestry.ioc.services;
import java.lang.reflect.Method;
+import java.util.Map;
import org.apache.tapestry.internal.annotations.Utility;
+import static org.apache.tapestry.util.CollectionFactory.newMap;
+
/**
* Handy method useful when creating new classes using
* {@link org.apache.tapestry.ioc.services.ClassFab}.
@@ -78,5 +81,56 @@
return false;
return method.getReturnType().equals(String.class);
+ }
+
+ /**
+ * Mapping between a primitive type and its Java VM representation Used for the encoding of
+ * array types
+ */
+ private static Map<String, String> PRIMITIVE_TYPE_CODES = newMap();
+
+ static
+ {
+ PRIMITIVE_TYPE_CODES.put("boolean", "Z");
+ PRIMITIVE_TYPE_CODES.put("short", "S");
+ PRIMITIVE_TYPE_CODES.put("int", "I");
+ PRIMITIVE_TYPE_CODES.put("long", "J");
+ PRIMITIVE_TYPE_CODES.put("float", "F");
+ PRIMITIVE_TYPE_CODES.put("double", "D");
+ PRIMITIVE_TYPE_CODES.put("char", "C");
+ PRIMITIVE_TYPE_CODES.put("byte", "B");
+ }
+
+ /**
+ * Translates types from standard Java format to Java VM format. For example, java.util.Locale
+ * remains java.util.Locale, but int[][] is translated to [[I and java.lang.Object[] to
+ * [Ljava.lang.Object;
+ */
+ public static String getJVMClassName(String type)
+ {
+ // if it is not an array, just return the type itself
+ if (!type.endsWith("[]"))
+ return type;
+
+ // if it is an array, convert it to JavaVM-style format
+ StringBuffer buffer = new StringBuffer();
+
+ while (type.endsWith("[]"))
+ {
+ buffer.append("[");
+ type = type.substring(0, type.length() - 2);
+ }
+
+ String primitiveIdentifier = (String) PRIMITIVE_TYPE_CODES.get(type);
+ if (primitiveIdentifier != null)
+ buffer.append(primitiveIdentifier);
+ else
+ {
+ buffer.append("L");
+ buffer.append(type);
+ buffer.append(";");
+ }
+
+ return buffer.toString();
}
}
Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/services/ClassFabUtilsTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/services/ClassFabUtilsTest.java?rev=426823&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/services/ClassFabUtilsTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/services/ClassFabUtilsTest.java Sat Jul 29 09:03:29 2006
@@ -0,0 +1,52 @@
+// Copyright 2006 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.tapestry.ioc.services;
+
+import org.apache.tapestry.test.BaseTestCase;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class ClassFabUtilsTest extends BaseTestCase
+{
+
+ @Test(dataProvider = "provider")
+ public void get_jvm_classname(String input, String expected)
+ {
+ String actual = ClassFabUtils.getJVMClassName(input);
+
+ assertEquals(actual, expected);
+ }
+
+ @DataProvider(name = "provider")
+ public Object[][] createInputs()
+ {
+ return new Object[][]
+ { new Object[]
+ { "java.lang.Object", "java.lang.Object" }, new Object[]
+ { "int", "int" },
+
+ new Object[]
+ { "int[]", "[I" }, new Object[]
+ { "java.lang.Throwable[]", "[Ljava.lang.Throwable;" }, new Object[]
+ { "byte[][]", "[[B" }, new Object[]
+ { "java.lang.Runnable[][]", "[[Ljava.lang.Runnable;" } };
+ }
+
+}