ExternalizableUtil.java 14.1 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Gaston Dombiak's avatar
Gaston Dombiak committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Gaston Dombiak's avatar
Gaston Dombiak committed
19 20 21 22
 */

package org.jivesoftware.util.cache;

Gaston Dombiak's avatar
Gaston Dombiak committed
23
import java.io.*;
24
import java.util.Collection;
Gaston Dombiak's avatar
Gaston Dombiak committed
25 26
import java.util.List;
import java.util.Map;
Gaston Dombiak's avatar
Gaston Dombiak committed
27
import java.util.Set;
Gaston Dombiak's avatar
Gaston Dombiak committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

/**
 * Utility methods to assist in working with the Externalizable interfaces. This class
 * is only used when running inside of a Cluser. When using the open source version
 * this class will use a dummy implementation. Anyway, this class is not used when
 * not using the Enterprise edition.<p>
 *
 * ExternalizableLite is very similar to the standard Externalizable interface, except that
 * it uses DataOutput/DataInput instead of the Object stream equivalents.
 *
 * @author Gaston Dombiak
 */
public class ExternalizableUtil {

    private static ExternalizableUtil instance = new ExternalizableUtil();

    private ExternalizableUtilStrategy strategy = new DummyExternalizableUtil();

    static {
        instance = new ExternalizableUtil();
    }

    public static ExternalizableUtil getInstance() {
        return instance;
    }
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75


    /**
     * Sets the implementation to use for serializing and deserializing
     * objects. 
     *
     * @param strategy the new strategy to use.
     */
    public void setStrategy(ExternalizableUtilStrategy strategy) {
        this.strategy = strategy;
    }

    /**
     * Returns the implementation to use for serializing and deserializing
     * objects.
     *
     * @return the implementation to use for serializing and deserializing
     * objects.
     */
    public ExternalizableUtilStrategy getStrategy() {
        return strategy;
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    /**
     * Hidding constructor. We only want one single instance.
     */
    private ExternalizableUtil() {
        super();
    }

    /**
     * Writes a Map of String key and value pairs. This method handles the
     * case when the Map is <tt>null</tt>.
     *
     * @param out       the output stream.
     * @param stringMap the Map of String key/value pairs.
     * @throws java.io.IOException if an error occurs.
     */
    public void writeStringMap(DataOutput out, Map<String, String> stringMap) throws IOException {
        strategy.writeStringMap(out, stringMap);
    }

    /**
     * Reads a Map of String key and value pairs. This method will return
     * <tt>null</tt> if the Map written to the stream was <tt>null</tt>.
     *
     * @param in the input stream.
     * @return a Map of String key/value pairs.
     * @throws IOException if an error occurs.
     */
    public Map<String, String> readStringMap(DataInput in) throws IOException {
        return strategy.readStringMap(in);
    }

    /**
     * Writes a Map of Long key and Integer value pairs. This method handles
     * the case when the Map is <tt>null</tt>.
     *
     * @param out the output stream.
     * @param map the Map of Long key/Integer value pairs.
     * @throws IOException if an error occurs.
     */
    public void writeLongIntMap(DataOutput out, Map<Long, Integer> map) throws IOException {
        strategy.writeLongIntMap(out, map);
    }

    /**
     * Reads a Map of Long key and Integer value pairs. This method will return
     * <tt>null</tt> if the Map written to the stream was <tt>null</tt>.
     *
     * @param in the input stream.
     * @return a Map of Long key/Integer value pairs.
     * @throws IOException if an error occurs.
     */
127
    public Map<Long, Integer> readLongIntMap(DataInput in) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
128 129 130 131 132 133 134 135 136 137 138
        return strategy.readLongIntMap(in);
    }

