openide.util.lookup/src/org/openide/util/lookup/implspi/NamedServicesProvider.java
1.1 --- a/openide.util.lookup/src/org/openide/util/lookup/implspi/NamedServicesProvider.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,137 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * Contributor(s):
1.28 - *
1.29 - * The Original Software is NetBeans. The Initial Developer of the Original
1.30 - * Software is Sun Microsystems, Inc.
1.31 - *
1.32 - * Portions Copyrighted 2006 Sun Microsystems, Inc.
1.33 - */
1.34 -
1.35 -package org.openide.util.lookup.implspi;
1.36 -
1.37 -import java.lang.ref.Reference;
1.38 -import java.lang.ref.WeakReference;
1.39 -import java.util.Collections;
1.40 -import java.util.HashMap;
1.41 -import java.util.Map;
1.42 -import org.openide.util.Lookup;
1.43 -import org.openide.util.lookup.Lookups;
1.44 -
1.45 -/** Infrastructure provider interface for those who control the overall
1.46 - * registration of services in the system. The first instance of this interface
1.47 - * found in {@link Lookup#getDefault()} is consulted when providing answers
1.48 - * to {@link Lookups#forPath(java.lang.String)} queries. Current implementation
1.49 - * is not ready for multiple instances of this interface (the first one wins)
1.50 - * and also changing the instances during runtime.
1.51 - *
1.52 - * <div class="nonnormative">
1.53 - * The basic implementation of this interface is provided in
1.54 - * <a href="@org-openide-filesystems@/overview-summary.html">Filesystem API</a>
1.55 - * and recognizes the
1.56 - * <a href="@org-openide-util@/org/openide/util/doc-files/api.html#instance-folders">.instance files</a>
1.57 - * registered in XML layers. As such one can rely on
1.58 - * <a href="@org-openide-util@/org/openide/util/doc-files/api.html#instance-folders">.instance files</a>
1.59 - * being recognized in unit tests, if the
1.60 - * <a href="@org-openide-filesystems@/overview-summary.html">Filesystem API</a>
1.61 - * is included.
1.62 - * The implementation
1.63 - * is then refined in
1.64 - * <a href="@org-netbeans-modules-settings@/overview-summary.html">Settings API</a>
1.65 - * to handle also <a href="@org-openide-util@/org/openide/util/doc-files/api.html#settings">.settings files</a>.
1.66 - * Again, including this module in unit tests
1.67 - * ensures
1.68 - * <a href="@org-openide-util@/org/openide/util/doc-files/api.html#settings">.settings files</a>
1.69 - * files are recognized.
1.70 - * </div>
1.71 - *
1.72 - * @author Jaroslav Tulach
1.73 - * @since 8.1
1.74 - */
1.75 -public abstract class NamedServicesProvider {
1.76 - private static final Map<String,Reference<Lookup>> namedServicesProviders = Collections.synchronizedMap(new HashMap<String,Reference<Lookup>>());
1.77 -
1.78 - public static Lookup forPath(String path) {
1.79 -
1.80 - Reference<Lookup> ref = namedServicesProviders.get(path);
1.81 - Lookup lkp = ref == null ? null : ref.get();
1.82 - if (lkp != null) {
1.83 - return lkp;
1.84 - }
1.85 - NamedServicesProvider prov = Lookup.getDefault().lookup(NamedServicesProvider.class);
1.86 - if (prov != null &&
1.87 - /* avoid stack overflow during initialization */
1.88 - !path.startsWith(
1.89 - "URLStreamHandler/"
1.90 - /*URLStreamHandlerRegistrationProcessor.REGISTRATION_PREFIX*/
1.91 - )
1.92 - ) {
1.93 - lkp = prov.create(path);
1.94 - } else {
1.95 - ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
1.96 - if (l == null) {
1.97 - l = Thread.currentThread().getContextClassLoader();
1.98 - if (l == null) {
1.99 - l = NamedServicesProvider.class.getClassLoader();
1.100 - }
1.101 - }
1.102 - lkp = Lookups.metaInfServices(l, "META-INF/namedservices/" + path);
1.103 - }
1.104 -
1.105 - namedServicesProviders.put(path, new WeakReference<Lookup>(lkp));
1.106 - return lkp;
1.107 - }
1.108 -
1.109 - /** Throws an exception. Prevents unwanted instantiation of this class
1.110 - * by unknown subclasses.
1.111 - */
1.112 - protected NamedServicesProvider() {
1.113 - if (getClass().getName().equals("org.openide.util.lookup.PathInLookupTest$P")) { // NOI18N
1.114 - // OK for tests
1.115 - return;
1.116 - }
1.117 - if (getClass().getName().equals("org.openide.util.UtilitiesTest$NamedServicesProviderImpl")) { // NOI18N
1.118 - // OK for tests
1.119 - return;
1.120 - }
1.121 - if (getClass().getName().equals("org.netbeans.modules.openide.filesystems.RecognizeInstanceFiles")) { // NOI18N
1.122 - // OK for openide.filesystems
1.123 - return;
1.124 - }
1.125 - if (getClass().getName().equals("org.netbeans.modules.settings.RecognizeInstanceObjects")) { // NOI18N
1.126 - // OK for settings
1.127 - return;
1.128 - }
1.129 - throw new IllegalStateException();
1.130 - }
1.131 -
1.132 - /** Create the lookup for given path. Called as a result of query to
1.133 - * {@link Lookups#forPath(java.lang.String)}.
1.134 - *
1.135 - * @param path the identification of the path
1.136 - * @return the lookup representing objects in this path.
1.137 - */
1.138 - protected abstract Lookup create(String path);
1.139 -
1.140 -}