KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > enode > FactoryWrapper


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 Nokia. Portions Copyright 2003 Nokia.
17  * All Rights Reserved.
18  */

19
20 package org.netbeans.modules.enode;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28 import org.openide.nodes.Node;
29 import org.openide.util.Lookup;
30 import org.openide.util.Lookup.Template;
31
32 import org.netbeans.spi.enode.LookupContentFactory;
33
34 /** Wrapper object for delaying of loading classes of the attached
35  * objects. Reads the file attributes needed for this optimalization.
36  * @author David Strupl
37  */

38 public class FactoryWrapper implements LookupContentFactory {
39
40     /**
41      * File object on the system filesystem. The attributes of this
42      * file object are used to determine the class of the result.
43      */

44     private FileObject f;
45     
46     /**
47      * The result of call to the <code> instantiate </code> method.
48      */

49     private Object JavaDoc obj;
50     
51     /** Just remembers the parameter.*/
52     public FactoryWrapper(FileObject f) {
53         this.f = f;
54     }
55     
56     /**
57      * Method from the LookupContentFactory interface. This method
58      * passes the <code>target</code> argument to the delegated
59      * LookupContentFactory.
60      */

61     public Object JavaDoc create(Node target) {
62         if (obj == null) {
63             obj = instantiate();
64         }
65         if (obj instanceof LookupContentFactory) {
66             LookupContentFactory lcf = (LookupContentFactory)obj;
67             return lcf.create(target);
68         }
69         return obj;
70     }
71     
72     /**
73      * Method from the LookupContentFactory interface. This method
74      * passes the <code>target</code> argument to the delegated
75      * LookupContentFactory.
76      */

77     public Lookup createLookup(Node target) {
78         if (obj == null) {
79             obj = instantiate();
80         }
81         if (obj instanceof LookupContentFactory) {
82             LookupContentFactory lcf = (LookupContentFactory)obj;
83             return lcf.createLookup(target);
84         }
85         return null;
86     }
87     
88     /**
89      * Checks whether we can match the template. If the resulting object
90      * has been computed we just use its class or if it has not the file
91      * object is examined for the "implements" attribute.
92      */

93     boolean matches(Template template) {
94         if (template.getType() != null) {
95             if (obj != null) {
96                 return template.getType().isAssignableFrom(obj.getClass());
97             }
98             if (! resultImplements().contains(template.getType().getName())) {
99                 return false;
100             }
101         }
102         return true;
103     }
104     
105     /**
106      * Parses the value of attribute "implements"
107      * @return List of String with names of classes/interfaces
108      * implemented by the resulting object
109      */

110     private List JavaDoc resultImplements() {
111         String JavaDoc classAttr = (String JavaDoc)f.getAttribute("implements"); // NOI18N
112
ArrayList JavaDoc res = new ArrayList JavaDoc();
113         StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(classAttr, ",");
114         while (t.hasMoreElements()) {
115             res.add(t.nextElement());
116         }
117         return res;
118     }
119     
120     /**
121      * We use the system classloader for resolving the class specified
122      * in the file attribute.
123      * @return Class of the resulting object. If the object has not
124      * been created yet the attribute "factoryClass" is consulted
125      * @throws IllegalStateException if something went wrong
126      */

127     private Class JavaDoc clazz() {
128         if (obj != null) {
129             return obj.getClass();
130         }
131         try {
132             String JavaDoc classAttr = (String JavaDoc)f.getAttribute("factoryClass"); // NOI18N
133
ClassLoader JavaDoc cl = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
134             if (classAttr != null) {
135                 Class JavaDoc c = Class.forName(classAttr, true, cl);
136                 return c;
137             } else {
138                 throw new IllegalStateException JavaDoc("Attribute factoryClass not specified for " + f); // NOI18N
139
}
140         } catch (ClassNotFoundException JavaDoc cnfe) {
141             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc();
142             ErrorManager.getDefault().annotate(ise, cnfe);
143             throw ise;
144         }
145     }
146     
147     /**
148      * After calling the clazz method newInstance of the resulting
149      * class is returned.
150      * @throws IllegalStateException if something went wrong
151      */

152     private Object JavaDoc instantiate() {
153         try {
154             return clazz().newInstance();
155         } catch (InstantiationException JavaDoc is) {
156             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc();
157             ErrorManager.getDefault().annotate(ise, is);
158             throw ise;
159         } catch (IllegalAccessException JavaDoc iae) {
160             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc();
161             ErrorManager.getDefault().annotate(ise, iae);
162             throw ise;
163         }
164     }
165     
166     /**
167      * @return Human readable description of the wrapper object.
168      */

169     public String JavaDoc toString() {
170         if (obj != null) {
171             return "FactoryWrapper[" + clazz().getName() + "]"; // NOI18N
172
}
173         return "FactoryWrapper[" + f.getAttribute("factoryClass") + "]"; // NOI18N
174
}
175     
176 }
177
Popular Tags