From 80c3581dc04fd37b0561df395a1fa6e9fbfcfc54 Mon Sep 17 00:00:00 2001 From: Tobias Meuser Date: Thu, 9 May 2019 10:19:14 +0200 Subject: [PATCH] Necessary extensions for remote control via REST --- .../impl/scenario/DefaultConfigurator.java | 187 ++++++++++-------- .../tud/kom/p2psim/impl/util/db/dao/DAO.java | 22 ++- .../util/db/dao/metric/ExperimentDAO.java | 20 ++ .../impl/util/db/dao/metric/MetricDAO.java | 25 ++- .../p2psim/impl/util/db/metric/Metric.java | 8 + 5 files changed, 167 insertions(+), 95 deletions(-) diff --git a/src/de/tud/kom/p2psim/impl/scenario/DefaultConfigurator.java b/src/de/tud/kom/p2psim/impl/scenario/DefaultConfigurator.java index 562d3c80..7d897e36 100644 --- a/src/de/tud/kom/p2psim/impl/scenario/DefaultConfigurator.java +++ b/src/de/tud/kom/p2psim/impl/scenario/DefaultConfigurator.java @@ -37,6 +37,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import javax.sound.midi.Synthesizer; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; @@ -282,9 +283,7 @@ public class DefaultConfigurator implements Configurator { } else if (elem.getName().equals(Configurator.DEFAULT_TAG)) { configureDefaults(elem); } else { - if (!_parseOnly) { - configureComponent(elem); - } + configureComponent(elem); } } } @@ -321,7 +320,6 @@ public class DefaultConfigurator implements Configurator { * @return configured component */ public Object configureComponent(Element elem) { - String name = elem.getName(); if (Configurator.SPECIAL_IF_EQUAL_STR.equalsIgnoreCase(name)) { processIfEqualStr(elem, new ToConfigureCallback() { @@ -358,46 +356,55 @@ public class DefaultConfigurator implements Configurator { Monitor.log(DefaultConfigurator.class, Level.DEBUG, "Configure component " + name); - /* - * FIXED (BR) - if a component specifies a class-tag, do NOT reuse the - * old component for configuration. Instead, create a new component. - */ - // Constructor Attributes - Object component = configurables.get(name); - Set consAttrs = new HashSet(); - String clazz = getAttributeValue(elem.attribute(CLASS_TAG)); - if (clazz != null) { - // Create component - component = createComponent(elem, consAttrs); - } - - // configure it - if (component != null) { - Monitor.log(DefaultConfigurator.class, Level.INFO, - "Configure component " - + component.getClass().getSimpleName() - + " with element " + name); - configureAttributes(component, elem, consAttrs); - // configure subcomponents - if (component instanceof Builder) { - Monitor.log(DefaultConfigurator.class, Level.INFO, - "Configure builder " + component); - Builder builder = (Builder) component; - builder.parse(elem, this); - } else { - for (Iterator iter = elem.elementIterator(); iter.hasNext();) { - Element child = (Element) iter.next(); - if (!consAttrs.contains(child.getName().toLowerCase())) { - processChild(component, child, consAttrs); - } - } - } + if (!_parseOnly) { + /* + * FIXED (BR) - if a component specifies a class-tag, do NOT reuse the + * old component for configuration. Instead, create a new component. + */ + // Constructor Attributes + Object component = configurables.get(name); + Set consAttrs = new HashSet(); + String clazz = getAttributeValue(elem.attribute(CLASS_TAG)); + if (clazz != null) { + // Create component + component = createComponent(elem, consAttrs); + } + + // configure it + if (component != null) { + Monitor.log(DefaultConfigurator.class, Level.INFO, + "Configure component " + + component.getClass().getSimpleName() + + " with element " + name); + configureAttributes(component, elem, consAttrs); + // configure subcomponents + if (component instanceof Builder) { + Monitor.log(DefaultConfigurator.class, Level.INFO, + "Configure builder " + component); + Builder builder = (Builder) component; + builder.parse(elem, this); + } else { + for (Iterator iter = elem.elementIterator(); iter.hasNext();) { + Element child = (Element) iter.next(); + if (!consAttrs.contains(child.getName().toLowerCase())) { + processChild(component, child, consAttrs); + } + } + } + } else { + // component cannot be created and has not been registered + Monitor.log(DefaultConfigurator.class, Level.WARN, + "Skip element " + name); + } + return component; } else { - // component cannot be created and has not been registered - Monitor.log(DefaultConfigurator.class, Level.WARN, - "Skip element " + name); + for (Iterator iter = elem.elementIterator(); iter.hasNext();) { + Element child = (Element) iter.next(); + processChild(null, child, null); + } + + return null; } - return component; } private Object createComponent(Element elem, Set consAttrs) { @@ -466,42 +473,44 @@ public class DefaultConfigurator implements Configurator { Object subcomponent = configureComponent(child); - String prefix = SET_METHOD_PREFIX_TAG; - String methodName = getMethodName(prefix, child.getName()); - Method[] methods = component.getClass().getMethods(); - Method match = null; - for (int i = 0; i < methods.length; i++) { - if (methodName.equals(methods[i].getName())) { - match = methods[i]; - Monitor.log(DefaultConfigurator.class, Level.DEBUG, - "Match " + match); - break; - } - } - if (match == null) { - Monitor.log(DefaultConfigurator.class, Level.WARN, - "Cannot set " + subcomponent + " as there is no method " - + methodName + " declared in " + component); - throw new ConfigurationException( - "Cannot set " + subcomponent + " as there is no method " - + methodName + " declared in " + component); - } else { - Class[] types = match.getParameterTypes(); - Monitor.log(DefaultConfigurator.class, Level.DEBUG, - "Param types" + Arrays.asList(types)); - if (types.length == 1) { - try { - match.invoke(component, types[0].cast(subcomponent)); - } catch (Exception e) { - throw new ConfigurationException( - "Failed to configure " + methodName + " in " - + component + " with " + subcomponent, - e); - } - } else { - throw new ConfigurationException("Wrong number of params for " - + methodName + " in " + component); - } + if (!_parseOnly) { + String prefix = SET_METHOD_PREFIX_TAG; + String methodName = getMethodName(prefix, child.getName()); + Method[] methods = component.getClass().getMethods(); + Method match = null; + for (int i = 0; i < methods.length; i++) { + if (methodName.equals(methods[i].getName())) { + match = methods[i]; + Monitor.log(DefaultConfigurator.class, Level.DEBUG, + "Match " + match); + break; + } + } + if (match == null) { + Monitor.log(DefaultConfigurator.class, Level.WARN, + "Cannot set " + subcomponent + " as there is no method " + + methodName + " declared in " + component); + throw new ConfigurationException( + "Cannot set " + subcomponent + " as there is no method " + + methodName + " declared in " + component); + } else { + Class[] types = match.getParameterTypes(); + Monitor.log(DefaultConfigurator.class, Level.DEBUG, + "Param types" + Arrays.asList(types)); + if (types.length == 1) { + try { + match.invoke(component, types[0].cast(subcomponent)); + } catch (Exception e) { + throw new ConfigurationException( + "Failed to configure " + methodName + " in " + + component + " with " + subcomponent, + e); + } + } else { + throw new ConfigurationException("Wrong number of params for " + + methodName + " in " + component); + } + } } } @@ -739,6 +748,9 @@ public class DefaultConfigurator implements Configurator { if (staticMethod == null) { Constructor[] cs = forName.getConstructors(); + Constructor currentConstructor = null; + String[] usedArgs = null; + Object[] parameters = null; for (Constructor c : cs) { XMLConfigurableConstructor a = c @@ -789,16 +801,23 @@ public class DefaultConfigurator implements Configurator { } if (!incompatible) { - component = c.newInstance(consArgs); - - for (String consAttr : cArgs) { - consAttrs.add(consAttr.toLowerCase()); - } - break; + if (currentConstructor == null || parameters.length < consArgs.length) { + currentConstructor = c; + parameters = consArgs; + usedArgs = cArgs; + } } } } + if (currentConstructor != null) { + component = currentConstructor.newInstance(parameters); + + for (String consAttr : usedArgs) { + consAttrs.add(consAttr.toLowerCase()); + } + } + if (component == null) component = forName.newInstance(); diff --git a/src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java b/src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java index 0baad8e4..b39728f8 100644 --- a/src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java +++ b/src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java @@ -25,6 +25,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import javax.persistence.EntityManager; @@ -32,11 +33,13 @@ import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.apache.commons.math3.analysis.function.Exp; +import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; +import org.hibernate.criterion.Restrictions; import org.hibernate.service.ServiceRegistry; import org.omg.CORBA.CustomMarshal; @@ -186,7 +189,7 @@ public class DAO { if (!WebConfigurationManager.isActive()) { database = pDatabase; } else { - database = WebConfigurationManager.getConfig().getOptions().getDatabase(); + database = WebConfigurationManager.getConfig().getOptions().getDatabase().getDatabase(); } } @@ -194,7 +197,7 @@ public class DAO { if (!WebConfigurationManager.isActive()) { username = pUsername; } else { - username = WebConfigurationManager.getConfig().getOptions().getUsername(); + username = WebConfigurationManager.getConfig().getOptions().getDatabase().getUsername(); } } @@ -202,7 +205,7 @@ public class DAO { if (!WebConfigurationManager.isActive()) { password = pPassword; } else { - password = WebConfigurationManager.getConfig().getOptions().getPassword(); + password = WebConfigurationManager.getConfig().getOptions().getDatabase().getPassword(); } } @@ -313,13 +316,14 @@ public class DAO { DAO.session.remove(); } - public static List retrieve(Class pClass) { - begin(); - List objects = getSession().createCriteria(pClass).list(); - commit(); + public static List retrieve(Class pClass) { + begin(); + Criteria criteria = getSession().createCriteria(pClass); + List objects = criteria.list(); + commit(); - return objects; - } + return objects; + } /** * Persist the given POJO directly to the DB. Should be make, if it exists diff --git a/src/de/tud/kom/p2psim/impl/util/db/dao/metric/ExperimentDAO.java b/src/de/tud/kom/p2psim/impl/util/db/dao/metric/ExperimentDAO.java index 6949c849..929a1990 100644 --- a/src/de/tud/kom/p2psim/impl/util/db/dao/metric/ExperimentDAO.java +++ b/src/de/tud/kom/p2psim/impl/util/db/dao/metric/ExperimentDAO.java @@ -25,6 +25,10 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; + +import org.hibernate.Criteria; +import org.hibernate.criterion.Restrictions; import de.tud.kom.p2psim.impl.simengine.Simulator; import de.tud.kom.p2psim.impl.util.db.dao.DAO; @@ -85,6 +89,22 @@ public class ExperimentDAO extends DAO { return experiment; } + public static Experiment retrieveExperiment(long pSeed, Map pVariables) { + begin(); + Criteria criteria = getSession().createCriteria(Experiment.class); + criteria = criteria.add(Restrictions.eq("seed", pSeed)); + for (Entry entry : pVariables.entrySet()) { + criteria = criteria.add(Restrictions.like("workload", "%" + entry.getKey() + "=" + entry.getValue() + "%")); + } + List experiments = criteria.list(); + commit(); + + if (experiments.size() > 0) { + return experiments.get(experiments.size() - 1); + } + return null; + } + /** Called by the {@link Time} when the simulation is shut down. */ public static void simulationFinished() { // If there is no experiment object, no measurements have been made, diff --git a/src/de/tud/kom/p2psim/impl/util/db/dao/metric/MetricDAO.java b/src/de/tud/kom/p2psim/impl/util/db/dao/metric/MetricDAO.java index 5a249695..65168c28 100644 --- a/src/de/tud/kom/p2psim/impl/util/db/dao/metric/MetricDAO.java +++ b/src/de/tud/kom/p2psim/impl/util/db/dao/metric/MetricDAO.java @@ -1,10 +1,16 @@ package de.tud.kom.p2psim.impl.util.db.dao.metric; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; + +import org.hibernate.Criteria; +import org.hibernate.criterion.Restrictions; import de.tud.kom.p2psim.impl.util.Tuple; import de.tud.kom.p2psim.impl.util.db.dao.DAO; +import de.tud.kom.p2psim.impl.util.db.metric.Experiment; import de.tud.kom.p2psim.impl.util.db.metric.Metric; import de.tud.kom.p2psim.impl.util.db.metric.MetricDescription; @@ -53,6 +59,21 @@ public class MetricDAO extends DAO { } } + public static List getMetricsForExperiment(int pExperimentID) { + begin(); + Criteria criteria = getSession().createCriteria(Experiment.class); + if (pExperimentID != -1) { + criteria = criteria.add(Restrictions.eq("experimentID", pExperimentID)); + } + List metrics = criteria.list(); + commit(); + return metrics; + } + + public List getMetrics() { + return getMetricsForExperiment(-1); + } + /** Retrieve a {@link Metric} object for the given MetricDescription * for single value metrics. * @@ -62,7 +83,7 @@ public class MetricDAO extends DAO { public static Metric lookupSingleMetric(MetricDescription metricDesc) { return lookupMetric(metricDesc, MetricType.SINGLE); } - + /** Retrieve a {@link Metric} object for the given MetricDescription * for spatial value metrics. * @@ -86,7 +107,7 @@ public class MetricDAO extends DAO { /** * Retrieve a {@link Metric} object for the given MetricDescription for pair * list metrics. - * + * * If there is no matching Metric object, it is created, persisted, and * cached automatically. */ diff --git a/src/de/tud/kom/p2psim/impl/util/db/metric/Metric.java b/src/de/tud/kom/p2psim/impl/util/db/metric/Metric.java index 53e0aa4d..bd41ba14 100644 --- a/src/de/tud/kom/p2psim/impl/util/db/metric/Metric.java +++ b/src/de/tud/kom/p2psim/impl/util/db/metric/Metric.java @@ -152,6 +152,14 @@ public class Metric { return true; } + public String getName() { + return name; + } + + public String getComment() { + return comment; + } + @Override public String toString() { return "Metric{" + -- GitLab