KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > oldformats > InstanceUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.registry.oldformats;
21
22 import org.openide.ErrorManager;
23 import org.openide.filesystems.FileObject;
24 import org.openide.util.Lookup;
25 import org.openide.util.SharedClassObject;
26 import org.openide.util.Utilities;
27
28 import java.io.*;
29 import java.lang.reflect.Method JavaDoc;
30
31 /**
32  *
33  * @author copy&pasted from core/settings
34  */

35 public class InstanceUtils {
36     private InstanceUtils() {}
37
38     public static Object JavaDoc newValue(String JavaDoc val) throws ClassNotFoundException JavaDoc {
39         val = Utilities.translate(val);
40         try {
41             Class JavaDoc c = findClass(val);
42             if (SharedClassObject.class.isAssignableFrom(c)) {
43                 Object JavaDoc o = SharedClassObject.findObject(c, false);
44                 if (null != o) {
45                     // instance already exists -> reset it to defaults
46
try {
47                         Method JavaDoc method = SharedClassObject.class.getDeclaredMethod("reset", new Class JavaDoc[0]); // NOI18N
48
method.setAccessible(true);
49                         method.invoke(o, new Object JavaDoc[0]);
50                     } catch (Exception JavaDoc e) {
51                         ErrorManager.getDefault().notify(e);
52                     }
53                 } else {
54                     o = SharedClassObject.findObject(c, true);
55                 }
56                 return o;
57             } else {
58                 return c.newInstance();
59             }
60         } catch (Exception JavaDoc e) {
61             ClassNotFoundException JavaDoc e2 = new ClassNotFoundException JavaDoc("Cannot instantiate class "+val);
62             ErrorManager.getDefault().annotate(e2, e);
63             throw e2;
64         }
65     }
66
67     public static Object JavaDoc serialValue(String JavaDoc val) throws ClassNotFoundException JavaDoc, IOException {
68         if ((val == null) ||(val.length() == 0)) {
69             return null;
70         }
71
72         byte[] bytes = new byte[val.length()/2];
73         int tempI;
74         int count = 0;
75         try {
76             for (int i = 0; i < val.length(); i++) {
77                 if (Character.isWhitespace(val.charAt(i))) {
78                     continue;
79                 }
80                 tempI = Integer.parseInt(val.substring(i,i+2),16);
81                 if (tempI > 127) {
82                     tempI -=256;
83                 }
84                 bytes[count++] = (byte) tempI;
85                 i++;
86             }
87         } catch (NumberFormatException JavaDoc e) {
88             e.printStackTrace();
89             IOException e2 = new IOException("Cannot read value of <serialvalue> attribute from file XXXX");
90             ErrorManager.getDefault().annotate(e2, e);
91             throw e2;
92         }
93         
94         ByteArrayInputStream bis = new ByteArrayInputStream(bytes, 0, count);
95         try {
96             ObjectInputStream ois = new SpecialObjectInputStream(bis);
97             return ois.readObject();
98         } catch (Exception JavaDoc e) {
99             IOException e2 = new IOException("Cannot read value of <serialvalue> attribute from file XXXXX");
100             ErrorManager.getDefault().annotate(e2, e);
101             throw e2;
102         }
103     }
104
105     
106     public static Object JavaDoc methodValue(String JavaDoc className, String JavaDoc methodName, FileObject fo) throws ClassNotFoundException JavaDoc, IOException {
107         int idx = methodName.lastIndexOf(".");
108          Class JavaDoc cls = findClass(((idx > 0) ? methodName.substring(0,idx) : className));
109          methodName = (idx > 0 ) ? methodName.substring(idx+1) : methodName;
110         
111         Object JavaDoc[] paramArray = new Object JavaDoc [] {
112             new Class JavaDoc[] {FileObject.class},
113             new Class JavaDoc[] {}
114         };
115
116         Object JavaDoc[] objectsList = new Object JavaDoc [] {
117             new Object JavaDoc[] {fo},
118             new Object JavaDoc[] {}
119         };
120
121         for (int i = 0; i < paramArray.length; i++) {
122             try {
123                 if (objectsList[i] == null) {
124                     continue;
125                 }
126                 Method JavaDoc method = cls.getDeclaredMethod(methodName, (Class JavaDoc[])paramArray [i]);
127                 if (method != null) {
128                     method.setAccessible(true);
129                     if (objectsList[i] != null) {
130                         return method.invoke(null,(Object JavaDoc[])objectsList[i]);
131                     }
132                 }
133             } catch (Exception JavaDoc nsmExc) {
134                 continue;
135             }
136         }
137         throw new IOException("Cannot instantiate object by method "+className+"."+methodName+". ");
138     }
139
140     private static Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
141         ClassLoader JavaDoc c = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
142         if (c == null) {
143             return Class.forName(name);
144         } else {
145             return Class.forName(name, true, c);
146         }
147     }
148
149     
150     /** Stream allowing upgrade to a new class inside the origin .settings file.
151      * This class cannot extend NBObjectInputStream because it needs to change
152      * its behaviour. See readClassDescriptor for more details.
153      */

