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