1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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
17 package net.sf.ehcache.distribution;
18
19 import net.sf.ehcache.Element;
20
21 import java.io.IOException;
22 import java.io.Serializable;
23 import java.lang.ref.SoftReference;
24
25 /***
26 * An Event Message, in respect of a particular cache.
27 * <p/>
28 * The message is Serializable, so that it can be sent across the network.
29 * <p/>
30 * The value of an Element is referenced with a SoftReference, so that a
31 * value will fail to be delivered in preference to an OutOfMemory error.
32 *
33 * @author Greg Luck
34 * @version $Id: EventMessage.html 13146 2011-08-01 17:12:39Z oletizi $
35 */
36 public class EventMessage implements Serializable {
37
38
39
40
41 /***
42 * A put or update event.
43 */
44 public static final int PUT = 0;
45
46 /***
47 * A remove or invalidate event.
48 */
49 public static final int REMOVE = 1;
50
51
52 /***
53 * A removeAll, which removes all elements from a cache
54 */
55 public static final int REMOVE_ALL = 3;
56
57 private static final long serialVersionUID = -293616939110963629L;
58
59 /***
60 * The event component.
61 */
62 private final int event;
63
64 /***
65 * The element component. This is held by a SoftReference, so as to prevent
66 * out of memory errors.
67 */
68 private transient SoftReference elementSoftReference;
69 /***
70 * The key component.
71 */
72 private final Serializable key;
73
74
75 /***
76 * Used to check if the value has been GCed
77 */
78 private final boolean wasElementNotNull;
79
80
81 /***
82 * Full constructor.
83 *
84 * @param event
85 * @param key
86 * @param element
87 */
88 public EventMessage(int event, Serializable key, Element element) {
89 this.event = event;
90 this.key = key;
91
92 wasElementNotNull = element != null;
93 elementSoftReference = new SoftReference(element);
94 }
95
96 /***
97 * Gets the event.
98 *
99 * @return either {@link #PUT} or {@link #REMOVE}
100 */
101 public final int getEvent() {
102 return event;
103 }
104
105 /***
106 * @return the element component of the message. null if a {@link #REMOVE} event
107 */
108 public final Element getElement() {
109 return (Element) elementSoftReference.get();
110 }
111
112 /***
113 * @return the key component of the message. null if a {@link #PUT} event
114 */
115 public final Serializable getSerializableKey() {
116 return key;
117 }
118
119
120 /***
121 * @return true if because of SoftReference GC this EventMessage is no longer valid
122 */
123 public boolean isValid() {
124 if (!wasElementNotNull) {
125 return true;
126 } else {
127 return getElement() != null;
128 }
129 }
130
131 private void writeObject(java.io.ObjectOutputStream out) throws IOException {
132 out.defaultWriteObject();
133 Element element = getElement();
134 out.writeObject(element);
135 }
136
137 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
138 in.defaultReadObject();
139 Element element = (Element) in.readObject();
140 elementSoftReference = new SoftReference(element);
141 }
142 }
143