01: /*
02: * $Id: MessageNotification.java 10789 2008-02-12 20:04:43Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.context.notification;
12:
13: import org.mule.DefaultMuleMessage;
14: import org.mule.api.MuleMessage;
15: import org.mule.api.context.notification.ServerNotification;
16: import org.mule.api.endpoint.ImmutableEndpoint;
17: import org.mule.api.transport.Connectable;
18:
19: import org.apache.commons.logging.Log;
20: import org.apache.commons.logging.LogFactory;
21:
22: /**
23: * These notifications are fire when either a message is received via an endpoint, or
24: * dispatcher of if a receive call is made on a dispatcher.
25: */
26: public class MessageNotification extends ServerNotification {
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
36: .getLog(MessageNotification.class);
37:
38: public static final int MESSAGE_RECEIVED = MESSAGE_EVENT_ACTION_START_RANGE + 1;
39: public static final int MESSAGE_DISPATCHED = MESSAGE_EVENT_ACTION_START_RANGE + 2;
40: public static final int MESSAGE_SENT = MESSAGE_EVENT_ACTION_START_RANGE + 3;
41: public static final int MESSAGE_REQUESTED = MESSAGE_EVENT_ACTION_START_RANGE + 4;
42:
43: static {
44: registerAction("received", MESSAGE_RECEIVED);
45: registerAction("dispatched", MESSAGE_DISPATCHED);
46: registerAction("sent", MESSAGE_SENT);
47: registerAction("requested", MESSAGE_REQUESTED);
48: }
49:
50: private ImmutableEndpoint endpoint;
51:
52: public MessageNotification(MuleMessage resource,
53: ImmutableEndpoint endpoint, String identifier, int action) {
54: super (cloneMessage(resource), action);
55: resourceIdentifier = identifier;
56: this .endpoint = endpoint;
57: }
58:
59: protected static MuleMessage cloneMessage(MuleMessage message) {
60: // TODO we probably need to support deep cloning here
61: synchronized (message) {
62: return new DefaultMuleMessage(message.getPayload(), message);
63: }
64: }
65:
66: protected String getPayloadToString() {
67: if (source instanceof Connectable) {
68: return ((Connectable) source).getConnectionDescription();
69: }
70: return source.toString();
71: }
72:
73: public String toString() {
74: return EVENT_NAME + "{action=" + getActionName(action)
75: + ", endpoint: " + endpoint.getEndpointURI()
76: + ", resourceId=" + resourceIdentifier + ", timestamp="
77: + timestamp + ", serverId=" + serverId + ", message: "
78: + source + "}";
79: }
80:
81: public ImmutableEndpoint getEndpoint() {
82: return endpoint;
83: }
84:
85: }
|