Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Various context classes

  • Loading branch information...
commit d6093b87d6288b0cbaecee6c4f76ef28f6fdc973 1 parent 1ce77d6
@sushantk authored
View
2  ants/src/ants/ObjectTree.java
@@ -7,7 +7,7 @@
*/
public final class ObjectTree {
- JsonNode node;
+ private JsonNode node;
public ObjectTree(JsonNode node) {
this.node = node;
View
12 ants/src/ants/ParamsDefault.java
@@ -10,7 +10,7 @@
import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
import ants.api.Configurable;
-import ants.api.Context;
+import ants.api.ExecuteContext;
import ants.api.IParams;
import ants.api.IString;
import ants.exception.ObjectEvaluateException;
@@ -23,7 +23,7 @@
implements IParams {
static final Logger logger = LoggerFactory.getLogger(ParamsDefault.class);
- LinkedHashMap<String, Configurable> params;
+ private LinkedHashMap<String, Configurable> params;
public ParamsDefault(String tagName, String id) {
super(tagName, id);
@@ -35,9 +35,9 @@ public void setList(LinkedHashMap<String, Configurable> params) {
}
@Override
- public LinkedHashMap<String, SimpleOrComplex> getPairs(Context context,
+ public LinkedHashMap<String, Type> getPairs(ExecuteContext context,
boolean simple) throws ObjectEvaluateException {
- LinkedHashMap<String, SimpleOrComplex> result = new LinkedHashMap<String, SimpleOrComplex>();
+ LinkedHashMap<String, Type> result = new LinkedHashMap<String, Type>();
Iterator<Entry<String, Configurable>> iter = this.params.entrySet().iterator();
while(iter.hasNext()) {
@@ -55,9 +55,9 @@ public void setList(LinkedHashMap<String, Configurable> params) {
if(simple) {
result.put(name, new Simple(string));
} else {
- SimpleOrComplex value = result.get(name);
+ Type value = result.get(name);
if(null == value) {
- value = new Complex(new LinkedHashMap<String, SimpleOrComplex>());
+ value = new Complex(new LinkedHashMap<String, Type>());
result.put(name, value);
}
value.getComplex().put(configurable.getId(), new Simple(string));
View
16 ants/src/ants/StringDefault.java
@@ -6,13 +6,13 @@
import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
import ants.api.Configurable;
-import ants.api.Context;
+import ants.api.ExecuteContext;
import ants.api.IString;
@ConfigurableClass(expectsValue=true)
public class StringDefault extends Configurable
implements IString {
- static final Logger logger = LoggerFactory.getLogger(StringDefault.class);
+ private static final Logger logger = LoggerFactory.getLogger(StringDefault.class);
public StringDefault(String tagName, String id) {
super(tagName, id);
@@ -26,16 +26,12 @@ public void setValue(String value) {
}
@Override
- public String getValue(Context context) {
- return StringDefault.evaluate(context, this.value);
- }
-
- public static String evaluate(Context context, String text) {
- String evaledText = text;
- if(context.isLogging()) logger.debug("{} - Evaluated {}=>{}", new Object[]{context, text, evaledText});
+ public String getValue(ExecuteContext context) {
+ String evaledText = this.value;
+ if(context.isLogging()) logger.debug("{} - Evaluated {}=>{}", new Object[]{context, this.value, evaledText});
return evaledText;
}
-
+
public String toString() {
return this.value;
}
View
4 ants/src/ants/StringLiteral.java
@@ -3,7 +3,7 @@
import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
import ants.api.Configurable;
-import ants.api.Context;
+import ants.api.ExecuteContext;
import ants.api.IString;
@ConfigurableClass(expectsValue=true)
@@ -22,7 +22,7 @@ public void setValue(String value) {
}
@Override
- public String getValue(Context context) {
+ public String getValue(ExecuteContext context) {
return this.value;
}
View
20 ants/src/ants/TaskManager.java → ants/src/ants/TaskExecutor.java
@@ -12,12 +12,12 @@
/**
* Provides foundation for task execution
*/
-public class TaskManager implements Task.AsyncMonitor {
+public class TaskExecutor implements Task.AsyncMonitor {
- static final Logger logger = LoggerFactory.getLogger(TaskManager.class);
+ private static final Logger logger = LoggerFactory.getLogger(TaskExecutor.class);
- static ThreadPoolExecutor cpuTaskExecutor;
- static ThreadPoolExecutor syncIOTaskExecutor;
+ private static ThreadPoolExecutor cpuTaskExecutor;
+ private static ThreadPoolExecutor syncIOTaskExecutor;
/*
* One time configuration to tune thread pools as per the need and system
@@ -32,7 +32,7 @@ public static void configure(int cpuTaskPoolSize, int syncIOTaskPoolSize) {
new LinkedBlockingQueue<Runnable>(cpuTaskPoolSize));
cpuTaskExecutor.allowCoreThreadTimeOut(false);
cpuTaskExecutor.prestartAllCoreThreads();
- TaskManager.cpuTaskExecutor = cpuTaskExecutor;
+ TaskExecutor.cpuTaskExecutor = cpuTaskExecutor;
// sync io tasks have a fixed size thread pool with an unbounded queue.
// threads start and grow up to the pool size as per the need and also
@@ -42,21 +42,21 @@ public static void configure(int cpuTaskPoolSize, int syncIOTaskPoolSize) {
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(
syncIOTaskPoolSize));
syncIOTaskExecutor.allowCoreThreadTimeOut(false);
- TaskManager.syncIOTaskExecutor = syncIOTaskExecutor;
+ TaskExecutor.syncIOTaskExecutor = syncIOTaskExecutor;
}
boolean logging;
- public TaskManager(boolean logging) {
+ public TaskExecutor(boolean logging) {
this.logging = logging;
}
public void run(final Task task) {
- final TaskManager self = this;
+ final TaskExecutor self = this;
switch (task.getType()) {
case CPU: {
- TaskManager.cpuTaskExecutor.submit(new Runnable() {
+ TaskExecutor.cpuTaskExecutor.submit(new Runnable() {
@Override
public void run() {
Iterable<Task> next = task.run();
@@ -66,7 +66,7 @@ public void run() {
}
break;
case SYNC_IO: {
- TaskManager.syncIOTaskExecutor.submit(new Runnable() {
+ TaskExecutor.syncIOTaskExecutor.submit(new Runnable() {
@Override
public void run() {
Iterable<Task> next = task.run();
View
42 ants/src/ants/api/Context.java
@@ -1,40 +1,26 @@
package ants.api;
-public class Context {
- private boolean logging;
+import java.util.LinkedHashMap;
- private RequestContext requestContext;
-
- private String iid;
- private String moduleId;
+import ants.api.IParams.Type;
- public Context(RequestContext requestContext, String iid, String moduleId) {
- this.requestContext = requestContext;
+public abstract class Context {
- this.iid = iid;
- this.moduleId = moduleId;
-
- //TODO: Use request context to check filters
- this.logging = true;
- }
+ private Context parent;
+ private LinkedHashMap<String, Type> params;
- public RequestContext getRequestContext() {
- return this.requestContext;
- }
-
- public String getInstanceId() {
- return this.iid;
+ public Context(Context parent, LinkedHashMap<String, Type> params) {
+ this.parent = parent;
+ this.params = params;
}
- public String getModuleId() {
- return this.moduleId;
+ public Context getParent() {
+ return this.parent;
}
-
- public boolean isLogging() {
- return this.logging;
+
+ public Type get(String key) {
+ return (null == this.params ? null : this.params.get(key));
}
- public String toString() {
- return "Context<" + this.moduleId + ", " + this.iid + ">";
- }
+ public abstract boolean isLogging();
}
View
35 ants/src/ants/api/ExecuteContext.java
@@ -0,0 +1,35 @@
+package ants.api;
+
+import java.util.LinkedHashMap;
+
+import ants.api.IParams.Type;
+
+public class ExecuteContext extends Context {
+
+ private ModuleContext moduleContext;
+
+ public ExecuteContext(ModuleContext moduleContext, LinkedHashMap<String, Type> params) {
+ super(moduleContext, params);
+
+ this.moduleContext = moduleContext;
+ }
+
+ public ExecuteContext(ExecuteContext executeContext, LinkedHashMap<String, Type> params) {
+ super(executeContext, params);
+
+ this.moduleContext = executeContext.getModuleContext();
+ }
+
+ public ModuleContext getModuleContext() {
+ return this.moduleContext;
+ }
+
+ @Override
+ public boolean isLogging() {
+ return this.moduleContext.isLogging();
+ }
+
+ public String toString() {
+ return this.moduleContext.toString();
+ }
+}
View
3  ants/src/ants/api/IList.java
@@ -6,5 +6,6 @@
public interface IList {
- LinkedHashMap<String, Configurable> getItems(Context context) throws ObjectEvaluateException;
+ LinkedHashMap<String, Configurable> getItems(ExecuteContext context)
+ throws ObjectEvaluateException;
}
View
43 ants/src/ants/api/IParams.java
@@ -8,13 +8,21 @@
public interface IParams {
- public static abstract class SimpleOrComplex {
- abstract public String getSimple();
+ public static abstract class Type {
+ public String getSimple() {
+ return null;
+ }
- abstract public LinkedHashMap<String, SimpleOrComplex> getComplex();
+ public LinkedHashMap<String, Type> getComplex() {
+ return null;
+ }
+
+ public Object getCustom() {
+ return null;
+ }
}
- public static class Simple extends SimpleOrComplex {
+ public static class Simple extends Type {
String string;
public Simple(String string) {
@@ -26,20 +34,15 @@ public Simple(String string) {
return string;
}
- @Override
- public LinkedHashMap<String, SimpleOrComplex> getComplex() {
- return null;
- }
-
public String toString() {
return this.string;
}
}
- public static class Complex extends SimpleOrComplex {
- LinkedHashMap<String, SimpleOrComplex> complex;
+ public static class Complex extends Type {
+ LinkedHashMap<String, Type> complex;
- public Complex(LinkedHashMap<String, SimpleOrComplex> complex) {
+ public Complex(LinkedHashMap<String, Type> complex) {
this.complex = complex;
}
@@ -53,7 +56,7 @@ public String getSimple() {
}
@Override
- public LinkedHashMap<String, SimpleOrComplex> getComplex() {
+ public LinkedHashMap<String, Type> getComplex() {
return complex;
}
@@ -62,6 +65,18 @@ public String toString() {
}
}
- LinkedHashMap<String, SimpleOrComplex> getPairs(Context context,
+ public static class Custom extends Type {
+ Object object;
+
+ public Custom(Object object) {
+ this.object = object;
+ }
+
+ public String toString() {
+ return (null == this.object ? null : this.object.toString());
+ }
+ }
+
+ LinkedHashMap<String, Type> getPairs(ExecuteContext context,
boolean simple) throws ObjectEvaluateException;
}
View
2  ants/src/ants/api/IString.java
@@ -4,5 +4,5 @@
public interface IString {
- String getValue(Context context) throws ObjectEvaluateException;
+ String getValue(ExecuteContext context) throws ObjectEvaluateException;
}
View
16 ants/src/ants/api/RequestContext.java
@@ -1,7 +1,17 @@
package ants.api;
-public class RequestContext {
+import java.util.LinkedHashMap;
- public RequestContext() {
- }
+import ants.api.IParams.Type;
+
+public class RequestContext extends Context {
+
+ public RequestContext(LinkedHashMap<String, Type> params) {
+ super(null, params);
+ }
+
+ public boolean isLogging() {
+ // TODO: use filters
+ return true;
+ }
}
View
10 ants/src/ants/api/Task.java
@@ -34,20 +34,14 @@
private Type type;
private Status status = Status.NOT_DONE;
private Result result = Result.FAILED;
- private Object userData;
private Data data;
private LinkedList<Callback> callbacks = new LinkedList<Callback>();
- public Task(Type type, Object userData) {
- this.userData = userData;
+ public Task(Type type) {
this.type = type;
}
- public Object getUserData() {
- return this.userData;
- }
-
public Type getType() {
return this.type;
}
@@ -88,7 +82,7 @@ public void addCallback(Callback callback) {
}
public String toString() {
- return "Task<" + userData + ">";
+ return "Task<" + this.type + ">";
}
/**
View
36 ants/test/ants/test/ParamsDefaultTest.java
@@ -12,8 +12,8 @@
import ants.ObjectTree;
import ants.ParamsDefault;
import ants.api.Context;
-import ants.api.IParams.SimpleOrComplex;
-import ants.api.RequestContext;
+import ants.api.IParams.Type;
+import ants.api.ExecuteContext;
import ants.exception.ObjectConfigureException;
import ants.exception.ObjectEvaluateException;
import ants.exception.ObjectParseException;
@@ -27,9 +27,9 @@ public void testSimple() throws ObjectParseException, ObjectConfigureException,
ObjectFactory factory = new ObjectFactory();
ObjectTree tree = factory.parse(jsonFile, is);
- Context context = new Context(new RequestContext(), "", "");
+ ExecuteContext context = Util.createExecuteContext();
ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
- LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, true);
+ LinkedHashMap<String, Type> result = params.getPairs(context, true);
assertEquals("Simple params size", 2, result.size());
assertEquals("Simple params name value", "v1", result.get("n1").getSimple());
assertEquals("Simple params name value", "v2", result.get("n2").getSimple());
@@ -42,9 +42,9 @@ public void testSimpleOrder() throws ObjectParseException, ObjectConfigureExcept
ObjectFactory factory = new ObjectFactory();
ObjectTree tree = factory.parse(jsonFile, is);
- Context context = new Context(new RequestContext(), "", "");
+ ExecuteContext context = Util.createExecuteContext();
ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
- LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, true);
+ LinkedHashMap<String, Type> result = params.getPairs(context, true);
assertEquals("Simple params size", 3, result.size());
Iterator<String> niter = result.keySet().iterator();
@@ -52,7 +52,7 @@ public void testSimpleOrder() throws ObjectParseException, ObjectConfigureExcept
assertEquals("Simple params name 2", "n2", niter.next());
assertEquals("Simple params name 3", "n3", niter.next());
- Iterator<SimpleOrComplex> viter = result.values().iterator();
+ Iterator<Type> viter = result.values().iterator();
assertEquals("Simple params value 1", "v1", viter.next().getSimple());
assertEquals("Simple params value 2", "v2", viter.next().getSimple());
assertEquals("Simple params value 3", "v3", viter.next().getSimple());
@@ -65,7 +65,7 @@ public void testComplexAsSimple() throws ObjectParseException, ObjectConfigureEx
ObjectFactory factory = new ObjectFactory();
ObjectTree tree = factory.parse(jsonFile, is);
- Context context = new Context(new RequestContext(), "", "");
+ ExecuteContext context = Util.createExecuteContext();
ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
boolean failed = false;
try {
@@ -84,16 +84,16 @@ public void testComplex() throws ObjectParseException, ObjectConfigureException,
ObjectFactory factory = new ObjectFactory();
ObjectTree tree = factory.parse(jsonFile, is);
- Context context = new Context(new RequestContext(), "", "");
+ ExecuteContext context = Util.createExecuteContext();
ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
- LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, false);
+ LinkedHashMap<String, Type> result = params.getPairs(context, false);
System.out.println(result);
assertEquals("Complex params size", 2, result.size());
assertEquals("Complex as simple value 1", "v1", result.get("n1").getSimple());
assertEquals("Complex not as simple", null, result.get("n2").getSimple());
- LinkedHashMap<String, SimpleOrComplex> complex = result.get("n2").getComplex();
+ LinkedHashMap<String, Type> complex = result.get("n2").getComplex();
assertEquals("Nested complex params size", 2, complex.size());
assertEquals("Nested complex value 1", "v21", complex.get("n21").getSimple());
assertEquals("Nested complex value 2", "v22", complex.get("n22").getSimple());
@@ -106,13 +106,13 @@ public void testMultiValue() throws ObjectParseException, ObjectConfigureExcepti
ObjectFactory factory = new ObjectFactory();
ObjectTree tree = factory.parse(jsonFile, is);
- Context context = new Context(new RequestContext(), "", "");
+ ExecuteContext context = Util.createExecuteContext();
ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
- LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, false);
+ LinkedHashMap<String, Type> result = params.getPairs(context, false);
assertEquals("Simple params size", 1, result.size());
- LinkedHashMap<String, SimpleOrComplex> value = result.get("n1").getComplex();
+ LinkedHashMap<String, Type> value = result.get("n1").getComplex();
assertEquals("Multivalue size", 2, value.size());
assertEquals("Multivalue value 1", "v1", value.get("id1").getSimple());
assertEquals("Multivalue value 2", "v2", value.get("id2").getSimple());
@@ -125,15 +125,15 @@ public void testMultiValueArray() throws ObjectParseException, ObjectConfigureEx
ObjectFactory factory = new ObjectFactory();
ObjectTree tree = factory.parse(jsonFile, is);
- Context context = new Context(new RequestContext(), "", "");
+ ExecuteContext context = Util.createExecuteContext();
ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
- LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, false);
+ LinkedHashMap<String, Type> result = params.getPairs(context, false);
assertEquals("Simple params size", 1, result.size());
- LinkedHashMap<String, SimpleOrComplex> value = result.get("n1").getComplex();
+ LinkedHashMap<String, Type> value = result.get("n1").getComplex();
assertEquals("Multivalue size", 2, value.size());
- Iterator<SimpleOrComplex> viter = value.values().iterator();
+ Iterator<Type> viter = value.values().iterator();
assertEquals("Multivalue value 1", "v1", viter.next().getSimple());
assertEquals("Multivalue value 2", "v2", viter.next().getSimple());
}
View
14 ants/test/ants/test/Util.java
@@ -0,0 +1,14 @@
+package ants.test;
+
+import ants.api.ExecuteContext;
+import ants.api.ModuleContext;
+import ants.api.RequestContext;
+
+public class Util {
+ public static ExecuteContext createExecuteContext() {
+ return new ExecuteContext(
+ new ModuleContext("test-instance", "test-module",
+ null, new RequestContext(null), null), null);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.