KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > debugger > Lookup


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.api.debugger;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.LinkedHashMap JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Set JavaDoc;
41
42 import org.netbeans.spi.debugger.ContextProvider;
43 import org.openide.ErrorManager;
44
45
46 /**
47  *
48  * @author Jan Jancura
49  */

50 abstract class Lookup implements ContextProvider {
51     
52     public Object JavaDoc lookupFirst (String JavaDoc folder, Class JavaDoc service) {
53         List JavaDoc l = lookup (folder, service);
54         if (l.isEmpty ()) return null;
55         return l.get (0);
56     }
57     
58     public abstract List JavaDoc lookup (String JavaDoc folder, Class JavaDoc service);
59     
60     
61     private static boolean verbose =
62         System.getProperty ("netbeans.debugger.registration") != null;
63     
64     
65     static class Instance extends Lookup {
66         private Object JavaDoc[] services;
67         
68         Instance (Object JavaDoc[] services) {
69             this.services = services;
70         }
71         
72         public List JavaDoc lookup (String JavaDoc folder, Class JavaDoc service) {
73             ArrayList JavaDoc l = new ArrayList JavaDoc ();
74             int i, k = services.length;
75             for (i = 0; i < k; i++)
76                 if (service.isAssignableFrom (services [i].getClass ())) {
77                     l.add (services [i]);
78                     if (verbose)
79                         System.out.println("\nR instance " + services [i] +
80                             " found");
81                 }
82             return l;
83         }
84         
85     }
86     
87     static class Compound extends Lookup {
88         private Lookup l1;
89         private Lookup l2;
90         
91         Compound (Lookup l1, Lookup l2) {
92             this.l1 = l1;
93             this.l2 = l2;
94             setContext (this);
95         }
96         
97         public List JavaDoc lookup (String JavaDoc folder, Class JavaDoc service) {
98             List JavaDoc l = new LookupList(null);
99             l.addAll (l1.lookup (folder, service));
100             l.addAll (l2.lookup (folder, service));
101             return l;
102         }
103         
104         void setContext (Lookup context) {
105             if (l1 instanceof Compound) ((Compound) l1).setContext (context);
106             if (l1 instanceof MetaInf) ((MetaInf) l1).setContext (context);
107             if (l2 instanceof Compound) ((Compound) l2).setContext (context);
108             if (l2 instanceof MetaInf) ((MetaInf) l2).setContext (context);
109         }
110     }
111     
112     static class MetaInf extends Lookup {
113         
114         private static final String JavaDoc HIDDEN = "-hidden"; // NOI18N
115

116         private String JavaDoc rootFolder;
117         private HashMap JavaDoc registrationCache = new HashMap JavaDoc ();
118         private HashMap JavaDoc instanceCache = new HashMap JavaDoc ();
119         private Lookup context;
120
121         
122         MetaInf (String JavaDoc rootFolder) {
123             this.rootFolder = rootFolder;
124         }
125         
126         void setContext (Lookup context) {
127             this.context = context;
128         }
129         
130         public List JavaDoc lookup (String JavaDoc folder, Class JavaDoc service) {
131             List JavaDoc l = list (folder, service);
132             
133             Set JavaDoc s = null;
134             int i, k = l.size ();
135             for (i = 0; i < k; i++) {
136                 String JavaDoc className = (String JavaDoc) l.get (i);
137                 if (className.endsWith(HIDDEN)) {
138                     if (s == null) {
139                         s = new HashSet JavaDoc();
140                     }
141                     s.add(className.substring(0, className.length() - HIDDEN.length()));
142                 }
143             }
144             
145             LookupList ll = new LookupList (s);
146             for (i = 0; i < k; i++) {
147                 String JavaDoc className = (String JavaDoc) l.get (i);
148                 if (className.endsWith(HIDDEN)) continue;
149                 if (s != null && s.contains (className)) continue;
150                 Object JavaDoc instance = null;
151                 instance = instanceCache.get (className);
152                 if (instance == null) {
153                     instance = createInstance (className);
154                     instanceCache.put (className, instance);
155                 }
156                 if (instance != null)
157                     ll.add (instance, className);
158             }
159             return ll;
160         }
161         
162         private List JavaDoc list (String JavaDoc folder, Class JavaDoc service) {
163             String JavaDoc name = service.getName ();
164             String JavaDoc resourceName = "META-INF/debugger/" +
165                 ((rootFolder == null) ? "" : rootFolder + "/") +
166                 ((folder == null) ? "" : folder + "/") +
167                 name;
168             if (!registrationCache.containsKey (resourceName))
169                 registrationCache.put (resourceName, loadMetaInf (resourceName));
170             return (List JavaDoc) registrationCache.get (resourceName);
171         }
172     
173         /**
174          * Loads instances of given class from META-INF/debugger from given
175          * folder. Given context isused as the parameter to constructor.
176          */

177         private ArrayList JavaDoc loadMetaInf (
178             String JavaDoc resourceName
179         ) {
180             ArrayList JavaDoc l = new ArrayList JavaDoc ();
181             try {
182                 ClassLoader JavaDoc cl = (ClassLoader JavaDoc) org.openide.util.Lookup.
183                     getDefault ().lookup (ClassLoader JavaDoc.class);
184                 String JavaDoc v = "\nR lookup " + resourceName;
185                 Enumeration JavaDoc e = cl.getResources (resourceName);
186                 while (e.hasMoreElements ()) {
187                     URL JavaDoc url = (URL JavaDoc) e.nextElement();
188                     InputStream JavaDoc is = url.openStream ();
189                     if (is == null) continue;
190                     BufferedReader JavaDoc br = new BufferedReader JavaDoc (
191                         new InputStreamReader JavaDoc (is)
192                     );
193                     for (String JavaDoc s = br.readLine(); s != null; s = br.readLine()) {
194                         if (s.startsWith ("#")) continue;
195                         if (s.length () == 0) continue;
196                         if (verbose)
197                             v += "\nR service " + s + " found";
198
199                         l.add (s);
200                     }
201                 }
202                 if (verbose)
203                     System.out.println (v);
204                 return l;
205             } catch (IOException JavaDoc e) {
206                 e.printStackTrace ();
207             }
208             throw new InternalError JavaDoc ("Can not read from Meta-inf!");
209         }
210         
211         private Object JavaDoc createInstance (String JavaDoc service) {
212             try {
213                 ClassLoader JavaDoc cl = (ClassLoader JavaDoc) org.openide.util.Lookup.
214                     getDefault ().lookup (ClassLoader JavaDoc.class);
215                 String JavaDoc method = null;
216                 if (service.endsWith("()")) {
217                     int lastdot = service.lastIndexOf('.');
218                     if (lastdot < 0) {
219                         ErrorManager.getDefault().log("Bad service - dot before method name is missing: " +
220                                 "'" + service + "'.");
221                         return null;
222                     }
223                     method = service.substring(lastdot + 1, service.length() - 2).trim();
224                     service = service.substring(0, lastdot);
225                 }
226                 Class JavaDoc cls = cl.loadClass (service);
227
228                 Object JavaDoc o = null;
229                 if (method != null) {
230                     Method JavaDoc m = null;
231                     if (context != null) {
232                         try {
233                             m = cls.getDeclaredMethod(method, new Class JavaDoc[] { Lookup.class });
234                         } catch (NoSuchMethodException JavaDoc nsmex) {}
235                     }
236                     if (m == null) {
237                         try {
238                             m = cls.getDeclaredMethod(method, new Class JavaDoc[] { });
239                         } catch (NoSuchMethodException JavaDoc nsmex) {}
240                     }
241                     if (m != null) {
242                         o = m.invoke(null, (m.getParameterTypes().length == 0)
243                                      ? new Object JavaDoc[] {} : new Object JavaDoc[] { context });
244                     }
245                 }
246                 if (o == null && context != null) {
247                     Constructor JavaDoc[] cs = cls.getConstructors ();
248                     int i, k = cs.length;
249                     for (i = 0; i < k; i++) {
250                         Constructor JavaDoc c = cs [i];
251                         if (c.getParameterTypes ().length != 1) continue;
252                         try {
253                             o = c.newInstance (new Object JavaDoc[] {context});
254                         } catch (IllegalAccessException JavaDoc e) {
255                             if (verbose) {
256                                 System.out.println("\nservice: " + service);
257                                 e.printStackTrace ();
258                             }
259                         } catch (IllegalArgumentException JavaDoc e) {
260                             if (verbose) {
261                                 System.out.println("\nservice: " + service);
262                                 e.printStackTrace ();
263                             }
264                         }
265                     }
266                 }
267                 if (o == null)
268                     o = cls.newInstance ();
269                 if (verbose)
270                     System.out.println("\nR instance " + o +
271                         " created");
272                 return o;
273             } catch (ClassNotFoundException JavaDoc e) {
274                 ErrorManager.getDefault().notify(
275                         ErrorManager.getDefault().annotate(
276                             e,
277                             "The service "+service+" is not found.")
278                         );
279             } catch (InstantiationException JavaDoc e) {
280                 ErrorManager.getDefault().notify(
281                         ErrorManager.getDefault().annotate(
282                             e,
283                             "The service "+service+" can not be instantiated.")
284                         );
285             } catch (IllegalAccessException JavaDoc e) {
286                 ErrorManager.getDefault().notify(
287                         ErrorManager.getDefault().annotate(
288                             e,
289                             "The service "+service+" can not be accessed.")
290                         );
291             } catch (InvocationTargetException JavaDoc ex) {
292                 ErrorManager.getDefault().notify(
293                         ErrorManager.getDefault().annotate(
294                             ex,
295                             "The service "+service+" can not be created.")
296                         );
297             } catch (ExceptionInInitializerError JavaDoc ex) {
298                 ErrorManager.getDefault().notify(
299                         ErrorManager.getDefault().annotate(
300                             ex,
301                             "The service "+service+" can not be initialized.")
302                         );
303             }
304             return null;
305         }
306         
307     }
308     
309     /**
310      * A special List implementation, which ensures that hidden elements
311      * are removed when adding items into the list.
312      */

313     private static final class LookupList extends ArrayList JavaDoc {
314
315         private Set JavaDoc hiddenClassNames;
316         private LinkedHashMap JavaDoc instanceClassNames = new LinkedHashMap JavaDoc();
317
318         public LookupList(Set JavaDoc hiddenClassNames) {
319             this.hiddenClassNames = hiddenClassNames;
320         }
321         
322         void add(Object JavaDoc instance, String JavaDoc className) {
323             super.add(instance);
324             instanceClassNames.put(instance, className);
325         }
326         
327         public boolean addAll(Collection JavaDoc c) {
328             if (c instanceof LookupList) {
329                 LookupList ll = (LookupList) c;
330                 Set JavaDoc newHiddenClassNames = ll.hiddenClassNames;
331                 if (newHiddenClassNames != null) {
332                     // Check the instances we have and remove the newly hidden ones:
333
for (Iterator JavaDoc it = newHiddenClassNames.iterator(); it.hasNext(); ) {
334                         String JavaDoc className = (String JavaDoc) it.next();
335                         if (instanceClassNames.containsValue(className)) {
336                             for (Iterator JavaDoc ii = instanceClassNames.keySet().iterator(); it.hasNext(); ) {
337                                 Object JavaDoc instance = ii.next();
338                                 if (className.equals(instanceClassNames.get(instance))) {
339                                     remove(instance);
340                                     instanceClassNames.remove(instance);
341                                     break;
342                                 }
343                             }
344                         }
345                     }
346                     if (hiddenClassNames != null) {
347                         hiddenClassNames.addAll(newHiddenClassNames);
348                     } else {
349                         hiddenClassNames = newHiddenClassNames;
350                     }
351                 }
352                 ensureCapacity(size() + ll.size());
353                 boolean addedAnything = false;
354                 for (Iterator JavaDoc it = ll.iterator(); it.hasNext(); ) {
355                     Object JavaDoc instance = it.next();
356                     String JavaDoc className = (String JavaDoc) ll.instanceClassNames.get(instance);
357                     if (hiddenClassNames == null || !hiddenClassNames.contains(className)) {
358                         add(instance, className);
359                         addedAnything = true;
360                     }
361                 }
362                 return addedAnything;
363             } else {
364                 return super.addAll(c);
365             }
366         }
367         
368     }
369     
370 }
371
Popular Tags