You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ek...@apache.org on 2015/05/13 11:35:45 UTC

[03/41] git commit: updated refs/heads/master to 45c0fa2

Refactoring the LibvirtComputingResource
  - Adding LibvirtStopCommandWrapper
  - LibvirtRequestWrapper
  - 1 unit tests

Refactored the RequestWrapper to make it better.
  - Changes also applied to the CitrixRequestWrapper


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/508f1052
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/508f1052
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/508f1052

Branch: refs/heads/master
Commit: 508f10527fc199a45900e456899893fb776e50da
Parents: c3e8d3d
Author: wilderrodrigues <wr...@schubergphilis.com>
Authored: Wed Apr 22 11:35:57 2015 +0200
Committer: wilderrodrigues <wr...@schubergphilis.com>
Committed: Wed May 6 19:20:39 2015 +0200

----------------------------------------------------------------------
 core/src/com/cloud/resource/RequestWrapper.java |   79 +
 .../kvm/resource/LibvirtComputingResource.java  | 1762 +++++++++---------
 .../resource/wrapper/LibvirtRequestWrapper.java |   72 +
 .../wrapper/LibvirtStopCommandWrapper.java      |   86 +
 .../resource/LibvirtComputingResourceTest.java  |  197 +-
 .../resource/wrapper/CitrixRequestWrapper.java  |   74 -
 .../wrapper/CitrixRequestWrapperTest.java       |    4 +-
 7 files changed, 1242 insertions(+), 1032 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/508f1052/core/src/com/cloud/resource/RequestWrapper.java
----------------------------------------------------------------------
diff --git a/core/src/com/cloud/resource/RequestWrapper.java b/core/src/com/cloud/resource/RequestWrapper.java
index 6fd1c66..0311e25 100644
--- a/core/src/com/cloud/resource/RequestWrapper.java
+++ b/core/src/com/cloud/resource/RequestWrapper.java
@@ -19,14 +19,93 @@
 
 package com.cloud.resource;
 
+import java.util.Hashtable;
+
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.Command;
 
 public abstract class RequestWrapper {
 
+    @SuppressWarnings("rawtypes")
+    protected Hashtable<Class<? extends ServerResource>, Hashtable<Class<? extends Command>, CommandWrapper>> resources = new Hashtable<Class<? extends ServerResource>, Hashtable<Class<? extends Command>, CommandWrapper>>();
+
     /**
      * @param command to be executed.
      * @return an Answer for the executed command.
      */
     public abstract Answer execute(Command command, ServerResource serverResource);
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    protected Hashtable<Class<? extends Command>, CommandWrapper> retrieveResource(final Command command, final Class<? extends ServerResource> resourceClass) {
+        Class<? extends ServerResource> keepResourceClass = resourceClass;
+        Hashtable<Class<? extends Command>, CommandWrapper> resource = resources.get(keepResourceClass);
+        while (resource == null) {
+            try {
+                final Class<? extends ServerResource> keepResourceClass2 = (Class<? extends ServerResource>) keepResourceClass.getSuperclass();
+                resource = resources.get(keepResourceClass2);
+
+                keepResourceClass = keepResourceClass2;
+            } catch (final ClassCastException e) {
+                throw new NullPointerException("No key found for '" + command.getClass() + "' in the Map!");
+            }
+        }
+        return resource;
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    protected CommandWrapper<Command, Answer, ServerResource> retrieveCommands(final Class<? extends Command> commandClass,
+            final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands) {
+
+        Class<? extends Command> keepCommandClass = commandClass;
+        CommandWrapper<Command, Answer, ServerResource> commandWrapper = resourceCommands.get(keepCommandClass);
+        while (commandWrapper == null) {
+            try {
+                final Class<? extends Command> commandClass2 = (Class<? extends Command>) keepCommandClass.getSuperclass();
+
+                if (commandClass2 == null) {
+                    throw new NullPointerException("All the COMMAND hierarchy tree has been visited but no compliant key has been found for '" + commandClass + "'.");
+                }
+
+                commandWrapper = resourceCommands.get(commandClass2);
+
+                keepCommandClass = commandClass2;
+            } catch (final NullPointerException e) {
+                // Will now traverse all the resource hierarchy. Returning null
+                // is not a problem.
+                // It is all being nicely checked and in case we do not have a
+                // resource, an Unsupported answer will be thrown by the base
+                // class.
+                return null;
+            }
+        }
+        return commandWrapper;
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    protected CommandWrapper<Command, Answer, ServerResource> retryWhenAllFails(final Command command, final Class<? extends ServerResource> resourceClass,
+            final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands) {
+
+        Class<? extends ServerResource> keepResourceClass = resourceClass;
+        CommandWrapper<Command, Answer, ServerResource> commandWrapper = resourceCommands.get(command.getClass());
+        while (commandWrapper == null) {
+            // Could not find the command in the given resource, will traverse
+            // the family tree.
+            try {
+                final Class<? extends ServerResource> resourceClass2 = (Class<? extends ServerResource>) keepResourceClass.getSuperclass();
+
+                if (resourceClass2 == null) {
+                    throw new NullPointerException("All the SERVER-RESOURCE hierarchy tree has been visited but no compliant key has been found for '" + command.getClass() + "'.");
+                }
+
+                final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands2 = retrieveResource(command,
+                        (Class<? extends ServerResource>) keepResourceClass.getSuperclass());
+                keepResourceClass = resourceClass2;
+
+                commandWrapper = retrieveCommands(command.getClass(), resourceCommands2);
+            } catch (final NullPointerException e) {
+                throw e;
+            }
+        }
+        return commandWrapper;
+    }
 }
\ No newline at end of file