KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > internal > notifications > MessageNotification


1 /*
2  * $Id: MessageNotification.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl.internal.notifications;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.impl.MuleMessage;
16 import org.mule.umo.UMOMessage;
17 import org.mule.umo.endpoint.UMOImmutableEndpoint;
18 import org.mule.umo.manager.UMOServerNotification;
19 import org.mule.umo.provider.UMOConnectable;
20
21 /**
22  * These notifications are fire when either a message is received via an endpoint, or
23  * dispatcher of if a receive call is made on a dispatcher.
24  */

25 public class MessageNotification extends UMOServerNotification
26 {
27     /**
28      * Serial version
29      */

30     private static final long serialVersionUID = -5118299601117624094L;
31
32     /**
33      * logger used by this class
34      */

35     protected static final Log logger = LogFactory.getLog(MessageNotification.class);
36
37     public static final int MESSAGE_RECEIVED = MESSAGE_EVENT_ACTION_START_RANGE + 1;
38     public static final int MESSAGE_DISPATCHED = MESSAGE_EVENT_ACTION_START_RANGE + 2;
39     public static final int MESSAGE_SENT = MESSAGE_EVENT_ACTION_START_RANGE + 3;
40     public static final int MESSAGE_REQUESTED = MESSAGE_EVENT_ACTION_START_RANGE + 4;
41
42     private static final transient String JavaDoc[] ACTIONS = new String JavaDoc[]{"received", "dispatched", "sent",
43         "requested"};
44
45     private UMOImmutableEndpoint endpoint;
46
47     public MessageNotification(UMOMessage resource,
48                                UMOImmutableEndpoint endpoint,
49                                String JavaDoc identifier,
50                                int action)
51     {
52         super(cloneMessage(resource), action);
53         resourceIdentifier = identifier;
54         this.endpoint = endpoint;
55
56     }
57
58     protected static UMOMessage cloneMessage(UMOMessage message)
59     {
60         // TODO we probably need to support deep cloning here
61
synchronized (message)
62         {
63             return new MuleMessage(message.getPayload(), message);
64         }
65     }
66
67     protected String JavaDoc getPayloadToString()
68     {
69         if (source instanceof UMOConnectable)
70         {
71             return ((UMOConnectable)source).getConnectionDescription();
72         }
73         return source.toString();
74     }
75
76     protected String JavaDoc getActionName(int action)
77     {
78         int i = action - MESSAGE_EVENT_ACTION_START_RANGE;
79         if (i - 1 > ACTIONS.length)
80         {
81             return String.valueOf(action);
82         }
83         return ACTIONS[i - 1];
84     }
85
86     public String JavaDoc toString()
87     {
88         return EVENT_NAME + "{action=" + getActionName(action) + ", endpoint: " + endpoint.getEndpointURI()
89                         + ", resourceId=" + resourceIdentifier + ", timestamp=" + timestamp + ", serverId="
90                         + serverId + ", message: " + source + "}";
91     }
92
93     public UMOImmutableEndpoint getEndpoint()
94     {
95         return endpoint;
96     }
97
98 }
99
Popular Tags