1
2
3
4
5
6
7
8
9
10
11 package org.mule.api.context.notification;
12
13 import org.mule.MuleServer;
14 import org.mule.endpoint.MuleEndpointURI;
15 import org.mule.util.ClassUtils;
16
17 import java.util.EventObject;
18 import java.util.Map;
19
20 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
21
22
23
24
25
26 public abstract class ServerNotification extends EventObject
27 {
28
29 public static final int NO_ACTION_ID = Integer.MIN_VALUE;
30 public static final String NO_ACTION_NAME = "none";
31
32 public static final String TYPE_INFO = "info";
33 public static final String TYPE_WARNING = "warn";
34 public static final String TYPE_ERROR = "error";
35 public static final String TYPE_FATAL = "fatal";
36
37 protected static final int CONTEXT_EVENT_ACTION_START_RANGE = 100;
38 protected static final int MODEL_EVENT_ACTION_START_RANGE = 200;
39 protected static final int SERVICE_EVENT_ACTION_START_RANGE = 300;
40 protected static final int SECURITY_EVENT_ACTION_START_RANGE = 400;
41 protected static final int MANAGEMENT_EVENT_ACTION_START_RANGE = 500;
42 protected static final int ADMIN_EVENT_ACTION_START_RANGE = 600;
43 protected static final int CONNECTION_EVENT_ACTION_START_RANGE = 700;
44 protected static final int MESSAGE_EVENT_ACTION_START_RANGE = 800;
45 protected static final int SPACE_EVENT_ACTION_START_RANGE = 900;
46 protected static final int REGISTRY_EVENT_ACTION_START_RANGE = 1000;
47 protected static final int EXCEPTION_EVENT_ACTION_START_RANGE = 1100;
48 protected static final int TRANSACTION_EVENT_ACTION_START_RANGE = 1200;
49 protected static final int ROUTING_EVENT_ACTION_START_RANGE = 1300;
50 protected static final int COMPONENT_EVENT_ACTION_START_RANGE = 1400;
51 protected static final int CUSTOM_EVENT_ACTION_START_RANGE = 100000;
52
53 public static final int NULL_ACTION = 0;
54 public static final Object NULL_MESSAGE = "";
55
56 public final String EVENT_NAME = ClassUtils.getClassName(getClass());
57
58 protected String serverId;
59
60 protected long timestamp;
61
62 protected int action = NULL_ACTION;
63 private static Map actionIdToName = new ConcurrentHashMap();
64 private static Map actionNameToId = new ConcurrentHashMap();
65
66
67
68
69
70
71
72
73 protected String resourceIdentifier = null;
74
75 public ServerNotification(Object message, int action)
76 {
77 this(message, action, null);
78 }
79
80 public ServerNotification(Object message, int action, String resourceIdentifier)
81 {
82 super((message == null ? NULL_MESSAGE : message));
83 this.action = action;
84 this.resourceIdentifier = resourceIdentifier;
85 if (MuleServer.getMuleContext().getRegistry() != null && null != message)
86 {
87 serverId = message.toString();
88 }
89 timestamp = System.currentTimeMillis();
90 }
91
92 public int getAction()
93 {
94 return action;
95 }
96
97 public String getServerId()
98 {
99 return serverId;
100 }
101
102 public String getResourceIdentifier()
103 {
104 return resourceIdentifier;
105 }
106
107 public long getTimestamp()
108 {
109 return timestamp;
110 }
111
112 public boolean isResourceIdentifierAnUri()
113 {
114 return MuleEndpointURI.isMuleUri(resourceIdentifier);
115 }
116
117 public String toString()
118 {
119 return EVENT_NAME + "{" + "action=" + getActionName(action) + ", resourceId=" + resourceIdentifier
120 + ", serverId=" + serverId + ", timestamp=" + timestamp + "}";
121 }
122
123 protected String getPayloadToString()
124 {
125 return source.toString();
126 }
127
128 public String getType()
129 {
130 return TYPE_INFO;
131 }
132
133 public String getActionName()
134 {
135 return getActionName(action);
136 }
137
138
139 protected static synchronized void registerAction(String name, int i)
140 {
141 String lowerCaseName = name.toLowerCase();
142 Integer id = new Integer(i);
143 if (actionNameToId.containsKey(lowerCaseName))
144 {
145 throw new IllegalStateException("Action " + name + " already registered");
146 }
147 if (actionIdToName.containsKey(id))
148 {
149 throw new IllegalStateException("Action id " + i + " already registered");
150 }
151 actionIdToName.put(id, lowerCaseName);
152 actionNameToId.put(lowerCaseName, id);
153 }
154
155 public static String getActionName(int action)
156 {
157 if (action == NO_ACTION_ID)
158 {
159 return NO_ACTION_NAME;
160 }
161 Integer key = new Integer(action);
162 if (actionIdToName.containsKey(key))
163 {
164 return (String) actionIdToName.get(key);
165 }
166 else
167 {
168 throw new IllegalArgumentException("No action with id: " + action);
169 }
170 }
171
172 public static int getActionId(String action)
173 {
174 String lowerCaseName = action.toLowerCase();
175 if (actionNameToId.containsKey(lowerCaseName))
176 {
177 return ((Integer) actionNameToId.get(lowerCaseName)).intValue();
178 }
179 else
180 {
181 throw new IllegalArgumentException("No action called: " + action);
182 }
183 }
184
185 }