summaryrefslogtreecommitdiff
path: root/java/common/generate
diff options
context:
space:
mode:
Diffstat (limited to 'java/common/generate')
-rwxr-xr-xjava/common/generate226
1 files changed, 226 insertions, 0 deletions
diff --git a/java/common/generate b/java/common/generate
new file mode 100755
index 0000000000..698c9b6a0b
--- /dev/null
+++ b/java/common/generate
@@ -0,0 +1,226 @@
+#!/usr/bin/python
+
+# Interim code generation script.
+
+import sys, os
+from cStringIO import StringIO
+
+sys.path.append("../../python")
+
+import mllib
+
+out_dir=sys.argv[1]
+out_pkg = sys.argv[2]
+spec_file = sys.argv[3]
+spec = mllib.xml_parse(spec_file)
+major = spec["amqp"]["@major"]
+minor = spec["amqp"]["@minor"]
+isfx = "_v%s_%s" % (major, minor)
+
+class Output:
+
+ def __init__(self, dir, package, name):
+ self.dir = dir
+ self.package = package
+ self.name = name
+ self.lines = []
+
+ def line(self, l = None):
+ self.lines.append(l)
+
+ def write(self):
+ dir = os.path.join(self.dir, *self.package.split("."))
+ if not os.path.exists(dir):
+ os.makedirs(dir)
+ file = os.path.join(dir, "%s.java" % self.name)
+ out = open(file, "write")
+ print >> out, "package %s;" % self.package
+ print >> out
+ print >> out, "import java.util.Map;"
+ print >> out, "import java.util.UUID;"
+ print >> out
+ for l in self.lines:
+ if l == None:
+ print >> out
+ else:
+ print >> out, l
+ out.close()
+
+TYPES = {
+ "longstr": "String",
+ "shortstr": "String",
+ "longlong": "long",
+ "long": "int",
+ "short": "short",
+ "octet": "byte",
+ "bit": "boolean",
+ "table": "Map<String,?>",
+ "timestamp": "long",
+ "content": "String",
+ "uuid": "UUID",
+ "rfc1982-long-set": "Range<Integer>[]",
+ "rfc1982-long": "int"
+ }
+
+def camel(offset, *args):
+ parts = []
+ for a in args:
+ parts.extend(a.split("-"))
+ return "".join(parts[:offset] + [p.capitalize() for p in parts[offset:]])
+
+def dromedary(s):
+ return s[0].lower() + s[1:]
+
+DOMAINS = {}
+
+for d in spec.query["amqp/domain"]:
+ DOMAINS[d["@name"]] = d["@type"]
+
+def resolve(type):
+ if (DOMAINS.has_key(type)):
+ return resolve(DOMAINS[type])
+ else:
+ return type
+
+class Struct:
+
+ def __init__(self, type, name):
+ self.type = type
+ self.name = name
+ self.fields = []
+
+ def field(self, type, name):
+ self.fields.append((type, name))
+
+ def interface(self, out):
+ out.line("public interface %s extends Method {" % self.name)
+ out.line()
+ out.line(" public static final int TYPE = %d;" % self.type)
+ out.line()
+ for type, name in self.fields:
+ out.line(" %s %s();" % (TYPES[type], camel(1, "get", name)))
+ out.line()
+ out.line("}")
+
+ def impl(self, out):
+ out.line("class %s%s extends AbstractMethod implements %s {" %
+ (self.name, isfx, self.name))
+
+ out.line()
+ out.line(" public int getEncodingType() {")
+ out.line(" return TYPE;")
+ out.line(" }")
+
+ out.line()
+ for type, name in self.fields:
+ out.line(" private final %s %s;" % (TYPES[type], name))
+
+ out.line()
+ out.line(" %s%s(Decoder dec) {" % (self.name, isfx))
+ for type, name in self.fields:
+ out.line(" %s = dec.read%s();" % (name, camel(0, type)))
+ out.line(" }")
+
+ out.line()
+ out.line(" %s%s(%s) {" % (self.name, isfx, self.parameters()))
+ for type, name in self.fields:
+ out.line(" this.%s = %s;" % (name, name))
+ out.line(" }")
+
+ out.line()
+ out.line(" public <C> void delegate(C context, Delegate<C> delegate) {")
+ out.line(" delegate.%s(context, this);" % dromedary(self.name))
+ out.line(" }")
+
+ out.line()
+ for type, name in self.fields:
+ out.line(" public %s %s() {" % (TYPES[type], camel(1, "get", name)))
+ out.line(" return %s;" % name)
+ out.line(" }")
+
+ out.line()
+ out.line(" public void write(Encoder enc) {")
+ for type, name in self.fields:
+ out.line(" enc.write%s(%s);" % (camel(0, type), name))
+ out.line(" }")
+
+ out.line("}")
+
+
+ def parameters(self):
+ return ", ".join(["%s %s" % (TYPES[type], name) for type, name in self.fields])
+
+ def arguments(self):
+ return ", ".join([name for (type, name) in self.fields])
+
+structs = []
+for m in spec.query["amqp/class/method"]:
+ struct = Struct(int(m.parent["@index"])*256 + int(m["@index"]),
+ camel(0, m.parent["@name"], m["@name"]))
+ for f in m.query["field"]:
+ struct.field(resolve(f["@domain"]), camel(1, f["@name"]))
+ structs.append(struct)
+ out = Output(out_dir, out_pkg, struct.name)
+ struct.interface(out)
+ out.write()
+ iout = Output(out_dir, out_pkg, struct.name + isfx)
+ struct.impl(iout)
+ iout.write()
+
+fct = Output(out_dir, out_pkg, "StructFactory")
+fct.line("public interface StructFactory {")
+fct.line(" Struct create(int type, Decoder dec);")
+for s in structs:
+ fct.line()
+ fct.line(" %s new%s(Decoder dec);" % (s.name, s.name))
+ fct.line(" %s new%s(%s);" % (s.name, s.name, s.parameters()))
+fct.line("}")
+fct.write()
+
+ifct_name = "StructFactory%s" % isfx
+ifct = Output(out_dir, out_pkg, ifct_name)
+ifct.line("class %s implements StructFactory {" % ifct_name)
+ifct.line(" public Struct create(int type, Decoder dec) {")
+ifct.line(" switch(type) {")
+for s in structs:
+ ifct.line(" case %s.TYPE:" % s.name)
+ ifct.line(" return new %s%s(dec);" % (s.name, isfx))
+ifct.line(" default:")
+ifct.line(' throw new IllegalArgumentException("type: " + type);')
+ifct.line(" }")
+ifct.line(" }")
+
+for s in structs:
+ ifct.line(" public %s new%s(Decoder dec) {" % (s.name, s.name))
+ ifct.line(" return new %s%s(dec);" % (s.name, isfx))
+ ifct.line(" }")
+
+ ifct.line(" public %s new%s(%s) {" % (s.name, s.name, s.parameters()))
+ ifct.line(" return new %s%s(%s);" % (s.name, isfx, s.arguments()))
+ ifct.line(" }")
+
+ifct.line("}");
+ifct.write()
+
+dlg = Output(out_dir, out_pkg, "Delegate")
+dlg.line("public abstract class Delegate<C> {")
+for s in structs:
+ dlg.line(" public void %s(C context, %s struct) {}" %
+ (dromedary(s.name), s.name))
+dlg.line("}")
+dlg.write()
+
+inv = Output(out_dir, out_pkg, "Invoker")
+inv.line("public abstract class Invoker {")
+inv.line()
+inv.line(" protected abstract void invoke(Method method);")
+inv.line(" protected abstract void invoke(Method method, Handler<Struct> handler);")
+inv.line(" protected abstract StructFactory getFactory();")
+inv.line()
+for s in structs:
+ dname = dromedary(s.name)
+ inv.line(" public void %s(%s) {" % (dname, s.parameters()))
+ inv.line(" invoke(getFactory().new%s(%s));" % (s.name, s.arguments()))
+ inv.line(" }")
+inv.line("}")
+inv.write()