KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > impl > ElementFactoryRegistry


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.modules.xml.wsdl.model.impl;
21
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import javax.xml.namespace.QName JavaDoc;
30 import org.netbeans.modules.xml.wsdl.model.spi.*;
31 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
32 import org.openide.util.Lookup;
33 import org.openide.util.lookup.Lookups;
34
35 /**
36  *
37  * @author rico
38  * @author Nam Nguyen
39  *
40  * Registry for factories of WSDL elements. In order to register an ElementFactory,
41  * a QName must be provided of an element for which the factory will create a
42  * WSDLComponent.
43  */

44 public class ElementFactoryRegistry {
45     
46     private static ElementFactoryRegistry registry = null;
47     private Map JavaDoc<QName JavaDoc, ElementFactory> factories = null;
48     
49     private ElementFactoryRegistry() {
50         initialize();
51     }
52     
53     public static ElementFactoryRegistry getDefault(){
54         if (registry == null) {
55             registry = new ElementFactoryRegistry();
56         }
57         return registry;
58     }
59
60     //TODO listen on changes when we have external extensions
61
private void initialize(){
62         factories = new Hashtable JavaDoc<QName JavaDoc, ElementFactory>();
63         Lookup.Result results = Lookup.getDefault().lookup(new Lookup.Template(ElementFactory.class));
64         for (Object JavaDoc service : results.allInstances()){
65             register((ElementFactory)service);
66         }
67         
68         //try meta-inf services lookup using this class's classloader
69
//if no factories are found, This is required for lookup to work
70
//from comp app project ant task
71
if (factories.size() < 1) {
72             Lookup lu2 = Lookups.metaInfServices(this.getClass().getClassLoader());
73             Lookup.Result results2 = lu2.lookup(new Lookup.Template(ElementFactory.class));
74             for (Object JavaDoc service : results2.allInstances()){
75                 register((ElementFactory)service);
76             }
77         }
78     }
79     
80     public void register(ElementFactory factory) {
81         for (QName JavaDoc q : factory.getElementQNames()) {
82             factories.put(q, factory);
83         }
84         resetQNameCache();
85     }
86     
87     public void unregister(ElementFactory fac){
88         for (QName JavaDoc q : fac.getElementQNames()) {
89             factories.remove(q);
90         }
91         resetQNameCache();
92     }
93     
94     public ElementFactory get(QName JavaDoc type){
95         return factories.get(type);
96     }
97     
98     private Set JavaDoc<Class JavaDoc> knownEmbeddedModelTypes = null;
99     private Set JavaDoc<QName JavaDoc> knownQNames = null;
100     private Set JavaDoc<String JavaDoc> knownNames = null;
101     
102     public void resetQNameCache() {
103         knownEmbeddedModelTypes = null;
104         knownQNames = null;
105         knownNames = null;
106     }
107     
108     public Set JavaDoc<QName JavaDoc> getKnownQNames() {
109         return Collections.unmodifiableSet(knownQNames());
110     }
111     
112     private Set JavaDoc<QName JavaDoc> knownQNames() {
113         if (knownQNames == null) {
114             knownQNames = new HashSet JavaDoc<QName JavaDoc>();
115             for (ElementFactory f : factories.values()) {
116                 for (QName JavaDoc q : f.getElementQNames()) {
117                     if (! knownQNames.add(q)) {
118                         String JavaDoc msg = "Duplicate factory for: "+q;
119                         Logger.getLogger(this.getClass().getName()).log(Level.FINE, "getKnownQNames", msg); //NOI18N
120
}
121                 }
122             }
123         }
124         return knownQNames;
125     }
126
127     public Set JavaDoc<String JavaDoc> getKnownElementNames() {
128         return Collections.unmodifiableSet(knownElementNames());
129     }
130     
131     private Set JavaDoc<String JavaDoc> knownElementNames() {
132         if (knownNames == null) {
133             knownNames = new HashSet JavaDoc<String JavaDoc>();
134             for (QName JavaDoc q : knownQNames()) {
135                 knownNames.add(q.getLocalPart());
136             }
137         }
138         return knownNames;
139     }
140     
141     public void addEmbeddedModelQNames(AbstractDocumentModel embeddedModel) {
142         if (knownEmbeddedModelTypes == null) {
143             knownEmbeddedModelTypes = new HashSet JavaDoc();
144         }
145         if (! knownEmbeddedModelTypes.contains(embeddedModel.getClass())) {
146             knownQNames().addAll(embeddedModel.getQNames());
147             knownNames = null;
148         }
149     }
150 }
151
Popular Tags