]> git.argeo.org Git - gpl/argeo-slc.git/blob - EventPublisherAspect.java
a2fb8b4e0c9fd8164ee252b4028f09808654c00b
[gpl/argeo-slc.git] / EventPublisherAspect.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.services;
17
18 import java.util.Iterator;
19 import java.util.List;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.argeo.slc.msg.MsgConstants;
24 import org.argeo.slc.msg.event.SlcEvent;
25 import org.argeo.slc.msg.event.SlcEventPublisher;
26 import org.argeo.slc.msg.process.SlcExecutionStatusRequest;
27 import org.argeo.slc.process.SlcExecution;
28 import org.argeo.slc.runtime.SlcAgentDescriptor;
29 import org.aspectj.lang.JoinPoint;
30 import org.aspectj.lang.annotation.After;
31 import org.aspectj.lang.annotation.Aspect;
32
33 @Aspect
34 public class EventPublisherAspect {
35 public final static String EVT_AGENT_REGISTERED = "agentRegistered";
36 public final static String EVT_AGENT_UNREGISTERED = "agentUnregistered";
37 public final static String EVT_NEW_SLC_EXECUTION = "newSlcExecution";
38 public final static String EVT_UPDATE_SLC_EXECUTION_STATUS = "updateSlcExecutionStatus";
39
40 private final static Log log = LogFactory
41 .getLog(EventPublisherAspect.class);
42
43 private List<SlcEventPublisher> eventPublishers;
44
45 @After("execution(void org.argeo.slc.services.AgentService.register(..))")
46 public void registerAgent(JoinPoint jp) throws Throwable {
47 SlcAgentDescriptor agentDescriptor = (SlcAgentDescriptor) jp.getArgs()[0];
48 SlcEvent event = new SlcEvent(EVT_AGENT_REGISTERED);
49 event.getHeaders().put(MsgConstants.PROPERTY_SLC_AGENT_ID,
50 agentDescriptor.getUuid());
51 publishEvent(event);
52 }
53
54 @After("execution(void org.argeo.slc.services.AgentService.unregister(..))")
55 public void unregisterAgent(JoinPoint jp) throws Throwable {
56 SlcAgentDescriptor agentDescriptor = (SlcAgentDescriptor) jp.getArgs()[0];
57 SlcEvent event = new SlcEvent(EVT_AGENT_UNREGISTERED);
58 event.getHeaders().put(MsgConstants.PROPERTY_SLC_AGENT_ID,
59 agentDescriptor.getUuid());
60 publishEvent(event);
61 }
62
63 @After("execution(void org.argeo.slc.services.SlcExecutionService.newExecution(..))")
64 public void newSlcExecution(JoinPoint jp) throws Throwable {
65 SlcExecution slcExecution = (SlcExecution) jp.getArgs()[0];
66 SlcEvent event = new SlcEvent(EVT_NEW_SLC_EXECUTION);
67 event.getHeaders().put(MsgConstants.PROPERTY_SLC_EXECUTION_ID,
68 slcExecution.getUuid());
69 publishEvent(event);
70 }
71
72 @After("execution(void org.argeo.slc.services.SlcExecutionService.updateStatus(..))")
73 public void updateSlcExecutionStatus(JoinPoint jp) throws Throwable {
74 SlcExecutionStatusRequest msg = (SlcExecutionStatusRequest) jp
75 .getArgs()[0];
76 SlcEvent event = new SlcEvent(EVT_UPDATE_SLC_EXECUTION_STATUS);
77 event.getHeaders().put(MsgConstants.PROPERTY_SLC_EXECUTION_ID,
78 msg.getSlcExecutionUuid());
79 event.getHeaders().put(MsgConstants.PROPERTY_SLC_EXECUTION_STATUS,
80 msg.getNewStatus());
81 publishEvent(event);
82 }
83
84 public void setEventPublishers(List<SlcEventPublisher> eventPublishers) {
85 this.eventPublishers = eventPublishers;
86 }
87
88 protected void publishEvent(SlcEvent event) {
89
90 for (Iterator<SlcEventPublisher> it = eventPublishers.iterator(); it
91 .hasNext();) {
92 SlcEventPublisher eventPublisher = it.next();
93 if (log.isTraceEnabled())
94 log.debug("Publish event: "
95 + event.getHeaders().get(SlcEvent.EVENT_TYPE) + " to "
96 + eventPublisher);
97 eventPublisher.publish(event);
98 }
99 }
100 }