    /**
     * Writes a List of Strings. This method handles the case when the List is
     * <tt>null</tt>.
     *
     * @param out        the output stream.
     * @param stringList the List of Strings.
     * @throws IOException if an error occurs.
     */
139
    public void writeStringList(DataOutput out, List<String> stringList) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        strategy.writeStringList(out, stringList);
    }

    /**
     * Reads a List of Strings. This method will return <tt>null</tt> if the List
     * written to the stream was <tt>null</tt>.
     *
     * @param in the input stream.
     * @return a List of Strings.
     * @throws IOException if an error occurs.
     */
    public List<String> readStringList(DataInput in) throws IOException {
        return strategy.readStringList(in);
    }

    /**
     * Writes an array of long values. This method handles the case when the
     * array is <tt>null</tt>.
     *
     * @param out   the output stream.
     * @param array the array of long values.
     * @throws IOException if an error occurs.
     */
    public void writeLongArray(DataOutput out, long [] array) throws IOException {
        strategy.writeLongArray(out, array);
    }

    /**
     * Reads an array of long values. This method will return <tt>null</tt> if
     * the array written to the stream was <tt>null</tt>.
     *
     * @param in the input stream.
     * @return an array of long values.
     * @throws IOException if an error occurs.
     */
    public long [] readLongArray(DataInput in) throws IOException {
        return strategy.readLongArray(in);
    }

179
    public void writeLong(DataOutput out, long value) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
180 181 182
        strategy.writeLong(out, value);
    }

183
    public long readLong(DataInput in) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
184 185 186
        return strategy.readLong(in);
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
187 188 189 190 191 192 193 194
    public void writeInt(DataOutput out, int value) throws IOException {
        strategy.writeInt(out, value);
    }

    public int readInt(DataInput in) throws IOException {
        return strategy.readInt(in);
    }

195
    public void writeBoolean(DataOutput out, boolean value) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
196 197 198
        strategy.writeBoolean(out, value);
    }

199
    public boolean readBoolean(DataInput in) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
200 201 202
        return strategy.readBoolean(in);
    }

203 204 205 206 207 208 209 210
    public void writeByteArray(DataOutput out, byte[] value) throws IOException {
        strategy.writeByteArray(out, value);
    }

    public byte[] readByteArray(DataInput in) throws IOException {
        return strategy.readByteArray(in);
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
211 212 213 214 215 216 217 218
    public void writeSerializable(DataOutput out, Serializable value) throws IOException {
        strategy.writeSerializable(out, value);
    }

    public Serializable readSerializable(DataInput in) throws IOException {
        return strategy.readSerializable(in);
    }

219
    public void writeSafeUTF(DataOutput out, String value) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
220 221 222
        strategy.writeSafeUTF(out, value);
    }

223
    public String readSafeUTF(DataInput in) throws IOException {
Gaston Dombiak's avatar
Gaston Dombiak committed
224 225
        return strategy.readSafeUTF(in);
    }
226 227 228

    /**
     * Writes a collection of Externalizable objects. The collection passed as a parameter
229
     * must be a collection and not a <tt>null</tt> value.
230 231 232 233 234 235 236 237 238
     *
     * @param out   the output stream.
     * @param value the collection of Externalizable objects. This value must not be null.
     * @throws IOException if an error occurs.
     */
    public void writeExternalizableCollection(DataOutput out, Collection<? extends Externalizable> value) throws IOException {
        strategy.writeExternalizableCollection(out, value);
    }

239 240
    /**
     * Writes a collection of Serializable objects. The collection passed as a parameter
241
     * must be a collection and not a <tt>null</tt> value.
242 243 244 245 246 247 248 249 250
     *
     * @param out   the output stream.
     * @param value the collection of Serializable objects. This value must not be null.
     * @throws IOException if an error occurs.
     */
    public void writeSerializableCollection(DataOutput out, Collection<? extends Serializable> value) throws IOException {
        strategy.writeSerializableCollection(out, value);
    }
    
251 252
    /**
     * Reads a collection of Externalizable objects and adds them to the collection passed as a parameter. The
253
     * collection passed as a parameter must be a collection and not a <tt>null</tt> value.
254 255 256 257 258 259 260 261 262 263 264
     *
     * @param in the input stream.
     * @param value the collection of Externalizable objects. This value must not be null.
     * @param loader class loader to use to build elements inside of the serialized collection.
     * @throws IOException if an error occurs.
     * @return the number of elements added to the collection.
     */
    public int readExternalizableCollection(DataInput in, Collection<? extends Externalizable> value, ClassLoader loader)
            throws IOException {
        return strategy.readExternalizableCollection(in, value, loader);
    }
Gaston Dombiak's avatar
Gaston Dombiak committed
265 266 267 268 269 270 271 272 273 274 275 276 277

    /**
     * Writes a Map of String key and value pairs. This method handles the
     * case when the Map is <tt>null</tt>.
     *
     * @param out       the output stream.
     * @param map       the Map of String key and Externalizable value pairs.
     * @throws java.io.IOException if an error occurs.
     */
    public void writeExternalizableMap(DataOutput out, Map<String, ? extends Externalizable> map) throws IOException {
        strategy.writeExternalizableMap(out, map);
    }

