01: /**
02: *
03: */package clime.messadmin.providers.serializable;
04:
05: import java.io.IOException;
06: import java.io.ObjectOutputStream;
07: import java.io.ObjectStreamException;
08: import java.io.OutputStream;
09:
10: import clime.messadmin.providers.spi.SerializableProvider;
11:
12: /**
13: * Determines if an object is serializable by serializing it.
14: * Note: this is an expansive, although accurate, operation.
15: * @author Cédrik LIME
16: */
17: public class FullySerializableProvider implements SerializableProvider {
18: private static final OutputStream nullOutputStream = new OutputStream() {
19: /** {@inheritDoc} */
20: public void write(int b) throws IOException {
21: }
22:
23: /** {@inheritDoc} */
24: public void write(byte[] b, int off, int len)
25: throws IOException {
26: }
27: };
28:
29: /**
30: *
31: */
32: public FullySerializableProvider() {
33: super ();
34: }
35:
36: /**
37: * {@inheritDoc}
38: */
39: public boolean isSerializable(Object obj) {
40: try {
41: return isFullySerializable(obj);
42: } catch (RuntimeException rte) {
43: return false;
44: }
45: }
46:
47: /**
48: * {@inheritDoc}
49: */
50: public int getPriority() {
51: return 0;
52: }
53:
54: /**
55: * Determines if an object is serializable by serializing it.
56: * Note: this is an expansive operation.
57: * @param o
58: * @return true if o is Serializable, false if not
59: */
60: public static boolean isFullySerializable(Object o) {
61: try {
62: ObjectOutputStream out = new ObjectOutputStream(
63: nullOutputStream);
64: out.writeObject(o);
65: out.flush();
66: out.close();
67: } catch (ObjectStreamException ose) {
68: return false;
69: } catch (IOException ioe) {
70: //throw new RuntimeException("Exception while computing serialization state: " + ioe);
71: return false;
72: }
73: return true;
74: }
75: }
|