View Javadoc

1   /*
2    Copyright 2007 Ramon Servadei
3   
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7   
8    http://www.apache.org/licenses/LICENSE-2.0
9   
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15   */
16  package fulmine.distribution.events;
17  
18  import static fulmine.util.Utils.string;
19  
20  import java.util.Arrays;
21  
22  import fulmine.context.IFrameworkContext;
23  import fulmine.distribution.connection.IConnection;
24  import fulmine.event.AbstractEvent;
25  import fulmine.event.listener.IEventListener;
26  import fulmine.event.system.ISystemEvent;
27  import fulmine.event.system.ISystemEventSource;
28  import fulmine.model.container.IContainer;
29  
30  /**
31   * Raised by an {@link IConnection} and encapsulates a <code>byte[]</code>
32   * message received from a remote {@link IFrameworkContext}.
33   * <p>
34   * This implementation of an {@link ISystemEvent} can be raised by a non-
35   * {@link ISystemEventSource}. This is so that an {@link IEventListener} for an
36   * {@link IContainer} will not receive these events.
37   * 
38   * @author Ramon Servadei
39   */
40  public final class MessageEvent extends AbstractEvent implements ISystemEvent
41  {
42      /** The data */
43      private final byte[] data;
44  
45      /**
46       * Standard constructor to wrap the <code>byte[]</code> message from a
47       * remote {@link IFrameworkContext}
48       * 
49       * @param connection
50       *            the connection to the remote context, the source of the data
51       * @param data
52       *            the byte[] message received from a remote context
53       */
54      public MessageEvent(IConnection connection, byte[] data)
55      {
56          super();
57          this.setSource(connection);
58          this.data = data;
59      }
60  
61      /**
62       * Get the <code>byte[]</code> message from the remote context
63       * 
64       * @return a <code>byte[]</code> message from the remote context
65       */
66      public byte[] getData()
67      {
68          return this.data;
69      }
70  
71      /**
72       * Get the message from the remote context as a string
73       * 
74       * @return a string version of the message from the remote context
75       * 
76       */
77      public String getDataAsString()
78      {
79          return new String(this.data);
80      }
81  
82      /**
83       * Provides a trace string the shows the byte level contents of the data.
84       * Used for fine grain debugging.
85       * 
86       * @return a trace string
87       */
88      public String getTraceString()
89      {
90          return string(this, "data=" + Arrays.toString(getData()) + ", source="
91              + getSource());
92      }
93  
94      @Override
95      protected String getAdditionalToString()
96      {
97          return "data=[" + getData().length + " bytes]";
98      }
99  }