Retire unused files to avoid confusions

This commit is contained in:
Kelven Yang 2013-04-02 15:53:09 -07:00
parent 7e54f40a02
commit 58c962ef15
8 changed files with 0 additions and 467 deletions

View File

@ -1,33 +0,0 @@
// 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.configuration;
import com.cloud.event.ActionEventUtils;
import com.cloud.utils.component.AnnotationInterceptor;
import com.cloud.utils.component.InterceptorLibrary;
import com.cloud.utils.db.DatabaseCallback;
import java.util.List;
public class DefaultInterceptorLibrary implements InterceptorLibrary {
@Override
public void addInterceptors(List<AnnotationInterceptor<?>> interceptors) {
interceptors.add(new DatabaseCallback());
interceptors.add(new ActionEventUtils.ActionEventCallback());
}
}

View File

@ -22,14 +22,9 @@ import com.cloud.server.ManagementServer;
import com.cloud.user.Account;
import com.cloud.user.AccountVO;
import com.cloud.user.User;
import com.cloud.user.UserContext;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDao;
import com.cloud.utils.component.AnnotationInterceptor;
import com.cloud.utils.component.ComponentContext;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.cloudstack.framework.events.EventBus;
import org.apache.cloudstack.framework.events.EventBusException;
import org.apache.log4j.Logger;
@ -38,8 +33,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@ -185,111 +178,4 @@ public class ActionEventUtils {
AccountVO account = _accountDao.findByIdIncludingRemoved(accountId);
return account.getDomainId();
}
public static class ActionEventCallback implements MethodInterceptor, AnnotationInterceptor<EventVO> {
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
EventVO event = interceptStart(method);
boolean success = true;
try {
return methodProxy.invokeSuper(object, args);
} catch (Exception e){
success = false;
interceptException(method, event);
throw e;
} finally {
if(success){
interceptComplete(method, event);
}
}
}
@Override
public boolean needToIntercept(AnnotatedElement element) {
if (!(element instanceof Method)) {
return false;
}
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
return true;
}
return false;
}
@Override
public EventVO interceptStart(AnnotatedElement element) {
EventVO event = null;
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
boolean async = actionEvent.async();
if(async){
UserContext ctx = UserContext.current();
long userId = ctx.getCallerUserId();
long accountId = ctx.getAccountId();
long startEventId = ctx.getStartEventId();
String eventDescription = actionEvent.eventDescription();
if(ctx.getEventDetails() != null){
eventDescription += ". "+ctx.getEventDetails();
}
ActionEventUtils.onStartedActionEvent(userId, accountId, actionEvent.eventType(), eventDescription, startEventId);
}
}
return event;
}
@Override
public void interceptComplete(AnnotatedElement element, EventVO event) {
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
UserContext ctx = UserContext.current();
long userId = ctx.getCallerUserId();
long accountId = ctx.getAccountId();
long startEventId = ctx.getStartEventId();
String eventDescription = actionEvent.eventDescription();
if(ctx.getEventDetails() != null){
eventDescription += ". "+ctx.getEventDetails();
}
if(actionEvent.create()){
//This start event has to be used for subsequent events of this action
startEventId = ActionEventUtils.onCreatedActionEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully created entity for " + eventDescription);
ctx.setStartEventId(startEventId);
} else {
ActionEventUtils.onCompletedActionEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully completed " + eventDescription, startEventId);
}
}
}
@Override
public void interceptException(AnnotatedElement element, EventVO event) {
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
UserContext ctx = UserContext.current();
long userId = ctx.getCallerUserId();
long accountId = ctx.getAccountId();
long startEventId = ctx.getStartEventId();
String eventDescription = actionEvent.eventDescription();
if(ctx.getEventDetails() != null){
eventDescription += ". "+ctx.getEventDetails();
}
if(actionEvent.create()){
long eventId = ActionEventUtils.onCreatedActionEvent(userId, accountId, EventVO.LEVEL_ERROR, actionEvent.eventType(), "Error while creating entity for " + eventDescription);
ctx.setStartEventId(eventId);
} else {
ActionEventUtils.onCompletedActionEvent(userId, accountId, EventVO.LEVEL_ERROR, actionEvent.eventType(), "Error while " + eventDescription, startEventId);
}
}
}
@Override
public Callback getCallback() {
return this;
}
}
}

View File

@ -1,36 +0,0 @@
// 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
// 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.utils.component;
import java.lang.reflect.AnnotatedElement;
import net.sf.cglib.proxy.Callback;
/**
* AnnotationIntercepter says it can intercept an annotation.
*/
public interface AnnotationInterceptor<T> {
boolean needToIntercept(AnnotatedElement element);
T interceptStart(AnnotatedElement element);
void interceptComplete(AnnotatedElement element, T attach);
void interceptException(AnnotatedElement element, T attach);
Callback getCallback();
}

