001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.distribution;
016:
017: import net.sf.ehcache.Element;
018:
019: import java.io.Serializable;
020: import java.io.IOException;
021: import java.lang.ref.SoftReference;
022:
023: /**
024: * An Event Message, in respect of a particular cache.
025: * <p/>
026: * The message is Serializable, so that it can be sent across the network.
027: * <p/>
028: * The value of an Element is referenced with a SoftReference, so that a
029: * value will fail to be delivered in preference to an OutOfMemory error.
030: *
031: * @author Greg Luck
032: * @version $Id: EventMessage.java 519 2007-07-27 07:11:45Z gregluck $
033: * @noinspection SerializableHasSerializationMethods
034: */
035: public class EventMessage implements Serializable {
036:
037: /**
038: * A put or update event.
039: */
040: public static final int PUT = 0;
041:
042: /**
043: * A remove or invalidate event.
044: */
045: public static final int REMOVE = 1;
046:
047: /**
048: * A removeAll, which removes all elements from a cache
049: */
050: public static final int REMOVE_ALL = 3;
051:
052: private static final long serialVersionUID = -293616939110963629L;
053:
054: /**
055: * The event component.
056: */
057: private final int event;
058:
059: /**
060: * The element component. This is held by a SoftReference, so as to prevent
061: * out of memory errors.
062: */
063: private transient SoftReference elementSoftReference;
064: /**
065: * The key component.
066: */
067: private final Serializable key;
068:
069: /**
070: * Used to check if the value has been GCed
071: */
072: private final boolean wasElementNotNull;
073:
074: /**
075: * Full constructor.
076: *
077: * @param event
078: * @param key
079: * @param element
080: */
081: public EventMessage(int event, Serializable key, Element element) {
082: this .event = event;
083: this .key = key;
084:
085: wasElementNotNull = element != null;
086: elementSoftReference = new SoftReference(element);
087: }
088:
089: /**
090: * Gets the event.
091: *
092: * @return either {@link #PUT} or {@link #REMOVE}
093: */
094: public final int getEvent() {
095: return event;
096: }
097:
098: /**
099: * @return the element component of the message. null if a {@link #REMOVE} event
100: */
101: public final Element getElement() {
102: return (Element) elementSoftReference.get();
103: }
104:
105: /**
106: * @return the key component of the message. null if a {@link #PUT} event
107: */
108: public final Serializable getSerializableKey() {
109: return key;
110: }
111:
112: /**
113: * @return true if because of SoftReference GC this EventMessage is no longer valid
114: */
115: public boolean isValid() {
116: if (!wasElementNotNull) {
117: return true;
118: } else {
119: return getElement() != null;
120: }
121: }
122:
123: private void writeObject(java.io.ObjectOutputStream out)
124: throws IOException {
125: out.defaultWriteObject();
126: Element element = getElement();
127: out.writeObject(element);
128: }
129:
130: private void readObject(java.io.ObjectInputStream in)
131: throws IOException, ClassNotFoundException {
132: in.defaultReadObject();
133: Element element = (Element) in.readObject();
134: elementSoftReference = new SoftReference(element);
135: }
136: }
|