From 0e3b3b2a1ebd123dc7a18bdffa510162a42e434a Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Sat, 22 Aug 2015 11:56:43 +0200 Subject: [PATCH] [Minor] [New] Implemented and fixed remaining policy tests --- .../transienttest/data/Resources.xml | 2 +- .../src/main/java/li/strolch/model/Order.java | 3 ++ .../main/java/li/strolch/model/Resource.java | 3 ++ .../li/strolch/model/activity/Activity.java | 4 ++ .../model/xml/StrolchElementToDomVisitor.java | 6 +-- .../model/xml/StrolchElementToSaxVisitor.java | 44 ++++++++++++++++--- .../java/li/strolch/policy/JavaPolicyDef.java | 5 +++ .../java/li/strolch/policy/KeyPolicyDef.java | 5 +++ .../java/li/strolch/policy/PolicyDef.java | 35 +++++++++++++++ .../java/li/strolch/policy/PolicyDefs.java | 13 ++++++ .../resources/data/resources/Templates.xml | 2 +- 11 files changed, 110 insertions(+), 12 deletions(-) diff --git a/li.strolch.agent/src/test/resources/transienttest/data/Resources.xml b/li.strolch.agent/src/test/resources/transienttest/data/Resources.xml index f39067c97..42f477139 100644 --- a/li.strolch.agent/src/test/resources/transienttest/data/Resources.xml +++ b/li.strolch.agent/src/test/resources/transienttest/data/Resources.xml @@ -38,7 +38,7 @@ - + diff --git a/li.strolch.model/src/main/java/li/strolch/model/Order.java b/li.strolch.model/src/main/java/li/strolch/model/Order.java index 0b38b3ced..a37dc53f6 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/Order.java +++ b/li.strolch.model/src/main/java/li/strolch/model/Order.java @@ -118,6 +118,9 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle clone.setDate(this.date); clone.setState(this.state); + if (this.policyDefs != null) + clone.setPolicyDefs(this.policyDefs.getClone()); + return clone; } diff --git a/li.strolch.model/src/main/java/li/strolch/model/Resource.java b/li.strolch.model/src/main/java/li/strolch/model/Resource.java index 1fdf4c414..797bd2df4 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/Resource.java +++ b/li.strolch.model/src/main/java/li/strolch/model/Resource.java @@ -132,6 +132,9 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot } } + if (this.policyDefs != null) + clone.setPolicyDefs(this.policyDefs.getClone()); + return clone; } diff --git a/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java b/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java index cdd5c0041..cb191f080 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java +++ b/li.strolch.model/src/main/java/li/strolch/model/activity/Activity.java @@ -242,6 +242,10 @@ public class Activity extends GroupedParameterizedElement for (IActivityElement element : this.elements.values()) { clone.addElement(element.getClone()); } + + if (this.policyDefs != null) + clone.setPolicyDefs(this.policyDefs.getClone()); + return clone; } diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java index 375f74edd..a4ecc3027 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToDomVisitor.java @@ -109,6 +109,9 @@ public class StrolchElementToDomVisitor { Element element = document.createElement(Tags.ACTIVITY); fillElement(element, activity); + if (activity.hasPolicyDefs()) + fillElement(element, activity.getPolicyDefs()); + if (activity.hasElements()) { Iterator> iter = activity.elementIterator(); while (iter.hasNext()) { @@ -123,9 +126,6 @@ public class StrolchElementToDomVisitor { } } - if (activity.hasPolicyDefs()) - fillElement(element, activity.getPolicyDefs()); - return element; } diff --git a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxVisitor.java b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxVisitor.java index e06f581a1..e45ebc0b0 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxVisitor.java +++ b/li.strolch.model/src/main/java/li/strolch/model/xml/StrolchElementToSaxVisitor.java @@ -23,6 +23,12 @@ import java.util.Map.Entry; import java.util.Set; import java.util.SortedSet; +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; import li.strolch.model.GroupedParameterizedElement; import li.strolch.model.Order; import li.strolch.model.ParameterBag; @@ -37,13 +43,8 @@ import li.strolch.model.timedstate.StrolchTimedState; import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValueChange; - -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; - -import ch.eitchnet.utils.iso8601.ISO8601FormatFactory; +import li.strolch.policy.PolicyDef; +import li.strolch.policy.PolicyDefs; /** * @author Robert von Burg @@ -146,6 +147,9 @@ public abstract class StrolchElementToSaxVisitor { this.contentHandler.endElement(null, null, Tags.TIMED_STATE); } + if (resource.hasPolicyDefs()) + toSax(resource.getPolicyDefs()); + this.contentHandler.endElement(null, null, Tags.RESOURCE); } @@ -162,6 +166,9 @@ public abstract class StrolchElementToSaxVisitor { toSax((GroupedParameterizedElement) order); + if (order.hasPolicyDefs()) + toSax(order.getPolicyDefs()); + this.contentHandler.endElement(null, null, Tags.ORDER); } @@ -169,6 +176,9 @@ public abstract class StrolchElementToSaxVisitor { this.contentHandler.startElement(null, null, Tags.ACTIVITY, attributesFor(activity)); toSax((GroupedParameterizedElement) activity); + if (activity.hasPolicyDefs()) + toSax(activity.getPolicyDefs()); + Iterator> iter = activity.elementIterator(); while (iter.hasNext()) { IActivityElement activityElement = iter.next().getValue(); @@ -185,6 +195,26 @@ public abstract class StrolchElementToSaxVisitor { this.contentHandler.endElement(null, null, Tags.ACTIVITY); } + private void toSax(PolicyDefs policyDefs) throws SAXException { + if (!policyDefs.hasPolicyDefs()) + return; + + this.contentHandler.startElement(null, null, Tags.POLICIES, null); + for (String type : policyDefs.getPolicyTypes()) { + PolicyDef policyDef = policyDefs.getPolicyDef(type); + this.contentHandler.startElement(null, null, Tags.POLICY, attributesFor(policyDef)); + this.contentHandler.endElement(null, null, Tags.POLICY); + } + this.contentHandler.endElement(null, null, Tags.POLICIES); + } + + protected AttributesImpl attributesFor(PolicyDef policyDef) { + AttributesImpl attributes = new AttributesImpl(); + attributes.addAttribute(null, null, Tags.TYPE, Tags.CDATA, policyDef.getType()); + attributes.addAttribute(null, null, Tags.VALUE, Tags.CDATA, policyDef.getValueForXml()); + return attributes; + } + protected AttributesImpl attributesFor(Action action) { AttributesImpl attributes = attributesFor((StrolchElement) action); attributes.addAttribute(null, null, Tags.RESOURCE_ID, Tags.CDATA, action.getResourceId()); diff --git a/li.strolch.model/src/main/java/li/strolch/policy/JavaPolicyDef.java b/li.strolch.model/src/main/java/li/strolch/policy/JavaPolicyDef.java index 787f094f2..77b40db0a 100644 --- a/li.strolch.model/src/main/java/li/strolch/policy/JavaPolicyDef.java +++ b/li.strolch.model/src/main/java/li/strolch/policy/JavaPolicyDef.java @@ -37,4 +37,9 @@ public class JavaPolicyDef extends PolicyDef { public String getValueForXml() { return XML_PREFIX + this.value; } + + @Override + public JavaPolicyDef getClone() { + return new JavaPolicyDef(this.type, this.value); + } } \ No newline at end of file diff --git a/li.strolch.model/src/main/java/li/strolch/policy/KeyPolicyDef.java b/li.strolch.model/src/main/java/li/strolch/policy/KeyPolicyDef.java index 219b1cea1..1d43926d4 100644 --- a/li.strolch.model/src/main/java/li/strolch/policy/KeyPolicyDef.java +++ b/li.strolch.model/src/main/java/li/strolch/policy/KeyPolicyDef.java @@ -39,4 +39,9 @@ public class KeyPolicyDef extends PolicyDef { public String getValueForXml() { return XML_PREFIX + this.value; } + + @Override + public KeyPolicyDef getClone() { + return new KeyPolicyDef(this.type, this.value); + } } \ No newline at end of file diff --git a/li.strolch.model/src/main/java/li/strolch/policy/PolicyDef.java b/li.strolch.model/src/main/java/li/strolch/policy/PolicyDef.java index 50f295bcc..605952bef 100644 --- a/li.strolch.model/src/main/java/li/strolch/policy/PolicyDef.java +++ b/li.strolch.model/src/main/java/li/strolch/policy/PolicyDef.java @@ -75,8 +75,21 @@ public abstract class PolicyDef { */ public abstract T accept(PolicyDefVisitor visitor); + /** + * Returns the value formatted for XML marshalling, so that the {@link #valueOf(String, String)} can then again + * parse the value and instantiate a concrete {@link PolicyDef} instance + * + * @return the value formatted for XML marshalling + */ public abstract String getValueForXml(); + /** + * Return a clone of this {@link PolicyDef} instance + * + * @return a clone of this {@link PolicyDef} instance + */ + public abstract PolicyDef getClone(); + @Override public String toString() { StringBuilder sb = new StringBuilder(); @@ -88,13 +101,35 @@ public abstract class PolicyDef { return sb.toString(); } + /** + * Returns a {@link PolicyDef} instance which handles the given type of XML Value + * + * @param type + * the type + * @param xmlValue + * the XML formatted value with the prefix denoting the {@link PolicyDef} type + * + * @return a {@link PolicyDef} instance which handles the given type of XML Value + */ public static PolicyDef valueOf(String type, String xmlValue) { + if (xmlValue.startsWith(JavaPolicyDef.XML_PREFIX)) { + String value = xmlValue.substring(JavaPolicyDef.XML_PREFIX.length()); + + try { + Class.forName(value); + } catch (ClassNotFoundException e) { + throw new StrolchPolicyException("Invalid policy configuration. Policy does not exist: " + value); + } + return new JavaPolicyDef(type, value); + } else if (xmlValue.startsWith(KeyPolicyDef.XML_PREFIX)) { + String value = xmlValue.substring(KeyPolicyDef.XML_PREFIX.length()); return new KeyPolicyDef(type, value); + } else { throw new StrolchPolicyException("Unhandled PolicyDef from xml value " + xmlValue); } diff --git a/li.strolch.model/src/main/java/li/strolch/policy/PolicyDefs.java b/li.strolch.model/src/main/java/li/strolch/policy/PolicyDefs.java index dd95337c4..2f4cd074e 100644 --- a/li.strolch.model/src/main/java/li/strolch/policy/PolicyDefs.java +++ b/li.strolch.model/src/main/java/li/strolch/policy/PolicyDefs.java @@ -85,4 +85,17 @@ public class PolicyDefs { sb.append("]"); return sb.toString(); } + + /** + * Returns a clone of this {@link PolicyDefs} + * + * @return a clone of this {@link PolicyDefs} + */ + public PolicyDefs getClone() { + PolicyDefs clone = new PolicyDefs(); + for (PolicyDef policyDef : this.policyDefMap.values()) { + clone.addOrUpdate(policyDef.getClone()); + } + return clone; + } } diff --git a/li.strolch.model/src/test/resources/data/resources/Templates.xml b/li.strolch.model/src/test/resources/data/resources/Templates.xml index c230c8cd9..bac3dbea6 100644 --- a/li.strolch.model/src/test/resources/data/resources/Templates.xml +++ b/li.strolch.model/src/test/resources/data/resources/Templates.xml @@ -12,7 +12,7 @@ - +