Permalink
Browse files

changing ResultHandler interface to allow asynchronous flex testing

  • Loading branch information...
2 parents aeb8100 + 21c6efe commit d7490735a5b8bf3b7098af88f9616b94d538c3a2 dzamroz committed Apr 25, 2012
@@ -1,67 +1,67 @@
-package org.adaptiveplatform.codegenerator;
-
-import java.io.IOException;
-import java.io.StringReader;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-
-import org.codehaus.plexus.component.annotations.Component;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonatype.flexmojos.generator.api.GenerationException;
-import org.sonatype.flexmojos.generator.api.GenerationRequest;
-import org.sonatype.flexmojos.generator.api.Generator;
-
-@Component(role = org.sonatype.flexmojos.generator.api.Generator.class, hint = "adaptiveCodeGenerator")
-public class Java2FlexGeneratorFlexMojoRunner implements Generator {
- private final Logger logger = LoggerFactory.getLogger(Java2FlexGeneratorFlexMojoRunner.class);
-
- private static final String TRANSLATE_PACKAGES_EXTRA_OPTION = "translatePackages";
-
- public void generate(GenerationRequest request) throws GenerationException {
- Java2FlexCodeGeneratorConfiguration configuration = new Java2FlexCodeGeneratorConfiguration();
- configuration.translatePackages(loadPropertiesMap(request.getExtraOptions()
- .get(TRANSLATE_PACKAGES_EXTRA_OPTION)));
- configuration.addConstrainAnnotationsPackage("org.adaptiveplatform");
- configuration.configureFreemarkerTemplatingEngine("/", ".ftl");
- configuration.configureFileExporter(request.getPersistentOutputFolder(), request.getTransientOutputFolder());
- Java2FlexCodeGenerator generator = configuration.build();
- generator.generate(loadClasses(request.getClasses().keySet(), request.getClassLoader()));
- }
-
- private Set<Class<?>> loadClasses(Set<String> classesStrings, ClassLoader classLoader) {
- Set<Class<?>> classes = new HashSet<Class<?>>();
- for (String classString : classesStrings) {
- try {
- classes.add(classLoader.loadClass(classString));
- } catch (ClassNotFoundException e) {
- logger.warn("Class not found: " + classString);
- }
- }
- return classes;
- }
-
- private Map<String, String> loadPropertiesMap(String string) throws GenerationException {
- Properties properties = new Properties();
- try {
- properties.load(new StringReader(string));
- Map<String, String> map = new HashMap<String, String>();
- for (Map.Entry<Object, Object> entry : properties.entrySet()) {
- map.put(toTrimmedString(entry.getKey()), toTrimmedString(entry.getValue()));
- }
- return map;
- } catch (IOException cause) {
- throw new GenerationException("cannot parse extra option: " + TRANSLATE_PACKAGES_EXTRA_OPTION, cause);
- }
- }
-
- private String toTrimmedString(Object value) {
- if (value == null) {
- return null;
- }
- return value.toString().trim();
- }
-}
+package org.adaptiveplatform.codegenerator;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import org.codehaus.plexus.component.annotations.Component;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonatype.flexmojos.generator.GenerationException;
+import org.sonatype.flexmojos.generator.GenerationRequest;
+import org.sonatype.flexmojos.generator.Generator;
+
+@Component(role = org.sonatype.flexmojos.generator.Generator.class, hint = "adaptiveCodeGenerator")
+public class Java2FlexGeneratorFlexMojoRunner implements Generator {
+ private final Logger logger = LoggerFactory.getLogger(Java2FlexGeneratorFlexMojoRunner.class);
+
+ private static final String TRANSLATE_PACKAGES_EXTRA_OPTION = "translatePackages";
+
+ public void generate(GenerationRequest request) throws GenerationException {
+ Java2FlexCodeGeneratorConfiguration configuration = new Java2FlexCodeGeneratorConfiguration();
+ configuration.translatePackages(loadPropertiesMap(request.getExtraOptions()
+ .get(TRANSLATE_PACKAGES_EXTRA_OPTION)));
+ configuration.addConstrainAnnotationsPackage("org.adaptiveplatform");
+ configuration.configureFreemarkerTemplatingEngine("/", ".ftl");
+ configuration.configureFileExporter(request.getPersistentOutputFolder(), request.getTransientOutputFolder());
+ Java2FlexCodeGenerator generator = configuration.build();
+ generator.generate(loadClasses(request.getClasses().keySet(), request.getClassLoader()));
+ }
+
+ private Set<Class<?>> loadClasses(Set<String> classesStrings, ClassLoader classLoader) {
+ Set<Class<?>> classes = new HashSet<Class<?>>();
+ for (String classString : classesStrings) {
+ try {
+ classes.add(classLoader.loadClass(classString));
+ } catch (ClassNotFoundException e) {
+ logger.warn("Class not found: " + classString);
+ }
+ }
+ return classes;
+ }
+
+ private Map<String, String> loadPropertiesMap(String string) throws GenerationException {
+ Properties properties = new Properties();
+ try {
+ properties.load(new StringReader(string));
+ Map<String, String> map = new HashMap<String, String>();
+ for (Map.Entry<Object, Object> entry : properties.entrySet()) {
+ map.put(toTrimmedString(entry.getKey()), toTrimmedString(entry.getValue()));
+ }
+ return map;
+ } catch (IOException cause) {
+ throw new GenerationException("cannot parse extra option: " + TRANSLATE_PACKAGES_EXTRA_OPTION, cause);
+ }
+ }
+
+ private String toTrimmedString(Object value) {
+ if (value == null) {
+ return null;
+ }
+ return value.toString().trim();
+ }
+}
@@ -1,47 +1,52 @@
-package org.adaptiveplatform.communication {
- import mx.rpc.AsyncResponder;
- import mx.rpc.AsyncToken;
- import mx.rpc.events.FaultEvent;
- import mx.rpc.events.ResultEvent;
-
- public class AsyncTokenResultHandler implements ResultHandler {
- private var token:AsyncToken;
-
- public function AsyncTokenResultHandler(token:AsyncToken) {
- this.token=token;
- }
-
- public function onSuccess(success:Function):ResultHandler {
- token.addResponder(new AsyncResponder( //
- function(event:ResultEvent, token:Object=null):void {
- success(event.result);
- }, function(event:FaultEvent, token:Object=null):void {
- // do nothing
- }));
- return this;
- }
-
- public function onFault(fault:Function):ResultHandler {
- token.addResponder(new AsyncResponder( //
- function(event:ResultEvent, token:Object=null):void {
- // do nothing
- }, function(event:FaultEvent, token:Object=null):void {
- fault(event.fault);
- }));
- return this;
- }
-
-
- public function onError(error:String, fault:Function):ResultHandler {
- token.addResponder(new AsyncResponder( //
- function(event:ResultEvent, token:Object=null):void {
- // do nothing
- }, function(event:FaultEvent, token:Object=null):void {
- if (event.fault.faultCode == error) {
- fault(event.fault);
- }
- }));
- return this;
- }
- }
-}
+package org.adaptiveplatform.communication {
+ import mx.rpc.AsyncResponder;
+ import mx.rpc.AsyncToken;
+ import mx.rpc.events.FaultEvent;
+ import mx.rpc.events.ResultEvent;
+
+ public class AsyncTokenResultHandler implements ResultHandler {
+ private var token:AsyncToken;
+
+ public function AsyncTokenResultHandler(token:AsyncToken) {
+ this.token=token;
+ }
+
+ public function onSuccess(success:Function):ResultHandler {
+ token.addResponder(new AsyncResponder( //
+ function(event:ResultEvent, token:Object=null):void {
+ success(event.result);
+ }, function(event:FaultEvent, token:Object=null):void {
+ // do nothing
+ }));
+ return this;
+ }
+
+ public function onFault(fault:Function):ResultHandler {
+ token.addResponder(new AsyncResponder( //
+ function(event:ResultEvent, token:Object=null):void {
+ // do nothing
+ }, function(event:FaultEvent, token:Object=null):void {
+ fault(event.fault);
+ }));
+ return this;
+ }
+
+
+ public function onError(error:String, fault:Function):ResultHandler {
+ token.addResponder(new AsyncResponder( //
+ function(event:ResultEvent, token:Object=null):void {
+ // do nothing
+ }, function(event:FaultEvent, token:Object=null):void {
+ if (event.fault.faultCode == error) {
+ fault(event.fault);
+ }
+ }));
+ return this;
+ }
+
+ public function addListener(onSuccess:Function, onFailure:Function):ResultHandler {
+ token.addResponder(new AsyncResponder(onSuccess, onFailure));
+ return this;
+ }
+ }
+}
@@ -1,34 +1,33 @@
-package org.adaptiveplatform.communication {
- import mx.rpc.Fault;
-
- public class FaultResultHandler implements ResultHandler {
- private var faultReason:Fault;
-
- public function FaultResultHandler(code:String, string:String, detail:String=null, ... arguments) {
- this.faultReason=new Fault(code, string, detail);
- }
-
- public function onSuccess(success:Function):ResultHandler {
- // do nothing
- return this;
- }
-
- public function onFault(fault:Function):ResultHandler {
- fault(faultReason);
- return this;
- }
-
- public function onError(error:String, fault:Function):ResultHandler {
- // LATER create new Fault class to handle error messages
- if (error == faultReason.faultCode) {
- fault(faultReason);
- }
- return this;
- }
-
- public function onSuccessGoto(view:String):ResultHandler {
- // do nothing
- return this;
- }
- }
-}
+package org.adaptiveplatform.communication {
+ import mx.rpc.Fault;
+
+ public class FaultResultHandler implements ResultHandler {
+ private var faultReason:Fault;
+
+ public function FaultResultHandler(code:String, string:String, detail:String=null, ... arguments) {
+ this.faultReason=new Fault(code, string, detail);
+ }
+
+ public function onSuccess(success:Function):ResultHandler {
+ // do nothing
+ return this;
+ }
+
+ public function onFault(fault:Function):ResultHandler {
+ fault(faultReason);
+ return this;
+ }
+
+ public function onError(error:String, fault:Function):ResultHandler {
+ // LATER create new Fault class to handle error messages
+ if (error == faultReason.faultCode) {
+ fault(faultReason);
+ }
+ return this;
+ }
+
+ public function addListener(onSuccess:Function, onFailure:Function):ResultHandler{
+ throw new Error("not yet implemented");
+ }
+ }
+}
@@ -1,43 +1,49 @@
-package org.adaptiveplatform.communication {
- import flash.events.Event;
-
- import mx.rpc.events.FaultEvent;
- import mx.rpc.events.ResultEvent;
- import mx.rpc.remoting.RemoteObject;
-
- public class RemoteMethodResultHandler implements ResultHandler {
-
- private var remoteMethod:RemoteObject;
-
- public function RemoteMethodResultHandler(remoteMethod:RemoteObject) {
- this.remoteMethod=remoteMethod;
- }
-
- public function onSuccess(success:Function):ResultHandler {
- remoteMethod.addEventListener(ResultEvent.RESULT, function(event:ResultEvent):void {
- if (success.length == 0) {
- success();
- } else {
- success(event.result);
- }
- });
- return this;
- }
-
- public function onFault(fault:Function):ResultHandler {
- remoteMethod.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void {
- fault(event.fault);
- });
- return this;
- }
-
- public function onError(error:String, fault:Function):ResultHandler {
- remoteMethod.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void {
- if (event.fault.faultCode == error) {
- fault(event.fault);
- }
- });
- return this;
- }
- }
-}
+package org.adaptiveplatform.communication {
+ import flash.events.Event;
+
+ import mx.rpc.events.FaultEvent;
+ import mx.rpc.events.ResultEvent;
+ import mx.rpc.remoting.RemoteObject;
+
+ public class RemoteMethodResultHandler implements ResultHandler {
+
+ private var remoteMethod:RemoteObject;
+
+ public function RemoteMethodResultHandler(remoteMethod:RemoteObject) {
+ this.remoteMethod=remoteMethod;
+ }
+
+ public function onSuccess(success:Function):ResultHandler {
+ remoteMethod.addEventListener(ResultEvent.RESULT, function(event:ResultEvent):void {
+ if (success.length == 0) {
+ success();
+ } else {
+ success(event.result);
+ }
+ });
+ return this;
+ }
+
+ public function onFault(fault:Function):ResultHandler {
+ remoteMethod.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void {
+ fault(event.fault);
+ });
+ return this;
+ }
+
+ public function onError(error:String, fault:Function):ResultHandler {
+ remoteMethod.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void {
+ if (event.fault.faultCode == error) {
+ fault(event.fault);
+ }
+ });
+ return this;
+ }
+
+ public function addListener(onSuccess:Function, onFailure:Function):ResultHandler {
+ remoteMethod.addEventListener(ResultEvent.RESULT, onSuccess);
+ remoteMethod.addEventListener(FaultEvent.FAULT, onFailure);
+ return this;
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit d749073

Please sign in to comment.