incubator-cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From muralire...@apache.org
Subject [3/5] git commit: refs/heads/events-framework - - remove Publish, Subscrie annotation as they are not used - merge ActionEvent callback into ActionEventUtils - static initialize event bus from component locator
Date Thu, 31 Jan 2013 14:08:38 GMT
- remove Publish, Subscrie annotation as they are not used
- merge ActionEvent callback into ActionEventUtils
- static initialize event bus from component locator


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/e439d982
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/e439d982
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/e439d982

Branch: refs/heads/events-framework
Commit: e439d9824f2068c2700636548327f80d0e39677d
Parents: 7079bc2
Author: Murali Reddy <murali.reddy@citrix.com>
Authored: Thu Jan 31 15:54:57 2013 +0530
Committer: Murali Reddy <murali.reddy@citrix.com>
Committed: Thu Jan 31 19:37:51 2013 +0530

----------------------------------------------------------------------
 .../framework/events/EventPublishCallback.java     |  102 ------
 .../cloudstack/framework/events/Publish.java       |   38 --
 .../cloudstack/framework/events/Subscribe.java     |   35 --
 .../configuration/DefaultInterceptorLibrary.java   |    8 +-
 .../src/com/cloud/event/ActionEventCallback.java   |  136 --------
 server/src/com/cloud/event/ActionEventUtils.java   |  271 ++++++++++-----
 server/src/com/cloud/event/AlertGenerator.java     |   49 ++--
 server/src/com/cloud/event/UsageEventUtils.java    |   38 +--
 .../com/cloud/network/NetworkStateListener.java    |   65 ++--
 .../storage/listener/VolumeStateListener.java      |   65 ++--
 server/src/com/cloud/vm/UserVmStateListener.java   |   71 ++--
 11 files changed, 332 insertions(+), 546 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/framework/events/src/org/apache/cloudstack/framework/events/EventPublishCallback.java
