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.ByteArrayInputStream;
24  import java.io.IOException;
25  import java.io.ObjectInputStream;
26  import java.util.Map;
27  
28  import org.apache.wsrp4j.log.LogManager;
29  import org.apache.wsrp4j.log.Logger;
30  
31  /**
32   *
33   * @author Stephan.Laertz@de.ibm.com
34   *
35   * Helper to deserialize the parameter map of the portlet url
36   **/
37  public class ObjectDeserializer {
38  
39      //     for logging and exception support
40      private static Logger logger = LogManager.getLogManager().getLogger(
41              ObjectDeserializer.class);
42  
43      public static Object deserialize(byte[] map) throws IOException,
44              ClassNotFoundException {
45  
46          String MN = "deserialize(byte[])";
47          if (logger.isLogging(Logger.TRACE_HIGH)) {
48              logger.entry(Logger.TRACE_HIGH, MN, map);
49          }
50  
51          ByteArrayInputStream bytes = new ByteArrayInputStream(map);
52          ObjectInputStream in = new ObjectInputStream(bytes);
53          Object obj = in.readObject();
54          in.close();
55  
56          if (logger.isLogging(Logger.TRACE_HIGH)) {
57              logger.exit(Logger.TRACE_HIGH, MN, obj);
58          }
59  
60          return obj;
61      }
62  
63      public static Map deserializeMap(byte[] map) throws IOException,
64              ClassNotFoundException {
65          Object obj = deserialize(map);
66          
67          String MN = "deserializeMap";
68          if (!(obj instanceof Map)) {
69              obj = null;
70              logger.text(Logger.ERROR, MN, "Deserialized object is not a map.",
71                      obj);
72          }
73          
74          return (Map) obj;
75      }
76  
77      public static String deserializeString(byte[] string) throws IOException,
78              ClassNotFoundException {
79          return (String) deserialize(string);
80      }
81  }