@Test public void readSubscribe () { String cu = "812:org.powertac.common.CustomerInfo::1895::new::Village::10"; String ca = "1255:org.powertac.common.Broker::601::new::CrocodileAgent"; String r = "1773:org.powertac.common.Rate::200076920::new::1878::-1::-1::-1::-1::0.0::true::-0.045598969348039364::0.0::0::0.0::0.1"; String ts = "1774:org.powertac.common.TariffSpecification::1878::new::601::INTERRUPTIBLE_CONSUMPTION::0::0::0.0::0.0::-0.6::null"; String sub = "9204:org.powertac.common.TariffSubscription::2588::new::1895::1878"; String add = "9204:org.powertac.common.TariffSubscription::2588::subscribe::10"; try { CustomerInfo customer = (CustomerInfo) dor.readObject(cu); assertNotNull(customer, "valid customer"); Broker crocodile = (Broker) dor.readObject(ca); assertNotNull(crocodile, "valid broker"); Rate rate = (Rate) dor.readObject(r); assertNotNull(rate, "valid rate"); TariffSpecification spec = (TariffSpecification) dor.readObject(ts); spec.addRate(rate); assertNotNull(spec, "valid tariff spec"); assertEquals(rate, spec.getRates().get(0), "spec contains rate"); TariffSubscription tsub = (TariffSubscription) dor.readObject(sub); assertNotNull(tsub, "valid subscription"); } catch (MissingDomainObject mdo) { fail("missing DO " + mdo.toString()); } }
public Object readObject (String line) throws MissingDomainObject { log.debug("readObject(" + line + ")"); String body = line.substring(line.indexOf(':') + 1); String[] tokens = body.split("::"); Class<?> clazz; String classname = StateLogging.unabbreviate(tokens[0]); if (ignores.contains(classname)) { return null; } try { clazz = Class.forName(classname); } catch (ClassNotFoundException e) { Class<?> subst = substitutes.get(classname); if (null == subst) { log.warn("class " + classname + " not found"); return null; } else { clazz = subst; } } long id = -1; try { id = Long.parseLong(tokens[1]); } catch (NumberFormatException nfe) { if (clazz == TimeService.class) { updateTime(tokens[3]); return null; } else if (noIdTypes.contains(clazz)) { id = 0; } else { log.debug("Number format exception reading id"); return null; } } String methodName = tokens[2]; log.debug("methodName=" + methodName); if (methodName.equals("new")) { if (clazz == TimeslotUpdate.class && timeslotPause > 0) { try { Thread.sleep(timeslotPause); } catch (InterruptedException e) { } } Object newInst = constructInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { if (!noIdTypes.contains(clazz)) { setId(newInst, id); idMap.put(id, newInst); } log.debug("Created new instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else if (methodName.equals("-rr")) { Object newInst = restoreInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { setId(newInst, id); idMap.put(id, newInst); log.debug("Restored instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else { Object inst = idMap.get(id); if (null == inst) { log.warn("Cannot find instance for id " + id + " of type " + clazz.getCanonicalName()); return null; } Method[] methods = clazz.getMethods(); ArrayList<Method> candidates = new ArrayList<>(); for (Method method : methods) { if (method.getName().equals(methodName)) { candidates.add(method); } } if (0 == candidates.size()) { log.error("Cannot find method " + methodName + " for class " + clazz.getName()); return null; } if (1 == candidates.size()) { if (!tryMethodCall(inst, candidates.get(0), Arrays.copyOfRange(tokens, 3, tokens.length))) { log.error("Failed to invoke method " + methodName + " on instance of " + clazz.getName()); } } else { boolean success = false; for (Method candidate : candidates) { success = tryMethodCall(inst, candidate, Arrays.copyOfRange(tokens, 3, tokens.length)); if (success) break; } if (!success) { log.error("Failed to find viable candidate for " + methodName + " on instance of " + clazz.getName()); } } } return null; }
DomainObjectReader { public Object readObject (String line) throws MissingDomainObject { log.debug("readObject(" + line + ")"); String body = line.substring(line.indexOf(':') + 1); String[] tokens = body.split("::"); Class<?> clazz; String classname = StateLogging.unabbreviate(tokens[0]); if (ignores.contains(classname)) { return null; } try { clazz = Class.forName(classname); } catch (ClassNotFoundException e) { Class<?> subst = substitutes.get(classname); if (null == subst) { log.warn("class " + classname + " not found"); return null; } else { clazz = subst; } } long id = -1; try { id = Long.parseLong(tokens[1]); } catch (NumberFormatException nfe) { if (clazz == TimeService.class) { updateTime(tokens[3]); return null; } else if (noIdTypes.contains(clazz)) { id = 0; } else { log.debug("Number format exception reading id"); return null; } } String methodName = tokens[2]; log.debug("methodName=" + methodName); if (methodName.equals("new")) { if (clazz == TimeslotUpdate.class && timeslotPause > 0) { try { Thread.sleep(timeslotPause); } catch (InterruptedException e) { } } Object newInst = constructInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { if (!noIdTypes.contains(clazz)) { setId(newInst, id); idMap.put(id, newInst); } log.debug("Created new instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else if (methodName.equals("-rr")) { Object newInst = restoreInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { setId(newInst, id); idMap.put(id, newInst); log.debug("Restored instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else { Object inst = idMap.get(id); if (null == inst) { log.warn("Cannot find instance for id " + id + " of type " + clazz.getCanonicalName()); return null; } Method[] methods = clazz.getMethods(); ArrayList<Method> candidates = new ArrayList<>(); for (Method method : methods) { if (method.getName().equals(methodName)) { candidates.add(method); } } if (0 == candidates.size()) { log.error("Cannot find method " + methodName + " for class " + clazz.getName()); return null; } if (1 == candidates.size()) { if (!tryMethodCall(inst, candidates.get(0), Arrays.copyOfRange(tokens, 3, tokens.length))) { log.error("Failed to invoke method " + methodName + " on instance of " + clazz.getName()); } } else { boolean success = false; for (Method candidate : candidates) { success = tryMethodCall(inst, candidate, Arrays.copyOfRange(tokens, 3, tokens.length)); if (success) break; } if (!success) { log.error("Failed to find viable candidate for " + methodName + " on instance of " + clazz.getName()); } } } return null; } }
DomainObjectReader { public Object readObject (String line) throws MissingDomainObject { log.debug("readObject(" + line + ")"); String body = line.substring(line.indexOf(':') + 1); String[] tokens = body.split("::"); Class<?> clazz; String classname = StateLogging.unabbreviate(tokens[0]); if (ignores.contains(classname)) { return null; } try { clazz = Class.forName(classname); } catch (ClassNotFoundException e) { Class<?> subst = substitutes.get(classname); if (null == subst) { log.warn("class " + classname + " not found"); return null; } else { clazz = subst; } } long id = -1; try { id = Long.parseLong(tokens[1]); } catch (NumberFormatException nfe) { if (clazz == TimeService.class) { updateTime(tokens[3]); return null; } else if (noIdTypes.contains(clazz)) { id = 0; } else { log.debug("Number format exception reading id"); return null; } } String methodName = tokens[2]; log.debug("methodName=" + methodName); if (methodName.equals("new")) { if (clazz == TimeslotUpdate.class && timeslotPause > 0) { try { Thread.sleep(timeslotPause); } catch (InterruptedException e) { } } Object newInst = constructInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { if (!noIdTypes.contains(clazz)) { setId(newInst, id); idMap.put(id, newInst); } log.debug("Created new instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else if (methodName.equals("-rr")) { Object newInst = restoreInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { setId(newInst, id); idMap.put(id, newInst); log.debug("Restored instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else { Object inst = idMap.get(id); if (null == inst) { log.warn("Cannot find instance for id " + id + " of type " + clazz.getCanonicalName()); return null; } Method[] methods = clazz.getMethods(); ArrayList<Method> candidates = new ArrayList<>(); for (Method method : methods) { if (method.getName().equals(methodName)) { candidates.add(method); } } if (0 == candidates.size()) { log.error("Cannot find method " + methodName + " for class " + clazz.getName()); return null; } if (1 == candidates.size()) { if (!tryMethodCall(inst, candidates.get(0), Arrays.copyOfRange(tokens, 3, tokens.length))) { log.error("Failed to invoke method " + methodName + " on instance of " + clazz.getName()); } } else { boolean success = false; for (Method candidate : candidates) { success = tryMethodCall(inst, candidate, Arrays.copyOfRange(tokens, 3, tokens.length)); if (success) break; } if (!success) { log.error("Failed to find viable candidate for " + methodName + " on instance of " + clazz.getName()); } } } return null; } DomainObjectReader(); }
DomainObjectReader { public Object readObject (String line) throws MissingDomainObject { log.debug("readObject(" + line + ")"); String body = line.substring(line.indexOf(':') + 1); String[] tokens = body.split("::"); Class<?> clazz; String classname = StateLogging.unabbreviate(tokens[0]); if (ignores.contains(classname)) { return null; } try { clazz = Class.forName(classname); } catch (ClassNotFoundException e) { Class<?> subst = substitutes.get(classname); if (null == subst) { log.warn("class " + classname + " not found"); return null; } else { clazz = subst; } } long id = -1; try { id = Long.parseLong(tokens[1]); } catch (NumberFormatException nfe) { if (clazz == TimeService.class) { updateTime(tokens[3]); return null; } else if (noIdTypes.contains(clazz)) { id = 0; } else { log.debug("Number format exception reading id"); return null; } } String methodName = tokens[2]; log.debug("methodName=" + methodName); if (methodName.equals("new")) { if (clazz == TimeslotUpdate.class && timeslotPause > 0) { try { Thread.sleep(timeslotPause); } catch (InterruptedException e) { } } Object newInst = constructInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { if (!noIdTypes.contains(clazz)) { setId(newInst, id); idMap.put(id, newInst); } log.debug("Created new instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else if (methodName.equals("-rr")) { Object newInst = restoreInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { setId(newInst, id); idMap.put(id, newInst); log.debug("Restored instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else { Object inst = idMap.get(id); if (null == inst) { log.warn("Cannot find instance for id " + id + " of type " + clazz.getCanonicalName()); return null; } Method[] methods = clazz.getMethods(); ArrayList<Method> candidates = new ArrayList<>(); for (Method method : methods) { if (method.getName().equals(methodName)) { candidates.add(method); } } if (0 == candidates.size()) { log.error("Cannot find method " + methodName + " for class " + clazz.getName()); return null; } if (1 == candidates.size()) { if (!tryMethodCall(inst, candidates.get(0), Arrays.copyOfRange(tokens, 3, tokens.length))) { log.error("Failed to invoke method " + methodName + " on instance of " + clazz.getName()); } } else { boolean success = false; for (Method candidate : candidates) { success = tryMethodCall(inst, candidate, Arrays.copyOfRange(tokens, 3, tokens.length)); if (success) break; } if (!success) { log.error("Failed to find viable candidate for " + methodName + " on instance of " + clazz.getName()); } } } return null; } DomainObjectReader(); void setTimeslotPause(int msec); int getTimeslotPause(); void registerNewObjectListener(NewObjectListener listener,
                                         Class<?> type); void registerMessageListener(LogtoolContext listener, Class<?> type); void setSchema(HashMap<String, String[]> schema); Object readObject(String line); Object getById(long id); }
DomainObjectReader { public Object readObject (String line) throws MissingDomainObject { log.debug("readObject(" + line + ")"); String body = line.substring(line.indexOf(':') + 1); String[] tokens = body.split("::"); Class<?> clazz; String classname = StateLogging.unabbreviate(tokens[0]); if (ignores.contains(classname)) { return null; } try { clazz = Class.forName(classname); } catch (ClassNotFoundException e) { Class<?> subst = substitutes.get(classname); if (null == subst) { log.warn("class " + classname + " not found"); return null; } else { clazz = subst; } } long id = -1; try { id = Long.parseLong(tokens[1]); } catch (NumberFormatException nfe) { if (clazz == TimeService.class) { updateTime(tokens[3]); return null; } else if (noIdTypes.contains(clazz)) { id = 0; } else { log.debug("Number format exception reading id"); return null; } } String methodName = tokens[2]; log.debug("methodName=" + methodName); if (methodName.equals("new")) { if (clazz == TimeslotUpdate.class && timeslotPause > 0) { try { Thread.sleep(timeslotPause); } catch (InterruptedException e) { } } Object newInst = constructInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { if (!noIdTypes.contains(clazz)) { setId(newInst, id); idMap.put(id, newInst); } log.debug("Created new instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else if (methodName.equals("-rr")) { Object newInst = restoreInstance(clazz, Arrays.copyOfRange(tokens, 3, tokens.length)); if (null != newInst) { setId(newInst, id); idMap.put(id, newInst); log.debug("Restored instance " + id + " of class " + tokens[0]); fireNewObjectEvent(newInst); } return newInst; } else { Object inst = idMap.get(id); if (null == inst) { log.warn("Cannot find instance for id " + id + " of type " + clazz.getCanonicalName()); return null; } Method[] methods = clazz.getMethods(); ArrayList<Method> candidates = new ArrayList<>(); for (Method method : methods) { if (method.getName().equals(methodName)) { candidates.add(method); } } if (0 == candidates.size()) { log.error("Cannot find method " + methodName + " for class " + clazz.getName()); return null; } if (1 == candidates.size()) { if (!tryMethodCall(inst, candidates.get(0), Arrays.copyOfRange(tokens, 3, tokens.length))) { log.error("Failed to invoke method " + methodName + " on instance of " + clazz.getName()); } } else { boolean success = false; for (Method candidate : candidates) { success = tryMethodCall(inst, candidate, Arrays.copyOfRange(tokens, 3, tokens.length)); if (success) break; } if (!success) { log.error("Failed to find viable candidate for " + methodName + " on instance of " + clazz.getName()); } } } return null; } DomainObjectReader(); void setTimeslotPause(int msec); int getTimeslotPause(); void registerNewObjectListener(NewObjectListener listener,
                                         Class<?> type); void registerMessageListener(LogtoolContext listener, Class<?> type); void setSchema(HashMap<String, String[]> schema); Object readObject(String line); Object getById(long id);  }