View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.api.context.notification;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.config.MuleConfiguration;
13  import org.mule.api.context.MuleContextAware;
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   * <code>ServerNotification</code> is an event triggered by something happening in
24   * the Server itself such as the server starting or a service being registered.
25   */
26  public abstract class ServerNotification extends EventObject implements MuleContextAware
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_TRACE = "trace";
33      public static final String TYPE_INFO = "info";
34      public static final String TYPE_WARNING = "warn";
35      public static final String TYPE_ERROR = "error";
36      public static final String TYPE_FATAL = "fatal";
37  
38      protected static final int CONTEXT_EVENT_ACTION_START_RANGE = 100;
39      protected static final int MODEL_EVENT_ACTION_START_RANGE = 200;
40      protected static final int SERVICE_EVENT_ACTION_START_RANGE = 300;
41      protected static final int SECURITY_EVENT_ACTION_START_RANGE = 400;
42      protected static final int MANAGEMENT_EVENT_ACTION_START_RANGE = 500;
43      protected static final int ADMIN_EVENT_ACTION_START_RANGE = 600;
44      protected static final int CONNECTION_EVENT_ACTION_START_RANGE = 700;
45      protected static final int MESSAGE_EVENT_ACTION_START_RANGE = 800;
46      protected static final int SPACE_EVENT_ACTION_START_RANGE = 900;
47      protected static final int REGISTRY_EVENT_ACTION_START_RANGE = 1000;
48      protected static final int EXCEPTION_EVENT_ACTION_START_RANGE = 1100;
49      protected static final int TRANSACTION_EVENT_ACTION_START_RANGE = 1200;
50      protected static final int ROUTING_EVENT_ACTION_START_RANGE = 1300;
51      protected static final int COMPONENT_EVENT_ACTION_START_RANGE = 1400;
52      protected static final int FLOW_CONSTRUCT_EVENT_ACTION_START_RANGE = 1500;
53      protected static final int MESSAGE_PROCESSOR_EVENT_ACTION_START_RANGE = 1600;
54  
55      public static final int CUSTOM_EVENT_ACTION_START_RANGE = 100000;
56  
57      public static final int NULL_ACTION = 0;
58      public static final Object NULL_MESSAGE = "";
59  
60      public final String EVENT_NAME = ClassUtils.getClassName(getClass());
61  
62      protected String serverId;
63  
64      protected long timestamp;
65  
66      protected int action = NULL_ACTION;
67  
68      @SuppressWarnings("unchecked")
69      private static Map<Integer, String> actionIdToName = new ConcurrentHashMap();
70  
71      @SuppressWarnings("unchecked")
72      private static Map<String, Integer> actionNameToId = new ConcurrentHashMap();
73  
74      /**
75       * The resourceIdentifier is used when firing inbound server notifications such
76       * as Admin notifications or other action notifications triggered by an external
77       * source Used to associate the event with a particular resource. For example, if
78       * the event was a ServiceNotification the resourceIdentifier could be the name
79       * of a particular service
80       */
81      protected String resourceIdentifier = null;
82  
83      protected transient MuleContext muleContext;
84  
85      public ServerNotification(Object message, int action)
86      {
87          this(message, action, null);
88      }
89  
90      public ServerNotification(Object message, int action, String resourceIdentifier)
91      {
92          super((message == null ? NULL_MESSAGE : message));
93          this.action = action;
94          this.resourceIdentifier = resourceIdentifier;
95          timestamp = System.currentTimeMillis();
96      }
97  
98      public void setMuleContext(MuleContext context)
99      {
100         muleContext = context;
101         serverId = generateId(context);
102     }
103 
104     protected static String generateId(MuleContext context)
105     {
106         MuleConfiguration conf = context.getConfiguration();
107         return String.format("%s.%s.%s",
108                              conf.getDomainId(),
109                              conf.getClusterId(),
110                              conf.getId());
111     }
112 
113     protected static MuleMessage cloneMessage(MuleMessage message)
114     {
115         if (message == null)
116         {
117             return null;
118         }
119         return new DefaultMuleMessage(message);
120     }
121 
122     public int getAction()
123     {
124         return action;
125     }
126 
127     public String getServerId()
128     {
129         return serverId;
130     }
131 
132     public String getResourceIdentifier()
133     {
134         return resourceIdentifier;
135     }
136 
137     public long getTimestamp()
138     {
139         return timestamp;
140     }
141 
142     public boolean isResourceIdentifierAnUri()
143     {
144         return MuleEndpointURI.isMuleUri(resourceIdentifier);
145     }
146 
147     @Override
148     public String toString()
149     {
150         return EVENT_NAME + "{" + "action=" + getActionName(action) + ", resourceId=" + resourceIdentifier
151                + ", serverId=" + serverId + ", timestamp=" + timestamp + "}";
152     }
153 
154     protected String getPayloadToString()
155     {
156         return source.toString();
157     }
158 
159     public String getType()
160     {
161         return TYPE_INFO;
162     }
163 
164     public String getActionName()
165     {
166         return getActionName(action);
167     }
168 
169     protected static synchronized void registerAction(String name, int i)
170     {
171         String lowerCaseName = name.toLowerCase();
172         Integer id = new Integer(i);
173         if (actionNameToId.containsKey(lowerCaseName))
174         {
175             throw new IllegalStateException("Action " + name + " already registered");
176         }
177         if (actionIdToName.containsKey(id))
178         {
179             throw new IllegalStateException("Action id " + i + " already registered");
180         }
181         actionIdToName.put(id, lowerCaseName);
182         actionNameToId.put(lowerCaseName, id);
183     }
184 
185     public static String getActionName(int action)
186     {
187         if (action == NO_ACTION_ID)
188         {
189             return NO_ACTION_NAME;
190         }
191         Integer key = new Integer(action);
192         if (actionIdToName.containsKey(key))
193         {
194             return actionIdToName.get(key);
195         }
196         else
197         {
198             throw new IllegalArgumentException("No action with id: " + action);
199         }
200     }
201 
202     public static int getActionId(String action)
203     {
204         String lowerCaseName = action.toLowerCase();
205         if (actionNameToId.containsKey(lowerCaseName))
206         {
207             return ((Integer) actionNameToId.get(lowerCaseName)).intValue();
208         }
209         else
210         {
211             throw new IllegalArgumentException("No action called: " + action);
212         }
213     }
214 
215 }