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
58 public static final int CUSTOM_EVENT_ACTION_START_RANGE = 100000;
59
60 public static final int NULL_ACTION = 0;
61 public static final Object NULL_MESSAGE = "";
62
63 public final String EVENT_NAME = ClassUtils.getClassName(getClass());
64
65 protected String serverId;
66
67 protected long timestamp;
68
69 protected int action = NULL_ACTION;
70
71 @SuppressWarnings("unchecked")
72 private static Map<Integer, String> actionIdToName = new ConcurrentHashMap();
73
74 @SuppressWarnings("unchecked")
75 private static Map<String, Integer> actionNameToId = new ConcurrentHashMap();
76
77
78
79
80
81
82
83
84 protected String resourceIdentifier = null;
85
86 protected transient MuleContext muleContext;
87
88 public ServerNotification(Object message, int action)
89 {
90 this(message, action, null);
91 }
92
93 public ServerNotification(Object message, int action, String resourceIdentifier)
94 {
95 super((message == null ? NULL_MESSAGE : message));
96 this.action = action;
97 this.resourceIdentifier = resourceIdentifier;
98 timestamp = System.currentTimeMillis();
99 }
100
101 public void setMuleContext(MuleContext context)
102 {
103 muleContext = context;
104 serverId = generateId(context);
105 }
106
107 protected static String generateId(MuleContext context)
108 {
109 MuleConfiguration conf = context.getConfiguration();
110 return String.format("%s.%s.%s",
111 conf.getDomainId(),
112 conf.getClusterId(),
113 conf.getId());
114 }
115
116 protected static MuleMessage cloneMessage(MuleMessage message)
117 {
118 if (message == null)
119 {
120 return null;
121 }
122 return new DefaultMuleMessage(message);
123 }
124
125 public int getAction()
126 {
127 return action;
128 }
129
130 public String getServerId()
131 {
132 return serverId;
133 }
134
135 public String getResourceIdentifier()
136 {
137 return resourceIdentifier;
138 }
139
140 public long getTimestamp()
141 {
142 return timestamp;
143 }
144
145 public boolean isResourceIdentifierAnUri()
146 {
147 return MuleEndpointURI.isMuleUri(resourceIdentifier);
148 }
149
150 @Override
151 public String toString()
152 {
153 return EVENT_NAME + "{" + "action=" + getActionName(action) + ", resourceId=" + resourceIdentifier
154 + ", serverId=" + serverId + ", timestamp=" + timestamp + "}";
155 }
156
157 protected String getPayloadToString()
158 {
159 return source.toString();
160 }
161
162 public String getType()
163 {
164 return TYPE_INFO;
165 }
166
167 public String getActionName()
168 {
169 return getActionName(action);
170 }
171
172 protected static synchronized void registerAction(String name, int i)
173 {
174 String lowerCaseName = name.toLowerCase();
175 Integer id = new Integer(i);
176 if (actionNameToId.containsKey(lowerCaseName))
177 {
178 throw new IllegalStateException("Action " + name + " already registered");
179 }
180 if (actionIdToName.containsKey(id))
181 {
182 throw new IllegalStateException("Action id " + i + " already registered");
183 }
184 actionIdToName.put(id, lowerCaseName);
185 actionNameToId.put(lowerCaseName, id);
186 }
187
188 public static String getActionName(int action)
189 {
190 if (action == NO_ACTION_ID)
191 {
192 return NO_ACTION_NAME;
193 }
194 Integer key = new Integer(action);
195 if (actionIdToName.containsKey(key))
196 {
197 return actionIdToName.get(key);
198 }
199 else
200 {
201 throw new IllegalArgumentException("No action with id: " + action);
202 }
203 }
204
205 public static int getActionId(String action)
206 {
207 String lowerCaseName = action.toLowerCase();
208 if (actionNameToId.containsKey(lowerCaseName))
209 {
210 return ((Integer) actionNameToId.get(lowerCaseName)).intValue();
211 }
212 else
213 {
214 throw new IllegalArgumentException("No action called: " + action);
215 }
216 }
217
218 }