----------------------------------------------------------------------
diff --git a/framework/events/src/org/apache/cloudstack/framework/events/EventPublishCallback.java b/framework/events/src/org/apache/cloudstack/framework/events/EventPublishCallback.java
deleted file mode 100644
index 81c8ac4..0000000
--- a/framework/events/src/org/apache/cloudstack/framework/events/EventPublishCallback.java
+++ /dev/null
@@ -1,102 +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 org.apache.cloudstack.framework.events;
-
-import com.cloud.utils.component.Adapters;
-import com.cloud.utils.component.AnnotationInterceptor;
-import com.cloud.utils.component.ComponentLocator;
-import net.sf.cglib.proxy.Callback;
-import net.sf.cglib.proxy.MethodInterceptor;
-import net.sf.cglib.proxy.MethodProxy;
-
-import java.lang.reflect.AnnotatedElement;
-import java.lang.reflect.Method;
-import java.util.Enumeration;
-
-public class EventPublishCallback implements MethodInterceptor, AnnotationInterceptor<Publish> {
-
-    private static EventBus _eventBus = null;
-
-    @Override
-    public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
-        Publish 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;
-        Publish event = method.getAnnotation(Publish.class);
-        if (event != null) {
-            return true;
-        }
-        return false;
-    }
-
-    @Override
-    public Publish interceptStart(AnnotatedElement element) {
-        return null;
-    }
-
-    @Override
-    public void interceptComplete(AnnotatedElement element, Publish event) {
-        _eventBus = getEventBus();
-        if (_eventBus != null) {
-
-        }
-    }
-
-    @Override
-    public void interceptException(AnnotatedElement element, Publish attach) {
-        return;
-    }
-
-    @Override
-    public Callback getCallback() {
-        return this;
-    }
-
-    private EventBus getEventBus() {
-        if (_eventBus == null) {
-            ComponentLocator locator = ComponentLocator.getLocator("management-server");
-            Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
-            if (eventBusImpls != null) {
-                Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
-                _eventBus = eventBusenum.nextElement();
-            }
-        }
-        return _eventBus;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/framework/events/src/org/apache/cloudstack/framework/events/Publish.java
----------------------------------------------------------------------
diff --git a/framework/events/src/org/apache/cloudstack/framework/events/Publish.java b/framework/events/src/org/apache/cloudstack/framework/events/Publish.java
deleted file mode 100644
index 8f10972..0000000
--- a/framework/events/src/org/apache/cloudstack/framework/events/Publish.java
+++ /dev/null
@@ -1,38 +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 org.apache.cloudstack.framework.events;
-
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.ElementType.TYPE;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-@Target({ TYPE, METHOD })
-@Retention(RUNTIME)
-public @interface Publish {
-
-    String eventCategory();
-
-    String eventType();
-
-    String eventDescription();
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/framework/events/src/org/apache/cloudstack/framework/events/Subscribe.java
----------------------------------------------------------------------
diff --git a/framework/events/src/org/apache/cloudstack/framework/events/Subscribe.java b/framework/events/src/org/apache/cloudstack/framework/events/Subscribe.java
deleted file mode 100644
index 74997f6..0000000
--- a/framework/events/src/org/apache/cloudstack/framework/events/Subscribe.java
+++ /dev/null
@@ -1,35 +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 org.apache.cloudstack.framework.events;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
-
-@Target({METHOD })
-@Retention(RUNTIME)
-public @interface        Subscribe {
-	
-    String eventCategory();
-
-    String eventType();
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java b/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java
index 380552d..6feff4c 100644
--- a/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java
+++ b/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java
@@ -16,20 +16,20 @@
 // under the License.
 package com.cloud.configuration;
 
-import java.util.List;
-
-import com.cloud.event.ActionEventCallback;
+import com.cloud.event.ActionEventUtils;
 import com.cloud.utils.component.AnnotationInterceptor;
 import com.cloud.utils.component.InterceptorLibrary;
 import com.cloud.utils.db.DatabaseCallback;
 import org.apache.cloudstack.framework.events.EventPublishCallback;
 
+import java.util.List;
+
 public class DefaultInterceptorLibrary implements InterceptorLibrary {
 
     @Override
     public void addInterceptors(List<AnnotationInterceptor<?>> interceptors) {
         interceptors.add(new DatabaseCallback());
-        interceptors.add(new ActionEventCallback());
+        interceptors.add(new ActionEventUtils.ActionEventCallback());
         interceptors.add(new EventPublishCallback());
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/server/src/com/cloud/event/ActionEventCallback.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/event/ActionEventCallback.java b/server/src/com/cloud/event/ActionEventCallback.java
deleted file mode 100644
index 72ce418..0000000
--- a/server/src/com/cloud/event/ActionEventCallback.java
+++ /dev/null
@@ -1,136 +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.event;
-
-import com.cloud.user.UserContext;
-import com.cloud.utils.component.AnnotationInterceptor;
-import net.sf.cglib.proxy.Callback;
-import net.sf.cglib.proxy.MethodInterceptor;
-import net.sf.cglib.proxy.MethodProxy;
-import org.apache.log4j.Logger;
-
-import java.lang.reflect.AnnotatedElement;
-import java.lang.reflect.Method;
-
-public class ActionEventCallback implements MethodInterceptor, AnnotationInterceptor<EventVO> {
-
-    private static final Logger s_logger = Logger.getLogger(ActionEventCallback.class);
-
-    @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;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/server/src/com/cloud/event/ActionEventUtils.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/event/ActionEventUtils.java b/server/src/com/cloud/event/ActionEventUtils.java
index f7157ba..744f46f 100755
--- a/server/src/com/cloud/event/ActionEventUtils.java
+++ b/server/src/com/cloud/event/ActionEventUtils.java
@@ -22,145 +22,149 @@ 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.Adapters;
+import com.cloud.utils.component.AnnotationInterceptor;
 import com.cloud.utils.component.ComponentLocator;
+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;
 
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Method;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 
 public class ActionEventUtils {
 
+    private static EventDao _eventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(EventDao.class);
+    private static AccountDao _accountDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(AccountDao.class);
+    protected static UserDao _userDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UserDao.class);
     private static final Logger s_logger = Logger.getLogger(ActionEventUtils.class);
 
-    private static EventDao _eventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(EventDao.class);
-	private static AccountDao _accountDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(AccountDao.class);
-    protected static UserDao _userDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UserDao.class);;
+    // get the event bus provider if configured
     protected static EventBus _eventBus = null;
-    protected static boolean _eventBusProviderLoaded = false;
+
+    static {
+        Adapters<EventBus> eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class);
+        if (eventBusImpls != null) {
+            Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
+            if (eventBusenum != null && eventBusenum.hasMoreElements()) {
+                _eventBus = eventBusenum.nextElement(); // configure event bus if configured
+            }
+        }
+    }
 
     public static Long onActionEvent(Long userId, Long accountId, Long domainId, String type, String description) {
 
-        publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(),
-                type, com.cloud.event.Event.State.Scheduled);
+        publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(),
+                type, com.cloud.event.Event.State.Completed);
+
+        Event event = persistActionEvent(userId, accountId, domainId, null, type, Event.State.Completed,
+                description, null);
 
-        EventVO event = new EventVO();
-        event.setUserId(userId);
-        event.setAccountId(accountId);
-        event.setDomainId(domainId);
-        event.setType(type);
-        event.setDescription(description);
-        event = _eventDao.persist(event);
         return event.getId();
     }
-    
+
     /*
      * Save event after scheduling an async job
      */
-    public static Long onScheduledActionEvent(Long userId, Long accountId, String type, String description, long startEventId) {
+    public static Long onScheduledActionEvent(Long userId, Long accountId, String type, String description,
+                                              long startEventId) {
 
-        publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
+        publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
                 com.cloud.event.Event.State.Scheduled);
 
-        EventVO event = new EventVO();
-        event.setUserId(userId);
-        event.setAccountId(accountId);
-        event.setDomainId(getDomainId(accountId));
-        event.setType(type);
-        event.setStartId(startEventId);
-        event.setState(Event.State.Scheduled);
-        event.setDescription("Scheduled async job for "+description);
-        event = _eventDao.persist(event);
+        Event event = persistActionEvent(userId, accountId, null, null, type, Event.State.Scheduled,
+                description, startEventId);
+
         return event.getId();
     }
-    
+
     /*
      * Save event after starting execution of an async job
      */
-    public static Long onStartedActionEvent(Long userId, Long accountId, String type, String description, long startEventId) {
+    public static Long onStartedActionEvent(Long userId, Long accountId, String type, String description,
+                                            long startEventId) {
 
-        publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
+        publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
                 com.cloud.event.Event.State.Started);
 
-        EventVO event = new EventVO();
-        event.setUserId(userId);
-        event.setAccountId(accountId);
-        event.setDomainId(getDomainId(accountId));
-        event.setType(type);
-        event.setState(Event.State.Started);
-        event.setDescription("Starting job for "+description);
-        event.setStartId(startEventId);
-        event = _eventDao.persist(event);
-    	return event.getId();
-    }    
+        Event event = persistActionEvent(userId, accountId, null, null, type, Event.State.Started,
+                description, startEventId);
+        return event.getId();
+    }
 
-    public static Long onCompletedActionEvent(Long userId, Long accountId, String level, String type, String description, long startEventId) {
+    public static Long onCompletedActionEvent(Long userId, Long accountId, String level, String type,
+                                              String description, long startEventId) {
 
-        publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
+        publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
                 com.cloud.event.Event.State.Completed);
 
-        EventVO event = new EventVO();
-        event.setUserId(userId);
-        event.setAccountId(accountId);
-        event.setDomainId(getDomainId(accountId));
-        event.setType(type);
-        event.setDescription(description);
-        event.setLevel(level);
-        event.setStartId(startEventId);
-        event = _eventDao.persist(event);
-        return (event != null ? event.getId() : null);
+        Event event = persistActionEvent(userId, accountId, null, level, type, Event.State.Completed,
+                description, startEventId);
+
+        return event.getId();
     }
-    
+
     public static Long onCreatedActionEvent(Long userId, Long accountId, String level, String type, String description) {
 
-        publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
+        publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
                 com.cloud.event.Event.State.Created);
 
+        Event event = persistActionEvent(userId, accountId, null, level, type, Event.State.Created, description, null);
+
+        return event.getId();
+    }
+
+    private static Event persistActionEvent(Long userId, Long accountId, Long domainId, String level, String type,
+                                           Event.State state, String description, Long startEventId) {
         EventVO event = new EventVO();
         event.setUserId(userId);
         event.setAccountId(accountId);
-        event.setDomainId(getDomainId(accountId));
         event.setType(type);
-        event.setLevel(level);
-        event.setState(Event.State.Created);
+        event.setState(state);
         event.setDescription(description);
+        if (domainId != null) {
+            event.setDomainId(domainId);
+        } else {
+            event.setDomainId(getDomainId(accountId));
+        }
+        if (level != null && !level.isEmpty()) {
+            event.setLevel(level);
+        }
+        if (startEventId != null) {
+            event.setStartId(startEventId);
+        }
         event = _eventDao.persist(event);
-        return event.getId();
-    }
-
-    private static long getDomainId(long accountId){
-    	AccountVO account = _accountDao.findByIdIncludingRemoved(accountId);
-    	return account.getDomainId();
+        return event;
     }
 
