m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java42
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java7
2 files changed, 48 insertions, 1 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
index 12682a6..4ac6f5c 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
@@ -1,10 +1,18 @@
package pl.edu.mimuw.cloudatlas.agent;
+import java.io.PrintStream;
+
import java.rmi.RemoteException;
-import java.util.Set;
+import java.util.List;
import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import pl.edu.mimuw.cloudatlas.interpreter.Interpreter;
+import pl.edu.mimuw.cloudatlas.interpreter.InterpreterException;
+import pl.edu.mimuw.cloudatlas.interpreter.Main;
+import pl.edu.mimuw.cloudatlas.interpreter.QueryResult;
import pl.edu.mimuw.cloudatlas.model.Attribute;
import pl.edu.mimuw.cloudatlas.model.AttributesMap;
import pl.edu.mimuw.cloudatlas.model.PathName;
@@ -13,6 +21,7 @@ import pl.edu.mimuw.cloudatlas.model.Value;
import pl.edu.mimuw.cloudatlas.model.ValueQuery;
import pl.edu.mimuw.cloudatlas.model.ValueSet;
import pl.edu.mimuw.cloudatlas.model.ValueNull;
+import pl.edu.mimuw.cloudatlas.model.Type;
import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
import pl.edu.mimuw.cloudatlas.model.ZMI;
import pl.edu.mimuw.cloudatlas.api.Api;
@@ -53,6 +62,7 @@ public class ApiImplementation implements Api {
ValueQuery query = new ValueQuery(queryCode);
Attribute attributeName = new Attribute(name);
installQueryInHierarchy(root, attributeName, query);
+ executeAllQueries(root);
} catch (Exception e) {
throw new RemoteException("Failed to install query", e);
}
@@ -84,11 +94,41 @@ public class ApiImplementation implements Api {
try {
ZMI zmi = root.findDescendant(new PathName(zoneName));
zmi.getAttributes().addOrChange(new Attribute(attributeName), value);
+ executeAllQueries(root);
} catch (ZMI.NoSuchZoneException e) {
throw new RemoteException("Zone not found", e);
}
}
+ private void executeAllQueries(ZMI zmi) {
+ if(!zmi.getSons().isEmpty()) {
+ for(ZMI son : zmi.getSons()) {
+ executeAllQueries(son);
+ }
+
+ Interpreter interpreter = new Interpreter(zmi);
+ for (ValueQuery query : getQueries(zmi)) {
+ try {
+ List<QueryResult> result = interpreter.interpretProgram(query.getQuery());
+ for(QueryResult r : result) {
+ zmi.getAttributes().addOrChange(r.getName(), r.getValue());
+ }
+ } catch(InterpreterException exception) {}
+ }
+ }
+ }
+
+ private Set<ValueQuery> getQueries(ZMI zmi) {
+ Set<ValueQuery> querySet = new HashSet<ValueQuery>();
+ for (Map.Entry<Attribute, Value> attribute : zmi.getAttributes()) {
+ if (attribute.getValue().getType().getPrimaryType() == Type.PrimaryType.QUERY) {
+ querySet.add((ValueQuery) attribute.getValue());
+ }
+ }
+
+ return querySet;
+ }
+
public void setFallbackContacts(Set<ValueContact> contacts) throws RemoteException {
this.contacts = contacts;
}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java
new file mode 100644
index 0000000..cbd4b91
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java
@@ -0,0 +1,7 @@
+package pl.edu.mimuw.cloudatlas.agent;
+
+import java.io.OutputStream;
+
+class NoopOutputStream extends OutputStream {
+ public void write(int b) {}
+}