View Javadoc

1   /*
2    * $Id: ServerNotification.java 23030 2011-09-26 18:02:33Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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  import java.util.concurrent.ConcurrentHashMap;
24  
25  /**
26   * <code>ServerNotification</code> is an event triggered by something happening in
27   * the Server itself such as the server starting or a service being registered.
28   */
29  public abstract class ServerNotification extends EventObject implements MuleContextAware
30  {
31  
32      public static final int NO_ACTION_ID = Integer.MIN_VALUE;
33      public static final String NO_ACTION_NAME = "none";
34  
35      public static final String TYPE_TRACE = "trace";
36      public static final String TYPE_INFO = "info";
37      public static final String TYPE_WARNING = "warn";
38      public static final String TYPE_ERROR = "error";
39      public static final String TYPE_FATAL = "fatal";
40  
41      protected static final int CONTEXT_EVENT_ACTION_START_RANGE = 100;
42      protected static final int MODEL_EVENT_ACTION_START_RANGE = 200;
43      protected static final int SERVICE_EVENT_ACTION_START_RANGE = 300;
44      protected static final int SECURITY_EVENT_ACTION_START_RANGE = 400;
45      protected static final int MANAGEMENT_EVENT_ACTION_START_RANGE = 500;
46      protected static final int ADMIN_EVENT_ACTION_START_RANGE = 600;
47      protected static final int CONNECTION_EVENT_ACTION_START_RANGE = 700;
48      protected static final int MESSAGE_EVENT_ACTION_START_RANGE = 800;
49      protected static final int MESSAGE_EVENT_END_ACTION_START_RANGE = 850;
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      private static Map<Integer, String> actionIdToName = new ConcurrentHashMap<Integer, String>();
73  
74      private static Map<String, Integer> actionNameToId = new ConcurrentHashMap<String, Integer>();
75  
76      /**
77       * The resourceIdentifier is used when firing inbound server notifications such
78       * as Admin notifications or other action notifications triggered by an external
79       * source Used to associate the event with a particular resource. For example, if
80       * the event was a ServiceNotification the resourceIdentifier could be the name
81       * of a particular service
82       */
83      protected String resourceIdentifier = null;
84  
85      protected transient MuleContext muleContext;
86  
87      public ServerNotification(Object message, int action)
88      {
89          this(message, action, null);
90      }
91  
92      public ServerNotification(Object message, int action, String resourceIdentifier)
93      {
94          super((message == null ? NULL_MESSAGE : message));
95          this.action = action;
96          this.resourceIdentifier = resourceIdentifier;
97          timestamp = System.currentTimeMillis();
98      }
99  
100     @Override
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                              context.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 actionNameToId.get(lowerCaseName).intValue();
211         }
212         else
213         {
214             throw new IllegalArgumentException("No action called: " + action);
215         }
216     }
217 
218 }