Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- ObjectFactory modularization

- Executor service for renderers
  • Loading branch information...
commit 6a4c47ac257808404fe80526b04e47cf4ca7b41d 1 parent e4356cd
@sushantk authored
View
7 src/siri/Consts.java
@@ -3,8 +3,9 @@
public final class Consts {
// tags
- public static final String renderer = "renderer";
+ public static final String module = "module";
public static final String source = "source";
+ public static final String renderer = "renderer";
public static final String url = "url";
// classes
@@ -18,4 +19,8 @@
public static final String renderer_id = "renderer_id";
public static final String _default = "default";
+ public static final class error {
+ public static final String required = "Missing element";
+ }
+
}
View
36 src/siri/ModuleDefault.java
@@ -1,8 +1,13 @@
package siri;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
public class ModuleDefault extends Configurable
implements IModule, IRequestCallback {
+ static final Logger logger = LoggerFactory.getLogger(ModuleDefault.class);
+
static public interface IController {
String getSource(Context a_context, ObjectTree a_tree);
}
@@ -19,41 +24,42 @@ public Result execute(Context a_context, IRequestCallback a_callback) {
m_context = a_context;
m_callback = a_callback;
- // TODO: ask controller for the source id
- ISource source = (ISource) ObjectFactory.create(m_context, this,
- Consts.source, Consts._default, Consts.SourceWebService, null, true);
- if(null == source) {
+ // TODO: ask controller for the source tree
+ ObjectTree sourceTree = ObjectFactory.find(m_context, m_tree, Consts.source, null);
+ if(null == sourceTree) {
+ ObjectFactory.logError(m_context, Consts.module, m_tree, Consts.source, Consts.error.required);
return Result.INVALID_OBJECT_TREE;
}
-
+ ISource source = (ISource) ObjectFactory.create(m_context, sourceTree, Consts.SourceWebService);
return source.get(a_context, this);
}
@Override
public Data render(Context a_context, Data a_data) {
- // TODO Ask controller for the renderer id
- String rendererId = a_context.get(Consts.renderer_id);
+ if(a_context.isLogging()) logger.debug("{} - render", a_context);
- //TODO: stringtemplate?
- IRenderer renderer = (IRenderer) ObjectFactory.create(m_context, this,
- Consts.renderer, rendererId, Consts.RendererIdentity, null, false);
- if(null == renderer) {
- return null;
+ // TODO: Ask controller for the renderer tree
+ IRenderer renderer = null;
+ ObjectTree rendererTree = ObjectFactory.find(m_context, m_tree, Consts.renderer, null);
+ if(null != rendererTree) {
+ // TODO: default renderer class?
+ renderer = (IRenderer) ObjectFactory.create(m_context, rendererTree, Consts.RendererIdentity);
+ } else {
+ renderer = new RendererIdentity(null);
}
-
return renderer.transform(a_context, a_data);
}
@Override
public void done(Context a_context, Data a_data) {
- // TODO Ask controller for the renderer id
+ // TODO: Ask controller for the renderer id
// a_context.set(Consts.renderer_id, Consts._default);
m_callback.done(a_context, a_data);
}
@Override
public void failed(Context a_context, Data a_data, boolean a_timedout) {
- // TODO Ask controller for the renderer id
+ // TODO: Ask controller for the renderer id
// a_context.set(Consts.renderer_id, Consts._default);
m_callback.failed(a_context, a_data, a_timedout);
}
View
41 src/siri/ModuleHandler.java
@@ -1,11 +1,17 @@
package siri;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ModuleHandler implements IRequestCallback {
static final Logger logger = LoggerFactory.getLogger(ModuleHandler.class);
+ static ThreadPoolExecutor s_rendererExecutor = null;
public static void main (String[] args) {
@@ -13,21 +19,34 @@ public static void main (String[] args) {
System.out.println("Specify a module id");
return;
}
-
+
+ int poolSize = 2;
+ s_rendererExecutor = new ThreadPoolExecutor(poolSize, poolSize, 0, TimeUnit.MILLISECONDS,
+ new ArrayBlockingQueue<Runnable>(poolSize), new ThreadPoolExecutor.CallerRunsPolicy());
+ s_rendererExecutor.prestartAllCoreThreads();
+ System.out.println("Active renderer threads: " + s_rendererExecutor.getActiveCount());
+
RequestContext requestContext = new RequestContext();
String moduleId = args[ 0 ];
ModuleHandler handler1 = new ModuleHandler(requestContext, "iid1", moduleId);
ModuleHandler handler2 = new ModuleHandler(requestContext, "iid2", moduleId);
+ ModuleHandler handler3 = new ModuleHandler(requestContext, "iid3", moduleId);
+ ModuleHandler handler4 = new ModuleHandler(requestContext, "iid4", moduleId);
handler1.execute();
handler2.execute();
+ handler3.execute();
+ handler4.execute();
TaskManager taskManager = requestContext.getTaskManager();
taskManager.run();
System.out.println("data1: " + handler1.getData());
System.out.println("data2: " + handler2.getData());
+ System.out.println("data3: " + handler3.getData());
+ System.out.println("data4: " + handler4.getData());
+ s_rendererExecutor.shutdown();
System.out.println("exit");
}
@@ -35,6 +54,7 @@ public static void main (String[] args) {
Context m_context;
IModule m_module;
Data m_finalData;
+ Future<?> m_rendererTask = null;
ModuleHandler(RequestContext a_requestContext, String a_iid, String a_moduleId) {
m_context = new Context(a_requestContext, a_iid, a_moduleId);
@@ -52,13 +72,30 @@ Result execute() {
}
Data getData() {
+ if(null == m_rendererTask) {
+ return null;
+ }
+
+ try {
+ m_rendererTask.get();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
return m_finalData;
}
@Override
public void done(Context a_context, Data a_data) {
System.out.println("Module request done: " + a_context.getModuleId());
- m_finalData = m_module.render(a_context, a_data);
+ System.out.println("Active renderer threads: " + s_rendererExecutor.getActiveCount());
+
+ final Data data = a_data;
+ m_rendererTask = s_rendererExecutor.submit(new Runnable() {
+ @Override
+ public void run() {
+ m_finalData = m_module.render(m_context, data);
+ }
+ });
}
@Override
View
155 src/siri/ObjectFactory.java
@@ -32,69 +32,104 @@ public static ObjectTree parse(Context a_context, String a_json) {
if((null == node) || !node.isObject()) {
logger.error("{} - Invalid object tree: {}", a_context, a_json);
- logger.error("node: {}", node.asToken());
return null;
}
return new ObjectTree((ObjectNode)node);
}
-
- //TODO: could be generic to return the right interface
- public static IConfigurable create(Context a_context, IConfigurable a_parent,
- String a_tag, String a_id, String a_defaultClass, ObjectTree a_tree,
- boolean a_required) {
-
- JsonNode root = a_parent.getObjectTree().getRoot();
+
+ public static ObjectTree find(Context a_context, ObjectTree a_parent, String a_tag, String a_id) {
+ JsonNode node = ObjectFactory.findNode(a_context, a_parent, a_tag, a_id);
+ if(null != node) {
+ return new ObjectTree(node);
+ }
+
+ return null;
+ }
+
+ public static ObjectTree fork(Context a_context, ObjectTree a_parent, String a_tag, String a_id) {
+ JsonNode root = a_parent.getRoot();
JsonNode node = root.get(a_tag);
- //TODO: handle array with a_id
- if((null == node) || !node.isObject()) {
- ObjectFactory.logError(a_context, a_parent, a_tag, null, a_required);
- return null;
+ ObjectNode forkedNode = s_jsonFactory.objectNode();
+ if(null != node) {
+ if((null == a_id) || a_id.isEmpty()) {
+ if(!node.isObject()) return null;
+ forkedNode.putAll((ObjectNode)node);
+ } else if(node.isArray()) {
+ // look for id
+ }
+
+ return new ObjectTree(forkedNode);
}
- //TODO: will clone be faster?
- ObjectNode clonedTree = s_jsonFactory.objectNode();
- clonedTree.putAll((ObjectNode)node);
- if(null != a_tree) {
- clonedTree.putAll((ObjectNode)a_tree.getRoot());
+ return null;
+ }
+
+ //TODO: could it be generic to return the right interface
+ public static IConfigurable create(Context a_context, ObjectTree a_parent, String a_tag, String a_id, String a_defaultClass) {
+ JsonNode node = ObjectFactory.findNode(a_context, a_parent, a_tag, a_id);
+ if(null != node) {
+ if(node.isObject()) {
+ ObjectFactory.create(a_context, (ObjectNode)node, a_defaultClass);
+ } else {
+ throw new Error(ObjectFactory.fatalError(a_context, a_parent.getRoot(), a_tag, "Expecting object"));
+ }
}
- return ObjectFactory.create(a_context, a_parent, clonedTree, a_tag, a_id, a_defaultClass);
+ return null;
}
- public static String getString(Context a_context, IConfigurable a_parent,
- String a_tag, boolean a_required) {
+ public static IConfigurable create(Context a_context, ObjectTree a_tree, String a_defaultClass) {
+ JsonNode node = a_tree.getRoot();
+ if(null != node) {
+ if(node.isObject()) {
+ return ObjectFactory.create(a_context, (ObjectNode)node, a_defaultClass);
+ } else {
+ throw new Error(ObjectFactory.fatalError(a_context, a_tree.getRoot(), "Expecting object"));
+ }
+ }
- ObjectTree tree = a_parent.getObjectTree();
- JsonNode root = tree.getRoot();
+ return null;
+ }
+
+ public static String getString(Context a_context, ObjectTree a_parent, String a_tag) {
+ JsonNode root = a_parent.getRoot();
JsonNode node = root.get(a_tag);
- if(null == node) {
- ObjectFactory.logError(a_context, a_parent, a_tag, a_required);
- return null;
- }
+ if(null == node) return null;
IString stringObject = null;
if(node.isValueNode()) {
stringObject = new StringDefault(new ObjectTree(node));
} else if(node.isObject()) {
- stringObject = (IString)ObjectFactory.create(a_context, a_parent, (ObjectNode)node, a_tag, null, null);
- if(null == stringObject)
- return null;
+ stringObject = (IString)ObjectFactory.create(a_context, (ObjectNode)node, null);
} else {
- ObjectFactory.logError(a_context, a_parent, a_tag, "Invalid type for a string");
- return null;
+ throw new Error(ObjectFactory.fatalError(a_context, root, a_tag, "Invalid object tree for a string"));
}
return stringObject.get(a_context);
}
- private static IConfigurable create(Context a_context, IConfigurable a_parent,
- ObjectNode a_node, String a_tag, String a_id, String a_defaultClass) {
+ private static JsonNode findNode(Context a_context, ObjectTree a_parent, String a_tag, String a_id) {
+ JsonNode root = a_parent.getRoot();
+ JsonNode node = root.get(a_tag);
+ if(null != node) {
+ if((null == a_id) || a_id.isEmpty()) {
+ return node;
+ }
+
+ // look for id
+ if(node.isArray()) {
+ }
+ }
+
+ return null;
+ }
- JsonNode classAttr = a_node.get("@class");
+ private static IConfigurable create(Context a_context, ObjectNode a_node, String a_defaultClass) {
String className = a_defaultClass;
- //TODO: handle class name attribute
- if(null != classAttr) {
+ JsonNode classAttr = a_node.get("@class");
+ if((null != classAttr) && classAttr.isTextual()) {
+ className = classAttr.asText();
}
try {
@@ -103,44 +138,34 @@ private static IConfigurable create(Context a_context, IConfigurable a_parent,
Object t = tc.newInstance(new ObjectTree(a_node));
return (IConfigurable)t;
} catch (Exception ex) {
- ObjectFactory.logError(a_context, a_parent, a_tag, a_id, "Failed to instantiate " + className);
- logger.error("", ex);
+ throw new Error(ObjectFactory.fatalError(a_context, a_node, ex.getMessage()));
}
-
- return null;
}
- private static void logError(Context a_context, IConfigurable a_parent,
- String a_tag, boolean a_required) {
- if(a_required) {
- ObjectFactory.logError(a_context, a_parent, a_tag, null, "A required element is missing");
- }
+ public static void logError(Context a_context, String a_parentTag, ObjectTree a_parent,
+ String a_tag, String a_error) {
+ ObjectFactory.logError(a_context, a_parentTag, a_parent, a_tag, null, a_error);
}
- private static void logError(Context a_context, IConfigurable a_parent,
- String a_tag, String a_id, boolean a_required) {
- if(a_required) {
- ObjectFactory.logError(a_context, a_parent,
- a_tag, a_id, "Could not find a required element");
+ public static void logError(Context a_context, String a_parentTag, ObjectTree a_parent,
+ String a_tag, String a_id, String a_error) {
+ if(null == a_id) {
+ logger.error("{} - " + a_error + ", \"{}\" inside \"{}\"",
+ new Object[]{a_context, a_tag, a_parentTag});
+ } else {
+ logger.error("{} - " + a_error + ", \"{}\" with id \"{}\" inside \"{}\"",
+ new Object[]{a_context, a_tag, a_id, a_parentTag});
}
+
+ if(a_context.isLogging()) logger.debug("{} - {} object tree: {}",
+ new Object[] { a_context, a_parentTag, a_parent.getRoot().toString()});
}
- private static void logError(Context a_context, IConfigurable a_parent,
- String a_tag, String a_error) {
- ObjectFactory.logError(a_context, a_parent, a_tag, a_error);
+ private static String fatalError(Context a_context, JsonNode a_node, String a_error) {
+ return (a_context.toString() + " - " + a_error + ", object tree: " + a_node.toString());
}
- private static void logError(Context a_context, IConfigurable a_parent,
- String a_tag, String a_id, String a_error) {
- if(null == a_id) {
- logger.error("{} - " + a_error + ", \"{}\" inside {}",
- new Object[]{a_context, a_tag, a_parent.getClass().getName()});
- } else {
- logger.error("{} - " + a_error + ", \"{}\" with id \"{}\" inside {}",
- new Object[]{a_context, a_tag, a_id, a_parent.getClass().getName()});
- }
-
- if(a_context.isLogging()) logger.debug("{} - {} Object tree: {}",
- new Object[] { a_context, a_parent.getClass().getName(), a_parent.getObjectTree().getRoot().toString()});
+ private static String fatalError(Context a_context, JsonNode a_parent, String a_tag, String a_error) {
+ return (a_context.toString() + " - " + a_error + ", \"" + a_tag + "\" inside: " + a_parent.toString());
}
}
View
8 src/siri/SourceWebService.java
@@ -39,9 +39,11 @@ public Result get(Context a_context, IRequestCallback a_callback) {
m_context = a_context;
m_callback = a_callback;
- m_url = ObjectFactory.getString(m_context, this, Consts.url, true);
- if(null == m_url)
- return Result.FAILED;
+ m_url = ObjectFactory.getString(m_context, m_tree, Consts.url);
+ if(null == m_url) {
+ ObjectFactory.logError(m_context, "", m_tree, Consts.url, Consts.error.required);
+ return Result.INVALID_OBJECT_TREE;
+ }
final TaskManager taskManager = a_context.getRequestContext().getTaskManager();
m_task = this.new WebServiceTask();
Please sign in to comment.
Something went wrong with that request. Please try again.