-    public static void publishActionEvent(long userId, long accountId, String eventCategory,
+    private static void publishOnEventBus(long userId, long accountId, String eventCategory,
                                           String eventType, Event.State state) {
-
-        if (getEventBusProvider() == null) {
-            return; // no provider is configured to provider events bus, so just return
+        if (_eventBus == null) {
+            return; // no provider is configured to provide events bus, so just return
         }
 
+        org.apache.cloudstack.framework.events.Event event = new org.apache.cloudstack.framework.events.Event(
+                ManagementServer.Name,
+                eventCategory,
+                eventType,
+                EventTypes.getEntityForEvent(eventType), null);
+
         Map<String, String> eventDescription = new HashMap<String, String>();
         Account account = _accountDao.findById(accountId);
         User user = _userDao.findById(userId);
-
         eventDescription.put("user", user.getUuid());
         eventDescription.put("account", account.getUuid());
         eventDescription.put("event", eventType);
         eventDescription.put("status", state.toString());
-
-        String resourceType = EventTypes.getEntityForEvent(eventType);
-
-        org.apache.cloudstack.framework.events.Event event = new org.apache.cloudstack.framework.events.Event(
-                ManagementServer.Name,
-                eventCategory,
-                eventType,
-                resourceType, null);
         event.setDescription(eventDescription);
 
         try {
@@ -170,18 +174,115 @@ public class ActionEventUtils {
         }
     }
 
-    private static EventBus getEventBusProvider() {
-        if (!_eventBusProviderLoaded) {
-            ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
-            Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
-            if (eventBusImpls != null) {
-                Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
-                if (eventBusenum != null && eventBusenum.hasMoreElements()) {
-                    _eventBus = eventBusenum.nextElement();
+    private static long getDomainId(long accountId){
+        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);
                 }
             }
-            _eventBusProviderLoaded = true;
         }
-        return _eventBus;
+
+        @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;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/server/src/com/cloud/event/AlertGenerator.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/event/AlertGenerator.java b/server/src/com/cloud/event/AlertGenerator.java
index 640f604..4286377 100644
--- a/server/src/com/cloud/event/AlertGenerator.java
+++ b/server/src/com/cloud/event/AlertGenerator.java
@@ -34,17 +34,33 @@ import java.util.Map;
 public class AlertGenerator {
 
     private static final Logger s_logger = Logger.getLogger(AlertGenerator.class);
-    protected static EventBus _eventBus = null;
-    protected static boolean _eventBusProviderLoaded = false;
-
     private static DataCenterDao _dcDao =  ComponentLocator.getLocator(ManagementServer.Name).getDao(DataCenterDao.class);
     private static HostPodDao _podDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(HostPodDao.class);
 
-    public static void publishAlertOnEventBus(String alertType, long dataCenterId, Long podId, String subject, String body) {
-        if (getEventBusProvider() == null) {
+    // get the event bus provider if configured
+    protected static EventBus _eventBus = null;
+    static {
+        Adapters<EventBus> eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class);
+        if (eventBusImpls != null) {
+            Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
+            if (eventBusenum != null && eventBusenum.hasMoreElements()) {
+                _eventBus = eventBusenum.nextElement(); // configure event bus if configured
+            }
+        }
+    }
 
+    public static void publishAlertOnEventBus(String alertType, long dataCenterId, Long podId, String subject, String body) {
+        if (_eventBus == null) {
+            return; // no provider is configured to provider events bus, so just return
         }
 
+        org.apache.cloudstack.framework.events.Event event =
+                new org.apache.cloudstack.framework.events.Event(ManagementServer.Name,
+                        EventCategory.ALERT_EVENT.getName(),
+                        alertType,
+                        null,
+                        null);
+
         Map<String, String> eventDescription = new HashMap<String, String>();
         DataCenterVO dc = _dcDao.findById(dataCenterId);
         HostPodVO pod = _podDao.findById(podId);
@@ -55,38 +71,17 @@ public class AlertGenerator {
         } else {
             eventDescription.put("dataCenterId", null);
         }
-
         if (pod != null) {
             eventDescription.put("podId", pod.getUuid());
         } else {
             eventDescription.put("podId", null);
         }
-        org.apache.cloudstack.framework.events.Event event =
-                new org.apache.cloudstack.framework.events.Event(ManagementServer.Name,
-                        EventCategory.ALERT_EVENT.getName(),
-                        alertType,
-                        null,
-                        null);
         event.setDescription(eventDescription);
+
         try {
             _eventBus.publish(event);
         } catch (EventBusException e) {
             s_logger.warn("Failed to publish alert on the the event bus.");
         }
     }
-
-    private static EventBus getEventBusProvider() {
-        if (!_eventBusProviderLoaded) {
-            ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
-            Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
-            if (eventBusImpls != null) {
-                Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
-                if (eventBusenum != null && eventBusenum.hasMoreElements()) {
-                    _eventBus = eventBusenum.nextElement();
-                }
-            }
-            _eventBusProviderLoaded = true;
-        }
-        return _eventBus;
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/server/src/com/cloud/event/UsageEventUtils.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/event/UsageEventUtils.java b/server/src/com/cloud/event/UsageEventUtils.java
index ebe8f0c..904525e 100644
--- a/server/src/com/cloud/event/UsageEventUtils.java
+++ b/server/src/com/cloud/event/UsageEventUtils.java
@@ -22,10 +22,19 @@ public class UsageEventUtils {
     private static UsageEventDao _usageEventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UsageEventDao.class);
     private static AccountDao _accountDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(AccountDao.class);
     private static DataCenterDao _dcDao =  ComponentLocator.getLocator(ManagementServer.Name).getDao(DataCenterDao.class);
-
     private static final Logger s_logger = Logger.getLogger(UsageEventUtils.class);
+
+    // get the event bus provider if configured
     protected static EventBus _eventBus = null;
-    protected static boolean _eventBusLoaded = false;
+    static {
+        Adapters<EventBus> eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class);
+        if (eventBusImpls != null) {
+            Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
+            if (eventBusenum != null && eventBusenum.hasMoreElements()) {
+                _eventBus = eventBusenum.nextElement(); // configure event bus if configured
+            }
+        }
+    }
 
     public static void publishUsageEvent(String usageType, long accountId, long zoneId,
                                          long resourceId, String resourceName,
@@ -83,43 +92,28 @@ public class UsageEventUtils {
 
     private static void publishUsageEvent(String usageEventType, Long accountId, Long zoneId, String resourceType, String resourceUUID) {
 
-        if (getEventBusProvider() == null) {
+        if (_eventBus == null) {
             return; // no provider is configured to provider events bus, so just return
         }
 
         Account account = _accountDao.findById(accountId);
         DataCenterVO dc = _dcDao.findById(zoneId);
 
-        Map<String, String> eventDescription = new HashMap<String, String>();
+        Event event = new Event(ManagementServer.Name, EventCategory.USAGE_EVENT.getName(), usageEventType,
+                resourceType, resourceUUID);
 
+        Map<String, String> eventDescription = new HashMap<String, String>();
         eventDescription.put("account", account.getUuid());
         eventDescription.put("zone", dc.getUuid());
         eventDescription.put("event", usageEventType);
         eventDescription.put("resource", resourceType);
         eventDescription.put("id", resourceUUID);
-
-        Event event = new Event(ManagementServer.Name, EventCategory.USAGE_EVENT.getName(), usageEventType,
-                resourceType, resourceUUID);
         event.setDescription(eventDescription);
+
         try {
             _eventBus.publish(event);
         } catch (EventBusException e) {
             s_logger.warn("Failed to publish usage event on the the event bus.");
         }
     }
-
-    private static EventBus getEventBusProvider() {
-        if (!_eventBusLoaded) {
-            _eventBusLoaded = true;
-            ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
-            Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
-            if (eventBusImpls != null) {
-                Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
-                if (eventBusenum != null && eventBusenum.hasMoreElements()) {
-                    _eventBus = eventBusenum.nextElement();
-                }
-            }
-        }
-        return _eventBus;
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/server/src/com/cloud/network/NetworkStateListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkStateListener.java b/server/src/com/cloud/network/NetworkStateListener.java
index 7235fea..b0522b2 100644
--- a/server/src/com/cloud/network/NetworkStateListener.java
+++ b/server/src/com/cloud/network/NetworkStateListener.java
@@ -21,14 +21,24 @@ public class NetworkStateListener implements StateListener<State, Event, Network
 
     protected UsageEventDao _usageEventDao;
     protected NetworkDao _networkDao;
+
+    // get the event bus provider if configured
     protected static EventBus _eventBus = null;
+    static {
+        Adapters<EventBus> eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class);
+        if (eventBusImpls != null) {
+            Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
+            if (eventBusenum != null && eventBusenum.hasMoreElements()) {
+                _eventBus = eventBusenum.nextElement(); // configure event bus if configured
+            }
+        }
+    }
 
     private static final Logger s_logger = Logger.getLogger(NetworkStateListener.class);
 
     public NetworkStateListener(UsageEventDao usageEventDao, NetworkDao networkDao) {
         this._usageEventDao = usageEventDao;
         this._networkDao = networkDao;
-        initEventBusProvider();
     }
 
     @Override
@@ -44,25 +54,28 @@ public class NetworkStateListener implements StateListener<State, Event, Network
     }
 
     private void pubishOnEventBus(String event, String status, Network vo, State oldState, State newState) {
-        if (_eventBus != null) {
-            String resourceName = getEntityFromClassName(Network.class.getName());
-            org.apache.cloudstack.framework.events.Event eventMsg =  new org.apache.cloudstack.framework.events.Event(
-                    ManagementServer.Name,
-                    EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(),
-                    event,
-                    resourceName,
-                    vo.getUuid());
-            Map<String, String> eventDescription = new HashMap<String, String>();
-            eventDescription.put("resource", resourceName);
-            eventDescription.put("id", vo.getUuid());
-            eventDescription.put("old-state", oldState.name());
-            eventDescription.put("new-state", newState.name());
-            eventMsg.setDescription(eventDescription);
-            try {
-                _eventBus.publish(eventMsg);
-            } catch (EventBusException e) {
-                s_logger.warn("Failed to publish action event on the the event bus.");
-            }
+
+        if (_eventBus == null) {
+            return; // no provider is configured to provide events bus, so just return
+        }
+
+        String resourceName = getEntityFromClassName(Network.class.getName());
+        org.apache.cloudstack.framework.events.Event eventMsg =  new org.apache.cloudstack.framework.events.Event(
+                ManagementServer.Name,
+                EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(),
+                event,
+                resourceName,
+                vo.getUuid());
+        Map<String, String> eventDescription = new HashMap<String, String>();
+        eventDescription.put("resource", resourceName);
+        eventDescription.put("id", vo.getUuid());
+        eventDescription.put("old-state", oldState.name());
+        eventDescription.put("new-state", newState.name());
+        eventMsg.setDescription(eventDescription);
+        try {
+            _eventBus.publish(eventMsg);
+        } catch (EventBusException e) {
+            s_logger.warn("Failed to publish action event on the the event bus.");
         }
     }
 
@@ -74,16 +87,4 @@ public class NetworkStateListener implements StateListener<State, Event, Network
         }
         return entityName;
     }
-
-    private void initEventBusProvider() {
-        ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
-        Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
-        if (eventBusImpls != null) {
-            Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
-            if (eventBusenum != null && eventBusenum.hasMoreElements()) {
-                _eventBus = eventBusenum.nextElement();
-            }
-        }
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/server/src/com/cloud/storage/listener/VolumeStateListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/listener/VolumeStateListener.java b/server/src/com/cloud/storage/listener/VolumeStateListener.java
index da0ed11..46e2b97 100644
--- a/server/src/com/cloud/storage/listener/VolumeStateListener.java
+++ b/server/src/com/cloud/storage/listener/VolumeStateListener.java
@@ -18,12 +18,22 @@ import java.util.Map;
 
 public class VolumeStateListener implements StateListener<State, Event, Volume> {
 
+    // get the event bus provider if configured
     protected static EventBus _eventBus = null;
+    static {
+        Adapters<EventBus> eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class);
+        if (eventBusImpls != null) {
+            Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
+            if (eventBusenum != null && eventBusenum.hasMoreElements()) {
+                _eventBus = eventBusenum.nextElement(); // configure event bus if configured
+            }
+        }
+    }
 
     private static final Logger s_logger = Logger.getLogger(VolumeStateListener.class);
 
     public VolumeStateListener() {
-        initEventBusProvider();
+
     }
 
     @Override
@@ -39,25 +49,28 @@ public class VolumeStateListener implements StateListener<State, Event, Volume>
     }
 
     private void pubishOnEventBus(String event, String status, Volume vo, State oldState, State newState) {
-        if (_eventBus != null) {
-            String resourceName = getEntityFromClassName(Volume.class.getName());
-            org.apache.cloudstack.framework.events.Event eventMsg =  new org.apache.cloudstack.framework.events.Event(
-                    ManagementServer.Name,
-                    EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(),
-                    event,
-                    resourceName,
-                    vo.getUuid());
-            Map<String, String> eventDescription = new HashMap<String, String>();
-            eventDescription.put("resource", resourceName);
-            eventDescription.put("id", vo.getUuid());
-            eventDescription.put("old-state", oldState.name());
-            eventDescription.put("new-state", newState.name());
-            eventMsg.setDescription(eventDescription);
-            try {
-                _eventBus.publish(eventMsg);
-            } catch (EventBusException e) {
-                s_logger.warn("Failed to publish action event on the the event bus.");
-            }
+
+        if (_eventBus == null) {
+            return;  // no provider is configured to provide events bus, so just return
+        }
+
+        String resourceName = getEntityFromClassName(Volume.class.getName());
+        org.apache.cloudstack.framework.events.Event eventMsg =  new org.apache.cloudstack.framework.events.Event(
+                ManagementServer.Name,
+                EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(),
+                event,
+                resourceName,
+                vo.getUuid());
+        Map<String, String> eventDescription = new HashMap<String, String>();
+        eventDescription.put("resource", resourceName);
+        eventDescription.put("id", vo.getUuid());
+        eventDescription.put("old-state", oldState.name());
+        eventDescription.put("new-state", newState.name());
+        eventMsg.setDescription(eventDescription);
+        try {
+            _eventBus.publish(eventMsg);
+        } catch (EventBusException e) {
+            s_logger.warn("Failed to publish action event on the the event bus.");
         }
     }
 
@@ -69,16 +82,4 @@ public class VolumeStateListener implements StateListener<State, Event, Volume>
         }
         return entityName;
     }
-
-    private void initEventBusProvider() {
-        ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
-        Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
-        if (eventBusImpls != null) {
-            Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
-            if (eventBusenum != null && eventBusenum.hasMoreElements()) {
-                _eventBus = eventBusenum.nextElement();
-            }
-        }
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/e439d982/server/src/com/cloud/vm/UserVmStateListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/UserVmStateListener.java b/server/src/com/cloud/vm/UserVmStateListener.java
index 8f8e3fe..ab6b8a9 100644
--- a/server/src/com/cloud/vm/UserVmStateListener.java
+++ b/server/src/com/cloud/vm/UserVmStateListener.java
@@ -44,14 +44,24 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
     protected UsageEventDao _usageEventDao;
     protected NetworkDao _networkDao;
     protected NicDao _nicDao;
-    protected static EventBus _eventBus = null;
     private static final Logger s_logger = Logger.getLogger(UserVmStateListener.class);
 
+    // get the event bus provider if configured
+    protected static EventBus _eventBus = null;
+    static {
+        Adapters<EventBus> eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class);
+        if (eventBusImpls != null) {
+            Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
+            if (eventBusenum != null && eventBusenum.hasMoreElements()) {
+                _eventBus = eventBusenum.nextElement(); // configure event bus if configured
+            }
+        }
+    }
+
     public UserVmStateListener(UsageEventDao usageEventDao, NetworkDao networkDao, NicDao nicDao) {
         this._usageEventDao = usageEventDao;
         this._networkDao = networkDao;
         this._nicDao = nicDao;
-        initEventBusProvider();
     }
     
     @Override
@@ -69,7 +79,9 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
         if(vo.getType() != VirtualMachine.Type.User){
             return true;
         }
-        
+
+        pubishOnEventBus(event.name(), "postStateTransitionEvent", vo, oldState, newState);
+
         if (VirtualMachine.State.isVmCreated(oldState, event, newState)) {
             UsageEventUtils.saveUsageEvent(EventTypes.EVENT_VM_CREATE, vo.getAccountId(), vo.getDataCenterIdToDeployIn(), vo.getId(), vo.getHostName(), vo.getServiceOfferingId(),
                     vo.getTemplateId(), vo.getHypervisorType().toString());
@@ -91,26 +103,30 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
     }
 
     private void pubishOnEventBus(String event, String status, VirtualMachine vo, VirtualMachine.State oldState, VirtualMachine.State newState) {
-        if (_eventBus != null) {
-            String resourceName = getEntityFromClassName(Network.class.getName());
-            org.apache.cloudstack.framework.events.Event eventMsg =  new org.apache.cloudstack.framework.events.Event(
-                    ManagementServer.Name,
-                    EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(),
-                    event,
-                    resourceName,
-                    vo.getUuid());
-            Map<String, String> eventDescription = new HashMap<String, String>();
-            eventDescription.put("resource", resourceName);
-            eventDescription.put("id", vo.getUuid());
-            eventDescription.put("old-state", oldState.name());
-            eventDescription.put("new-state", newState.name());
-            eventMsg.setDescription(eventDescription);
-            try {
-                _eventBus.publish(eventMsg);
-            } catch (EventBusException e) {
-                s_logger.warn("Failed to publish action event on the the event bus.");
-            }
+
+        if (_eventBus == null) {
+            return; // no provider is configured to provide events bus, so just return
+        }
+
+        String resourceName = getEntityFromClassName(Network.class.getName());
+        org.apache.cloudstack.framework.events.Event eventMsg =  new org.apache.cloudstack.framework.events.Event(
+                ManagementServer.Name,
+                EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(),
+                event,
+                resourceName,
+                vo.getUuid());
+        Map<String, String> eventDescription = new HashMap<String, String>();
+        eventDescription.put("resource", resourceName);
+        eventDescription.put("id", vo.getUuid());
+        eventDescription.put("old-state", oldState.name());
+        eventDescription.put("new-state", newState.name());
+        eventMsg.setDescription(eventDescription);
+        try {
+            _eventBus.publish(eventMsg);
+        } catch (EventBusException e) {
+            s_logger.warn("Failed to publish action event on the the event bus.");
         }
+
     }
 
     private String getEntityFromClassName(String entityClassName) {
@@ -121,15 +137,4 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
         }
         return entityName;
     }
-
-    private void initEventBusProvider() {
-        ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
-        Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
-        if (eventBusImpls != null) {
-            Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
-            if (eventBusenum != null && eventBusenum.hasMoreElements()) {
-                _eventBus = eventBusenum.nextElement();
-            }
-        }
-    }
 }


Mime
View raw message