KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > externalizer > TypeAsBytesExternalizer


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.externalizer;
13
14 import com.versant.core.common.BindingSupportImpl;
15 import com.versant.core.common.Utils;
16
17 import javax.jdo.PersistenceManager;
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.io.*;
21
22 /**
23  * This externalizer can convert any type to/from byte[]. The type must have
24  * a constructor that accepts a byte[] and a method called toBytes that
25  * will provide a byte[].
26  *
27  * This class is Serializable. This is only a requirement if you are using
28  * remote PMs.
29  */

30 public class TypeAsBytesExternalizer implements Externalizer, Externalizable {
31
32     public static final String JavaDoc SHORT_NAME = "BYTES";
33
34     private Class JavaDoc type;
35     private Constructor JavaDoc constructor;
36     private Method JavaDoc toBytes;
37
38     public TypeAsBytesExternalizer(Class JavaDoc type) {
39         this.type = type;
40         init();
41     }
42
43     public TypeAsBytesExternalizer() {
44     }
45
46     private void init() {
47         try {
48             constructor = type.getConstructor(new Class JavaDoc[]{byte[].class});
49         } catch (NoSuchMethodException JavaDoc e) {
50             throw BindingSupportImpl.getInstance().runtime(type + " does not have a " +
51                 "constructor that accepts a byte[]", e);
52         }
53         try {
54             toBytes = type.getMethod("toBytes", null);
55         } catch (NoSuchMethodException JavaDoc e) {
56             throw BindingSupportImpl.getInstance().runtime(type + " does not have a " +
57                 "public toBytes() method", e);
58         }
59         if (toBytes.getReturnType() != byte[].class) {
60             throw BindingSupportImpl.getInstance().runtime(type + ".toBytes() does not " +
61                 "return byte[]");
62         }
63     }
64
65     public Object JavaDoc toExternalForm(PersistenceManager pm, Object JavaDoc o) {
66         if (o == null) return null;
67         try {
68             return toBytes.invoke(o, null);
69         } catch (Throwable JavaDoc x) {
70             throw BindingSupportImpl.getInstance().fatalDatastore("Unable to convert instance of " +
71                 type.getName() + " '" + Utils.toString(o) + "' to byte[]: " + x, x);
72         }
73     }
74
75     public Object JavaDoc fromExternalForm(PersistenceManager pm, Object JavaDoc o) {
76         if (o == null) return null;
77         if (!(o instanceof byte[])) {
78             throw BindingSupportImpl.getInstance().runtime(
79                 "Expected byte[] to create instance of " +
80                 type.getName() + ", got: " + Utils.toString(o));
81         }
82         try {
83             return constructor.newInstance(new Object JavaDoc[]{o});
84         } catch (Throwable JavaDoc x) {
85             throw BindingSupportImpl.getInstance().runtime(
86                 "Unable to create instance of " +
87                 type.getName() + " from " + Utils.toString(o) + ": " + x, x);
88         }
89     }
90
91     public Class JavaDoc getExternalType() {
92         return byte[].class;
93     }
94
95     public void writeExternal(ObjectOutput out) throws IOException {
96         out.writeObject(type);
97     }
98
99     public void readExternal(ObjectInput in) throws IOException,
100             ClassNotFoundException JavaDoc {
101         type = (Class JavaDoc)in.readObject();
102         init();
103     }
104 }
105
106
Popular Tags