KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > SearchTypeInputStream


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 2003 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.search;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 /**
26  * Object input stream with a customized class resolution.
27  * It allows to read search criteria (saved as instances of class
28  * {@link org.openidex.search.SearchType SearchType} or its subclass), although
29  * some of the classes are not defined by the Utilities module (these classes
30  * are called "external" in the rest of this text).
31  *
32  * <p>The following three assumptions are used in the algorithm
33  * for class resolution:</p>
34  * <ul>
35  * <li>for each type of search criterion to be read,
36  * a default instance of the class representing the criterion
37  * is registered in a lookup</li>
38  * <li>during serialization, the first external class to be resolved
39  * is the class representing the search criterion
40  * (and hence its instance is available in the lookup)</li>
41  * <li>the classloader which has loaded the search criterion's class
42  * is able to load all other external classes needed during the search
43  * criterion's deserialization</li>
44  * </ul>
45  *
46  * @see #resolveClass
47  * @see org.openidex.search.SearchType SearchType
48  * @author Marian Petras
49  */

50 class SearchTypeInputStream extends java.io.ObjectInputStream JavaDoc {
51     
52     /**
53      * holds the resolved <code>SearchType</code> class so that its classloader
54      * may be used for resolution of other classes
55      *
56      * @see #extClassLoader
57      * @see #resolveExtClass
58      */

59     private Class JavaDoc extSearchType = null;
60     /**
61      * classloader of the resolved <code>SearchType</code>
62      *
63      * @see #extSearchType
64      */

65     private ClassLoader JavaDoc extClassLoader = null;
66
67     /** Creates a new instance of SearchTypeInputStream */
68     public SearchTypeInputStream(InputStream JavaDoc in) throws IOException JavaDoc {
69         super(in);
70     }
71     
72     /**
73      * Loads the local class equivalent of the specified stream class
74      * description. Uses the default
75      * {@link java.io.ObjectInputStream ObjectInputStream}'s method first and
76      * if it fails, tries to lookup the class among the registered
77      * <code>SearchType</code>s or to use their classloaders.
78      */

79     protected Class JavaDoc resolveClass(java.io.ObjectStreamClass JavaDoc objectStreamClass)
80             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
81         try {
82             return super.resolveClass(objectStreamClass);
83         } catch (ClassNotFoundException JavaDoc ex) {
84             Class JavaDoc extClass = resolveExtClass(objectStreamClass.getName());
85             if (extClass != null) {
86                 return extClass;
87             } else {
88                 throw ex;
89             }
90         }
91     }
92
93     /**
94      * Resolves the specified class.
95      * If this method is used for the first time (on this instance), it just
96      * tries to lookup the class among the registed <code>SearchType</code>s.
97      * Next time this method is called, it uses the <code>SearchType</code>'s
98      * classloader for loading classes.
99      *
100      * @param className name of a class to be resolved
101      * @return local class matching the specified class name
102      * or <code>null</code> if no matching class was found
103      */

104     private Class JavaDoc resolveExtClass(final String JavaDoc className) {
105         if (extSearchType == null) {
106
107             /* first time class resolution must be for the ext. SearchType */
108             extSearchType = Utils.searchTypeForName(className);
109             return extSearchType;
110         }
111
112         /*
113          * We expect that the classloader that was used for loading
114          * the Search Type class is suitable also for other classes:
115          */

116         if (extClassLoader == null) {
117             try {
118                 extClassLoader = extSearchType.getClassLoader();
119             } catch (SecurityException JavaDoc ex) {
120                 return null;
121             }
122         }
123         try {
124             return extClassLoader.loadClass(className);
125         } catch (ClassNotFoundException JavaDoc ex) {
126             return null;
127         }
128     }
129     
130 }
131
Popular Tags