From 2d9dffc7ec94b0f58853704e66fb6fa3ea9e3a88 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 27 Sep 2005 22:27:04 +0000 Subject: [PATCH] Automatically generate the provider interface when generating the FooMarshaller and FooDispatcher. This means we will no longer be able to make FooProvider a class that directly handles FooService methods, but the savings in confusion for first time users of the framework will more than make up for the minor inconvenience. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3714 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/tools/GenServiceTask.java | 47 +++++++++++++++++++ .../presents/tools/InvocationTask.java | 6 +-- .../threerings/presents/tools/marshaller.tmpl | 4 +- .../threerings/presents/tools/provider.tmpl | 24 ++++++++++ 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 src/java/com/threerings/presents/tools/provider.tmpl diff --git a/src/java/com/threerings/presents/tools/GenServiceTask.java b/src/java/com/threerings/presents/tools/GenServiceTask.java index 0ac5796ba..8ff04b4f4 100644 --- a/src/java/com/threerings/presents/tools/GenServiceTask.java +++ b/src/java/com/threerings/presents/tools/GenServiceTask.java @@ -27,6 +27,7 @@ import com.threerings.presents.data.InvocationMarshaller; import com.threerings.presents.dobj.InvocationResponseEvent; import com.threerings.presents.server.InvocationDispatcher; import com.threerings.presents.server.InvocationException; +import com.threerings.presents.server.InvocationProvider; /** * An Ant task for generating invocation service marshalling and @@ -132,6 +133,8 @@ public class GenServiceTask extends InvocationTask imports.keySet().iterator()); generateDispatcher(source, sname, spackage, methods, imports.keySet().iterator()); + generateProvider(source, sname, spackage, methods, listeners, + imports.keySet().iterator()); } protected void generateMarshaller ( @@ -217,6 +220,46 @@ public class GenServiceTask extends InvocationTask } } + protected void generateProvider ( + File source, String sname, String spackage, List methods, + List listeners, Iterator imports) + { + String name = StringUtil.replace(sname, "Service", ""); + String mname = StringUtil.replace(sname, "Service", "Provider"); + String mpackage = StringUtil.replace(spackage, ".client", ".server"); + + // construct our imports list + SortableArrayList implist = new SortableArrayList(); + CollectionUtil.addAll(implist, imports); + checkedAdd(implist, ClientObject.class.getName()); + checkedAdd(implist, InvocationProvider.class.getName()); + checkedAdd(implist, InvocationException.class.getName()); + implist.sort(); + + VelocityContext ctx = new VelocityContext(); + ctx.put("name", name); + ctx.put("package", mpackage); + ctx.put("methods", methods); + ctx.put("listeners", listeners); + ctx.put("imports", implist); + + try { + StringWriter sw = new StringWriter(); + _velocity.mergeTemplate(PROVIDER_TMPL, "UTF-8", ctx, sw); + + // determine the path to our provider file + String mpath = source.getPath(); + mpath = StringUtil.replace(mpath, "Service", "Provider"); + mpath = StringUtil.replace(mpath, "/client/", "/server/"); + + writeFile(mpath, sw.toString()); + + } catch (Exception e) { + System.err.println("Failed processing template"); + e.printStackTrace(System.err); + } + } + /** Specifies the path to the marshaller template. */ protected static final String MARSHALLER_TMPL = "com/threerings/presents/tools/marshaller.tmpl"; @@ -224,4 +267,8 @@ public class GenServiceTask extends InvocationTask /** Specifies the path to the dispatcher template. */ protected static final String DISPATCHER_TMPL = "com/threerings/presents/tools/dispatcher.tmpl"; + + /** Specifies the path to the provider template. */ + protected static final String PROVIDER_TMPL = + "com/threerings/presents/tools/provider.tmpl"; } diff --git a/src/java/com/threerings/presents/tools/InvocationTask.java b/src/java/com/threerings/presents/tools/InvocationTask.java index 92bd55563..1ced66bb7 100644 --- a/src/java/com/threerings/presents/tools/InvocationTask.java +++ b/src/java/com/threerings/presents/tools/InvocationTask.java @@ -119,16 +119,16 @@ public abstract class InvocationTask extends Task return StringUtil.unStudlyName(method.getName()).toUpperCase(); } - public String getArgList () + public String getArgList (boolean providerMode) { StringBuffer buf = new StringBuffer(); Class[] args = method.getParameterTypes(); - for (int ii = 0; ii < args.length; ii++) { + for (int ii = providerMode ? 1 : 0; ii < args.length; ii++) { if (buf.length() > 0) { buf.append(", "); } buf.append(GenUtil.simpleName(args[ii])); - buf.append(" arg").append(ii+1); + buf.append(" arg").append(providerMode ? ii : ii+1); } return buf.toString(); } diff --git a/src/java/com/threerings/presents/tools/marshaller.tmpl b/src/java/com/threerings/presents/tools/marshaller.tmpl index 4063b86d5..54d6f415a 100644 --- a/src/java/com/threerings/presents/tools/marshaller.tmpl +++ b/src/java/com/threerings/presents/tools/marshaller.tmpl @@ -25,7 +25,7 @@ public class ${name}Marshaller extends InvocationMarshaller public static final int $lm.code = $velocityCount; // documentation inherited from interface - public void $lm.method.name ($lm.argList) + public void $lm.method.name ($lm.getArgList(false)) { omgr.postEvent(new InvocationResponseEvent( callerOid, requestId, $lm.code, @@ -56,7 +56,7 @@ public class ${name}Marshaller extends InvocationMarshaller public static final int $m.code = $velocityCount; // documentation inherited from interface - public void $m.method.name ($m.argList) + public void $m.method.name ($m.getArgList(false)) { #foreach ($la in $m.listenerArgs) $la.marshaller listener$la.index = new ${la.marshaller}(); diff --git a/src/java/com/threerings/presents/tools/provider.tmpl b/src/java/com/threerings/presents/tools/provider.tmpl new file mode 100644 index 000000000..8d329001b --- /dev/null +++ b/src/java/com/threerings/presents/tools/provider.tmpl @@ -0,0 +1,24 @@ +package $package; + +#foreach ($import in $imports) +import $import; +#end + +/** + * Defines the server-side of the {@link ${name}Service}. + */ +public interface ${name}Provider extends InvocationProvider +{ +#foreach ($m in $methods) + /** + * Handles a {@link ${name}Service#$m.method.name} request. + */ +#if ($m.listenerArgs.size() > 0) + public void $m.method.name (ClientObject caller, $m.getArgList(true)) + throws InvocationException; +#else + public void $m.method.name (ClientObject caller, $m.getArgList(true)); +#end + +#end +}