KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > remote > hessian > service > HessianUtil


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.remote.hessian.service;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.Serializable JavaDoc;
25
26 import org.apache.cayenne.map.EntityResolver;
27 import org.apache.cayenne.remote.hessian.HessianConfig;
28 import org.apache.cayenne.remote.hessian.HessianConnection;
29
30 import com.caucho.hessian.io.HessianInput;
31 import com.caucho.hessian.io.HessianOutput;
32
33 /**
34  * Hessian related utilities.
35  *
36  * @since 1.2
37  * @author Andrus Adamchik
38  */

39 public final class HessianUtil {
40
41     /**
42      * A utility method that clones an object using Hessian serialization/deserialization
43      * mechanism, which is different from default Java serialization.
44      */

45     public static Object JavaDoc cloneViaClientServerSerialization(
46             Serializable JavaDoc object,
47             EntityResolver serverResolver) throws Exception JavaDoc {
48         ByteArrayOutputStream JavaDoc bytes = new ByteArrayOutputStream JavaDoc();
49         HessianOutput out = new HessianOutput(bytes);
50         out.setSerializerFactory(HessianConfig.createFactory(
51                 HessianConnection.CLIENT_SERIALIZER_FACTORIES,
52                 null));
53         out.writeObject(object);
54
55         byte[] data = bytes.toByteArray();
56
57         HessianInput in = new HessianInput(new ByteArrayInputStream JavaDoc(data));
58         in.setSerializerFactory(HessianConfig.createFactory(
59                 HessianService.SERVER_SERIALIZER_FACTORIES,
60                 serverResolver));
61
62         return in.readObject();
63     }
64
65     public static Object JavaDoc cloneViaServerClientSerialization(
66             Serializable JavaDoc object,
67             EntityResolver serverResolver) throws Exception JavaDoc {
68         ByteArrayOutputStream JavaDoc bytes = new ByteArrayOutputStream JavaDoc();
69         HessianOutput out = new HessianOutput(bytes);
70         out.setSerializerFactory(HessianConfig.createFactory(
71                 HessianService.SERVER_SERIALIZER_FACTORIES,
72                 serverResolver));
73         out.writeObject(object);
74
75         byte[] data = bytes.toByteArray();
76
77         HessianInput in = new HessianInput(new ByteArrayInputStream JavaDoc(data));
78         in.setSerializerFactory(HessianConfig.createFactory(
79                 HessianConnection.CLIENT_SERIALIZER_FACTORIES,
80                 null));
81         return in.readObject();
82     }
83
84     private HessianUtil() {
85
86     }
87 }
88
Popular Tags