1
16
17
20
21 package org.apache.wsrp4j.producer.util;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.ObjectOutputStream;
26 import java.io.Serializable;
27
28 import org.apache.wsrp4j.log.LogManager;
29 import org.apache.wsrp4j.log.Logger;
30
31
36 public class ObjectSerializer {
37
38 private static Logger logger = LogManager.getLogManager().getLogger(
40 ObjectSerializer.class);
41
42 public static byte[] serialize(Object object) throws IOException {
43
44 String MN = "serialize(Serializable)";
45 if (logger.isLogging(Logger.TRACE_HIGH)) {
46 logger.entry(Logger.TRACE_HIGH, MN, object);
47 }
48
49 byte[] result = null;
50
51 if (object instanceof Serializable) {
52
53 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
54 ObjectOutputStream out = new ObjectOutputStream(bytes);
55 out.writeObject(object);
56 out.flush();
57 result = bytes.toByteArray();
58 out.close();
59
60 }
61 else {
62
63 if (logger.isLogging(Logger.WARN)) {
64 logger.text(Logger.WARN, MN,
65 "Unable to serialize Object.");
66 }
67 }
68
69 if (logger.isLogging(Logger.TRACE_HIGH)) {
70 logger.exit(Logger.TRACE_HIGH, MN, result);
71 }
72
73 return result;
74 }
75 }