Permalink
Browse files

- module oriented design

- echo and file example
  • Loading branch information...
1 parent a0128c9 commit a150330c97a7c130027861baf3a1ec7b0aea3e4f @sushantk committed Oct 31, 2012
Showing with 780 additions and 440 deletions.
  1. +5 −0 ants/src/ants/Const.java
  2. +0 −49 ants/src/ants/FetcherEcho.java
  3. +41 −0 ants/src/ants/ModuleEcho.java
  4. +96 −0 ants/src/ants/ModuleFile.java
  5. +0 −36 ants/src/ants/ModuleTask.java
  6. +5 −5 ants/src/ants/ObjectFactory.java
  7. +27 −27 ants/src/ants/ParamsDefault.java
  8. +0 −6 ants/src/ants/Result.java
  9. +2 −2 ants/src/ants/StringDefault.java
  10. +2 −2 ants/src/ants/StringLiteral.java
  11. +59 −37 ants/src/ants/TaskExecutor.java
  12. +25 −0 ants/src/ants/api/CallbackCopy.java
  13. +5 −1 ants/src/ants/api/Configurable.java
  14. +45 −0 ants/src/ants/api/ContextModule.java
  15. +2 −2 ants/src/ants/api/{RequestContext.java → ContextRequest.java}
  16. +17 −1 ants/src/ants/api/Data.java
  17. +0 −41 ants/src/ants/api/ExecuteContext.java
  18. +0 −34 ants/src/ants/api/FetcherTask.java
  19. +0 −7 ants/src/ants/api/IFetcher.java
  20. +1 −1 ants/src/ants/api/IList.java
  21. +1 −1 ants/src/ants/api/IModule.java
  22. +1 −3 ants/src/ants/api/IParams.java
  23. +1 −1 ants/src/ants/api/IString.java
  24. +17 −0 ants/src/ants/api/Module.java
  25. +0 −36 ants/src/ants/api/ModuleContext.java
  26. +63 −32 ants/src/ants/api/Task.java
  27. +26 −0 ants/src/ants/api/TaskCompleted.java
  28. +34 −0 ants/src/ants/api/TaskFailed.java
  29. +29 −0 ants/src/ants/api/TaskModule.java
  30. +51 −0 ants/src/ants/api/TaskNIO.java
  31. +2 −14 ants/src/ants/exception/EvaluateException.java
  32. +12 −0 ants/src/ants/exception/ExecuteException.java
  33. +2 −2 ants/src/ants/exception/InvalidStateException.java
  34. +15 −15 ants/test/ants/test/{FetcherEchoTest.java → ModuleEchoTest.java}
  35. +66 −0 ants/test/ants/test/ModuleFileTest.java
  36. +10 −10 ants/test/ants/test/{FetcherTaskTest.java → ModuleTaskTest.java}
  37. +8 −9 ants/test/ants/test/ParamsDefaultTest.java
  38. +0 −4 ants/test/ants/test/Util.java
  39. +1 −0 ants/test/ants/test/data/dummy
  40. +0 −12 ants/test/ants/test/stub/TestExecuteContext.java
  41. +0 −35 ants/test/ants/test/stub/TestFetcher.java
  42. +49 −0 ants/test/ants/test/stub/TestModule.java
  43. +10 −0 ants/test/ants/test/stub/TestModuleContext.java
  44. +12 −6 ants/test/ants/test/stub/TestString.java
  45. +38 −9 ants/test/ants/test/stub/TestTaskExecutor.java
