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