Permalink
Browse files

- FetcherEcho class

- More Task related updates
  • Loading branch information...
1 parent d6093b8 commit 1c07611879e89b786d26a2db69c1ec7dc8935857 @sushantk committed Oct 14, 2012
Showing with 588 additions and 236 deletions.
  1. +1 −0 ants/.classpath
  2. +9 −0 ants/src/ants/Const.java
  3. +53 −0 ants/src/ants/FetcherEcho.java
  4. +36 −0 ants/src/ants/ModuleTask.java
  5. +30 −25 ants/src/ants/ObjectFactory.java
  6. +33 −0 ants/src/ants/ObjectStore.java
  7. +0 −23 ants/src/ants/ObjectTree.java
  8. +3 −3 ants/src/ants/ParamsDefault.java
  9. +2 −2 ants/src/ants/StringDefault.java
  10. +37 −30 ants/src/ants/TaskExecutor.java
  11. +5 −0 ants/src/ants/api/Configurable.java
  12. +6 −6 ants/src/ants/api/Data.java
  13. +7 −0 ants/src/ants/api/IFetcher.java
  14. +2 −2 ants/src/ants/api/IList.java
  15. +7 −0 ants/src/ants/api/IModule.java
  16. +11 −0 ants/src/ants/api/IObjectFactory.java
  17. +2 −2 ants/src/ants/api/IParams.java
  18. +2 −2 ants/src/ants/api/IString.java
  19. +49 −0 ants/src/ants/api/ModuleContext.java
  20. +10 −0 ants/src/ants/api/RequestContext.java
  21. +19 −0 ants/src/ants/api/SourceTask.java
  22. +61 −41 ants/src/ants/api/Task.java
  23. +21 −0 ants/src/ants/exception/EvaluateException.java
  24. +8 −0 ants/src/ants/exception/InvalidStateException.java
  25. +0 −21 ants/src/ants/exception/ObjectEvaluateException.java
  26. +0 −12 ants/src/ants/exception/ObjectParseException.java
  27. +12 −0 ants/src/ants/exception/ParseException.java
  28. +49 −0 ants/test/ants/test/FetcherEchoTest.java
  29. +20 −27 ants/test/ants/test/ObjectFactoryTest.java
  30. +29 −34 ants/test/ants/test/ParamsDefaultTest.java
  31. +0 −6 ants/test/ants/test/Util.java
  32. +12 −0 ants/test/ants/test/stub/TestExecuteContext.java
  33. +33 −0 ants/test/ants/test/stub/TestString.java
  34. +19 −0 ants/test/ants/test/stub/TestTaskExecutor.java
