View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
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.Serializable;
22  import java.io.IOException;
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.java 512 2007-07-10 09:18:45Z gregluck $
35   * @noinspection SerializableHasSerializationMethods
36   */
37  public class EventMessage implements Serializable {
38  
39  
40  
41  
42      /**
43       * A put or update event.
44       */
45      public static final int PUT = 0;
46  
47      /**
48       * A remove or invalidate event.
49       */
50      public static final int REMOVE = 1;
51  
52  
53      /**
54       * A removeAll, which removes all elements from a cache
55       */
56      public static final int REMOVE_ALL = 3;
57  
58      private static final long serialVersionUID = -293616939110963629L;
59  
60      /**
61       * The event component.
62       */
63      private final int event;
64  
65      /**
66       * The element component. This is held by a SoftReference, so as to prevent
67       * out of memory errors.
68       */
69      private transient SoftReference elementSoftReference;
70      /**
71       * The key component.
72       */
73      private final Serializable key;
74  
75  
76      /**
77       * Used to check if the value has been GCed
78       */
79      private final boolean wasElementNotNull;
80  
81  
82      /**
83       * Full constructor.
84       *
85       * @param event
86       * @param key
87       * @param element
88       */
89      public EventMessage(int event, Serializable key, Element element) {
90          this.event = event;
91          this.key = key;
92  
93          wasElementNotNull = element != null;
94          elementSoftReference = new SoftReference(element);
95      }
96  
97      /**
98       * Gets the event.
99       *
100      * @return either {@link #PUT} or {@link #REMOVE}
101      */
102     public final int getEvent() {
103         return event;
104     }
105 
106     /**
107      * @return the element component of the message. null if a {@link #REMOVE} event
108      */
109     public final Element getElement() {
110         return (Element) elementSoftReference.get();
111     }
112 
113     /**
114      * @return the key component of the message. null if a {@link #PUT} event
115      */
116     public final Serializable getSerializableKey() {
117         return key;
118     }
119 
120 
121     /**
122      * @return true if because of SoftReference GC this EventMessage is no longer valid
123      */
124     public boolean isValid() {
125         if (!wasElementNotNull) {
126             return true;
127         } else {
128             return getElement() != null;
129         }
130     }
131 
132     private void writeObject(java.io.ObjectOutputStream out) throws IOException {
133         out.defaultWriteObject();
134         Element element = getElement();
135         out.writeObject(element);
136     }
137 
138     private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
139         in.defaultReadObject();
140         Element element = (Element) in.readObject();
141         elementSoftReference = new SoftReference(element);
142     }
143 }
144