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 | 0 | super(type); |
33 | 0 | } |
34 | |
|
35 | |
public NotificationLifecycleObject(Class type, Class notificationClass) |
36 | |
{ |
37 | 0 | super(type); |
38 | |
|
39 | 0 | if (notificationClass==null) |
40 | |
{ |
41 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("notificationClass").toString()); |
42 | |
} |
43 | |
|
44 | |
|
45 | 0 | notificationClass = ClassUtils.initializeClass(notificationClass); |
46 | |
|
47 | 0 | if (!ServerNotification.class.isAssignableFrom(notificationClass)) |
48 | |
{ |
49 | 0 | throw new ClassCastException("Notification class must be of type: " + ServerNotification.class.getName() + ". Offending class is: " + notificationClass.getName()); |
50 | |
} |
51 | |
|
52 | 0 | ctor = ClassUtils.getConstructor(notificationClass, new Class[]{Object.class, String.class}); |
53 | 0 | if(ctor==null) |
54 | |
{ |
55 | 0 | throw new IllegalArgumentException("No constructor defined in Notification class: " + notificationClass + " with arguments (Object.class, String.class)"); |
56 | |
} |
57 | 0 | } |
58 | |
|
59 | |
public NotificationLifecycleObject(Class type, Class notificationClass, int preNotification, int postNotification) |
60 | |
{ |
61 | 0 | this(type, notificationClass); |
62 | 0 | setPreNotificationName(MuleContextNotification.getActionName(preNotification)); |
63 | 0 | setPostNotificationName(MuleContextNotification.getActionName(postNotification)); |
64 | 0 | } |
65 | |
|
66 | |
public String getPostNotificationName() |
67 | |
{ |
68 | 0 | return postNotificationName; |
69 | |
} |
70 | |
|
71 | |
public void setPostNotificationName(String postNotificationName) |
72 | |
{ |
73 | 0 | this.postNotificationName = postNotificationName; |
74 | 0 | } |
75 | |
|
76 | |
public String getPreNotificationName() |
77 | |
{ |
78 | 0 | return preNotificationName; |
79 | |
} |
80 | |
|
81 | |
public void setPreNotificationName(String preNotificationName) |
82 | |
{ |
83 | 0 | this.preNotificationName = preNotificationName; |
84 | 0 | } |
85 | |
|
86 | |
@Override |
87 | |
public void firePreNotification(MuleContext context) |
88 | |
{ |
89 | 0 | if(getPreNotificationName()!=null) |
90 | |
{ |
91 | 0 | setPreNotification(createNotification(context, getPreNotificationName())); |
92 | |
} |
93 | 0 | super.firePreNotification(context); |
94 | |
|
95 | 0 | } |
96 | |
|
97 | |
@Override |
98 | |
public void firePostNotification(MuleContext context) |
99 | |
{ |
100 | 0 | if(getPostNotificationName()!=null) |
101 | |
{ |
102 | 0 | setPostNotification(createNotification(context, getPostNotificationName())); |
103 | |
} |
104 | 0 | super.firePostNotification(context); |
105 | 0 | } |
106 | |
|
107 | |
protected ServerNotification createNotification(MuleContext context, String action) |
108 | |
{ |
109 | |
try |
110 | |
{ |
111 | 0 | return (ServerNotification)ctor.newInstance(context, action); |
112 | |
} |
113 | 0 | catch (Exception e) |
114 | |
{ |
115 | 0 | throw new MuleRuntimeException(CoreMessages.failedToCreate("Notification:" + action) ,e); |
116 | |
} |
117 | |
} |
118 | |
} |