View File

@ -1,90 +0,0 @@
// 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
// 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.utils.component;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.WeakHashMap;
public class ComponentMethodProxyCache {
private static WeakHashMap<TargetKey, WeakReference<Method>> s_cache = new WeakHashMap<TargetKey, WeakReference<Method>>();
public ComponentMethodProxyCache() {
}
public static Method getTargetMethod(Method method, Object target) {
synchronized(s_cache) {
WeakReference<Method> targetMethod = s_cache.get(new TargetKey(method, target));
if(targetMethod != null && targetMethod.get() != null)
return targetMethod.get();
Class<?> clazz = target.getClass();
for(Method m : clazz.getMethods()) {
if(isMethodMatched(method, m)) {
s_cache.put(new TargetKey(method, target), new WeakReference<Method>(m));
return m;
}
}
return method;
}
}
private static boolean isMethodMatched(Method m1, Method m2) {
if(!m1.getName().equals(m2.getName()))
return false;
Class<?>[] params1 = m1.getParameterTypes();
Class<?>[] params2 = m2.getParameterTypes();
if(params1.length != params2.length)
return false;
for(int i = 0; i < params1.length; i++) {
if(!params1[i].isAssignableFrom(params2[i]))
return false;
}
return true;
}
public static class TargetKey {
Method _method;
Object _target;
public TargetKey(Method method, Object target) {
_method = method;
_target = target;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof TargetKey))
return false;
// for target object, we just check the reference
return _method.equals(((TargetKey)obj)._method) &&
_target == ((TargetKey)obj)._target;
}
public int hashCode() {
return _target.hashCode() ^ _target.hashCode();
}
}
}

View File

@ -1,25 +0,0 @@
// 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
// 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.utils.component;
import java.util.List;
public interface InterceptorLibrary {
void addInterceptors(List<AnnotationInterceptor<?>> interceptors);
}

View File

@ -1,27 +0,0 @@
// 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
// 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.utils.component;
import java.lang.reflect.Method;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
public class MatchAnyMethodPointcut extends StaticMethodMatcherPointcut {
public boolean matches(Method method, Class<?> cls) {
return true;
}
}

View File

@ -1,84 +0,0 @@
// 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
// 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.utils.db;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import com.cloud.utils.component.AnnotationInterceptor;
public class DatabaseCallback implements MethodInterceptor, AnnotationInterceptor<Transaction> {
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Transaction txn = interceptStart(method);
try {
return methodProxy.invokeSuper(object, args);
} finally {
interceptComplete(method, txn);
}
}
@Override
public boolean needToIntercept(AnnotatedElement element) {
if (!(element instanceof Method)) {
return false;
}
Method method = (Method)element;
DB db = method.getAnnotation(DB.class);
if (db != null) {
return db.txn();
}
Class<?> clazz = method.getDeclaringClass();
do {
db = clazz.getAnnotation(DB.class);
if (db != null) {
return db.txn();
}
clazz = clazz.getSuperclass();
} while (clazz != Object.class && clazz != null);
return false;
}
@Override
public Transaction interceptStart(AnnotatedElement element) {
return Transaction.open(((Method)element).getName());
}
@Override
public void interceptComplete(AnnotatedElement element, Transaction txn) {
txn.close();
}
@Override
public void interceptException(AnnotatedElement element, Transaction txn) {
txn.close();
}
@Override
public Callback getCallback() {
return this;
}
}

View File

@ -1,58 +0,0 @@
// 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
// 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.utils.db;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.CallbackFilter;
public class DatabaseCallbackFilter implements CallbackFilter {
@Override
public int accept(Method method) {
return checkAnnotation(method) ? 1 : 0;
}
public static boolean checkAnnotation(Method method) {
/*Check self*/
DB db = method.getAnnotation(DB.class);
if (db != null) {
return db.txn();
}
Class<?> clazz = method.getDeclaringClass();
/*Check parent method*/
try {
Method pMethod = clazz.getMethod(method.getName(), method.getParameterTypes());
db = pMethod.getAnnotation(DB.class);
if (db != null) {
return db.txn();
}
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
}
/*Check class's annotation and ancestor's annotation*/
do {
db = clazz.getAnnotation(DB.class);
if (db != null) {
return db.txn();
}
clazz = clazz.getSuperclass();
} while (clazz != Object.class && clazz != null);
return false;
}
}