154     private static class SpecialObjectInputStream extends java.io.ObjectInputStream JavaDoc {
155         
156         // copied from NBObjectInputStream
157
public SpecialObjectInputStream(InputStream is) throws IOException {
158             super(is);
159             try {
160                 enableResolveObject (true);
161             } catch (SecurityException JavaDoc ex) {
162                 throw new IOException (ex.toString ());
163             }
164         }
165
166         // copied from NBObjectInputStream
167
protected Class JavaDoc resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException JavaDoc {
168             ClassLoader JavaDoc cl = getNBClassLoader();
169             try {
170                 return Class.forName(v.getName(), false, cl);
171             } catch (ClassNotFoundException JavaDoc cnfe) {
172                 String JavaDoc msg = "Offending classloader: " + cl; // NOI18N
173
ErrorManager.getDefault ().annotate(cnfe, ErrorManager.INFORMATIONAL, msg, null, null, null);
174                 throw cnfe;
175             }
176         }
177         
178         /**
179          * The difference from NBObjectInputStream.readClassDescriptor is that
180          * conversion of the class is done only when old class does not exist.
181          * If it exist then conversion is skipped and client can handle it readResolve
182          * method.
183          */

184         protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException JavaDoc {
185             ObjectStreamClass ose = super.readClassDescriptor();
186
187             String JavaDoc name = ose.getName();
188             String JavaDoc newN = org.openide.util.Utilities.translate(name);
189
190             if (name == newN) {
191                 // no translation
192
return ose;
193             }
194
195             ClassLoader JavaDoc cl = getNBClassLoader();
196             try {
197                 // Try to load OLD class first.
198
// If it succeeds return it. The clients must handle it in readResolve().
199
Class JavaDoc origCl = Class.forName(name, false, cl);
200                 return ObjectStreamClass.lookup(origCl);
201             } catch (ClassNotFoundException JavaDoc ex) {
202                 // ignore. no problem.
203
}
204             
205             // load the new class here
206
Class JavaDoc clazz = Class.forName(newN, false, cl);
207             ObjectStreamClass newOse = ObjectStreamClass.lookup(clazz);
208
209             // #28021 - it is possible that lookup return null. In that case the conversion
210
// table contains class which is not Serializable or Externalizable.
211
if (newOse == null) {
212                 throw new java.io.NotSerializableException JavaDoc(newN);
213             }
214             
215             return newOse;
216         }
217     
218         // copied from NBObjectInputStream
219
private static ClassLoader JavaDoc getNBClassLoader() {
220             ClassLoader JavaDoc c = (ClassLoader JavaDoc) org.openide.util.Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
221             return c != null ? c : ClassLoader.getSystemClassLoader();
222         }
223         
224     }
225     
226 }
227
Popular Tags