KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > FactoryRegistry


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
21 package org.netbeans.modules.i18n;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import org.openide.util.Lookup;
32 import org.openide.util.LookupEvent;
33 import org.openide.util.LookupListener;
34
35
36 /**
37  * Registry which gets i18n support factories for specified data objects.
38  * It gets the factories which are registered in SFS/Services/i18n directory
39  * via lookup.
40  *
41  * @author Peter Zavadsky
42  * @see I18nSupport.Factory
43  * @see org.netbeans.modules.i18n.form.FormI18nSupport.Factory
44  * @see org.netbeans.modules.i18n.jsp.JspI18nSupport.Factory
45  */

46 public final class FactoryRegistry extends Object JavaDoc {
47
48     private FactoryRegistry() {};
49     
50     /** All i18n supports kept as <code>Lookup.Result</code>. */
51     private static Lookup.Result result;
52     private static final Set JavaDoc cache = Collections.synchronizedSet(new HashSet JavaDoc(5));
53     private static final Set JavaDoc ncache = Collections.synchronizedSet(new HashSet JavaDoc(50));
54     
55     /** Gets lookup result holding script type instances. */
56     private static Lookup.Result getSupports() {
57         if(result == null) {
58             result = Lookup.getDefault().lookup(new Lookup.Template(I18nSupport.Factory.class));
59             result.addLookupListener(new LookupListener() {
60                 public void resultChanged(LookupEvent e) {
61                     cache.clear();
62                     ncache.clear();
63                 }
64             });
65         }
66         
67         return result;
68     }
69     
70     /** Gets <code>I18nSupportFactory</code> for specified data object class.
71      * @return factory for specified data object class or <code>null</code> */

72     public static I18nSupport.Factory getFactory(Class JavaDoc dataObjectClass) {
73         
74         List JavaDoc candidates = new ArrayList JavaDoc(3);
75         
76         for(Iterator JavaDoc it = getSupports().allInstances().iterator(); it.hasNext(); ) {
77             I18nSupport.Factory factory = (I18nSupport.Factory)it.next();
78
79             // XXX it has to be checked for null, for cases Jsp support and java support
80
// don't have their modules available, see JspI18nSupportFactory.getDataObjectClass.
81
Class JavaDoc clazz = factory.getDataObjectClass();
82             
83             if(clazz != null && clazz.isAssignableFrom(dataObjectClass)) {
84                 candidates.add(factory);
85             }
86         }
87         
88         if(candidates.size() == 0) {
89             return null;
90         } else if(candidates.size() == 1) {
91             return (I18nSupport.Factory)candidates.get(0);
92         } else {
93             I18nSupport.Factory chosen = null;
94             
95             // Find factory which supported class data object
96
// is the lowest one in the class hierarchy.
97
for(Iterator JavaDoc it = candidates.iterator(); it.hasNext(); ) {
98                 I18nSupport.Factory fct = (I18nSupport.Factory)it.next();
99                 
100                 if(chosen == null) {
101                     chosen = fct;
102                     continue;
103                 }
104
105                 if(chosen.getDataObjectClass().isAssignableFrom(fct.getDataObjectClass()) ) {
106                     chosen = fct;
107                 }
108             }
109             
110             return chosen;
111         }
112     }
113
114     /**
115      * Indicates if there is a factory for that data object class.
116      * It queried very often from interactive mode.
117      */

118     public static boolean hasFactory(Class JavaDoc dataObjectClass) {
119         
120         if (cache.contains(dataObjectClass)) return true;
121         if (ncache.contains(dataObjectClass)) return false;
122         
123         for(Iterator JavaDoc it = getSupports().allInstances().iterator(); it.hasNext(); ) {
124             I18nSupport.Factory factory = (I18nSupport.Factory)it.next();
125
126             // XXX it has to be checked for null, for cases Jsp support and java support
127
// don't have their modules available, see JspI18nSupportFactory.getDataObjectClass.
128
Class JavaDoc clazz = factory.getDataObjectClass();
129             
130             if(clazz != null && clazz.isAssignableFrom(dataObjectClass)) {
131                 cache.add(dataObjectClass);
132                 return true;
133             }
134         }
135
136         ncache.add(dataObjectClass);
137         return false;
138     }
139         
140 }
141
Popular Tags