01: /*
02: * Copyright 2000-2001,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: /*
18:
19: */
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: /**
32: * @author Stephan.Laertz@de.ibm.com
33: *
34: * Helper to serialize the parameter map of the portlet url
35: **/
36: public class ObjectSerializer {
37:
38: // for logging and exception support
39: private static Logger logger = LogManager.getLogManager()
40: .getLogger(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: } else {
61:
62: if (logger.isLogging(Logger.WARN)) {
63: logger.text(Logger.WARN, MN,
64: "Unable to serialize Object.");
65: }
66: }
67:
68: if (logger.isLogging(Logger.TRACE_HIGH)) {
69: logger.exit(Logger.TRACE_HIGH, MN, result);
70: }
71:
72: return result;
73: }
74: }
|