diff --git a/api/src/com/cloud/event/ActionEvent.java b/api/src/com/cloud/event/ActionEvent.java index b890172ec77..1a705babbf7 100644 --- a/api/src/com/cloud/event/ActionEvent.java +++ b/api/src/com/cloud/event/ActionEvent.java @@ -17,5 +17,15 @@ */ package com.cloud.event; +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 ActionEvent { + boolean create() default false; } diff --git a/api/src/com/cloud/event/Event.java b/api/src/com/cloud/event/Event.java index 68d08a1a45e..8e89090ac58 100644 --- a/api/src/com/cloud/event/Event.java +++ b/api/src/com/cloud/event/Event.java @@ -21,6 +21,7 @@ import java.util.Date; public interface Event { public enum State { + Created, Scheduled, Started, Completed; diff --git a/client/tomcatconf/components.xml.in b/client/tomcatconf/components.xml.in index 5f1d7fd299e..84b56bebca6 100755 --- a/client/tomcatconf/components.xml.in +++ b/client/tomcatconf/components.xml.in @@ -23,7 +23,7 @@ documented, please contact the author. --> - + diff --git a/server/src/com/cloud/api/ApiDispatcher.java b/server/src/com/cloud/api/ApiDispatcher.java index 778c5602388..b6e56be5efb 100644 --- a/server/src/com/cloud/api/ApiDispatcher.java +++ b/server/src/com/cloud/api/ApiDispatcher.java @@ -64,56 +64,41 @@ public class ApiDispatcher { public void dispatchCreateCmd(BaseAsyncCreateCmd cmd, Map params) { - boolean created = false; String errorMsg = ""; - long startId = 0; - - if(cmd.getCreateEventType() != null){ - startId = cmd.saveStartedEvent(cmd.getCreateEventType(), cmd.getCreateEventDescription(), 0L); - } setupParameters(cmd, params); try { cmd.create(); - created = true; } catch (Throwable t) { if (t instanceof InvalidParameterValueException || t instanceof IllegalArgumentException) { s_logger.info(t.getMessage()); - errorMsg = "Parameter error"; throw new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage()); } else if (t instanceof PermissionDeniedException) { s_logger.info("PermissionDenied: " + t.getMessage()); - errorMsg = "Permission denied"; throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, t.getMessage()); } else if (t instanceof AccountLimitException) { s_logger.info(t.getMessage()); - errorMsg = "Account resource limit error"; throw new ServerApiException(BaseCmd.ACCOUNT_RESOURCE_LIMIT_ERROR, t.getMessage()); }else if (t instanceof InsufficientCapacityException) { s_logger.info(t.getMessage()); - errorMsg = "Insufficient capacity"; throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, t.getMessage()); } else if (t instanceof ResourceAllocationException) { s_logger.info(t.getMessage()); - errorMsg = "Resource allocation error"; throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, t.getMessage()); } else if (t instanceof ResourceUnavailableException) { s_logger.warn("Exception: ", t); - errorMsg = "Resource unavailable error"; throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, t.getMessage()); } else if (t instanceof AsyncCommandQueued) { throw (AsyncCommandQueued)t; } else if (t instanceof ServerApiException) { s_logger.warn(t.getClass() + " : " + ((ServerApiException) t).getDescription()); - errorMsg = ((ServerApiException) t).getDescription(); if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, errorMsg); } else { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE); } } else { - errorMsg = "Internal error"; s_logger.error("Exception while executing " + cmd.getClass().getSimpleName() + ":", t); if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, t.getMessage()); @@ -121,64 +106,42 @@ public class ApiDispatcher { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE); } } - } finally { - if(cmd.getCreateEventType() != null){ - if (created){ - cmd.saveCompletedEvent(EventVO.LEVEL_INFO, cmd.getCreateEventType(), cmd.getCreateEventDescription()+" successfull. Id: "+cmd.getEntityId(), startId); - } else { - cmd.saveCompletedEvent(EventVO.LEVEL_ERROR, cmd.getCreateEventType(), cmd.getCreateEventDescription()+" failed. "+errorMsg, startId); - } - } } } public void dispatch(BaseCmd cmd, Map params) { - boolean success = false; - String errorMsg = ""; setupParameters(cmd, params); try { - if(cmd instanceof BaseAsyncCmd){ - ((BaseAsyncCmd)cmd).saveStartedEvent(); - } cmd.execute(); - success = true; } catch (Throwable t) { if (t instanceof InvalidParameterValueException || t instanceof IllegalArgumentException) { s_logger.info(t.getMessage()); - errorMsg = "Parameter error"; throw new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage()); } else if (t instanceof PermissionDeniedException) { s_logger.info("PermissionDenied: " + t.getMessage()); - errorMsg = "Permission denied"; throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, t.getMessage()); } else if (t instanceof AccountLimitException) { s_logger.info(t.getMessage()); - errorMsg = "Account resource limit error"; throw new ServerApiException(BaseCmd.ACCOUNT_RESOURCE_LIMIT_ERROR, t.getMessage()); } else if (t instanceof InsufficientCapacityException) { s_logger.info(t.getMessage()); - errorMsg = "Insufficient capacity"; throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, t.getMessage()); } else if (t instanceof ResourceAllocationException) { s_logger.warn("Exception: ", t); - errorMsg = "Resource allocation error"; throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, t.getMessage()); } else if (t instanceof ResourceUnavailableException) { s_logger.warn("Exception: ", t); - errorMsg = "Resource unavailable error"; throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, t.getMessage()); } else if (t instanceof AsyncCommandQueued) { throw (AsyncCommandQueued)t; } else if (t instanceof ServerApiException) { - errorMsg = ((ServerApiException) t).getDescription(); s_logger.warn(t.getClass() + " : " + ((ServerApiException) t).getDescription()); if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, errorMsg); + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ((ServerApiException) t).getDescription()); } else { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE); } } else { - errorMsg = "Internal error"; s_logger.error("Exception while executing " + cmd.getClass().getSimpleName() + ":", t); if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, t.getMessage()); @@ -186,15 +149,6 @@ public class ApiDispatcher { throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE); } } - } finally { - if(cmd instanceof BaseAsyncCmd){ - BaseAsyncCmd asyncCmd = (BaseAsyncCmd)cmd; - if(success){ - asyncCmd.saveCompletedEvent(EventVO.LEVEL_INFO, asyncCmd.getEventDescription()+" completed successfully"); - } else { - asyncCmd.saveCompletedEvent(EventVO.LEVEL_ERROR, asyncCmd.getEventDescription()+" failed. "+errorMsg); - } - } } } diff --git a/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java b/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java index 846f8d0533b..8128c218434 100644 --- a/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java +++ b/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java @@ -19,6 +19,7 @@ package com.cloud.configuration; import java.util.List; +import com.cloud.event.ActionEventCallback; import com.cloud.utils.component.AnnotationInterceptor; import com.cloud.utils.component.InterceptorLibrary; import com.cloud.utils.db.DatabaseCallback; @@ -28,5 +29,6 @@ public class DefaultInterceptorLibrary implements InterceptorLibrary { @Override public void addInterceptors(List> interceptors) { interceptors.add(new DatabaseCallback()); + interceptors.add(new ActionEventCallback()); } } diff --git a/server/src/com/cloud/event/EventUtils.java b/server/src/com/cloud/event/EventUtils.java index 6810f0784c6..40fd1848843 100755 --- a/server/src/com/cloud/event/EventUtils.java +++ b/server/src/com/cloud/event/EventUtils.java @@ -40,7 +40,7 @@ public class EventUtils { event.setAccountId(accountId); event.setType(type); event.setState(Event.State.Started); - event.setDescription(description); + event.setDescription("Starting job for "+description); event.setStartId(startEventId); event = _eventDao.persist(event); return event.getId(); @@ -104,4 +104,16 @@ public class EventUtils { event = _eventDao.persist(event); return event.getId(); } + + public static Long saveCreatedEvent(Long userId, Long accountId, String level, String type, String description) { + EventVO event = new EventVO(); + event.setUserId(userId); + event.setAccountId(accountId); + event.setType(type); + event.setLevel(level); + event.setState(Event.State.Created); + event.setDescription(description); + event = _eventDao.persist(event); + return event.getId(); + } } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 0ec6d3b4642..536553b4211 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -163,6 +163,7 @@ import com.cloud.dc.dao.PodVlanMapDao; import com.cloud.dc.dao.VlanDao; import com.cloud.domain.DomainVO; import com.cloud.domain.dao.DomainDao; +import com.cloud.event.ActionEvent; import com.cloud.event.Event; import com.cloud.event.EventTypes; import com.cloud.event.EventUtils; @@ -2917,7 +2918,7 @@ public class ManagementServerImpl implements ManagementServer { } } - @Override + @Override @ActionEvent public boolean deleteDomain(DeleteDomainCmd cmd) throws InvalidParameterValueException, PermissionDeniedException { Account account = UserContext.current().getCaller(); Long domainId = cmd.getId(); diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index 774046f2a09..0ed7b773c63 100755 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -89,6 +89,7 @@ import com.cloud.dc.dao.HostPodDao; import com.cloud.deploy.DeployDestination; import com.cloud.domain.Domain; import com.cloud.domain.dao.DomainDao; +import com.cloud.event.ActionEvent; import com.cloud.event.Event; import com.cloud.event.EventTypes; import com.cloud.event.EventUtils; @@ -1569,7 +1570,7 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag /*Just allocate a volume in the database, don't send the createvolume cmd to hypervisor. The volume will be finally created only when it's attached to a VM.*/ - @Override + @Override @ActionEvent (create=true) public VolumeVO allocVolume(CreateVolumeCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, ResourceAllocationException { // FIXME: some of the scheduled event stuff might be missing here... Account account = UserContext.current().getCaller(); @@ -1727,7 +1728,7 @@ public class StorageManagerImpl implements StorageManager, StorageService, Manag return volume; } - @Override @DB + @Override @DB @ActionEvent public VolumeVO createVolume(CreateVolumeCmd cmd) { VolumeVO volume = _volsDao.findById(cmd.getEntityId()); // VolumeVO createdVolume = null; diff --git a/utils/src/com/cloud/utils/component/ComponentLocator.java b/utils/src/com/cloud/utils/component/ComponentLocator.java index 3bdaf05986a..6743bf7a222 100755 --- a/utils/src/com/cloud/utils/component/ComponentLocator.java +++ b/utils/src/com/cloud/utils/component/ComponentLocator.java @@ -853,7 +853,7 @@ public class ComponentLocator implements ComponentLocatorMBean { @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { - if (qName.equals("interceptors") && s_interceptors.size() == 0) { + if (qName.equals("interceptor") && s_interceptors.size() == 0) { synchronized(s_interceptors){ if (s_interceptors.size() == 0) { String libraryName = getAttribute(atts, "library");