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