KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > types > SerializableTypeFactory


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 package org.apache.cayenne.access.types;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.io.Serializable JavaDoc;
26
27 import org.apache.cayenne.CayenneRuntimeException;
28
29 /**
30  * ExtendedTypeFactory for handling serializable objects. Returned ExtendedType is simply
31  * an object serialization wrapper on top of byte[] ExtendedType.
32  *
33  * @since 3.0
34  * @author Andrus Adamchik
35  */

36 class SerializableTypeFactory implements ExtendedTypeFactory {
37
38     private ExtendedTypeMap map;
39
40     SerializableTypeFactory(ExtendedTypeMap map) {
41         this.map = map;
42     }
43
44     public ExtendedType getType(Class JavaDoc objectClass) {
45
46         if (Serializable JavaDoc.class.isAssignableFrom(objectClass)) {
47
48             // using a binary stream delegate instead of byte[] may actually speed up
49
// things in some dbs, but at least byte[] type works consistently across
50
// adapters...
51

52             // note - can't use "getRegisteredType" as it causes infinite recursion
53
ExtendedType bytesType = map.getExplictlyRegisteredType("byte[]");
54
55             // not sure if this type of recursion can occur, still worth checking
56
if (bytesType instanceof SerializableType) {
57                 throw new IllegalStateException JavaDoc(
58                         "Can't create Serializable ExtendedType for "
59                                 + objectClass.getName()
60                                 + ": no ExtendedType exists for byte[]");
61             }
62
63             return new SerializableType(objectClass, bytesType);
64         }
65
66         return null;
67     }
68
69     /**
70      * A serialization wrapper on top of byte[] ExtendedType
71      */

72     final class SerializableType extends ExtendedTypeDecorator {
73
74         private Class JavaDoc javaClass;
75
76         SerializableType(Class JavaDoc javaClass, ExtendedType bytesType) {
77             super(bytesType);
78             this.javaClass = javaClass;
79         }
80
81         public String JavaDoc getClassName() {
82             return javaClass.getName();
83         }
84
85         Object JavaDoc fromJavaObject(Object JavaDoc object) {
86             ByteArrayOutputStream JavaDoc bytes = new ByteArrayOutputStream JavaDoc() {
87
88                 // avoid unneeded array copy...
89
public synchronized byte[] toByteArray() {
90                     return buf;
91                 }
92             };
93
94             try {
95                 ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(bytes);
96                 out.writeObject(object);
97                 out.close();
98             }
99             catch (Exception JavaDoc e) {
100                 throw new CayenneRuntimeException("Error serializing object", e);
101             }
102
103             return bytes.toByteArray();
104         }
105
106         Object JavaDoc toJavaObject(Object JavaDoc object) {
107             byte[] bytes = (byte[]) object;
108             try {
109                 return bytes != null && bytes.length > 0 ? new ObjectInputStream JavaDoc(
110                         new ByteArrayInputStream JavaDoc(bytes)).readObject() : null;
111             }
112             catch (Exception JavaDoc e) {
113                 throw new CayenneRuntimeException("Error deserializing object", e);
114             }
115         }
116     }
117 }
118
Popular Tags