001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.wsdl.model.impl;
043:
044: import java.util.Collections;
045: import java.util.HashSet;
046: import java.util.Hashtable;
047: import java.util.Map;
048: import java.util.Set;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import javax.xml.namespace.QName;
052: import org.netbeans.modules.xml.wsdl.model.spi.*;
053: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
054: import org.openide.util.Lookup;
055: import org.openide.util.lookup.Lookups;
056:
057: /**
058: *
059: * @author rico
060: * @author Nam Nguyen
061: *
062: * Registry for factories of WSDL elements. In order to register an ElementFactory,
063: * a QName must be provided of an element for which the factory will create a
064: * WSDLComponent.
065: */
066: public class ElementFactoryRegistry {
067:
068: private static ElementFactoryRegistry registry = null;
069: private Map<QName, ElementFactory> factories = null;
070:
071: private ElementFactoryRegistry() {
072: initialize();
073: }
074:
075: public static ElementFactoryRegistry getDefault() {
076: if (registry == null) {
077: registry = new ElementFactoryRegistry();
078: }
079: return registry;
080: }
081:
082: //TODO listen on changes when we have external extensions
083: private void initialize() {
084: factories = new Hashtable<QName, ElementFactory>();
085: Lookup.Result results = Lookup.getDefault().lookup(
086: new Lookup.Template(ElementFactory.class));
087: for (Object service : results.allInstances()) {
088: register((ElementFactory) service);
089: }
090:
091: //try meta-inf services lookup using this class's classloader
092: //if no factories are found, This is required for lookup to work
093: //from comp app project ant task
094: if (factories.size() < 1) {
095: Lookup lu2 = Lookups.metaInfServices(this .getClass()
096: .getClassLoader());
097: Lookup.Result results2 = lu2.lookup(new Lookup.Template(
098: ElementFactory.class));
099: for (Object service : results2.allInstances()) {
100: register((ElementFactory) service);
101: }
102: }
103: }
104:
105: public void register(ElementFactory factory) {
106: for (QName q : factory.getElementQNames()) {
107: factories.put(q, factory);
108: }
109: resetQNameCache();
110: }
111:
112: public void unregister(ElementFactory fac) {
113: for (QName q : fac.getElementQNames()) {
114: factories.remove(q);
115: }
116: resetQNameCache();
117: }
118:
119: public ElementFactory get(QName type) {
120: return factories.get(type);
121: }
122:
123: private Set<Class> knownEmbeddedModelTypes = null;
124: private Set<QName> knownQNames = null;
125: private Set<String> knownNames = null;
126:
127: public void resetQNameCache() {
128: knownEmbeddedModelTypes = null;
129: knownQNames = null;
130: knownNames = null;
131: }
132:
133: public Set<QName> getKnownQNames() {
134: return Collections.unmodifiableSet(knownQNames());
135: }
136:
137: private Set<QName> knownQNames() {
138: if (knownQNames == null) {
139: knownQNames = new HashSet<QName>();
140: for (ElementFactory f : factories.values()) {
141: for (QName q : f.getElementQNames()) {
142: if (!knownQNames.add(q)) {
143: String msg = "Duplicate factory for: " + q;
144: Logger.getLogger(this .getClass().getName())
145: .log(Level.FINE, "getKnownQNames", msg); //NOI18N
146: }
147: }
148: }
149: }
150: return knownQNames;
151: }
152:
153: public Set<String> getKnownElementNames() {
154: return Collections.unmodifiableSet(knownElementNames());
155: }
156:
157: private Set<String> knownElementNames() {
158: if (knownNames == null) {
159: knownNames = new HashSet<String>();
160: for (QName q : knownQNames()) {
161: knownNames.add(q.getLocalPart());
162: }
163: }
164: return knownNames;
165: }
166:
167: public void addEmbeddedModelQNames(
168: AbstractDocumentModel embeddedModel) {
169: if (knownEmbeddedModelTypes == null) {
170: knownEmbeddedModelTypes = new HashSet();
171: }
172: if (!knownEmbeddedModelTypes.contains(embeddedModel.getClass())) {
173: knownQNames().addAll(embeddedModel.getQNames());
174: knownNames = null;
175: }
176: }
177: }
|