List of usage examples for com.fasterxml.jackson.databind.node NullNode getInstance
public static NullNode getInstance()
From source file:org.ocsoft.olivia.models.contents.config.JsonObject.java
public int getAsInt(String key, int defaultValue) { JsonNode value = root.get(key);// ww w. ja va 2 s .c o m if (value == NullNode.getInstance()) return defaultValue; return value.asInt(); }
From source file:com.almende.eve.state.redis.RedisState.java
@Override public JsonNode get(String key) { final Jedis redis = provider.getInstance(); final String nkey = makeKey(key); JsonNode res = NullNode.getInstance(); try {// w w w . ja v a 2 s . c om final String data = redis.get(nkey); if (data != null && !data.trim().isEmpty()) { res = JOM.getInstance().readTree(data); } } catch (JsonProcessingException e) { LOG.log(Level.WARNING, "Couldn't read:" + nkey, e); } catch (IOException e) { LOG.log(Level.WARNING, "Couldn't read:" + nkey, e); } provider.returnInstance(redis); return res; }
From source file:com.baasbox.commands.ScriptsResource.java
private static JsonNode storageCommand(JsonNode command, JsonCallback callback) throws CommandException { JsonNode moduleId = command.get(ScriptCommand.ID); if (moduleId == null || !moduleId.isTextual()) { throw new CommandParsingException(command, "error parsing module id"); }//from w w w .ja v a2 s .c o m String id = moduleId.asText(); JsonNode params = command.get(ScriptCommand.PARAMS); if (params == null || !params.isObject()) { throw new CommandParsingException(command, "error parsing params"); } JsonNode actionNode = params.get("action"); if (actionNode == null || !actionNode.isTextual()) { throw new CommandParsingException(command, "error parsing action"); } String action = actionNode.asText(); ODocument result; if ("get".equals(action)) { try { result = ScriptingService.getStore(id); } catch (ScriptException e) { //should never happen throw new CommandExecutionException(command, "script does not exists"); } } else if ("set".equals(action)) { JsonNode args = params.get("args"); if (args == null) { args = NullNode.getInstance(); } if (!args.isObject() && !args.isNull()) { throw new CommandExecutionException(command, "Stored values should be objects or null"); } try { result = ScriptingService.resetStore(id, args); } catch (ScriptException e) { throw new CommandExecutionException(command, "script does not exists"); } } else if ("swap".equals(action)) { if (callback == null) throw new CommandExecutionException(command, "missing function callback"); try { result = ScriptingService.swap(id, callback); } catch (ScriptException e) { throw new CommandExecutionException(command, ExceptionUtils.getMessage(e), e); } } else if ("trade".equals(action)) { if (callback == null) throw new CommandExecutionException(command, "missing function callback"); try { result = ScriptingService.trade(id, callback); } catch (ScriptException e) { throw new CommandExecutionException(command, ExceptionUtils.getMessage(e), e); } } else { throw new CommandParsingException(command, "unknown action: " + action); } if (result == null) { return NullNode.getInstance(); } else { String s = result.toJSON(); try { ObjectNode jsonNode = (ObjectNode) Json.mapper().readTree(s); jsonNode.remove("@version"); jsonNode.remove("@type"); return jsonNode; } catch (IOException e) { throw new CommandExecutionException(command, "error converting result", e); } } }
From source file:org.ocsoft.olivia.models.contents.config.JsonObject.java
public double getAsDouble(String key, double defaultValue) { JsonNode value = root.get(key);/*from w w w . j av a 2 s.co m*/ if (value == NullNode.getInstance()) return defaultValue; return value.asDouble(); }
From source file:com.almende.eve.state.redis.RedisState.java
@Override public JsonNode locPut(final String key, JsonNode value) { if (value == null) { value = NullNode.getInstance(); }/*from ww w .ja v a 2s. com*/ final Jedis redis = provider.getInstance(); final String nkey = makeKey(key); redis.set(nkey, value.toString()); redis.sadd(getId() + "_" + KEYS, nkey); provider.returnInstance(redis); return value; }
From source file:com.almende.eve.state.redis.RedisState.java
@Override public boolean locPutIfUnchanged(final String key, final JsonNode newVal, JsonNode oldVal) { boolean result = false; final Jedis redis = provider.getInstance(); try {/*from www . j av a2 s . c o m*/ JsonNode cur = get(key); if (oldVal == null) { oldVal = NullNode.getInstance(); } final String nkey = makeKey(key); if (oldVal.equals(cur) || oldVal.toString().equals(cur.toString())) { redis.set(nkey, newVal.toString()); redis.sadd(getId() + "_" + KEYS, nkey); result = true; } } catch (Exception e) { LOG.log(Level.WARNING, "", e); // Don't let users loop if exception is thrown. They // would get into a deadlock.... result = true; } provider.returnInstance(redis); return result; }
From source file:com.digitalpebble.storm.crawler.parse.ParseFilters.java
@SuppressWarnings("rawtypes") @Override// w w w. j ava 2 s .c o m public void configure(Map stormConf, JsonNode filtersConf) { // initialises the filters List<ParseFilter> filterLists = new ArrayList<>(); // get the filters part String name = getClass().getCanonicalName(); filtersConf = filtersConf.get(name); if (filtersConf == null) { LOG.info("No field {} in JSON config. Skipping", name); filters = new ParseFilter[0]; return; } // conf node contains a list of objects Iterator<JsonNode> filterIter = filtersConf.elements(); while (filterIter.hasNext()) { JsonNode afilterConf = filterIter.next(); String filterName = "<unnamed>"; JsonNode nameNode = afilterConf.get("name"); if (nameNode != null) { filterName = nameNode.textValue(); } JsonNode classNode = afilterConf.get("class"); if (classNode == null) { LOG.error("Filter {} doesn't specified a 'class' attribute", filterName); continue; } String className = classNode.textValue().trim(); filterName += '[' + className + ']'; // check that it is available and implements the interface // ParseFilter try { Class<?> filterClass = Class.forName(className); boolean subClassOK = ParseFilter.class.isAssignableFrom(filterClass); if (!subClassOK) { LOG.error("Filter {} does not extend ParseFilter", filterName); continue; } ParseFilter filterInstance = (ParseFilter) filterClass.newInstance(); JsonNode paramNode = afilterConf.get("params"); if (paramNode != null) { filterInstance.configure(stormConf, paramNode); } else { // Pass in a nullNode if missing filterInstance.configure(stormConf, NullNode.getInstance()); } filterLists.add(filterInstance); LOG.info("Setup {}", filterName); } catch (Exception e) { LOG.error("Can't setup {}: {}", filterName, e); throw new RuntimeException("Can't setup " + filterName, e); } } filters = filterLists.toArray(new ParseFilter[filterLists.size()]); }
From source file:com.digitalpebble.storm.crawler.filtering.URLFilters.java
@Override public void configure(Map stormConf, JsonNode jsonNode) { // initialises the filters List<URLFilter> filterLists = new ArrayList<>(); // get the filters part String name = getClass().getCanonicalName(); jsonNode = jsonNode.get(name);// www . j a v a 2 s . c o m if (jsonNode == null) { LOG.info("No field {} in JSON config. Skipping", name); filters = new URLFilter[0]; return; } // conf node contains a list of objects Iterator<JsonNode> filterIter = jsonNode.elements(); while (filterIter.hasNext()) { JsonNode afilterNode = filterIter.next(); String filterName = "<unnamed>"; JsonNode nameNode = afilterNode.get("name"); if (nameNode != null) { filterName = nameNode.textValue(); } JsonNode classNode = afilterNode.get("class"); if (classNode == null) { LOG.error("Filter {} doesn't specified a 'class' attribute", filterName); continue; } String className = classNode.textValue().trim(); filterName += '[' + className + ']'; // check that it is available and implements the interface URLFilter try { Class<?> filterClass = Class.forName(className); boolean interfaceOK = URLFilter.class.isAssignableFrom(filterClass); if (!interfaceOK) { LOG.error("Class {} does not implement URLFilter", className); continue; } URLFilter filterInstance = (URLFilter) filterClass.newInstance(); JsonNode paramNode = afilterNode.get("params"); if (paramNode != null) { filterInstance.configure(stormConf, paramNode); } else { filterInstance.configure(stormConf, NullNode.getInstance()); } filterLists.add(filterInstance); LOG.info("Loaded instance of class {}", className); } catch (Exception e) { LOG.error("Can't setup {}: {}", filterName, e); continue; } } filters = filterLists.toArray(new URLFilter[filterLists.size()]); }