summaryrefslogtreecommitdiff
path: root/qpid/gentools
diff options
context:
space:
mode:
Diffstat (limited to 'qpid/gentools')
-rw-r--r--qpid/gentools/src/org/apache/qpid/gentools/JavaGenerator.java122
-rw-r--r--qpid/gentools/templ.java/MethodBodyClass.tmpl97
-rw-r--r--qpid/gentools/templ.java/MethodRegistryClass.tmpl43
3 files changed, 193 insertions, 69 deletions
diff --git a/qpid/gentools/src/org/apache/qpid/gentools/JavaGenerator.java b/qpid/gentools/src/org/apache/qpid/gentools/JavaGenerator.java
index a18338f3fe..13560f5c17 100644
--- a/qpid/gentools/src/org/apache/qpid/gentools/JavaGenerator.java
+++ b/qpid/gentools/src/org/apache/qpid/gentools/JavaGenerator.java
@@ -61,7 +61,9 @@ public class JavaGenerator extends Generator
static private Method mbGetGenerateMethod;
static private Method mbMangledGetGenerateMethod;
static private Method mbParamListGenerateMethod;
+ static private Method mbPassedParamListGenerateMethod;
static private Method mbMangledParamListGenerateMethod;
+ static private Method mbMangledPassedParamListGenerateMethod;
static private Method mbBodyInitGenerateMethod;
static private Method mbMangledBodyInitGenerateMethod;
static private Method mbSizeGenerateMethod;
@@ -131,11 +133,22 @@ public class JavaGenerator extends Generator
AmqpVersionSet.class, int.class, int.class, boolean.class); }
catch (NoSuchMethodException e) { e.printStackTrace(); }
+
+ try { mbPassedParamListGenerateMethod = JavaGenerator.class.getDeclaredMethod(
+ "generateMbPassedParamList", String.class, AmqpField.class,
+ AmqpVersionSet.class, int.class, int.class, boolean.class); }
+ catch (NoSuchMethodException e) { e.printStackTrace(); }
+
try { mbMangledParamListGenerateMethod = JavaGenerator.class.getDeclaredMethod(
"generateMbMangledParamList", AmqpField.class,
int.class, int.class, boolean.class); }
catch (NoSuchMethodException e) { e.printStackTrace(); }
+ try { mbMangledPassedParamListGenerateMethod = JavaGenerator.class.getDeclaredMethod(
+ "generateMbMangledPassedParamList", AmqpField.class,
+ int.class, int.class, boolean.class); }
+ catch (NoSuchMethodException e) { e.printStackTrace(); }
+
try { mbBodyInitGenerateMethod = JavaGenerator.class.getDeclaredMethod(
"generateMbBodyInit", String.class, AmqpField.class,
AmqpVersionSet.class, int.class, int.class, boolean.class); }
@@ -457,11 +470,11 @@ public class JavaGenerator extends Generator
if (token.compareTo("${CLASS}") == 0 && thisClass != null)
return thisClass.name;
if (token.compareTo("${CLASS_ID_INIT}") == 0 && thisClass != null)
- return generateIndexInitializer("classIdMap", thisClass.indexMap, 8);
+ return generateIndexInitializer("registerClassId", thisClass.indexMap, 8);
if (token.compareTo("${METHOD}") == 0 && method != null)
return method.name;
if (token.compareTo("${METHOD_ID_INIT}") == 0 && method != null)
- return generateIndexInitializer("methodIdMap", method.indexMap, 8);
+ return generateIndexInitializer("registerMethodId", method.indexMap, 8);
if (token.compareTo("${FIELD}") == 0 && field != null)
return field.name;
@@ -573,6 +586,16 @@ public class JavaGenerator extends Generator
codeSnippet += fieldMap.parseFieldMap(mbParamListGenerateMethod,
mbMangledParamListGenerateMethod, 42, 4, this);
}
+
+ else if (token.compareTo("${mb_field_passed_parameter_list}") == 0)
+ {
+ // <cringe> The code generated by this is ugly... It puts a comma on a line by itself!
+ // TODO: Find a more elegant solution here sometime...
+ codeSnippet = fieldMap.size() > 0 ? Utils.createSpaces(42) + "," + cr : "";
+ // </cringe>
+ codeSnippet += fieldMap.parseFieldMap(mbPassedParamListGenerateMethod,
+ mbMangledPassedParamListGenerateMethod, 42, 4, this);
+ }
else if (token.compareTo("${mb_field_body_initialize}") == 0)
{
codeSnippet = fieldMap.parseFieldMap(mbBodyInitGenerateMethod,
@@ -721,7 +744,7 @@ public class JavaGenerator extends Generator
while (vItr.hasNext())
{
AmqpVersion version = vItr.next();
- sb.append(indent + mapName + ".put(\"" + version.toString() + "\", " + index + ");" + cr);
+ sb.append(indent + mapName + "( (byte) " + version.getMajor() +", (byte) " + version.getMinor() + ", " + index + ");" + cr);
}
}
return sb.toString();
@@ -746,12 +769,12 @@ public class JavaGenerator extends Generator
{
int classIndex = findIndex(thisClass.indexMap, version);
int methodIndex = findIndex(method.indexMap, version);
- sb.append(indent + "classIDMethodIDVersionBodyMap.put(" + cr);
- sb.append(indent + tab + "createMapKey((short)" + classIndex +
- ", (short)" + methodIndex + ", (byte)" + version.getMajor() +
- ", (byte)" + version.getMinor() + "), " + cr);
+ sb.append(indent + "registerMethod(" + cr);
+ sb.append(indent + tab + "(short)" + classIndex +
+ ", (short)" + methodIndex + ", (byte)" + version.getMajor() +
+ ", (byte)" + version.getMinor() + ", " + cr);
sb.append(indent + tab + Utils.firstUpper(thisClass.name) +
- Utils.firstUpper(method.name) + "Body.class);" + cr);
+ Utils.firstUpper(method.name) + "Body.getFactory());" + cr);
}
catch (Exception e) {} // Ignore
}
@@ -924,6 +947,15 @@ public class JavaGenerator extends Generator
(nextFlag ? "," : "") + " // AMQP version(s): " + versionSet + cr;
}
+
+ protected String generateMbPassedParamList(String codeType, AmqpField field,
+ AmqpVersionSet versionSet, int indentSize, int tabSize, boolean nextFlag)
+ {
+ return Utils.createSpaces(indentSize) + field.name +
+ (nextFlag ? "," : "") + " // AMQP version(s): " + versionSet + cr;
+ }
+
+
protected String generateMbMangledParamList(AmqpField field, int indentSize,
int tabSize, boolean nextFlag)
throws AmqpTypeMappingException
@@ -943,10 +975,29 @@ public class JavaGenerator extends Generator
return sb.toString();
}
+ protected String generateMbMangledPassedParamList(AmqpField field, int indentSize,
+ int tabSize, boolean nextFlag)
+ throws AmqpTypeMappingException
+ {
+ StringBuffer sb = new StringBuffer();
+ Iterator<String> dItr = field.domainMap.keySet().iterator();
+ int domainCntr = 0;
+ while (dItr.hasNext())
+ {
+ String domainName = dItr.next();
+ AmqpVersionSet versionSet = field.domainMap.get(domainName);
+ sb.append(Utils.createSpaces(indentSize) + field.name + "_" +
+ (domainCntr++) + (nextFlag ? "," : "") + " // AMQP version(s): " +
+ versionSet + cr);
+ }
+ return sb.toString();
+ }
+
+
protected String generateMbBodyInit(String codeType, AmqpField field,
AmqpVersionSet versionSet, int indentSize, int tabSize, boolean nextFlag)
{
- return Utils.createSpaces(indentSize) + "body." + field.name + " = " + field.name +
+ return Utils.createSpaces(indentSize) + "this." + field.name + " = " + field.name +
";" + cr;
}
@@ -960,7 +1011,7 @@ public class JavaGenerator extends Generator
while (dItr.hasNext())
{
dItr.next();
- sb.append(Utils.createSpaces(indentSize) + "body." + field.name + "_" + domainCntr +
+ sb.append(Utils.createSpaces(indentSize) + "this." + field.name + "_" + domainCntr +
" = " + field.name + "_" + (domainCntr++) + ";" + cr);
}
return sb.toString();
@@ -1004,23 +1055,26 @@ public class JavaGenerator extends Generator
int ordinal, int indentSize, int tabSize)
{
String indent = Utils.createSpaces(indentSize);
- String bitArrayName = "bitArray_" + ordinal;
- StringBuffer sb = new StringBuffer(indent + "boolean[] " + bitArrayName +
- " = new boolean[] { ");
- for (int i=0; i<bitFieldList.size(); i++)
+
+ StringBuilder sb = new StringBuilder();
+ int i = 0;
+ while(i <bitFieldList.size())
{
- if (i != 0)
+
+ StringBuilder line = new StringBuilder();
+
+ for (int j=0; i<bitFieldList.size() && j<8; i++, j++)
{
- if ((i + 3) % 6 == 0)
- sb.append("," + cr + indent + Utils.createSpaces(tabSize));
- else
- sb.append(", ");
+ if (j != 0)
+ {
+ line.append(", ");
+ }
+ line.append(bitFieldList.get(i));
}
- sb.append(bitFieldList.get(i));
+
+ sb.append(Utils.createSpaces(indentSize) +
+ typeMap.get("bit").encodeExpression.replaceAll("#", line.toString()) + ";" + cr);
}
- sb.append(" };" + cr);
- sb.append(Utils.createSpaces(indentSize) +
- typeMap.get("bit").encodeExpression.replaceAll("#", bitArrayName) + ";" + cr);
return sb.toString();
}
@@ -1038,14 +1092,22 @@ public class JavaGenerator extends Generator
int ordinal, int indentSize, int tabSize)
{
String indent = Utils.createSpaces(indentSize);
- String bitArrayName = "bitArray_" + ordinal;
- StringBuffer sb = new StringBuffer();
- sb.append(indent +
- typeMap.get("bit").decodeExpression.replaceAll("#", "boolean[] " + bitArrayName) +
- ";" + cr);
- for (int i=0; i<bitFieldList.size(); i++)
+
+ StringBuilder sb = new StringBuilder(indent);
+ sb.append("byte packedValue;");
+ sb.append(cr);
+
+ // RG HERE!
+
+ int i = 0;
+ while(i < bitFieldList.size())
{
- sb.append(indent + bitFieldList.get(i) + " = " + bitArrayName + "[" + i + "];" + cr);
+ sb.append(indent + "packedValue = EncodingUtils.readByte(buffer);" + cr);
+
+ for(int j = 0; i < bitFieldList.size() && j < 8; i++, j++)
+ {
+ sb.append(indent + bitFieldList.get(i) + " = ( packedValue & (byte) (1 << " + j + ") ) != 0;" + cr);
+ }
}
return sb.toString();
}
diff --git a/qpid/gentools/templ.java/MethodBodyClass.tmpl b/qpid/gentools/templ.java/MethodBodyClass.tmpl
index 04ce639f23..249738e109 100644
--- a/qpid/gentools/templ.java/MethodBodyClass.tmpl
+++ b/qpid/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/qpid/gentools/templ.java/MethodRegistryClass.tmpl b/qpid/gentools/templ.java/MethodRegistryClass.tmpl
index 0f15918f90..a243ace00e 100644
--- a/qpid/gentools/templ.java/MethodRegistryClass.tmpl
+++ b/qpid/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);
}
+
}