Coverage Report - org.mule.api.context.notification.ServerNotification
 
Classes in this File Line Coverage Branch Coverage Complexity
ServerNotification
77%
33/43
56%
9/16
1.929
 
 1  
 /*
 2  
  * $Id: ServerNotification.java 12247 2008-07-07 21:25:01Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.MuleServer;
 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
 24  
  * in the Server itself such as the server starting or a service being registered.
 25  
  */
 26  
 public abstract class ServerNotification extends EventObject
 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_INFO = "info";
 33  
     public static final String TYPE_WARNING = "warn";
 34  
     public static final String TYPE_ERROR = "error";
 35  
     public static final String TYPE_FATAL = "fatal";
 36  
 
 37  
     protected static final int CONTEXT_EVENT_ACTION_START_RANGE = 100;
 38  
     protected static final int MODEL_EVENT_ACTION_START_RANGE = 200;
 39  
     protected static final int SERVICE_EVENT_ACTION_START_RANGE = 300;
 40  
     protected static final int SECURITY_EVENT_ACTION_START_RANGE = 400;
 41  
     protected static final int MANAGEMENT_EVENT_ACTION_START_RANGE = 500;
 42  
     protected static final int ADMIN_EVENT_ACTION_START_RANGE = 600;
 43  
     protected static final int CONNECTION_EVENT_ACTION_START_RANGE = 700;
 44  
     protected static final int MESSAGE_EVENT_ACTION_START_RANGE = 800;
 45  
     protected static final int SPACE_EVENT_ACTION_START_RANGE = 900;
 46  
     protected static final int REGISTRY_EVENT_ACTION_START_RANGE = 1000;
 47  
     protected static final int EXCEPTION_EVENT_ACTION_START_RANGE = 1100;
 48  
     protected static final int TRANSACTION_EVENT_ACTION_START_RANGE = 1200;
 49  
     protected static final int ROUTING_EVENT_ACTION_START_RANGE = 1300;
 50  
     protected static final int COMPONENT_EVENT_ACTION_START_RANGE = 1400;
 51  
     protected static final int CUSTOM_EVENT_ACTION_START_RANGE = 100000;
 52  
 
 53  
     public static final int NULL_ACTION = 0;
 54  2
     public static final Object NULL_MESSAGE = "";
 55  
 
 56  10926
     public final String EVENT_NAME = ClassUtils.getClassName(getClass());
 57  
 
 58  
     protected String serverId;
 59  
 
 60  
     protected long timestamp;
 61  
 
 62  10926
     protected int action = NULL_ACTION;
 63  2
     private static Map actionIdToName = new ConcurrentHashMap();
 64  2
     private static Map actionNameToId = new ConcurrentHashMap();
 65  
 
 66  
     /**
 67  
      * The resourceIdentifier is used when firing inbound server notifications such
 68  
      * as Admin notifications or other action notifications triggered by an external
 69  
      * source Used to associate the event with a particular resource. For example, if
 70  
      * the event was a ServiceNotification the resourceIdentifier could be the name
 71  
      * of a particular service
 72  
      */
 73  10926
     protected String resourceIdentifier = null;
 74  
 
 75  
     public ServerNotification(Object message, int action)
 76  
     {
 77  10922
         this(message, action, null);
 78  10922
     }
 79  
 
 80  
     public ServerNotification(Object message, int action, String resourceIdentifier)
 81  
     {
 82  10926
         super((message == null ? NULL_MESSAGE : message));
 83  10926
         this.action = action;
 84  10926
         this.resourceIdentifier = resourceIdentifier;
 85  10926
         if (MuleServer.getMuleContext().getRegistry() != null && null != message)
 86  
         {
 87  9782
             serverId = message.toString();
 88  
         }
 89  10926
         timestamp = System.currentTimeMillis();
 90  10926
     }
 91  
 
 92  
     public int getAction()
 93  
     {
 94  198
         return action;
 95  
     }
 96  
 
 97  
     public String getServerId()
 98  
     {
 99  0
         return serverId;
 100  
     }
 101  
 
 102  
     public String getResourceIdentifier()
 103  
     {
 104  108
         return resourceIdentifier;
 105  
     }
 106  
 
 107  
     public long getTimestamp()
 108  
     {
 109  0
         return timestamp;
 110  
     }
 111  
 
 112  
     public boolean isResourceIdentifierAnUri()
 113  
     {
 114  0
         return MuleEndpointURI.isMuleUri(resourceIdentifier);
 115  
     }
 116  
 
 117  
     public String toString()
 118  
     {
 119  4
         return EVENT_NAME + "{" + "action=" + getActionName(action) + ", resourceId=" + resourceIdentifier
 120  
                 + ", serverId=" + serverId + ", timestamp=" + timestamp + "}";
 121  
     }
 122  
 
 123  
     protected String getPayloadToString()
 124  
     {
 125  0
         return source.toString();
 126  
     }
 127  
 
 128  
     public String getType()
 129  
     {
 130  0
         return TYPE_INFO;
 131  
     }
 132  
 
 133  
     public String getActionName()
 134  
     {
 135  8
         return getActionName(action);
 136  
     }
 137  
 
 138  
 
 139  
     protected static synchronized void registerAction(String name, int i)
 140  
     {
 141  76
         String lowerCaseName = name.toLowerCase();
 142  76
         Integer id = new Integer(i);
 143  76
         if (actionNameToId.containsKey(lowerCaseName))
 144  
         {
 145  0
             throw new IllegalStateException("Action " + name + " already registered");
 146  
         }
 147  76
         if (actionIdToName.containsKey(id))
 148  
         {
 149  0
             throw new IllegalStateException("Action id " + i + " already registered");
 150  
         }
 151  76
         actionIdToName.put(id, lowerCaseName);
 152  76
         actionNameToId.put(lowerCaseName, id);
 153  76
     }
 154  
 
 155  
     public static String getActionName(int action)
 156  
     {
 157  6980
         if (action == NO_ACTION_ID)
 158  
         {
 159  0
             return NO_ACTION_NAME;
 160  
         }
 161  6980
         Integer key = new Integer(action);
 162  6980
         if (actionIdToName.containsKey(key))
 163  
         {
 164  6980
             return (String) actionIdToName.get(key);
 165  
         }
 166  
         else
 167  
         {
 168  0
             throw new IllegalArgumentException("No action with id: " + action);
 169  
         }
 170  
     }
 171  
 
 172  
     public static int getActionId(String action)
 173  
     {
 174  184
         String lowerCaseName = action.toLowerCase();
 175  184
         if (actionNameToId.containsKey(lowerCaseName))
 176  
         {
 177  184
             return ((Integer) actionNameToId.get(lowerCaseName)).intValue();
 178  
         }
 179  
         else
 180  
         {
 181  0
             throw new IllegalArgumentException("No action called: " + action);
 182  
         }
 183  
     }
 184  
 
 185  
 }