Permalink
Browse files

- more object tree fun.

  • Loading branch information...
1 parent 6a4c47a commit a6db2cedd96e1b06db714b6135da0e6614cd1fa3 @sushantk committed Nov 22, 2011
View
9 DESIGN
@@ -1,10 +1,17 @@
+Principles
+ * Simple for user
+ * Reduce boiler plate code
+ * Performance
+
Streaming
Open connection
-
Client side framework
+Callback
+ * callbacks should return object tree
+ * callback joiner
TaskManager
* Interleaves sync and async tasks
View
@@ -3,6 +3,7 @@
public final class Consts {
// tags
+ public static final String controller = "module";
public static final String module = "module";
public static final String source = "source";
public static final String renderer = "renderer";
@@ -11,6 +12,7 @@
// classes
public static final String RendererIdentity = "siri.RendererIdentity";
public static final String SourceWebService = "siri.SourceWebService";
+ public static final String ControllerDefault = "siri.ControllerDefault";
// attributes
public static final String id = "id";
@@ -20,7 +22,10 @@
public static final String _default = "default";
public static final class error {
- public static final String required = "Missing element";
+ public static final String missingElement = "Missing element";
+ public static final String missingClass = "Missing @class attribute";
}
+
+
}
View
@@ -5,6 +5,7 @@
public class Data {
private InputStream m_stream;
+ // result and timedout?
public Data() {
}
@@ -6,14 +6,28 @@
public class ModuleDefault extends Configurable
implements IModule, IRequestCallback {
- static final Logger logger = LoggerFactory.getLogger(ModuleDefault.class);
+ static final Logger s_logger = LoggerFactory.getLogger(ModuleDefault.class);
static public interface IController {
- String getSource(Context a_context, ObjectTree a_tree);
+ ObjectTree[] getSource(Context a_context, ObjectTree a_tree);
+ }
+
+ static public class Controller implements IController {
+
+ @Override
+ public ObjectTree[] getSource(Context a_context, ObjectTree a_tree) {
+ ObjectTree sourceTree = ObjectFactory.find(a_context, a_tree, Consts.source, true);
+ if(null == sourceTree) return null;
+
+ return new ObjectTree[] {sourceTree};
+ }
+
}
private Context m_context;
private IRequestCallback m_callback;
+ private IController m_controller = null;
+ private boolean m_aborted = true;
public ModuleDefault(ObjectTree a_tree) {
super(a_tree);
@@ -24,23 +38,36 @@ public Result execute(Context a_context, IRequestCallback a_callback) {
m_context = a_context;
m_callback = a_callback;
- // 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;
+ m_controller = (IController) ObjectFactory.create(a_context, m_tree, Consts.controller, Consts.ControllerDefault, false);
+ if(null == m_controller) m_controller = new Controller();
+
+ ObjectTree[] sourceTrees = m_controller.getSource(a_context, m_tree);
+ if(null == sourceTrees) return Result.FAILED;
+
+ for(ObjectTree sourceTree : sourceTrees) {
+ if(null != sourceTree) {
+ ISource source = (ISource) ObjectFactory.create(m_context, sourceTree, Consts.SourceWebService);
+ // sources must call us back for runtime failure, failed get call is fatal
+ if(Result.SUCCESS != source.get(a_context, this)) {
+ m_aborted = true;
+ break;
+ }
+ }
+
+ m_aborted = true;
+ break;
}
- ISource source = (ISource) ObjectFactory.create(m_context, sourceTree, Consts.SourceWebService);
- return source.get(a_context, this);
+
+ return Result.SUCCESS;
}
@Override
public Data render(Context a_context, Data a_data) {
- if(a_context.isLogging()) logger.debug("{} - render", a_context);
+ if(a_context.isLogging()) s_logger.debug("{} - render", a_context);
// TODO: Ask controller for the renderer tree
IRenderer renderer = null;
- ObjectTree rendererTree = ObjectFactory.find(m_context, m_tree, Consts.renderer, null);
+ ObjectTree rendererTree = ObjectFactory.find(m_context, m_tree, Consts.renderer, false);
if(null != rendererTree) {
// TODO: default renderer class?
renderer = (IRenderer) ObjectFactory.create(m_context, rendererTree, Consts.RendererIdentity);
@@ -10,7 +10,7 @@
public class ModuleHandler implements IRequestCallback {
- static final Logger logger = LoggerFactory.getLogger(ModuleHandler.class);
+ static final Logger s_logger = LoggerFactory.getLogger(ModuleHandler.class);
static ThreadPoolExecutor s_rendererExecutor = null;
public static void main (String[] args) {
@@ -63,7 +63,7 @@ public static void main (String[] args) {
// should take request context
Result execute() {
String tree = "{\"source\":{\"url\":\"http://news.yahoo.com/rss/\"}}";
- ObjectTree otree = ObjectFactory.parse(m_context, tree);
+ ObjectTree otree = ObjectFactory.parse(m_context, Consts.module, tree);
if(null == otree)
return Result.INVALID_OBJECT_TREE;
View
@@ -13,42 +13,56 @@
public class ObjectFactory {
- static final Logger logger = LoggerFactory.getLogger(ObjectFactory.class);
+ static final Logger s_logger = LoggerFactory.getLogger(ObjectFactory.class);
static JsonFactory s_jsonMapper = new MappingJsonFactory();
static JsonNodeFactory s_jsonFactory = JsonNodeFactory.instance;
- public static ObjectTree tree() {
- return new ObjectTree(s_jsonFactory.objectNode());
+ public static ObjectTree tree(String a_tag) {
+ return new ObjectTree(a_tag, s_jsonFactory.objectNode());
}
- public static ObjectTree parse(Context a_context, String a_json) {
+ public static ObjectTree parse(Context a_context, String a_tag, String a_json) {
JsonNode node = null;
try {
JsonParser parser = s_jsonMapper.createJsonParser(a_json);
node = parser.readValueAsTree();
} catch(Exception ex) {
- logger.error("", ex);
+ s_logger.error("", ex);
}
if((null == node) || !node.isObject()) {
- logger.error("{} - Invalid object tree: {}", a_context, a_json);
+ s_logger.error("{} - Invalid object tree for \"{}\": {}",
+ new Object[] {a_context, a_tag, a_json});
return null;
}
- return new ObjectTree((ObjectNode)node);
+ return new ObjectTree(a_tag, node);
}
-
+
+ public static ObjectTree find(Context a_context, ObjectTree a_parent, String a_tag, boolean a_required) {
+ JsonNode node = ObjectFactory.findNode(a_context, a_parent, a_tag, null);
+ if(null != node) {
+ return new ObjectTree(a_tag, node);
+ }
+
+ if(a_required) {
+ ObjectFactory.logError(a_context, a_parent, a_tag, Consts.error.missingElement);
+ }
+ return null;
+ }
+
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 new ObjectTree(a_tag, node);
}
+ ObjectFactory.logError(a_context, a_parent, a_tag, a_id, Consts.error.missingElement);
return null;
}
public static ObjectTree fork(Context a_context, ObjectTree a_parent, String a_tag, String a_id) {
- JsonNode root = a_parent.getRoot();
+ JsonNode root = a_parent.getNode();
JsonNode node = root.get(a_tag);
ObjectNode forkedNode = s_jsonFactory.objectNode();
if(null != node) {
@@ -59,58 +73,74 @@ public static ObjectTree fork(Context a_context, ObjectTree a_parent, String a_t
// look for id
}
- return new ObjectTree(forkedNode);
+ return new ObjectTree(a_tag, forkedNode);
}
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_defaultClass, boolean a_required) {
+ JsonNode node = ObjectFactory.findNode(a_context, a_parent, a_tag, null);
+ if(null != node) {
+ if(node.isObject()) {
+ return ObjectFactory.create(a_context, (ObjectNode)node, a_tag, a_defaultClass);
+ } else {
+ throw new Error(ObjectFactory.fatalError(a_context, a_parent.getNode(), a_tag, "Expecting object node"));
+ }
+ }
+
+ if(a_required) ObjectFactory.logError(a_context, a_parent, a_tag, Consts.error.missingElement);
+ return null;
+ }
+
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);
+ return ObjectFactory.create(a_context, (ObjectNode)node, a_tag, a_defaultClass);
} else {
- throw new Error(ObjectFactory.fatalError(a_context, a_parent.getRoot(), a_tag, "Expecting object"));
+ throw new Error(ObjectFactory.fatalError(a_context, a_parent.getNode(), a_tag, "Expecting object node"));
}
}
+ ObjectFactory.logError(a_context, a_parent, a_tag, a_id, Consts.error.missingElement);
return null;
}
public static IConfigurable create(Context a_context, ObjectTree a_tree, String a_defaultClass) {
- JsonNode node = a_tree.getRoot();
+ JsonNode node = a_tree.getNode();
if(null != node) {
if(node.isObject()) {
- return ObjectFactory.create(a_context, (ObjectNode)node, a_defaultClass);
+ return ObjectFactory.create(a_context, (ObjectNode)node, a_tree.getTag(), a_defaultClass);
} else {
- throw new Error(ObjectFactory.fatalError(a_context, a_tree.getRoot(), "Expecting object"));
+ throw new Error(ObjectFactory.fatalError(a_context, a_tree.getNode(), "Expecting object node"));
}
}
return null;
}
- public static String getString(Context a_context, ObjectTree a_parent, String a_tag) {
- JsonNode root = a_parent.getRoot();
+ public static String getString(Context a_context, ObjectTree a_parent, String a_tag, boolean a_required) {
+ JsonNode root = a_parent.getNode();
JsonNode node = root.get(a_tag);
- 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, (ObjectNode)node, null);
- } else {
- throw new Error(ObjectFactory.fatalError(a_context, root, a_tag, "Invalid object tree for a string"));
+ if(null != node) {
+ if(node.isValueNode()) {
+ return StringDefault.evaluate(a_context, node.asText());
+ } else if(node.isObject()) {
+ IString stringObject = (IString)ObjectFactory.create(a_context, (ObjectNode)node, a_tag, null);
+ return stringObject.get(a_context);
+ } else {
+ throw new Error(ObjectFactory.fatalError(a_context, root, a_tag, "Invalid object tree for a string"));
+ }
}
- return stringObject.get(a_context);
+ if(a_required) ObjectFactory.logError(a_context, a_parent, a_tag, Consts.error.missingElement);
+ return null;
}
private static JsonNode findNode(Context a_context, ObjectTree a_parent, String a_tag, String a_id) {
- JsonNode root = a_parent.getRoot();
+ JsonNode root = a_parent.getNode();
JsonNode node = root.get(a_tag);
if(null != node) {
if((null == a_id) || a_id.isEmpty()) {
@@ -125,40 +155,43 @@ private static JsonNode findNode(Context a_context, ObjectTree a_parent, String
return null;
}
- private static IConfigurable create(Context a_context, ObjectNode a_node, String a_defaultClass) {
+ private static IConfigurable create(Context a_context, ObjectNode a_node, String a_tag, String a_defaultClass) {
String className = a_defaultClass;
JsonNode classAttr = a_node.get("@class");
if((null != classAttr) && classAttr.isTextual()) {
className = classAttr.asText();
}
+ if((null == className) || className.isEmpty()) {
+ throw new Error(ObjectFactory.fatalError(a_context, a_node, Consts.error.missingClass));
+ }
try {
Class<?> _class = Class.forName(className);
Constructor<?> tc = _class.getConstructor(ObjectTree.class);
- Object t = tc.newInstance(new ObjectTree(a_node));
+ Object t = tc.newInstance(new ObjectTree(a_tag, a_node));
return (IConfigurable)t;
} catch (Exception ex) {
throw new Error(ObjectFactory.fatalError(a_context, a_node, ex.getMessage()));
}
}
- public static void logError(Context a_context, String a_parentTag, ObjectTree a_parent,
+ public static void logError(Context a_context, ObjectTree a_parent,
String a_tag, String a_error) {
- ObjectFactory.logError(a_context, a_parentTag, a_parent, a_tag, null, a_error);
+ ObjectFactory.logError(a_context, a_parent, a_tag, null, a_error);
}
- public static void logError(Context a_context, String a_parentTag, ObjectTree a_parent,
+ public static void logError(Context a_context, 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});
+ s_logger.error("{} - " + a_error + ", \"{}\" inside \"{}\"",
+ new Object[]{a_context, a_tag, a_parent.getTag()});
} else {
- logger.error("{} - " + a_error + ", \"{}\" with id \"{}\" inside \"{}\"",
- new Object[]{a_context, a_tag, a_id, a_parentTag});
+ s_logger.error("{} - " + a_error + ", \"{}\" with id \"{}\" inside \"{}\"",
+ new Object[]{a_context, a_tag, a_id, a_parent.getTag()});
}
- if(a_context.isLogging()) logger.debug("{} - {} object tree: {}",
- new Object[] { a_context, a_parentTag, a_parent.getRoot().toString()});
+ if(a_context.isLogging()) s_logger.debug("{} - {} object tree: {}",
+ new Object[] {a_context, a_parent.getTag(), a_parent.getNode().toString()});
}
private static String fatalError(Context a_context, JsonNode a_node, String a_error) {
View
@@ -4,13 +4,28 @@
public final class ObjectTree {
- JsonNode m_root;
+ String m_tag;
+ JsonNode m_node;
+ Object m_userData;
- public ObjectTree(JsonNode a_node) {
- m_root = a_node;
+ public ObjectTree(String a_tag, JsonNode a_node) {
+ m_tag = a_tag;
+ m_node = a_node;
}
- JsonNode getRoot() {
- return m_root;
+ String getTag() {
+ return m_tag;
+ }
+
+ JsonNode getNode() {
+ return m_node;
+ }
+
+ Object getUserData() {
+ return m_userData;
+ }
+
+ void setUserData(Object a_data) {
+ m_userData = a_data;
}
}
Oops, something went wrong.

0 comments on commit a6db2ce

Please sign in to comment.