|
| 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 | +// with 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 | +package com.cloud.event; |
| 18 | + |
| 19 | +import java.lang.reflect.AnnotatedElement; |
| 20 | +import java.lang.reflect.Method; |
| 21 | + |
| 22 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 23 | +import org.aspectj.lang.annotation.Around; |
| 24 | +import org.aspectj.lang.annotation.Aspect; |
| 25 | +import org.aspectj.lang.annotation.Pointcut; |
| 26 | +import org.aspectj.lang.reflect.MethodSignature; |
| 27 | + |
| 28 | +import com.cloud.user.UserContext; |
| 29 | + |
| 30 | +@Aspect |
| 31 | +public class ActionEventInterceptor { |
| 32 | + |
| 33 | + public ActionEventInterceptor() { |
| 34 | + } |
| 35 | + |
| 36 | + @Pointcut(value="execution( * *(..))") |
| 37 | + public void anyMethod() { |
| 38 | + } |
| 39 | + |
| 40 | + @Around("anyMethod() && @annotation(ActionEvent)") |
| 41 | + public Object AroundAnyMethod(ProceedingJoinPoint call) throws Throwable { |
| 42 | + MethodSignature methodSignature = (MethodSignature)call.getSignature(); |
| 43 | + Method targetMethod = methodSignature.getMethod(); |
| 44 | + if(needToIntercept(targetMethod)) { |
| 45 | + EventVO event = interceptStart(targetMethod); |
| 46 | + |
| 47 | + boolean success = true; |
| 48 | + Object ret = null; |
| 49 | + try { |
| 50 | + ret = call.proceed(); |
| 51 | + } catch (Throwable e) { |
| 52 | + success = false; |
| 53 | + interceptException(targetMethod, event); |
| 54 | + throw e; |
| 55 | + } finally { |
| 56 | + if(success){ |
| 57 | + interceptComplete(targetMethod, event); |
| 58 | + } |
| 59 | + } |
| 60 | + return ret; |
| 61 | + } |
| 62 | + return call.proceed(); |
| 63 | + } |
| 64 | + |
| 65 | + public EventVO interceptStart(AnnotatedElement element) { |
| 66 | + EventVO event = null; |
| 67 | + Method method = (Method)element; |
| 68 | + ActionEvent actionEvent = method.getAnnotation(ActionEvent.class); |
| 69 | + if (actionEvent != null) { |
| 70 | + boolean async = actionEvent.async(); |
| 71 | + if(async){ |
| 72 | + UserContext ctx = UserContext.current(); |
| 73 | + long userId = ctx.getCallerUserId(); |
| 74 | + long accountId = ctx.getAccountId(); |
| 75 | + long startEventId = ctx.getStartEventId(); |
| 76 | + String eventDescription = actionEvent.eventDescription(); |
| 77 | + if(ctx.getEventDetails() != null){ |
| 78 | + eventDescription += ". "+ctx.getEventDetails(); |
| 79 | + } |
| 80 | + EventUtils.saveStartedEvent(userId, accountId, actionEvent.eventType(), eventDescription, startEventId); |
| 81 | + } |
| 82 | + } |
| 83 | + return event; |
| 84 | + } |
| 85 | + |
| 86 | + public void interceptComplete(AnnotatedElement element, EventVO event) { |
| 87 | + Method method = (Method)element; |
| 88 | + ActionEvent actionEvent = method.getAnnotation(ActionEvent.class); |
| 89 | + if (actionEvent != null) { |
| 90 | + UserContext ctx = UserContext.current(); |
| 91 | + long userId = ctx.getCallerUserId(); |
| 92 | + long accountId = ctx.getAccountId(); |
| 93 | + long startEventId = ctx.getStartEventId(); |
| 94 | + String eventDescription = actionEvent.eventDescription(); |
| 95 | + if(ctx.getEventDetails() != null){ |
| 96 | + eventDescription += ". "+ctx.getEventDetails(); |
| 97 | + } |
| 98 | + if(actionEvent.create()){ |
| 99 | + //This start event has to be used for subsequent events of this action |
| 100 | + startEventId = EventUtils.saveCreatedEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully created entity for "+eventDescription); |
| 101 | + ctx.setStartEventId(startEventId); |
| 102 | + } else { |
| 103 | + EventUtils.saveEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully completed "+eventDescription, startEventId); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public void interceptException(AnnotatedElement element, EventVO event) { |
| 109 | + Method method = (Method)element; |
| 110 | + ActionEvent actionEvent = method.getAnnotation(ActionEvent.class); |
| 111 | + if (actionEvent != null) { |
| 112 | + UserContext ctx = UserContext.current(); |
| 113 | + long userId = ctx.getCallerUserId(); |
| 114 | + long accountId = ctx.getAccountId(); |
| 115 | + long startEventId = ctx.getStartEventId(); |
| 116 | + String eventDescription = actionEvent.eventDescription(); |
| 117 | + if(ctx.getEventDetails() != null){ |
| 118 | + eventDescription += ". "+ctx.getEventDetails(); |
| 119 | + } |
| 120 | + if(actionEvent.create()){ |
| 121 | + long eventId = EventUtils.saveCreatedEvent(userId, accountId, EventVO.LEVEL_ERROR, actionEvent.eventType(), "Error while creating entity for "+eventDescription); |
| 122 | + ctx.setStartEventId(eventId); |
| 123 | + } else { |
| 124 | + EventUtils.saveEvent(userId, accountId, EventVO.LEVEL_ERROR, actionEvent.eventType(), "Error while "+eventDescription, startEventId); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private boolean needToIntercept(Method method) { |
| 130 | + ActionEvent actionEvent = method.getAnnotation(ActionEvent.class); |
| 131 | + if (actionEvent != null) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + return false; |
| 136 | + } |
| 137 | +} |
0 commit comments