KickJava   Java API By Example, From Geeks To Geeks.

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


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.Externalizable JavaDoc;
21 import java.io.ObjectInput JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.ObjectOutput JavaDoc;
24
25 /**
26  * This externalizer can convert any type to/from String. The type must have
27  * a constructor that accepts a String. If it has a method 'String
28  * toExternalString()' then this is used to convert it to a String. Otherwise
29  * its toString() method is used.
30  *
31  * This class is Serializable. This is only a requirement if you are using
32  * remote PMs.
33  */

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