KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalog > spi > ProvidersRegistry


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 package org.netbeans.modules.xml.catalog.spi;
20
21 import java.util.*;
22 import java.io.IOException JavaDoc;
23
24 import org.openide.util.Lookup;
25
26 /**
27  * A utility class representing the registry of SPI implementations.
28  * It contains implementations classes.
29  * <p>
30  * It should be moved out of SPI package.
31  *
32  * @author Petr Kuzel
33  * @version 1.0
34  */

35 public final class ProvidersRegistry {
36
37     /**
38      * Queries Lookup for all registered catalog providers returning provided Classes.
39      * @param filter an array of SPI interfaces that must the catalog Class implement or <tt>null</tt>
40      * @return Iterator<Class> of currently registered catalogs.
41      */

42     public static final synchronized Iterator getProviderClasses(Class JavaDoc[] filter) {
43
44         Lookup.Template template = new Lookup.Template(CatalogProvider.class);
45         Lookup.Result res = Lookup.getDefault().lookup(template);
46         Iterator it = res.allInstances().iterator();
47         Set set = new LinkedHashSet();
48
49         while(it.hasNext()) {
50             try {
51                 CatalogProvider next = (CatalogProvider) it.next();
52                 set.add(next.provideClass());
53             } catch (ClassNotFoundException JavaDoc ex) {
54                 //ignore it
55
} catch (IOException JavaDoc ex) {
56                 //ignore it
57
}
58         }
59         
60         it = set.iterator();
61         
62         if (filter == null)
63             return it;
64
65         ArrayList list = new ArrayList();
66                 
67 try_next_provider_class:
68         while (it.hasNext()) {
69             Class JavaDoc next = (Class JavaDoc) it.next();
70             
71             // provider test
72

73             for (int i=0; i<filter.length; i++) {
74                 
75                 if (filter[i].isAssignableFrom(next) == false)
76                     continue try_next_provider_class;
77             }
78             
79             // test passed
80

81             list.add(next);
82         }
83         
84         return list.iterator();
85     }
86           
87 }
88
Popular Tags