@Test public void readOutOfOrder () { String soton = "169937:org.powertac.common.Broker::605::new::SotonPower"; String hc1 = "177116:org.powertac.common.HourlyCharge::600000390::new::600000389::-0.0010::2009-01-03T02:00:00.000Z"; String hc2 = "177116:org.powertac.common.HourlyCharge::600000392::new::600000389::-0.0010::2009-01-03T03:00:00.000Z"; String r1 = "177118:org.powertac.common.Rate::600000389::new::600000388::-1::-1::-1::-1::0.0::false::0.0::0.0::1::0.0::0.0"; String ts1 = "177120:org.powertac.common.TariffSpecification::600000388::new::605::CONSUMPTION::0::0::0.0::0.0::0.0::null"; Object result = null; try { result = dor.readObject(soton); Broker broker = (Broker) result; result = dor.readObject(hc1); assertNotNull(result, "succeeded hc1"); assertEquals(HourlyCharge.class, result.getClass(), "correct class"); HourlyCharge charge1 = (HourlyCharge)result; assertEquals(600000389, charge1.getRateId(), "correct Rate hc1"); result = dor.readObject(hc2); assertNotNull(result, "succeeded hc2"); assertEquals(HourlyCharge.class, result.getClass(), "correct class"); HourlyCharge charge2 = (HourlyCharge)result; assertEquals(600000389, charge2.getRateId(), "correct Rate hc2"); result = dor.readObject(r1); Rate rate = (Rate)result; assertEquals(600000389, rate.getId(), "correct Id"); assertEquals(600000388, rate.getTariffId(), "correct spec Id"); result = dor.readObject(ts1); TariffSpecification spec = (TariffSpecification)result; assertEquals(600000388, spec.getId(), "correct Id"); assertEquals(broker, spec.getBroker(), "correct broker"); } catch (MissingDomainObject mdo) { fail("should succeed: " + 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);  }