Skip to content

Commit ebc10b6

Browse files
Kelven Yangchipchilders
authored andcommitted
CLOUDSTACK-1568,CLOUDSTACK-1664: Fix ActionEvent interception and optimize @db lookup with caching
Signed-off-by: Chip Childers <chip.childers@gmail.com>
1 parent 88b750c commit ebc10b6

5 files changed

Lines changed: 119 additions & 14 deletions

File tree

‎console-proxy/src/com/cloud/consoleproxy/ConsoleProxyHttpHandlerHelper.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static Map<String, String> getQueryMap(String query) {
5353
ConsoleProxyClientParam param = encryptor.decryptObject(ConsoleProxyClientParam.class, map.get("token"));
5454

5555
// make sure we get information from token only
56-
map.clear();
56+
guardUserInput(map);
5757
if(param != null) {
5858
if(param.getClientHostAddress() != null)
5959
map.put("host", param.getClientHostAddress());
@@ -72,9 +72,19 @@ public static Map<String, String> getQueryMap(String query) {
7272
}
7373
} else {
7474
// we no longer accept information from parameter other than token
75-
map.clear();
75+
guardUserInput(map);
7676
}
7777

7878
return map;
7979
}
80+
81+
private static void guardUserInput(Map<String, String> map) {
82+
map.remove("host");
83+
map.remove("port");
84+
map.remove("tag");
85+
map.remove("sid");
86+
map.remove("consoleurl");
87+
map.remove("sessionref");
88+
map.remove("ticket");
89+
}
8090
}

‎server/src/com/cloud/event/ActionEventInterceptor.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,29 @@
1919
import java.lang.reflect.AnnotatedElement;
2020
import java.lang.reflect.Method;
2121

22+
import org.apache.log4j.Logger;
2223
import org.aspectj.lang.ProceedingJoinPoint;
2324
import org.aspectj.lang.reflect.MethodSignature;
2425

2526
import com.cloud.user.UserContext;
27+
import com.cloud.utils.component.ComponentMethodProxyCache;
2628

2729
public class ActionEventInterceptor {
30+
private static final Logger s_logger = Logger.getLogger(ActionEventInterceptor.class);
2831

2932
public ActionEventInterceptor() {
3033
}
3134

3235
public Object AroundAnyMethod(ProceedingJoinPoint call) throws Throwable {
3336
MethodSignature methodSignature = (MethodSignature)call.getSignature();
34-
Method targetMethod = methodSignature.getMethod();
35-
if(needToIntercept(targetMethod)) {
37+
38+
// Note: AOP for ActionEvent is triggered annotation, no need to check the annotation on method again
39+
Method targetMethod = ComponentMethodProxyCache.getTargetMethod(
40+
methodSignature.getMethod(), call.getTarget());
41+
42+
if(targetMethod != null) {
3643
EventVO event = interceptStart(targetMethod);
37-
44+
3845
boolean success = true;
3946
Object ret = null;
4047
try {
@@ -49,6 +56,8 @@ public Object AroundAnyMethod(ProceedingJoinPoint call) throws Throwable {
4956
}
5057
}
5158
return ret;
59+
} else {
60+
s_logger.error("Unable to find the proxied method behind. Method: " + methodSignature.getMethod().getName());
5261
}
5362
return call.proceed();
5463
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.utils.component;
19+
20+
import java.lang.ref.WeakReference;
21+
import java.lang.reflect.Method;
22+
import java.util.WeakHashMap;
23+
24+
public class ComponentMethodProxyCache {
25+
26+
private static WeakHashMap<TargetKey, WeakReference<Method>> s_cache = new WeakHashMap<TargetKey, WeakReference<Method>>();
27+
28+
public ComponentMethodProxyCache() {
29+
}
30+
31+
public static Method getTargetMethod(Method method, Object target) {
32+
synchronized(s_cache) {
33+
WeakReference<Method> targetMethod = s_cache.get(new TargetKey(method, target));
34+
if(targetMethod != null && targetMethod.get() != null)
35+
return targetMethod.get();
36+
37+
Class<?> clazz = target.getClass();
38+
for(Method m : clazz.getMethods()) {
39+
if(isMethodMatched(method, m)) {
40+
s_cache.put(new TargetKey(method, target), new WeakReference<Method>(m));
41+
return m;
42+
}
43+
}
44+
45+
return method;
46+
}
47+
}
48+
49+
private static boolean isMethodMatched(Method m1, Method m2) {
50+
if(!m1.getName().equals(m2.getName()))
51+
return false;
52+
53+
Class<?>[] params1 = m1.getParameterTypes();
54+
Class<?>[] params2 = m2.getParameterTypes();
55+
56+
if(params1.length != params2.length)
57+
return false;
58+
59+
for(int i = 0; i < params1.length; i++) {
60+
if(!params1[i].isAssignableFrom(params2[i]))
61+
return false;
62+
}
63+
64+
return true;
65+
}
66+
67+
public static class TargetKey {
68+
Method _method;
69+
Object _target;
70+
71+
public TargetKey(Method method, Object target) {
72+
_method = method;
73+
_target = target;
74+
}
75+
76+
@Override
77+
public boolean equals(Object obj) {
78+
if(!(obj instanceof TargetKey))
79+
return false;
80+
81+
// for target object, we just check the reference
82+
return _method.equals(((TargetKey)obj)._method) &&
83+
_target == ((TargetKey)obj)._target;
84+
}
85+
86+
public int hashCode() {
87+
return _target.hashCode() ^ _target.hashCode();
88+
}
89+
}
90+
}

‎utils/src/com/cloud/utils/component/SpringComponentScanUtils.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,4 @@ public static boolean includedInBasePackageClasses(String clazzName, ComponentSc
3838
}
3939
return false;
4040
}
41-
4241
}

‎utils/src/com/cloud/utils/db/TransactionContextBuilder.java‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.aspectj.lang.ProceedingJoinPoint;
2424
import org.aspectj.lang.reflect.MethodSignature;
2525

26+
import com.cloud.utils.component.ComponentMethodProxyCache;
27+
2628
public class TransactionContextBuilder implements MethodInterceptor {
2729
public TransactionContextBuilder() {
2830
}
@@ -69,14 +71,9 @@ private boolean needToIntercept(Method method, Object target) {
6971
Class<?> clazz = method.getDeclaringClass();
7072
if(clazz.isInterface()) {
7173
clazz = target.getClass();
72-
for(Method m : clazz.getMethods()) {
73-
// it is supposed that we need to check against type arguments,
74-
// this can be simplified by just checking method name
75-
if(m.getName().equals(method.getName())) {
76-
if(m.getAnnotation(DB.class) != null)
77-
return true;
78-
}
79-
}
74+
Method targetMethod = ComponentMethodProxyCache.getTargetMethod(method, target);
75+
if(targetMethod != null && targetMethod.getAnnotation(DB.class) != null)
76+
return true;
8077
}
8178

8279
do {

0 commit comments

Comments
 (0)