diff options
| author | Robert Greig <rgreig@apache.org> | 2007-01-09 23:22:52 +0000 |
|---|---|---|
| committer | Robert Greig <rgreig@apache.org> | 2007-01-09 23:22:52 +0000 |
| commit | 70f32b028e2395d5a9097a1f897e7ded54b7fb88 (patch) | |
| tree | 2d415e281da8c842bb0e2bec78dd3c1047a7db9a /gentools/templ.java | |
| parent | f214abfececda76c2bf10b7fde18d2eab72ad749 (diff) | |
| download | qpid-python-70f32b028e2395d5a9097a1f897e7ded54b7fb88.tar.gz | |
QPID-268 : (Patch supplied by Rob Godfrey) Improvements to performance of generated code
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@494650 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'gentools/templ.java')
| -rw-r--r-- | gentools/templ.java/MethodBodyClass.tmpl | 97 | ||||
| -rw-r--r-- | gentools/templ.java/MethodRegistryClass.tmpl | 43 |
2 files changed, 101 insertions, 39 deletions
diff --git a/gentools/templ.java/MethodBodyClass.tmpl b/gentools/templ.java/MethodBodyClass.tmpl index 04ce639f23..249738e109 100644 --- a/gentools/templ.java/MethodBodyClass.tmpl +++ b/gentools/templ.java/MethodBodyClass.tmpl @@ -28,36 +28,97 @@ package org.apache.qpid.framing; -import java.util.TreeMap; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.Arrays; import org.apache.mina.common.ByteBuffer; public class ${CLASS}${METHOD}Body extends AMQMethodBody implements EncodableAMQDataBlock { - public static final TreeMap<String, Integer> classIdMap = new TreeMap<String, Integer>(); - public static final TreeMap<String, Integer> methodIdMap = new TreeMap<String, Integer>(); + private static final AMQMethodBodyInstanceFactory factory = new AMQMethodBodyInstanceFactory() + { + public AMQMethodBody newInstance(byte major, byte minor, ByteBuffer in, long size) throws AMQFrameDecodingException + { + return new ${CLASS}${METHOD}Body(major, minor, in); + } + }; + + public static AMQMethodBodyInstanceFactory getFactory() + { + return factory; + } + + public static HashMap<Integer, Integer> classIdMap = new HashMap<Integer, Integer>(); + public static HashMap<Integer, Integer> methodIdMap = new HashMap<Integer, Integer>(); + + private static void registerMethodId(byte major, byte minor, int methodId) + { + methodIdMap.put((0xff & (int) major) | ((0xff & (int) minor)<<8), methodId); + } + + private static void registerClassId(byte major, byte minor, int classId) + { + classIdMap.put((0xff & (int) major) | ((0xff & (int) minor)<<8), classId); + } + static { -${CLASS_ID_INIT} -${METHOD_ID_INIT} + + ${CLASS_ID_INIT} + ${METHOD_ID_INIT} + } // Fields declared in specification %{FLIST} ${field_declaration} + private final int _clazz; + private final int _method; + + // Constructor - public ${CLASS}${METHOD}Body(byte major, byte minor) + + public ${CLASS}${METHOD}Body(byte major, byte minor, ByteBuffer buffer) throws AMQFrameDecodingException + { + super(major, minor); + _clazz = getClazz(major,minor); + _method = getMethod(major,minor); + %{FLIST} ${mb_field_decode} + } + public ${CLASS}${METHOD}Body(byte major, byte minor + %{FLIST} ${mb_field_parameter_list} + ) { super(major, minor); + _clazz = getClazz(major,minor); + _method = getMethod(major,minor); + %{FLIST} ${mb_field_body_initialize} + } + + public int getClazz() + { + return _clazz; + } + + public int getMethod() + { + return _method; } - public int getClazz() { return classIdMap.get(major + "-" + minor); } - public int getMethod() { return methodIdMap.get(major + "-" + minor); } - public static int getClazz(byte major, byte minor) { return classIdMap.get(major + "-" + minor); } - public static int getMethod(byte major, byte minor) { return methodIdMap.get(major + "-" + minor); } + public static int getClazz(byte major, byte minor) + { + return classIdMap.get((0xff & (int) major) | ((0xff & (int) minor)<<8)); + } - // Field methods + public static int getMethod(byte major, byte minor) + { + return methodIdMap.get((0xff & (int) major) | ((0xff & (int) minor)<<8)); + } + + + // Field methods %{FLIST} ${mb_field_get_method} protected int getBodySize() @@ -84,16 +145,16 @@ ${METHOD_ID_INIT} return buf.toString(); } - public static AMQFrame createAMQFrame(int _channelId, byte major, byte minor + public static AMQFrame createAMQFrame(int channelId, byte major, byte minor %{FLIST} ${mb_field_parameter_list} ) { - ${CLASS}${METHOD}Body body = new ${CLASS}${METHOD}Body(major, minor); -%{FLIST} ${mb_field_body_initialize} - - AMQFrame frame = new AMQFrame(); - frame.channel = _channelId; - frame.bodyFrame = body; + ${CLASS}${METHOD}Body body = new ${CLASS}${METHOD}Body(major, minor +%{FLIST} ${mb_field_passed_parameter_list} + ); + + + AMQFrame frame = new AMQFrame(channelId, body); return frame; } } diff --git a/gentools/templ.java/MethodRegistryClass.tmpl b/gentools/templ.java/MethodRegistryClass.tmpl index 0f15918f90..a243ace00e 100644 --- a/gentools/templ.java/MethodRegistryClass.tmpl +++ b/gentools/templ.java/MethodRegistryClass.tmpl @@ -31,41 +31,41 @@ package org.apache.qpid.framing; import java.util.HashMap; import java.lang.reflect.Constructor; import org.apache.log4j.Logger; +import org.apache.mina.common.ByteBuffer; class MainRegistry { + private static final HashMap<Long, AMQMethodBodyInstanceFactory> classIDMethodIDVersionBodyMap = new HashMap<Long, AMQMethodBodyInstanceFactory>(); + + private static final Logger _log = Logger.getLogger(MainRegistry.class); - private static HashMap<Long, Class> classIDMethodIDVersionBodyMap = new HashMap<Long, Class>(); static { %{CLIST} ${reg_map_put_method} } - public static AMQMethodBody get(short classID, short methodID, byte major, byte minor) + public static AMQMethodBody get(short classID, short methodID, byte major, byte minor, ByteBuffer in, long size) throws AMQFrameDecodingException { - Class bodyClass = classIDMethodIDVersionBodyMap.get( - createMapKey(classID, methodID, major, minor)); - if (bodyClass == null) - { - throw new AMQFrameDecodingException(_log, - "Unable to find a suitable decoder for class " + classID + " and method " + - methodID + " in AMQP version " + major + "-" + minor + "."); - } - try - { - Constructor initFn = bodyClass.getConstructor(byte.class, byte.class); - return (AMQMethodBody) initFn.newInstance(major, minor); - } - catch (Exception e) - { - throw new AMQFrameDecodingException(_log, - "Unable to instantiate body class for class " + classID + " and method " + - methodID + " in AMQP version " + major + "-" + minor + " : " + e, e); - } + AMQMethodBodyInstanceFactory bodyFactory = classIDMethodIDVersionBodyMap.get(createMapKey(classID,methodID,major,minor)); + if (bodyFactory == null) + { + throw new AMQFrameDecodingException(_log, + "Unable to find a suitable decoder for class " + classID + " and method " + + methodID + " in AMQP version " + major + "-" + minor + "."); + } + return bodyFactory.newInstance(major, minor, in, size); + + } + private static void registerMethod(short classID, short methodID, byte major, byte minor, AMQMethodBodyInstanceFactory instanceFactory ) + { + classIDMethodIDVersionBodyMap.put(createMapKey(classID,methodID,major,minor), instanceFactory); + } + + private static Long createMapKey(short classID, short methodID, byte major, byte minor) { /** @@ -77,4 +77,5 @@ class MainRegistry */ return new Long(((long)classID << 32) + ((long)methodID << 16) + ((long)major << 8) + minor); } + } |
