View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  package com.gridsystems.resource.events;
19  
20  import org.apache.log4j.Logger;
21  
22  import com.gridsystems.innergrid.kernel.KernelException;
23  import com.gridsystems.innergrid.kernel.event.Event;
24  import com.gridsystems.innergrid.kernel.genericutils.ApiUtils;
25  import com.gridsystems.resource.api.ResourceAssignment;
26  import com.gridsystems.resource.api.ResourceId;
27  import com.gridsystems.resource.api.ResourceProperties;
28  
29  /**
30   * Encapsulation for resource related events.
31   *
32   * @author Antonia Tugores
33   * @author Xmas
34   * @version 1.0
35   */
36  public class ResourceEvent implements Event<ResourceListener> {
37    /**
38     * Log writer instance.
39     */
40    private static Logger log = Logger.getLogger(ResourceEvent.class);
41  
42    /**
43     * Events of this type are notified after add operations.
44     */
45    public static final int EVENT_BEFORE_ADD = 0;
46  
47    /**
48     * Events of this type are notified after add operations.
49     */
50    public static final int EVENT_AFTER_ADD = 1;
51  
52    /**
53     * Events of this type are notified before add operations.
54     */
55    public static final int EVENT_AFTER_ASSIGNMENT_START = 2;
56  
57    /**
58     * Events of this type are notified after successful add operations.
59     */
60    public static final int EVENT_AFTER_ASSIGNMENT_FINISHED = 3;
61  
62    /**
63     * Events of this type are notified after interrupted add operations.
64     */
65    public static final int EVENT_AFTER_ASSIGNMENT_INTERRUPTED = 4;
66  
67    /**
68     * Events of this type are notified before delete operations.
69     */
70    public static final int EVENT_BEFORE_DELETE = 5;
71  
72    /**
73     * Events of this type are notified after delete operations.
74     */
75    public static final int EVENT_AFTER_DELETE = 6;
76  
77    /**
78     * Events of this type are notified on reset operations.
79     */
80    public static final int EVENT_RESET = 7;
81  
82    /**
83     * Events of this type are notified when the resource has been enabled.
84     */
85    public static final int EVENT_BEFORE_ENABLE = 8;
86  
87    /**
88     * Events of this type are notified when the resource has been enabled.
89     */
90    public static final int EVENT_AFTER_ENABLE = 9;
91  
92    /**
93     * Events of this type are notified when the resource has been enabled.
94     */
95    public static final int EVENT_BEFORE_DISABLE = 10;
96    /**
97     * Events of this type are notified when the resource has been disabled.
98     */
99    public static final int EVENT_AFTER_DISABLE = 11;
100 
101   /**
102    * Events of this type are notified when the resource has been enabled.
103    */
104   public static final int EVENT_BEFORE_ON_OVERLOAD_POLICY_CHANGE = 12;
105 
106   /**
107    * Events of this type are notified when the resource has been disabled.
108    */
109   public static final int EVENT_AFTER_ON_OVERLOAD_POLICY_CHANGE = 13;
110 
111   /**
112    * Events of this type are notified when the resource has been updated.
113    */
114   public static final int EVENT_BEFORE_UPDATE = 14;
115 
116   /**
117    * Events of this type are notified when the resource has been updated.
118    */
119   public static final int EVENT_AFTER_UPDATE = 15;
120 
121   /**
122    * Events of this type are notified when the resource keywords have been updated.
123    */
124   public static final int EVENT_BEFORE_KEYWORDS_CHANGE = 16;
125 
126   /**
127    * Events of this type are notified when the resource keywords have been updated.
128    */
129   public static final int EVENT_AFTER_KEYWORDS_CHANGE = 17;
130 
131   /**
132    * The event type.
133    */
134   private int type;
135 
136   /**
137    * The resource identifier.
138    */
139   private final ResourceId resourceId;
140 
141   /**
142    * The resource assignment.
143    */
144   private ResourceAssignment resourceAssignment;
145 
146   /**
147    * The Original resource properties.
148    */
149   private ResourceProperties oldResourceProperties;
150 
151   /**
152    * The New Resource properties.
153    */
154   private ResourceProperties newResourceProperties;
155 
156   /**
157    * Creates a new instance with the specified data.
158    *
159    * @param eventType The type of event
160    * @param resourceid The Resource this event is related to
161    *
162    */
163   public ResourceEvent(int eventType, ResourceId resourceid) {
164     this.type = eventType;
165     this.resourceId = resourceid;
166     this.resourceAssignment = null;
167     this.oldResourceProperties = null;
168     this.newResourceProperties = null;
169   }
170 
171   /**
172    * Creates a new instance with the specified data.
173    *
174    * @param eventType The type of event
175    * @param resourceid The Resource this event is related to
176    * @param resourceAssignment the resource assignment
177    */
178   public ResourceEvent(int eventType, ResourceId resourceid,
179                        ResourceAssignment resourceAssignment) {
180     this(eventType, resourceid);
181     this.resourceAssignment = resourceAssignment;
182   }
183 
184   /**
185    * Creates a new instance with the specified data.
186    *
187    * @param eventType The type of event
188    * @param resourceid The Resource this event is related to
189    * @param oldResourceProperties Actual resource properties
190    * @param newResourceProperties New resource properties
191    */
192   public ResourceEvent(int eventType, ResourceId resourceid,
193     ResourceProperties oldResourceProperties, ResourceProperties newResourceProperties) {
194     this.type = eventType;
195     this.resourceId = resourceid;
196     this.oldResourceProperties = oldResourceProperties;
197     this.newResourceProperties = newResourceProperties;
198   }
199 
200   /**
201    * Gets the Resource this event is related to.
202    *
203    * @return The Resource of this event
204    */
205   public ResourceId getResource() {
206     return this.resourceId;
207   }
208 
209   /**
210    * Gets the assignment for the resource this event is related to.
211    *
212    * @return a ResourceAssignment object
213    */
214   public ResourceAssignment getResourceAssignment() {
215     return this.resourceAssignment;
216   }
217 
218   /**
219    * Gets the type of this event.
220    *
221    * @return This event type
222    */
223   public int getType() {
224     return type;
225   }
226 
227   /**
228    * @param type The type to set.
229    */
230   public void setType(int type) {
231     this.type = type;
232   }
233 
234   /**
235    * Gets the source of this event.
236    * Silly method.
237    *
238    * @return This event source
239    */
240   public String getSource() {
241     return resourceId.getResourceName() + "(" + resourceId.getResourceType() + ")";
242   }
243 
244   /**
245    * Notifies a listener of this event. It calls the appropriate listener method,
246    * depending on this event type.
247    *
248    * @param listener the listener to notify
249    * @throws KernelException if the listener vetoes the operation (only
250    *                         in before-type events)
251    */
252   public void notify(ResourceListener listener) throws KernelException {
253     try {
254       switch (this.type) {
255         case EVENT_BEFORE_UPDATE:
256           listener.beforeUpdate(this);
257           break;
258         case EVENT_AFTER_UPDATE:
259           listener.afterUpdate(this);
260           break;
261         case ResourceEvent.EVENT_AFTER_ASSIGNMENT_START:
262           listener.afterAssignmentStart(this);
263           break;
264         case ResourceEvent.EVENT_AFTER_ASSIGNMENT_FINISHED:
265           listener.afterAssignmentFinished(this);
266           break;
267         case ResourceEvent.EVENT_AFTER_ASSIGNMENT_INTERRUPTED:
268           listener.afterAssignmentInterrupted(this);
269           break;
270         case ResourceEvent.EVENT_BEFORE_ADD:
271           listener.beforeAdd(this);
272           break;
273         case ResourceEvent.EVENT_AFTER_ADD:
274           listener.afterAdd(this);
275           break;
276         case ResourceEvent.EVENT_BEFORE_DELETE:
277           listener.beforeRemove(this);
278           break;
279         case ResourceEvent.EVENT_AFTER_DELETE:
280           listener.afterRemove(this);
281           break;
282         case ResourceEvent.EVENT_RESET:
283           listener.onReset(this);
284           break;
285         case ResourceEvent.EVENT_BEFORE_ENABLE:
286           listener.beforeEnabling(this);
287           break;
288         case ResourceEvent.EVENT_AFTER_ENABLE:
289           listener.afterEnabling(this);
290           break;
291         case ResourceEvent.EVENT_BEFORE_DISABLE:
292           listener.beforeDisabling(this);
293           break;
294         case ResourceEvent.EVENT_AFTER_DISABLE:
295           listener.afterDisabling(this);
296           break;
297         case ResourceEvent.EVENT_BEFORE_ON_OVERLOAD_POLICY_CHANGE:
298           listener.beforeOnOverloadPolicyChange(this);
299           break;
300         case ResourceEvent.EVENT_AFTER_ON_OVERLOAD_POLICY_CHANGE:
301           listener.afterOnOverloadPolicyChange(this);
302           break;
303         case EVENT_BEFORE_KEYWORDS_CHANGE:
304           listener.beforeKeywordsChange(this);
305           break;
306         case EVENT_AFTER_KEYWORDS_CHANGE:
307           listener.afterKeywordsChange(this);
308           break;
309         default:
310           break;
311       }
312     } catch (Exception e) {
313       switch (this.type) {
314         case ResourceEvent.EVENT_BEFORE_ADD:
315         case ResourceEvent.EVENT_BEFORE_DELETE:
316         case ResourceEvent.EVENT_BEFORE_ENABLE:
317         case ResourceEvent.EVENT_BEFORE_DISABLE:
318         case ResourceEvent.EVENT_BEFORE_UPDATE:
319           throw ApiUtils.processException(e);
320 
321         default:
322           log.error("Error while fire event of type " + this.type
323               + " for resource " + resourceId.getResourceName()
324               + " of type " + resourceId.getResourceType(), e);
325           break;
326       }
327     }
328   }
329 
330   /**
331    * @return Returns the original ResourceProperties.
332    */
333   public ResourceProperties getOldResourceProperties() {
334     return oldResourceProperties;
335   }
336 
337   /**
338    * @return Returns the new ResourceProperties.
339    */
340   public ResourceProperties getNewResourceProperties() {
341     return newResourceProperties;
342   }
343 
344 }