278 279
    /**
     * Reads a collection of Serializable objects and adds them to the collection passed as a parameter. The
280
     * collection passed as a parameter must be a collection and not a <tt>null</tt> value.
281 282 283 284 285 286 287 288 289 290 291 292 293
     *
     * @param in the input stream.
     * @param value the collection of Serializable objects. This value must not be null.
     * @param loader class loader to use to build elements inside of the serialized collection.
     * @throws IOException if an error occurs.
     * @return the number of elements added to the collection.
     */
    public int readSerializableCollection(DataInput in, Collection<? extends Serializable> value, ClassLoader loader)
            throws IOException {
        return strategy.readSerializableCollection(in, value, loader);
    }

    /**
294
     * Writes a Map of Serializable key and value pairs. This method handles the
295 296 297
     * case when the Map is <tt>null</tt>.
     *
     * @param out       the output stream.
298
     * @param map       the Map of Serializable key and value pairs.
299 300
     * @throws java.io.IOException if an error occurs.
     */
301
    public void writeSerializableMap(DataOutput out, Map<? extends Serializable, ? extends Serializable> map) throws IOException {
302 303 304
        strategy.writeSerializableMap(out, map);
    }
    
Gaston Dombiak's avatar
Gaston Dombiak committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318
    /**
     * Reads a Map of String key and value pairs. This method will return
     * <tt>null</tt> if the Map written to the stream was <tt>null</tt>.
     *
     * @param in the input stream.
     * @param map a Map of String key and Externalizable value pairs.
     * @param loader class loader to use to build elements inside of the serialized collection.
     * @throws IOException if an error occurs.
     * @return the number of elements added to the collection.
     */
    public int readExternalizableMap(DataInput in, Map<String, ? extends Externalizable> map, ClassLoader loader) throws IOException {
        return strategy.readExternalizableMap(in, map, loader);
    }

319
    /**
320
     * Reads a Map of Serializable key and value pairs. This method will return
321 322 323
     * <tt>null</tt> if the Map written to the stream was <tt>null</tt>.
     *
     * @param in the input stream.
324
     * @param map a Map of Serializable key and value pairs.
325 326 327 328
     * @param loader class loader to use to build elements inside of the serialized collection.
     * @throws IOException if an error occurs.
     * @return the number of elements added to the collection.
     */
329
    public int readSerializableMap(DataInput in, Map<? extends Serializable, ? extends Serializable> map, ClassLoader loader) throws IOException {
330 331 332
        return strategy.readSerializableMap(in, map, loader);
    }

Gaston Dombiak's avatar
Gaston Dombiak committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    /**
     * Writes a Map of String key and Set of Strings value pairs. This method DOES NOT handle the
     * case when the Map is <tt>null</tt>.
     *
     * @param out       the output stream.
     * @param map       the Map of String key and Set of Strings value pairs.
     * @throws java.io.IOException if an error occurs.
     */
    public void writeStringsMap(DataOutput out, Map<String, Set<String>> map) throws IOException {
        strategy.writeStringsMap(out, map);
    }

    /**
     * Reads a Map of String key and Set of Strings value pairs.
     *
     * @param in the input stream.
     * @param map a Map of String key and Set of Strings value pairs.
     * @return number of elements added to the collection.
     * @throws IOException if an error occurs.
     */
    public int readStringsMap(DataInput in, Map<String, Set<String>> map) throws IOException {
        return strategy.readStringsMap(in, map);
    }

    /**
     * Writes content of collection of strings to the output stream.
     *
     * @param out           the output stream.
     * @param collection    the Collection of Strings.
     * @throws IOException if an error occurs.
     */
    public void writeStrings(DataOutput out, Collection<String> collection) throws IOException {
        strategy.writeStrings(out, collection);
    }

    /**
     * Reads the string array from the input stream and adds them to the specified collection. 
     *
     * @param in the input stream.
     * @param collection the collection to add the read strings from the input stream.
     * @return number of elements added to the collection.
     * @throws IOException if an error occurs.
     */
    public int readStrings(DataInput in, Collection<String> collection) throws IOException {
        return strategy.readStrings(in, collection);
    }
Gaston Dombiak's avatar
Gaston Dombiak committed
379
}