View
@@ -2,6 +2,7 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
+ <classpathentry kind="src" path="store"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="external/slf4j-api-1.7.1.jar"/>
<classpathentry kind="lib" path="external/slf4j-simple-1.7.1.jar"/>
View
@@ -3,16 +3,25 @@
public final class Const {
public static class thefault {
+ public static final String ModuleClass = "ants.ModuleDefault";
public static final String StringClass = "ants.StringDefault";
public static final String ParamsClass = "ants.ParamsDefault";
}
+ public static class tag {
+ public static final String module = "module";
+ }
+
public static class attribute {
public static final String nodeId = "@id";
public static final String nodeClass = "@class";
public static final String name = "name";
}
+
+ public static class mime {
+ public static final String plain = "text/plain";
+ }
public static final String comma = ",";
@@ -0,0 +1,53 @@
+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);
+ }
+
+ /**
+ * In general fetchers will be sync_io or async_io. This is
+ * shown as an example.
+ */
+ @Override
+ public Task fetch(final ExecuteContext context) {
+ return new Task(Task.Type.SYNC_IO) {
+ 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 fetch data", e);
+ }
+ return Collections.emptyList();
+ }
+ };
+ }
+}
@@ -0,0 +1,36 @@
+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;
+ }
+
+}
@@ -19,35 +19,39 @@
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.MappingJsonFactory;
-import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
import ants.api.Configurable;
+import ants.api.IObjectFactory;
import ants.exception.ObjectConfigureException;
import ants.exception.ObjectCreateException;
import ants.exception.ObjectIncompleteException;
-import ants.exception.ObjectParseException;
+import ants.exception.ParseException;
-public class ObjectFactory {
+public class ObjectFactory implements IObjectFactory {
- static final Logger logger = LoggerFactory.getLogger(ObjectFactory.class);
- static JsonFactory jsonMapper = new MappingJsonFactory();
- static JsonNodeFactory jsonFactory = JsonNodeFactory.instance;
+ private static final Logger logger = LoggerFactory.getLogger(ObjectFactory.class);
+ private static JsonFactory jsonMapper = new MappingJsonFactory();
- static int autoId = 0;
+ private static int autoId = 0;
+
+ @Override
+ public Configurable create(String id, String defaultClass, String tagName) throws ObjectConfigureException, ParseException {
+ return ObjectStore.getInstance(id, defaultClass, tagName);
+ }
/**
* parse a json string into an object tree.
*
- * @throws ObjectParseException
+ * @throws ParseException
*/
- public ObjectTree parse(String json) throws ObjectParseException {
+ public static JsonNode parse(String json) throws ParseException {
try {
- return this.parse(json,
+ return ObjectFactory.parse(json,
new ByteArrayInputStream(json.getBytes("utf-8")));
} catch (UnsupportedEncodingException ex) {
// ignored, we dont expect this with utf-8
@@ -58,35 +62,35 @@ public ObjectTree parse(String json) throws ObjectParseException {
/**
* parse a json stream into an object tree.
*
- * @throws ObjectParseException
+ * @throws ParseException
*/
- public ObjectTree parse(String identifier, InputStream json)
- throws ObjectParseException {
+ public static JsonNode parse(String identifier, InputStream json)
+ throws ParseException {
JsonNode node = null;
try {
JsonParser parser = jsonMapper.createJsonParser(json);
node = parser.readValueAsTree();
} catch (IOException ex) {
- throw new ObjectParseException("Failed to parse JSON: "
+ throw new ParseException("Failed to parse JSON: "
+ identifier, ex);
}
- return new ObjectTree(node);
+ return node;
}
/**
* create and configure the object with the given object tree
*
* @throws ObjectConfigureException
*/
- public Configurable configure(ObjectTree tree, String defaultClass,
+ public static Configurable configure(JsonNode tree, String defaultClass,
String tagName, String id, String defaultListItemClass,
String listItemTag) throws ObjectConfigureException {
Stack<String> tagStack = new Stack<String>();
Exception ex = null;
try {
- return this.configure(tree.getNode(), defaultClass, tagName,
+ return ObjectFactory.configure(tree, defaultClass, tagName,
id, defaultListItemClass, listItemTag, tagStack);
} catch (SecurityException e) {
ex = e;
@@ -117,7 +121,7 @@ public Configurable configure(ObjectTree tree, String defaultClass,
* Internal method to create and configure the object recursively with the
* given node
*/
- private Configurable configure(JsonNode node,
+ private static Configurable configure(JsonNode node,
String defaultClass, String tagName, String id,
String defaultListItemClass, String listItemTag,
Stack<String> tagStack)
@@ -137,9 +141,9 @@ private Configurable configure(JsonNode node,
Method method = klass.getMethod(Const.setValue, String.class);
method.invoke(object, node.asText());
} else if (node.isArray() || ((null != kannotation) && kannotation.expectsList())) {
- this.configureWithList(object, node, defaultListItemClass, listItemTag, tagStack);
+ ObjectFactory.configureWithList(object, node, defaultListItemClass, listItemTag, tagStack);
} else if (node.isObject()) {
- this.configureWithObjects(object, (ObjectNode) node, tagStack);
+ ObjectFactory.configureWithObjects(object, (ObjectNode) node, tagStack);
}
tagStack.pop();
@@ -150,7 +154,7 @@ private Configurable configure(JsonNode node,
* Internal method to configure the object recursively with the given node's
* children, treating them as child objects
*/
- private void configureWithObjects(Configurable object, ObjectNode node,
+ private static void configureWithObjects(Configurable object, ObjectNode node,
Stack<String> tagStack) throws ObjectCreateException,
ObjectIncompleteException, SecurityException,
IllegalArgumentException, NoSuchMethodException,
@@ -194,7 +198,7 @@ private void configureWithObjects(Configurable object, ObjectNode node,
listItemTag = mannotation.listItemTag();
}
- Configurable childObject = this.configure(childNode,
+ Configurable childObject = ObjectFactory.configure(childNode,
defaultClass, childTagName, "",
defaultListItemClass, listItemTag, tagStack);
method.invoke(object, childObject);
@@ -210,7 +214,7 @@ private void configureWithObjects(Configurable object, ObjectNode node,
* Internal method to configure the object recursively with the given node's
* children treating them as a list of child objects
*/
- private void configureWithList(Configurable object, JsonNode node,
+ private static void configureWithList(Configurable object, JsonNode node,
String defaultListItemClass, String listItemTag,
Stack<String> tagStack)
throws ObjectCreateException, ObjectIncompleteException,
@@ -235,7 +239,7 @@ private void configureWithList(Configurable object, JsonNode node,
if (node.isArray()) {
Iterator<JsonNode> it = node.getElements();
while (it.hasNext()) {
- Configurable childObject = this.configure(it.next(),
+ Configurable childObject = ObjectFactory.configure(it.next(),
defaultListItemClass, listItemTag, "", "", "", tagStack);
childList.put(childObject.getId(), childObject);
}
@@ -252,7 +256,7 @@ private void configureWithList(Configurable object, JsonNode node,
continue;
}
- Configurable childObject = this.configure(childNode,
+ Configurable childObject = ObjectFactory.configure(childNode,
defaultListItemClass, listItemTag, childId, "", "", tagStack);
childList.put(childObject.getId(), childObject);
}
@@ -329,4 +333,5 @@ private static Configurable create(JsonNode node, String defaultClass,
Object t = tc.newInstance(tagName, id);
return (Configurable) t;
}
+
}
@@ -0,0 +1,33 @@
+package ants;
+
+import java.io.InputStream;
+
+import org.codehaus.jackson.JsonNode;
+
+import ants.api.Configurable;
+import ants.exception.ObjectConfigureException;
+import ants.exception.ParseException;
+
+public class ObjectStore {
+
+ // TODO: cache object instance
+ public static Configurable getInstance(String id, String defaultClass, String tagName) throws ObjectConfigureException, ParseException {
+ int idIndex = id.indexOf('.');
+ if(-1 == idIndex) {
+ throw new ParseException("Failed to extract object id " + " from " + id);
+ }
+
+ String jsonFile = id.substring(0, idIndex) + ".json";
+ String oid = id.substring(idIndex + 1);
+
+ InputStream is = ObjectStore.class.getResourceAsStream(jsonFile);
+ JsonNode tree = ObjectFactory.parse(jsonFile, is);
+ JsonNode node = tree.get(oid);
+ if(null == node) {
+ throw new ParseException("Failed to locate " + oid + " in " + jsonFile);
+ }
+
+ return ObjectFactory.configure(node, defaultClass, tagName, oid, "", "");
+ }
+
+}
@@ -1,23 +0,0 @@
-package ants;
-
-import org.codehaus.jackson.JsonNode;
-
-/**
- * Wrapper over jackson node class
- */
-public final class ObjectTree {
-
- private JsonNode node;
-
- public ObjectTree(JsonNode node) {
- this.node = node;
- }
-
- public JsonNode getNode() {
- return this.node;
- }
-
- public String toString() {
- return this.node.toString();
- }
-}
@@ -13,7 +13,7 @@
import ants.api.ExecuteContext;
import ants.api.IParams;
import ants.api.IString;
-import ants.exception.ObjectEvaluateException;
+import ants.exception.EvaluateException;
/**
*
@@ -36,7 +36,7 @@ public void setList(LinkedHashMap<String, Configurable> params) {
@Override
public LinkedHashMap<String, Type> getPairs(ExecuteContext context,
- boolean simple) throws ObjectEvaluateException {
+ boolean simple) throws EvaluateException {
LinkedHashMap<String, Type> result = new LinkedHashMap<String, Type>();
Iterator<Entry<String, Configurable>> iter = this.params.entrySet().iterator();
@@ -68,7 +68,7 @@ public void setList(LinkedHashMap<String, Configurable> params) {
result.put(name, new Complex(iparams.getPairs(context, false)));
}
else {
- throw new ObjectEvaluateException("Invalid parameter type, string, list or map expected: " + name,
+ throw new EvaluateException("Invalid parameter type, string, list or map expected: " + name,
context, this);
}
}
@@ -14,11 +14,11 @@
implements IString {
private static final Logger logger = LoggerFactory.getLogger(StringDefault.class);
+ private String value;
+
public StringDefault(String tagName, String id) {
super(tagName, id);
}
-
- private String value;
@ConfigurableMethod(required=true)
public void setValue(String value) {
Oops, something went wrong.

0 comments on commit 1c07611

Please sign in to comment.