Skip to content

Commit 508f105

Browse files
Refactoring the LibvirtComputingResource
- Adding LibvirtStopCommandWrapper - LibvirtRequestWrapper - 1 unit tests Refactored the RequestWrapper to make it better. - Changes also applied to the CitrixRequestWrapper
1 parent c3e8d3d commit 508f105

7 files changed

Lines changed: 1242 additions & 1032 deletions

File tree

‎core/src/com/cloud/resource/RequestWrapper.java‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,93 @@
1919

2020
package com.cloud.resource;
2121

22+
import java.util.Hashtable;
23+
2224
import com.cloud.agent.api.Answer;
2325
import com.cloud.agent.api.Command;
2426

2527
public abstract class RequestWrapper {
2628

29+
@SuppressWarnings("rawtypes")
30+
protected Hashtable<Class<? extends ServerResource>, Hashtable<Class<? extends Command>, CommandWrapper>> resources = new Hashtable<Class<? extends ServerResource>, Hashtable<Class<? extends Command>, CommandWrapper>>();
31+
2732
/**
2833
* @param command to be executed.
2934
* @return an Answer for the executed command.
3035
*/
3136
public abstract Answer execute(Command command, ServerResource serverResource);
37+
38+
@SuppressWarnings({ "rawtypes", "unchecked" })
39+
protected Hashtable<Class<? extends Command>, CommandWrapper> retrieveResource(final Command command, final Class<? extends ServerResource> resourceClass) {
40+
Class<? extends ServerResource> keepResourceClass = resourceClass;
41+
Hashtable<Class<? extends Command>, CommandWrapper> resource = resources.get(keepResourceClass);
42+
while (resource == null) {
43+
try {
44+
final Class<? extends ServerResource> keepResourceClass2 = (Class<? extends ServerResource>) keepResourceClass.getSuperclass();
45+
resource = resources.get(keepResourceClass2);
46+
47+
keepResourceClass = keepResourceClass2;
48+
} catch (final ClassCastException e) {
49+
throw new NullPointerException("No key found for '" + command.getClass() + "' in the Map!");
50+
}
51+
}
52+
return resource;
53+
}
54+
55+
@SuppressWarnings({ "rawtypes", "unchecked" })
56+
protected CommandWrapper<Command, Answer, ServerResource> retrieveCommands(final Class<? extends Command> commandClass,
57+
final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands) {
58+
59+
Class<? extends Command> keepCommandClass = commandClass;
60+
CommandWrapper<Command, Answer, ServerResource> commandWrapper = resourceCommands.get(keepCommandClass);
61+
while (commandWrapper == null) {
62+
try {
63+
final Class<? extends Command> commandClass2 = (Class<? extends Command>) keepCommandClass.getSuperclass();
64+
65+
if (commandClass2 == null) {
66+
throw new NullPointerException("All the COMMAND hierarchy tree has been visited but no compliant key has been found for '" + commandClass + "'.");
67+
}
68+
69+
commandWrapper = resourceCommands.get(commandClass2);
70+
71+
keepCommandClass = commandClass2;
72+
} catch (final NullPointerException e) {
73+
// Will now traverse all the resource hierarchy. Returning null
74+
// is not a problem.
75+
// It is all being nicely checked and in case we do not have a
76+
// resource, an Unsupported answer will be thrown by the base
77+
// class.
78+
return null;
79+
}
80+
}
81+
return commandWrapper;
82+
}
83+
84+
@SuppressWarnings({ "rawtypes", "unchecked" })
85+
protected CommandWrapper<Command, Answer, ServerResource> retryWhenAllFails(final Command command, final Class<? extends ServerResource> resourceClass,
86+
final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands) {
87+
88+
Class<? extends ServerResource> keepResourceClass = resourceClass;
89+
CommandWrapper<Command, Answer, ServerResource> commandWrapper = resourceCommands.get(command.getClass());
90+
while (commandWrapper == null) {
91+
// Could not find the command in the given resource, will traverse
92+
// the family tree.
93+
try {
94+
final Class<? extends ServerResource> resourceClass2 = (Class<? extends ServerResource>) keepResourceClass.getSuperclass();
95+
96+
if (resourceClass2 == null) {
97+
throw new NullPointerException("All the SERVER-RESOURCE hierarchy tree has been visited but no compliant key has been found for '" + command.getClass() + "'.");
98+
}
99+
100+
final Hashtable<Class<? extends Command>, CommandWrapper> resourceCommands2 = retrieveResource(command,
101+
(Class<? extends ServerResource>) keepResourceClass.getSuperclass());
102+
keepResourceClass = resourceClass2;
103+
104+
commandWrapper = retrieveCommands(command.getClass(), resourceCommands2);
105+
} catch (final NullPointerException e) {
106+
throw e;
107+
}
108+
}
109+
return commandWrapper;
110+
}
32111
}

0 commit comments

Comments
 (0)