001: /**
002: * $RCSfile$
003: * $Revision: $
004: * $Date: $
005: *
006: * Copyright (C) 2007 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.util.cache;
011:
012: import java.io.*;
013: import java.util.Collection;
014: import java.util.List;
015: import java.util.Map;
016: import java.util.Set;
017:
018: /**
019: * Utility methods to assist in working with the Externalizable interfaces. This class
020: * is only used when running inside of a Cluser. When using the open source version
021: * this class will use a dummy implementation. Anyway, this class is not used when
022: * not using the Enterprise edition.<p>
023: *
024: * ExternalizableLite is very similar to the standard Externalizable interface, except that
025: * it uses DataOutput/DataInput instead of the Object stream equivalents.
026: *
027: * @author Gaston Dombiak
028: */
029: public class ExternalizableUtil {
030:
031: private static ExternalizableUtil instance = new ExternalizableUtil();
032:
033: private ExternalizableUtilStrategy strategy = new DummyExternalizableUtil();
034:
035: static {
036: instance = new ExternalizableUtil();
037: }
038:
039: public static ExternalizableUtil getInstance() {
040: return instance;
041: }
042:
043: /**
044: * Sets the implementation to use for serializing and deserializing
045: * objects.
046: *
047: * @param strategy the new strategy to use.
048: */
049: public void setStrategy(ExternalizableUtilStrategy strategy) {
050: this .strategy = strategy;
051: }
052:
053: /**
054: * Returns the implementation to use for serializing and deserializing
055: * objects.
056: *
057: * @return the implementation to use for serializing and deserializing
058: * objects.
059: */
060: public ExternalizableUtilStrategy getStrategy() {
061: return strategy;
062: }
063:
064: /**
065: * Hidding constructor. We only want one single instance.
066: */
067: private ExternalizableUtil() {
068: super ();
069: }
070:
071: /**
072: * Writes a Map of String key and value pairs. This method handles the
073: * case when the Map is <tt>null</tt>.
074: *
075: * @param out the output stream.
076: * @param stringMap the Map of String key/value pairs.
077: * @throws java.io.IOException if an error occurs.
078: */
079: public void writeStringMap(DataOutput out,
080: Map<String, String> stringMap) throws IOException {
081: strategy.writeStringMap(out, stringMap);
082: }
083:
084: /**
085: * Reads a Map of String key and value pairs. This method will return
086: * <tt>null</tt> if the Map written to the stream was <tt>null</tt>.
087: *
088: * @param in the input stream.
089: * @return a Map of String key/value pairs.
090: * @throws IOException if an error occurs.
091: */
092: public Map<String, String> readStringMap(DataInput in)
093: throws IOException {
094: return strategy.readStringMap(in);
095: }
096:
097: /**
098: * Writes a Map of Long key and Integer value pairs. This method handles
099: * the case when the Map is <tt>null</tt>.
100: *
101: * @param out the output stream.
102: * @param map the Map of Long key/Integer value pairs.
103: * @throws IOException if an error occurs.
104: */
105: public void writeLongIntMap(DataOutput out, Map<Long, Integer> map)
106: throws IOException {
107: strategy.writeLongIntMap(out, map);
108: }
109:
110: /**
111: * Reads a Map of Long key and Integer value pairs. This method will return
112: * <tt>null</tt> if the Map written to the stream was <tt>null</tt>.
113: *
114: * @param in the input stream.
115: * @return a Map of Long key/Integer value pairs.
116: * @throws IOException if an error occurs.
117: */
118: public Map readLongIntMap(DataInput in) throws IOException {
119: return strategy.readLongIntMap(in);
120: }
121:
122: /**
123: * Writes a List of Strings. This method handles the case when the List is
124: * <tt>null</tt>.
125: *
126: * @param out the output stream.
127: * @param stringList the List of Strings.
128: * @throws IOException if an error occurs.
129: */
130: public void writeStringList(DataOutput out, List stringList)
131: throws IOException {
132: strategy.writeStringList(out, stringList);
133: }
134:
135: /**
136: * Reads a List of Strings. This method will return <tt>null</tt> if the List
137: * written to the stream was <tt>null</tt>.
138: *
139: * @param in the input stream.
140: * @return a List of Strings.
141: * @throws IOException if an error occurs.
142: */
143: public List<String> readStringList(DataInput in) throws IOException {
144: return strategy.readStringList(in);
145: }
146:
147: /**
148: * Writes an array of long values. This method handles the case when the
149: * array is <tt>null</tt>.
150: *
151: * @param out the output stream.
152: * @param array the array of long values.
153: * @throws IOException if an error occurs.
154: */
155: public void writeLongArray(DataOutput out, long[] array)
156: throws IOException {
157: strategy.writeLongArray(out, array);
158: }
159:
160: /**
161: * Reads an array of long values. This method will return <tt>null</tt> if
162: * the array written to the stream was <tt>null</tt>.
163: *
164: * @param in the input stream.
165: * @return an array of long values.
166: * @throws IOException if an error occurs.
167: */
168: public long[] readLongArray(DataInput in) throws IOException {
169: return strategy.readLongArray(in);
170: }
171:
172: public void writeLong(DataOutput out, long value)
173: throws IOException {
174: strategy.writeLong(out, value);
175: }
176:
177: public long readLong(DataInput in) throws IOException {
178: return strategy.readLong(in);
179: }
180:
181: public void writeInt(DataOutput out, int value) throws IOException {
182: strategy.writeInt(out, value);
183: }
184:
185: public int readInt(DataInput in) throws IOException {
186: return strategy.readInt(in);
187: }
188:
189: public void writeBoolean(DataOutput out, boolean value)
190: throws IOException {
191: strategy.writeBoolean(out, value);
192: }
193:
194: public boolean readBoolean(DataInput in) throws IOException {
195: return strategy.readBoolean(in);
196: }
197:
198: public void writeByteArray(DataOutput out, byte[] value)
199: throws IOException {
200: strategy.writeByteArray(out, value);
201: }
202:
203: public byte[] readByteArray(DataInput in) throws IOException {
204: return strategy.readByteArray(in);
205: }
206:
207: public void writeSerializable(DataOutput out, Serializable value)
208: throws IOException {
209: strategy.writeSerializable(out, value);
210: }
211:
212: public Serializable readSerializable(DataInput in)
213: throws IOException {
214: return strategy.readSerializable(in);
215: }
216:
217: public void writeSafeUTF(DataOutput out, String value)
218: throws IOException {
219: strategy.writeSafeUTF(out, value);
220: }
221:
222: public String readSafeUTF(DataInput in) throws IOException {
223: return strategy.readSafeUTF(in);
224: }
225:
226: /**
227: * Writes a collection of Externalizable objects. The collection passed as a parameter
228: * must be a collection and not a <tt>null</null> value.
229: *
230: * @param out the output stream.
231: * @param value the collection of Externalizable objects. This value must not be null.
232: * @throws IOException if an error occurs.
233: */
234: public void writeExternalizableCollection(DataOutput out,
235: Collection<? extends Externalizable> value)
236: throws IOException {
237: strategy.writeExternalizableCollection(out, value);
238: }
239:
240: /**
241: * Reads a collection of Externalizable objects and adds them to the collection passed as a parameter. The
242: * collection passed as a parameter must be a collection and not a <tt>null</null> value.
243: *
244: * @param in the input stream.
245: * @param value the collection of Externalizable objects. This value must not be null.
246: * @param loader class loader to use to build elements inside of the serialized collection.
247: * @throws IOException if an error occurs.
248: * @return the number of elements added to the collection.
249: */
250: public int readExternalizableCollection(DataInput in,
251: Collection<? extends Externalizable> value,
252: ClassLoader loader) throws IOException {
253: return strategy.readExternalizableCollection(in, value, loader);
254: }
255:
256: /**
257: * Writes a Map of String key and value pairs. This method handles the
258: * case when the Map is <tt>null</tt>.
259: *
260: * @param out the output stream.
261: * @param map the Map of String key and Externalizable value pairs.
262: * @throws java.io.IOException if an error occurs.
263: */
264: public void writeExternalizableMap(DataOutput out,
265: Map<String, ? extends Externalizable> map)
266: throws IOException {
267: strategy.writeExternalizableMap(out, map);
268: }
269:
270: /**
271: * Reads a Map of String key and value pairs. This method will return
272: * <tt>null</tt> if the Map written to the stream was <tt>null</tt>.
273: *
274: * @param in the input stream.
275: * @param map a Map of String key and Externalizable value pairs.
276: * @param loader class loader to use to build elements inside of the serialized collection.
277: * @throws IOException if an error occurs.
278: * @return the number of elements added to the collection.
279: */
280: public int readExternalizableMap(DataInput in,
281: Map<String, ? extends Externalizable> map,
282: ClassLoader loader) throws IOException {
283: return strategy.readExternalizableMap(in, map, loader);
284: }
285:
286: /**
287: * Writes a Map of String key and Set of Strings value pairs. This method DOES NOT handle the
288: * case when the Map is <tt>null</tt>.
289: *
290: * @param out the output stream.
291: * @param map the Map of String key and Set of Strings value pairs.
292: * @throws java.io.IOException if an error occurs.
293: */
294: public void writeStringsMap(DataOutput out,
295: Map<String, Set<String>> map) throws IOException {
296: strategy.writeStringsMap(out, map);
297: }
298:
299: /**
300: * Reads a Map of String key and Set of Strings value pairs.
301: *
302: * @param in the input stream.
303: * @param map a Map of String key and Set of Strings value pairs.
304: * @return number of elements added to the collection.
305: * @throws IOException if an error occurs.
306: */
307: public int readStringsMap(DataInput in, Map<String, Set<String>> map)
308: throws IOException {
309: return strategy.readStringsMap(in, map);
310: }
311:
312: /**
313: * Writes content of collection of strings to the output stream.
314: *
315: * @param out the output stream.
316: * @param collection the Collection of Strings.
317: * @throws IOException if an error occurs.
318: */
319: public void writeStrings(DataOutput out,
320: Collection<String> collection) throws IOException {
321: strategy.writeStrings(out, collection);
322: }
323:
324: /**
325: * Reads the string array from the input stream and adds them to the specified collection.
326: *
327: * @param in the input stream.
328: * @param collection the collection to add the read strings from the input stream.
329: * @return number of elements added to the collection.
330: * @throws IOException if an error occurs.
331: */
332: public int readStrings(DataInput in, Collection<String> collection)
333: throws IOException {
334: return strategy.readStrings(in, collection);
335: }
336: }
|