@Test public void testUpdateAll() { logger.info("updateAll"); String ruleName = "ccc"; String epl = Help.ExampleRules()[0]; String text = String.format("[{\"name\":\"%s\",\"text\":\"%s\"}]", ruleName, epl); Result result = RulesManager.updateAll(epService, text); assertEquals(200, result.getStatusCode()); String[] names = epService.getEPAdministrator().getStatementNames(); assertEquals(1, names.length); EPStatement st = epService.getEPAdministrator().getStatement(ruleName); assertEquals(epl, st.getText()); assertEquals(ruleName, st.getName()); }
public static synchronized Result updateAll(EPServiceProvider epService, String text) { try { long maxAge = Configuration.getMaxAge(); logger.debug(String.format("rule block text: %s",text)); org.json.JSONArray ja = new JSONArray(text); logger.debug(String.format("rules as JSONArray: %s", ja)); logger.info(String.format("put rules+contexts: %s", ja.length())); Map<String, String> newOnes = new LinkedHashMap<String, String>(); Set<String> oldOnesNames = new HashSet<String>(); long now = System.currentTimeMillis(); for (int i = 0; i < ja.length(); i++) { JSONObject jo = ja.getJSONObject(i); String name = jo.optString("name", ""); if ("".equals(name.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing name\"}"); } String newEpl = jo.optString("text", ""); if ("".equals(newEpl.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing text\"}"); } newOnes.put(name, newEpl); } oldOnesNames.addAll(Arrays.asList(epService.getEPAdministrator().getStatementNames())); for (String n : newOnes.keySet()) { String newEpl = newOnes.get(n); if (!oldOnesNames.contains(n)) { logger.debug(String.format("found new statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(n); String oldEPL = prevStmnt.getText(); if (!oldEPL.equals(newOnes.get(n))) { logger.debug(String.format("found changed statement: %s", n)); prevStmnt.destroy(); logger.debug(String.format("deleted statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("re-created statement: %s" ,n)); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { logger.debug(String.format("identical statement: %s", n)); } oldOnesNames.remove(n); } } for (String o : oldOnesNames) { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(o); logger.debug(String.format("unexpected statement: %s" ,o)); if (prevStmnt.getTimeLastStateChange() < now - maxAge) { logger.debug(String.format("unexpected statement, too old: %s", o)); prevStmnt.destroy(); logger.debug(String.format("deleted garbage statement: %s", o)); } } return new Result(HttpServletResponse.SC_OK, "{}"); } catch (EPException epe) { logger.error(String.format("creating statement %s", epe)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(epe.getMessage()))); } catch (JSONException je) { logger.error(String.format("creating statement %s", je)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(je.getMessage()))); } }
RulesManager { public static synchronized Result updateAll(EPServiceProvider epService, String text) { try { long maxAge = Configuration.getMaxAge(); logger.debug(String.format("rule block text: %s",text)); org.json.JSONArray ja = new JSONArray(text); logger.debug(String.format("rules as JSONArray: %s", ja)); logger.info(String.format("put rules+contexts: %s", ja.length())); Map<String, String> newOnes = new LinkedHashMap<String, String>(); Set<String> oldOnesNames = new HashSet<String>(); long now = System.currentTimeMillis(); for (int i = 0; i < ja.length(); i++) { JSONObject jo = ja.getJSONObject(i); String name = jo.optString("name", ""); if ("".equals(name.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing name\"}"); } String newEpl = jo.optString("text", ""); if ("".equals(newEpl.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing text\"}"); } newOnes.put(name, newEpl); } oldOnesNames.addAll(Arrays.asList(epService.getEPAdministrator().getStatementNames())); for (String n : newOnes.keySet()) { String newEpl = newOnes.get(n); if (!oldOnesNames.contains(n)) { logger.debug(String.format("found new statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(n); String oldEPL = prevStmnt.getText(); if (!oldEPL.equals(newOnes.get(n))) { logger.debug(String.format("found changed statement: %s", n)); prevStmnt.destroy(); logger.debug(String.format("deleted statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("re-created statement: %s" ,n)); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { logger.debug(String.format("identical statement: %s", n)); } oldOnesNames.remove(n); } } for (String o : oldOnesNames) { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(o); logger.debug(String.format("unexpected statement: %s" ,o)); if (prevStmnt.getTimeLastStateChange() < now - maxAge) { logger.debug(String.format("unexpected statement, too old: %s", o)); prevStmnt.destroy(); logger.debug(String.format("deleted garbage statement: %s", o)); } } return new Result(HttpServletResponse.SC_OK, "{}"); } catch (EPException epe) { logger.error(String.format("creating statement %s", epe)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(epe.getMessage()))); } catch (JSONException je) { logger.error(String.format("creating statement %s", je)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(je.getMessage()))); } } }
RulesManager { public static synchronized Result updateAll(EPServiceProvider epService, String text) { try { long maxAge = Configuration.getMaxAge(); logger.debug(String.format("rule block text: %s",text)); org.json.JSONArray ja = new JSONArray(text); logger.debug(String.format("rules as JSONArray: %s", ja)); logger.info(String.format("put rules+contexts: %s", ja.length())); Map<String, String> newOnes = new LinkedHashMap<String, String>(); Set<String> oldOnesNames = new HashSet<String>(); long now = System.currentTimeMillis(); for (int i = 0; i < ja.length(); i++) { JSONObject jo = ja.getJSONObject(i); String name = jo.optString("name", ""); if ("".equals(name.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing name\"}"); } String newEpl = jo.optString("text", ""); if ("".equals(newEpl.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing text\"}"); } newOnes.put(name, newEpl); } oldOnesNames.addAll(Arrays.asList(epService.getEPAdministrator().getStatementNames())); for (String n : newOnes.keySet()) { String newEpl = newOnes.get(n); if (!oldOnesNames.contains(n)) { logger.debug(String.format("found new statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(n); String oldEPL = prevStmnt.getText(); if (!oldEPL.equals(newOnes.get(n))) { logger.debug(String.format("found changed statement: %s", n)); prevStmnt.destroy(); logger.debug(String.format("deleted statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("re-created statement: %s" ,n)); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { logger.debug(String.format("identical statement: %s", n)); } oldOnesNames.remove(n); } } for (String o : oldOnesNames) { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(o); logger.debug(String.format("unexpected statement: %s" ,o)); if (prevStmnt.getTimeLastStateChange() < now - maxAge) { logger.debug(String.format("unexpected statement, too old: %s", o)); prevStmnt.destroy(); logger.debug(String.format("deleted garbage statement: %s", o)); } } return new Result(HttpServletResponse.SC_OK, "{}"); } catch (EPException epe) { logger.error(String.format("creating statement %s", epe)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(epe.getMessage()))); } catch (JSONException je) { logger.error(String.format("creating statement %s", je)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(je.getMessage()))); } }  }
RulesManager { public static synchronized Result updateAll(EPServiceProvider epService, String text) { try { long maxAge = Configuration.getMaxAge(); logger.debug(String.format("rule block text: %s",text)); org.json.JSONArray ja = new JSONArray(text); logger.debug(String.format("rules as JSONArray: %s", ja)); logger.info(String.format("put rules+contexts: %s", ja.length())); Map<String, String> newOnes = new LinkedHashMap<String, String>(); Set<String> oldOnesNames = new HashSet<String>(); long now = System.currentTimeMillis(); for (int i = 0; i < ja.length(); i++) { JSONObject jo = ja.getJSONObject(i); String name = jo.optString("name", ""); if ("".equals(name.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing name\"}"); } String newEpl = jo.optString("text", ""); if ("".equals(newEpl.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing text\"}"); } newOnes.put(name, newEpl); } oldOnesNames.addAll(Arrays.asList(epService.getEPAdministrator().getStatementNames())); for (String n : newOnes.keySet()) { String newEpl = newOnes.get(n); if (!oldOnesNames.contains(n)) { logger.debug(String.format("found new statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(n); String oldEPL = prevStmnt.getText(); if (!oldEPL.equals(newOnes.get(n))) { logger.debug(String.format("found changed statement: %s", n)); prevStmnt.destroy(); logger.debug(String.format("deleted statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("re-created statement: %s" ,n)); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { logger.debug(String.format("identical statement: %s", n)); } oldOnesNames.remove(n); } } for (String o : oldOnesNames) { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(o); logger.debug(String.format("unexpected statement: %s" ,o)); if (prevStmnt.getTimeLastStateChange() < now - maxAge) { logger.debug(String.format("unexpected statement, too old: %s", o)); prevStmnt.destroy(); logger.debug(String.format("deleted garbage statement: %s", o)); } } return new Result(HttpServletResponse.SC_OK, "{}"); } catch (EPException epe) { logger.error(String.format("creating statement %s", epe)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(epe.getMessage()))); } catch (JSONException je) { logger.error(String.format("creating statement %s", je)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(je.getMessage()))); } }  static synchronized Result get(EPServiceProvider epService, String ruleName); static synchronized Result make(EPServiceProvider epService, String text); static synchronized Result updateAll(EPServiceProvider epService, String text); static synchronized Result delete(EPServiceProvider epService, String ruleName); }
RulesManager { public static synchronized Result updateAll(EPServiceProvider epService, String text) { try { long maxAge = Configuration.getMaxAge(); logger.debug(String.format("rule block text: %s",text)); org.json.JSONArray ja = new JSONArray(text); logger.debug(String.format("rules as JSONArray: %s", ja)); logger.info(String.format("put rules+contexts: %s", ja.length())); Map<String, String> newOnes = new LinkedHashMap<String, String>(); Set<String> oldOnesNames = new HashSet<String>(); long now = System.currentTimeMillis(); for (int i = 0; i < ja.length(); i++) { JSONObject jo = ja.getJSONObject(i); String name = jo.optString("name", ""); if ("".equals(name.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing name\"}"); } String newEpl = jo.optString("text", ""); if ("".equals(newEpl.trim())) { return new Result(HttpServletResponse.SC_BAD_REQUEST, "{\"error\":\"missing text\"}"); } newOnes.put(name, newEpl); } oldOnesNames.addAll(Arrays.asList(epService.getEPAdministrator().getStatementNames())); for (String n : newOnes.keySet()) { String newEpl = newOnes.get(n); if (!oldOnesNames.contains(n)) { logger.debug(String.format("found new statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(n); String oldEPL = prevStmnt.getText(); if (!oldEPL.equals(newOnes.get(n))) { logger.debug(String.format("found changed statement: %s", n)); prevStmnt.destroy(); logger.debug(String.format("deleted statement: %s", n)); EPStatement statement = epService.getEPAdministrator().createEPL(newEpl, n); logger.debug(String.format("re-created statement: %s" ,n)); logger.debug(String.format("statement json: %s", Utils.Statement2JSONObject(statement))); statement.addListener(new GenericListener()); } else { logger.debug(String.format("identical statement: %s", n)); } oldOnesNames.remove(n); } } for (String o : oldOnesNames) { EPStatement prevStmnt = epService.getEPAdministrator().getStatement(o); logger.debug(String.format("unexpected statement: %s" ,o)); if (prevStmnt.getTimeLastStateChange() < now - maxAge) { logger.debug(String.format("unexpected statement, too old: %s", o)); prevStmnt.destroy(); logger.debug(String.format("deleted garbage statement: %s", o)); } } return new Result(HttpServletResponse.SC_OK, "{}"); } catch (EPException epe) { logger.error(String.format("creating statement %s", epe)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(epe.getMessage()))); } catch (JSONException je) { logger.error(String.format("creating statement %s", je)); return new Result(HttpServletResponse.SC_BAD_REQUEST, String.format("{\"error\":%s}%n", JSONObject.valueToString(je.getMessage()))); } }  static synchronized Result get(EPServiceProvider epService, String ruleName); static synchronized Result make(EPServiceProvider epService, String text); static synchronized Result updateAll(EPServiceProvider epService, String text); static synchronized Result delete(EPServiceProvider epService, String ruleName);  }