openide.util/src/org/netbeans/modules/openide/util/NamedServicesProvider.java
author Jesse Glick <jglick@netbeans.org>
Wed, 18 Nov 2009 12:27:13 -0500
changeset 852 b1cecb0b4f89
parent 829 3b2ed3e1f01b
child 866 571882a2e8c7
permissions -rw-r--r--
#176798: stack overflow fix in RecognizeInstanceFiles needed to be generalized to work in JNLP mode.
jtulach@274
     1
/*
jtulach@302
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@274
     3
 *
mzlamal@829
     4
 * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
jtulach@274
     5
 *
jtulach@302
     6
 * The contents of this file are subject to the terms of either the GNU
jtulach@302
     7
 * General Public License Version 2 only ("GPL") or the Common
jtulach@302
     8
 * Development and Distribution License("CDDL") (collectively, the
jtulach@302
     9
 * "License"). You may not use this file except in compliance with the
jtulach@302
    10
 * License. You can obtain a copy of the License at
jtulach@302
    11
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@302
    12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@302
    13
 * specific language governing permissions and limitations under the
jtulach@302
    14
 * License.  When distributing the software, include this License Header
jtulach@302
    15
 * Notice in each file and include the License file at
jtulach@302
    16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@302
    17
 * particular file as subject to the "Classpath" exception as provided
jtulach@302
    18
 * by Sun in the GPL Version 2 section of the License file that
jtulach@302
    19
 * accompanied this code. If applicable, add the following below the
jtulach@302
    20
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@302
    21
 * your own identifying information:
jtulach@274
    22
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@274
    23
 *
jtulach@302
    24
 * Contributor(s):
jtulach@302
    25
 *
jtulach@274
    26
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@302
    27
 * Software is Sun Microsystems, Inc.
jtulach@274
    28
 *
jtulach@274
    29
 * Portions Copyrighted 2006 Sun Microsystems, Inc.
jtulach@274
    30
 */
jtulach@274
    31
jtulach@274
    32
package org.netbeans.modules.openide.util;
jtulach@274
    33
jtulach@274
    34
import java.lang.ref.Reference;
jtulach@274
    35
import java.lang.ref.WeakReference;
jglick@386
    36
import java.util.Collections;
jtulach@274
    37
import java.util.HashMap;
jtulach@274
    38
import java.util.Map;
jtulach@274
    39
import org.openide.util.Lookup;
jtulach@274
    40
import org.openide.util.lookup.Lookups;
jtulach@274
    41
jtulach@274
    42
/** Interface for core/startup and core/settings
jtulach@274
    43
 * to provide lookup over system filesystem.
jtulach@274
    44
 *
jtulach@274
    45
 * @author Jaroslav Tulach
jtulach@274
    46
 */
jtulach@274
    47
public abstract class NamedServicesProvider {
jglick@386
    48
jglick@386
    49
    private static final Map<String,Reference<Lookup>> map = Collections.synchronizedMap(new HashMap<String,Reference<Lookup>>());
jtulach@274
    50
    
jtulach@274
    51
    public abstract Lookup create(String path);
jtulach@274
    52
    
jglick@386
    53
    public static Lookup find(String path) {
jtulach@274
    54
        if (!path.endsWith("/")) {
jtulach@274
    55
            path = path + "/";
jtulach@274
    56
        }
jtulach@274
    57
        
jtulach@274
    58
        Reference<Lookup> ref = map.get(path);
jtulach@274
    59
        Lookup lkp = ref == null ? null : ref.get();
jtulach@274
    60
        if (lkp != null) {
jtulach@274
    61
            return lkp;
jtulach@274
    62
        }
jtulach@274
    63
        NamedServicesProvider prov = Lookup.getDefault().lookup(NamedServicesProvider.class);
jglick@852
    64
        if (prov != null && /* avoid stack overflow during initialization */ !path.equals("URLStreamHandler/nbres/")) { // NOI18N
jtulach@274
    65
            lkp = prov.create(path);
jtulach@274
    66
        } else {
jtulach@274
    67
            ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
jtulach@274
    68
            if (l == null) {
jtulach@274
    69
                l = Thread.currentThread().getContextClassLoader();
jtulach@274
    70
                if (l == null) {
jtulach@274
    71
                    l = NamedServicesProvider.class.getClassLoader();
jtulach@274
    72
                }
jtulach@274
    73
            }
jtulach@274
    74
            lkp = Lookups.metaInfServices(l, "META-INF/namedservices/" + path);
jtulach@274
    75
        }
jtulach@274
    76
        
jtulach@274
    77
        map.put(path, new WeakReference<Lookup>(lkp));
jtulach@274
    78
        return lkp;
jtulach@274
    79
    }
jtulach@274
    80
    
jtulach@274
    81
}