1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
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().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  }