View
@@ -25,6 +25,11 @@
public static final String plain = "text/plain";
}
+ public static class charSet {
+ public static final String utf8 = "utf-8";
+ public static final String ascii = "us-ascii";
+ }
+
public static final String comma = ",";
public static final String set = "set";
@@ -1,49 +0,0 @@
-package ants;
-
-import java.util.Collection;
-import java.util.Collections;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import ants.annotation.ConfigurableMethod;
-import ants.api.Configurable;
-import ants.api.Data;
-import ants.api.ExecuteContext;
-import ants.api.IFetcher;
-import ants.api.IString;
-import ants.api.Task;
-import ants.exception.EvaluateException;
-
-public class FetcherEcho extends Configurable
- implements IFetcher {
-
- private static final Logger logger = LoggerFactory
- .getLogger(FetcherEcho.class);
-
- private IString value;
-
- public FetcherEcho(String tagName, String id) {
- super(tagName, id);
- }
-
- @ConfigurableMethod(required=true)
- public void setString(Configurable value) {
- this.value = IString.class.cast(value);
- }
-
- @Override
- public Task fetch(final ExecuteContext context) {
- return new Task(Task.Type.SYNC) {
- protected Collection<Task> runImpl() {
- try {
- String str = FetcherEcho.this.value.getValue(context);
- this.setData(new Data(str, Const.mime.plain), Task.Result.SUCCEDED);
- } catch (EvaluateException e) {
- logger.error(FetcherEcho.this.toContextString(context) + ": Failed to evaluate echo string", e);
- }
- return Collections.emptyList();
- }
- };
- }
-}
@@ -0,0 +1,41 @@
+package ants;
+
+import ants.annotation.ConfigurableMethod;
+import ants.api.Configurable;
+import ants.api.Data;
+import ants.api.TaskCompleted;
+import ants.api.IModule;
+import ants.api.IString;
+import ants.api.ContextModule;
+import ants.api.Task;
+import ants.api.TaskFailed;
+import ants.exception.EvaluateException;
+import ants.exception.ExecuteException;
+
+public class ModuleEcho extends Configurable implements IModule {
+
+ private IString value;
+
+ public ModuleEcho(String tagName, String id) {
+ super(tagName, id);
+ }
+
+ @ConfigurableMethod(required = true)
+ public void setString(Configurable value) {
+ this.value = IString.class.cast(value);
+ }
+
+ @Override
+ public Task execute(ContextModule context, Data data) {
+
+ try {
+ String str = this.value.getValue(context);
+ return new TaskCompleted(new Data(str, Const.mime.plain,
+ Const.charSet.utf8));
+ } catch (EvaluateException e) {
+ return new TaskFailed(new ExecuteException(this.toContextString(context),
+ "Failed to evaluate echo string", e));
+ }
+ }
+
+}
@@ -0,0 +1,96 @@
+package ants;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.AsynchronousFileChannel;
+import java.nio.file.Paths;
+import java.util.Collection;
+
+import ants.annotation.ConfigurableMethod;
+import ants.api.Configurable;
+import ants.api.Data;
+import ants.api.IModule;
+import ants.api.IString;
+import ants.api.ContextModule;
+import ants.api.Module;
+import ants.api.Task;
+import ants.api.TaskNIO;
+import ants.api.TaskFailed;
+import ants.exception.EvaluateException;
+import ants.exception.ExecuteException;
+
+public class ModuleFile extends Module implements IModule {
+
+ private IString path;
+ private IString mime;
+ private IString charSet;
+
+ public ModuleFile(String tagName, String id) {
+ super(tagName, id);
+ }
+
+ @ConfigurableMethod(required = true)
+ public void setPath(Configurable value) {
+ this.path = IString.class.cast(value);
+ }
+
+ @ConfigurableMethod(required = true, defaultClass = "ants.StringLiteral")
+ public void setMimeType(Configurable value) {
+ this.mime = IString.class.cast(value);
+ }
+
+ @ConfigurableMethod(defaultClass = "ants.StringLiteral")
+ public void setCharSet(Configurable value) {
+ this.charSet = IString.class.cast(value);
+ }
+
+ @Override
+ public Task execute(ContextModule context, Data input) {
+
+ String filePath = null;
+ try {
+ filePath = this.path.getValue(context);
+ String mime = this.mime.getValue(context);
+ String charSet = "";
+ if (null != this.charSet) {
+ charSet = this.charSet.getValue(context);
+ }
+
+ AsynchronousFileChannel channel = AsynchronousFileChannel
+ .open(Paths.get(filePath));
+ // TODO: long to int conversion check
+ ByteBuffer dst = ByteBuffer.allocate((int) channel.size());
+ Data data = new Data(dst, mime, charSet);
+ data.setUri(filePath);
+
+ TaskNIO task = new TaskFile(data, this.toContextString(context, filePath));
+ channel.read(dst, 0, null, task);
+
+ return task;
+ } catch (EvaluateException e) {
+ return new TaskFailed(new ExecuteException(this.toContextString(context),
+ "Failed to evaluate connfiguration", e));
+ } catch (IOException e) {
+ return new TaskFailed(new ExecuteException(this.toContextString(context, filePath),
+ "Failed to initiate file read", e));
+ }
+ }
+
+ private static class TaskFile extends TaskNIO {
+
+ private Data runData;
+
+ public TaskFile(Data data, String key) {
+ super(key);
+
+ this.runData = data;
+ }
+
+ @Override
+ protected Collection<Task> runImpl() {
+ this.setAsyncData(runData);
+ return super.runImpl();
+ }
+ }
+
+}
@@ -1,36 +0,0 @@
-package ants;
-
-import java.util.LinkedHashMap;
-
-import ants.api.Context;
-import ants.api.IModule;
-import ants.api.IObjectFactory;
-import ants.api.IParams;
-import ants.api.ModuleContext;
-import ants.api.RequestContext;
-import ants.api.IParams.Type;
-import ants.api.Task;
-import ants.exception.ObjectConfigureException;
-import ants.exception.ParseException;
-
-public class ModuleTask extends Task {
-
- ModuleContext context;
-
- public ModuleTask(String iid, String moduleId,
- LinkedHashMap<String, IParams.Type> params,
- RequestContext requestContext, Context parent) {
- super(Task.Type.CPU);
-
- this.context = new ModuleContext(iid, moduleId, params, requestContext, parent);
- }
-
- @Override
- public Iterable<Task> run() {
- IObjectFactory factory = this.requestContext.getFactory();
-
- IModule module = IModule.class.cast(factory.create(moduleId, Const.thefault.ModuleClass, Const.tag.module));
- return null;
- }
-
-}
@@ -164,7 +164,7 @@ private static void configureWithObjects(Configurable object, ObjectNode node,
Class<?> klass = object.getClass();
ConfigurableClass kannotation = klass.getAnnotation(ConfigurableClass.class);
- boolean expectsValue = (null == kannotation ? false : kannotation.expectsValue());
+ boolean classExpectsValue = (null == kannotation ? false : kannotation.expectsValue());
Iterator<Entry<String, JsonNode>> it = node.getFields();
while (it.hasNext()) {
Entry<String, JsonNode> entry = it.next();
@@ -181,7 +181,7 @@ private static void configureWithObjects(Configurable object, ObjectNode node,
+ childTagName.substring(0, 1).toUpperCase()
+ childTagName.substring(1);
Method method;
- if(expectsValue) {
+ if(classExpectsValue) {
method = klass.getMethod(setMethodName, String.class);
method.invoke(object, childNode.asText());
} else {
@@ -197,10 +197,10 @@ private static void configureWithObjects(Configurable object, ObjectNode node,
defaultListItemClass = mannotation.defaultListItemClass();
listItemTag = mannotation.listItemTag();
}
-
+
Configurable childObject = ObjectFactory.configure(childNode,
- defaultClass, childTagName, "",
- defaultListItemClass, listItemTag, tagStack);
+ defaultClass, childTagName, "",
+ defaultListItemClass, listItemTag, tagStack);
method.invoke(object, childObject);
}
@@ -10,71 +10,71 @@
import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
import ants.api.Configurable;
-import ants.api.ExecuteContext;
+import ants.api.ContextModule;
import ants.api.IParams;
import ants.api.IString;
import ants.exception.EvaluateException;
/**
*
*/
-@ConfigurableClass(expectsList=true)
-public class ParamsDefault extends Configurable
- implements IParams {
+@ConfigurableClass(expectsList = true)
+public class ParamsDefault extends Configurable implements IParams {
static final Logger logger = LoggerFactory.getLogger(ParamsDefault.class);
-
+
private LinkedHashMap<String, Configurable> params;
public ParamsDefault(String tagName, String id) {
super(tagName, id);
}
- @ConfigurableMethod(required=true, defaultListItemClass="ants.StringDefault", listItemTag="param")
+ @ConfigurableMethod(required = true, defaultListItemClass = "ants.StringDefault", listItemTag = "param")
public void setList(LinkedHashMap<String, Configurable> params) {
this.params = params;
}
@Override
- public LinkedHashMap<String, Type> getPairs(ExecuteContext context,
+ public LinkedHashMap<String, Type> getPairs(ContextModule context,
boolean simple) throws EvaluateException {
- LinkedHashMap<String, Type> result = new LinkedHashMap<String, Type>();
-
- Iterator<Entry<String, Configurable>> iter = this.params.entrySet().iterator();
- while(iter.hasNext()) {
+ LinkedHashMap<String, Type> result = new LinkedHashMap<String, Type>();
+
+ Iterator<Entry<String, Configurable>> iter = this.params.entrySet()
+ .iterator();
+ while (iter.hasNext()) {
Entry<String, Configurable> entry = iter.next();
Configurable configurable = entry.getValue();
-
- String name = configurable.getAttribute(Const.attribute.name);
- if(null == name) {
+
+ String name = configurable.getAttribute(Const.attribute.name);
+ if (null == name) {
name = entry.getKey();
}
- if(configurable instanceof IString) {
- IString istring = (IString)configurable;
+ if (configurable instanceof IString) {
+ IString istring = (IString) configurable;
String string = istring.getValue(context);
- if(simple) {
+ if (simple) {
result.put(name, new Simple(string));
} else {
Type value = result.get(name);
- if(null == value) {
+ if (null == value) {
value = new Complex(new LinkedHashMap<String, Type>());
result.put(name, value);
}
- value.getComplex().put(configurable.getId(), new Simple(string));
+ value.getComplex().put(configurable.getId(),
+ new Simple(string));
}
-
- } else if(!simple && (configurable instanceof IParams)) {
- IParams iparams = (IParams)configurable;
+
+ } else if (!simple && (configurable instanceof IParams)) {
+ IParams iparams = (IParams) configurable;
result.put(name, new Complex(iparams.getPairs(context, false)));
- }
- else {
- throw new EvaluateException("Invalid parameter type, string, list or map expected: " + name,
- context, this);
+ } else {
+ throw new EvaluateException(this.toContextString(context, name),
+ "Invalid parameter type, string, list or map expected");
}
}
return result;
- }
+ }
public String toString() {
return this.params.toString();
@@ -1,6 +0,0 @@
-package ants;
-
-public enum Result {
-
- SUCCESS, FAILED
-}
@@ -6,8 +6,8 @@
import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
import ants.api.Configurable;
-import ants.api.ExecuteContext;
import ants.api.IString;
+import ants.api.ContextModule;
@ConfigurableClass(expectsValue=true)
public class StringDefault extends Configurable
@@ -26,7 +26,7 @@ public void setValue(String value) {
}
@Override
- public String getValue(ExecuteContext context) {
+ public String getValue(ContextModule context) {
String evaledText = this.value;
if(context.isLogging()) logger.debug("{} - Evaluated {}=>{}", new Object[]{context, this.value, evaledText});
return evaledText;
Oops, something went wrong.

0 comments on commit a150330

Please sign in to comment.