Optionally generate action script listener adapters for designated services.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5814 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Jamie Doornbos
2009-06-04 05:37:45 +00:00
parent 1e6c35f9c1
commit f3c3269342
2 changed files with 92 additions and 3 deletions
@@ -143,6 +143,15 @@ public class GenServiceTask extends InvocationTask
} }
} }
/** Used to track services for which we should create listener adapters in actionscript. */
public class Adapter
{
public void setService (String className)
{
_aslistenerAdapters.add(className);
}
}
/** /**
* Configures the path to our ActionScript source files. * Configures the path to our ActionScript source files.
*/ */
@@ -151,12 +160,16 @@ public class GenServiceTask extends InvocationTask
_asroot = asroot; _asroot = asroot;
} }
// documentation inherited
public Providerless createProviderless () public Providerless createProviderless ()
{ {
return new Providerless(); return new Providerless();
} }
public Adapter createAdapter ()
{
return new Adapter();
}
// documentation inherited // documentation inherited
@Override @Override
protected void processService (File source, Class<?> service) protected void processService (File source, Class<?> service)
@@ -454,10 +467,19 @@ public class GenServiceTask extends InvocationTask
sw = new StringWriter(); sw = new StringWriter();
_velocity.mergeTemplate(AS_LISTENER_SERVICE_TMPL, "UTF-8", ctx, sw); _velocity.mergeTemplate(AS_LISTENER_SERVICE_TMPL, "UTF-8", ctx, sw);
String amlpath = _asroot + File.separator + sppath + String aslpath = _asroot + File.separator + sppath +
File.separator + sname + "_" + File.separator + sname + "_" +
listener.getName() + "Listener.as"; listener.getName() + "Listener.as";
writeFile(amlpath, sw.toString()); writeFile(aslpath, sw.toString());
if (_aslistenerAdapters.contains(sname)) {
sw = new StringWriter();
_velocity.mergeTemplate(AS_LISTENER_ADAPTER_SERVICE_TMPL, "UTF-8", ctx, sw);
String aslapath = _asroot + File.separator + sppath +
File.separator + sname + "_" +
listener.getName() + "ListenerAdapter.as";
writeFile(aslapath, sw.toString());
}
} }
} catch (Exception e) { } catch (Exception e) {
@@ -679,6 +701,9 @@ public class GenServiceTask extends InvocationTask
/** Services for which we should not generate provider interfaces. */ /** Services for which we should not generate provider interfaces. */
protected HashSet<String> _providerless = Sets.newHashSet(); protected HashSet<String> _providerless = Sets.newHashSet();
/** Services for which we should generate actionscript listener adapters. */
protected HashSet<String> _aslistenerAdapters = Sets.newHashSet();
/** Specifies the path to the marshaller template. */ /** Specifies the path to the marshaller template. */
protected static final String MARSHALLER_TMPL = protected static final String MARSHALLER_TMPL =
"com/threerings/presents/tools/marshaller.tmpl"; "com/threerings/presents/tools/marshaller.tmpl";
@@ -699,6 +724,10 @@ public class GenServiceTask extends InvocationTask
protected static final String AS_LISTENER_SERVICE_TMPL = protected static final String AS_LISTENER_SERVICE_TMPL =
"com/threerings/presents/tools/service_listener_as.tmpl"; "com/threerings/presents/tools/service_listener_as.tmpl";
/** Specifies the path to the ActionScript listener adapter service template. */
protected static final String AS_LISTENER_ADAPTER_SERVICE_TMPL =
"com/threerings/presents/tools/service_listener_adapter_as.tmpl";
/** Specifies the path to the ActionScript marshaller template. */ /** Specifies the path to the ActionScript marshaller template. */
protected static final String AS_MARSHALLER_TMPL = protected static final String AS_MARSHALLER_TMPL =
"com/threerings/presents/tools/marshaller_as.tmpl"; "com/threerings/presents/tools/marshaller_as.tmpl";
@@ -0,0 +1,60 @@
package $package {
#foreach ($import in $imports)
import $import;
#end
/**
* A functional adapter for the ${name}Service_${listener.name}Listener interface.
*/
public class ${name}Service_${listener.name}ListenerAdapter
implements ${name}Service_${listener.name}Listener
{
#set ($cparams = "")
#foreach ($lm in $listener.methods)
#set ($cparams = $cparams + "$lm.method.name :Function, ")
#end
/**
* Creates a new $name service $listener.name listener that will delegate to the given
* function(s). Any Function that is null will simply not be called.
*/
public function ${name}Service_${listener.name}ListenerAdapter (
${cparams}failed :Function)
{
#foreach ($lm in $listener.methods)
_$lm.method.name = $lm.method.name;
#end
_failed = failed;
}
#foreach ($lm in $listener.methods)
// from Java ${name}Service_${listener.name}Listener
public function $lm.method.name ($lm.getASArgList(false)) :void
{
#set ($alist = "")
#foreach ($type in $lm.method.getParameterTypes())
#if ($velocityCount > 1)
#set ($alist = $alist + ", ")
#end
#set ($alist = $alist + "arg$velocityCount")
#end
if (_$lm.method.name != null) {
_${lm.method.name}($alist);
}
}
#end
// from InvocationService_InvocationListener
public function requestFailed (cause :String) :void
{
if (_failed != null) {
_failed(cause);
}
}
#foreach ($lm in $listener.methods)
protected var _$lm.method.name :Function;
#end
protected var _failed :Function;
}
}