1
2
3
4
5
6
7
8
9
10 package org.mule.lifecycle;
11
12 import org.mule.api.MuleContext;
13 import org.mule.api.MuleRuntimeException;
14 import org.mule.api.context.notification.ServerNotification;
15 import org.mule.config.i18n.CoreMessages;
16 import org.mule.context.notification.MuleContextNotification;
17 import org.mule.util.ClassUtils;
18
19 import java.lang.reflect.Constructor;
20
21
22
23
24 public class NotificationLifecycleObject extends LifecycleObject
25 {
26 private String preNotificationName;
27 private String postNotificationName;
28 private Constructor ctor;
29
30 public NotificationLifecycleObject(Class type)
31 {
32 super(type);
33 }
34
35 public NotificationLifecycleObject(Class type, Class notificationClass)
36 {
37 super(type);
38
39 if (notificationClass==null)
40 {
41 throw new IllegalArgumentException(CoreMessages.objectIsNull("notificationClass").toString());
42 }
43
44
45 notificationClass = ClassUtils.initializeClass(notificationClass);
46
47 if (!ServerNotification.class.isAssignableFrom(notificationClass))
48 {
49 throw new IllegalArgumentException("Notification class must be of type: " + ServerNotification.class.getName());
50 }
51
52 ctor = ClassUtils.getConstructor(notificationClass, new Class[]{Object.class, String.class});
53 if(ctor==null)
54 {
55 throw new IllegalArgumentException("No constructor defined in Notification class: " + notificationClass + " with arguments (Object.class, String.class)");
56 }
57 }
58
59 public NotificationLifecycleObject(Class type, Class notificationClass, int preNotification, int postNotification)
60 {
61 this(type, notificationClass);
62 setPreNotificationName(MuleContextNotification.getActionName(preNotification));
63 setPostNotificationName(MuleContextNotification.getActionName(postNotification));
64 }
65
66 public String getPostNotificationName()
67 {
68 return postNotificationName;
69 }
70
71 public void setPostNotificationName(String postNotificationName)
72 {
73 this.postNotificationName = postNotificationName;
74 }
75
76 public String getPreNotificationName()
77 {
78 return preNotificationName;
79 }
80
81 public void setPreNotificationName(String preNotificationName)
82 {
83 this.preNotificationName = preNotificationName;
84 }
85
86
87 public void firePreNotification(MuleContext context)
88 {
89 if(getPreNotificationName()!=null)
90 {
91 setPreNotification(createNotification(context, getPreNotificationName()));
92 }
93 super.firePreNotification(context);
94
95 }
96
97
98 public void firePostNotification(MuleContext context)
99 {
100 if(getPostNotificationName()!=null)
101 {
102 setPostNotification(createNotification(context, getPostNotificationName()));
103 }
104 super.firePostNotification(context);
105 }
106
107 protected ServerNotification createNotification(MuleContext context, String action)
108 {
109 try
110 {
111 return (ServerNotification)ctor.newInstance(new Object[]{context, action});
112 }
113 catch (Exception e)
114 {
115 throw new MuleRuntimeException(CoreMessages.failedToCreate("Notification:" + action) ,e);
116 }
117 }
118 }