forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVmWorkJobHandlerProxy.java
More file actions
133 lines (104 loc) · 4.87 KB
/
VmWorkJobHandlerProxy.java
File metadata and controls
133 lines (104 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.vm;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import org.apache.cloudstack.framework.jobs.impl.JobSerializerHelper;
import org.apache.cloudstack.jobs.JobInfo;
import com.cloud.serializer.GsonHelper;
import com.cloud.utils.Pair;
/**
* VmWorkJobHandlerProxy can not be used as standalone due to run-time
* reflection usage in its implementation, run-time reflection conflicts with Spring proxy mode.
* It means that we can not instantiate VmWorkJobHandlerProxy beans directly in Spring and expect
* it can handle VmWork directly from there.
*
*/
public class VmWorkJobHandlerProxy implements VmWorkJobHandler {
private static final Logger s_logger = Logger.getLogger(VmWorkJobHandlerProxy.class);
private Object _target;
private Map<Class<?>, Method> _handlerMethodMap = new HashMap<Class<?>, Method>();
private Gson _gsonLogger;
public VmWorkJobHandlerProxy(Object target) {
_gsonLogger = GsonHelper.getGsonLogger();
buildLookupMap(target.getClass());
_target = target;
}
private void buildLookupMap(Class<?> hostClass) {
Class<?> clz = hostClass;
while (clz != null && clz != Object.class) {
Method[] hostHandlerMethods = clz.getDeclaredMethods();
for (Method method : hostHandlerMethods) {
if (isVmWorkJobHandlerMethod(method)) {
Class<?> paramType = method.getParameterTypes()[0];
assert (_handlerMethodMap.get(paramType) == null);
method.setAccessible(true);
_handlerMethodMap.put(paramType, method);
}
}
clz = clz.getSuperclass();
}
}
@SuppressWarnings("deprecation")
private boolean isVmWorkJobHandlerMethod(Method method) {
if (method.getParameterTypes().length != 1)
return false;
Class<?> returnType = method.getReturnType();
if (!Pair.class.isAssignableFrom(returnType))
return false;
Class<?> paramType = method.getParameterTypes()[0];
if (!VmWork.class.isAssignableFrom(paramType))
return false;
return true;
}
private Method getHandlerMethod(Class<?> paramType) {
return _handlerMethodMap.get(paramType);
}
@SuppressWarnings("unchecked")
@Override
public Pair<JobInfo.Status, String> handleVmWorkJob(VmWork work) throws Exception {
Method method = getHandlerMethod(work.getClass());
if (method != null) {
try {
if (s_logger.isDebugEnabled())
s_logger.debug("Execute VM work job: " + work.getClass().getName() + _gsonLogger.toJson(work));
Object obj = method.invoke(_target, work);
if (s_logger.isDebugEnabled())
s_logger.debug("Done executing VM work job: " + work.getClass().getName() + _gsonLogger.toJson(work));
assert (obj instanceof Pair);
return (Pair<JobInfo.Status, String>)obj;
} catch (InvocationTargetException e) {
s_logger.error("Invocation exception, caused by: " + e.getCause());
// legacy CloudStack code relies on checked exception for error handling
// we need to re-throw the real exception here
if (e.getCause() != null && e.getCause() instanceof Exception) {
s_logger.info("Rethrow exception " + e.getCause());
throw (Exception)e.getCause();
}
throw e;
}
} else {
s_logger.error("Unable to find handler for VM work job: " + work.getClass().getName() + _gsonLogger.toJson(work));
RuntimeException ex = new RuntimeException("Unable to find handler for VM work job: " + work.getClass().getName());
return new Pair<JobInfo.Status, String>(JobInfo.Status.FAILED, JobSerializerHelper.toObjectSerializedString(ex));
}
}
}