01: package org.jgroups.util;
02:
03: import org.apache.commons.logging.Log;
04: import org.apache.commons.logging.LogFactory;
05: import org.jgroups.ChannelException;
06: import org.jgroups.conf.ClassConfigurator;
07:
08: import java.io.IOException;
09: import java.io.ObjectOutputStream;
10: import java.io.ObjectStreamClass;
11: import java.io.OutputStream;
12:
13: /**
14: * Uses magic numbers for class descriptors
15: * @author Bela Ban
16: * @version $Id: MagicObjectOutputStream.java,v 1.5 2006/02/27 14:09:57 belaban Exp $
17: */
18: public class MagicObjectOutputStream extends ObjectOutputStream {
19: static volatile ClassConfigurator conf = null;
20: static final Log log = LogFactory
21: .getLog(MagicObjectOutputStream.class);
22:
23: public MagicObjectOutputStream(OutputStream out) throws IOException {
24: super (out);
25: if (conf == null) {
26: try {
27: conf = ClassConfigurator.getInstance(false);
28: } catch (ChannelException e) {
29: log.error(
30: "ClassConfigurator could not be instantiated",
31: e);
32: }
33: }
34: }
35:
36: protected void writeClassDescriptor(ObjectStreamClass desc)
37: throws IOException {
38: int magic_num;
39: if (conf == null) {
40: super .writeInt(-1);
41: super .writeClassDescriptor(desc);
42: return;
43: }
44: magic_num = conf.getMagicNumberFromObjectStreamClass(desc);
45: super .writeInt(magic_num);
46: if (magic_num == -1) {
47: if (log.isTraceEnabled()) // todo: remove
48: log.trace("could not find magic number for '"
49: + desc.getName()
50: + "': writing full class descriptor");
51: super .writeClassDescriptor(desc);
52: } else {
53: //if(log.isTraceEnabled())
54: // log.trace("writing descriptor (num=" + magic_num + "): " + desc.getName());
55: }
56: }
57:
58: }
|