2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc.
29
* Portions Copyrighted 2007 Sun Microsystems, Inc.
32
package org.netbeans.modules.settings;
34
import java.io.IOException;
35
import java.util.Collection;
36
import java.util.Collections;
37
import java.util.logging.Logger;
38
import org.netbeans.modules.openide.util.NamedServicesProvider;
39
import org.openide.filesystems.FileObject;
40
import org.openide.filesystems.FileUtil;
41
import org.openide.filesystems.FileUtil;
42
import org.openide.filesystems.Repository;
43
import org.openide.loaders.DataFolder;
44
import org.openide.util.Lookup;
45
import org.openide.util.LookupEvent;
46
import org.openide.util.LookupListener;
47
import org.openide.util.WeakListeners;
48
import org.openide.util.lookup.Lookups;
49
import org.openide.util.lookup.ProxyLookup;
51
/** Use FolderLookup to find out intances of named services.
53
* @author Jaroslav Tulach
55
public final class RecognizeInstanceObjects extends NamedServicesProvider {
56
private static final Logger LOG = Logger.getLogger(RecognizeInstanceObjects.class.getName());
59
public Lookup create(String path) {
60
return new OverObjects(path);
64
private static final class OverObjects extends ProxyLookup
65
implements LookupListener {
66
private static Lookup.Result<ClassLoader> CL = Lookup.getDefault().lookupResult(ClassLoader.class);
68
private final String path;
70
public OverObjects(String path) {
71
super(delegates(path));
73
CL.addLookupListener(WeakListeners.create(LookupListener.class, this, CL));
76
@SuppressWarnings("deprecation")
77
private static Lookup[] delegates(String path) {
78
Collection<? extends ClassLoader> allCL = CL.allInstances();
79
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
81
allCL = Collections.singleton(ccl);
83
if (allCL.isEmpty()) {
84
allCL = Collections.singleton(RecognizeInstanceObjects.class.getClassLoader());
88
FileObject fo = FileUtil.createFolder(Repository.getDefault().getDefaultFileSystem().getRoot(), path);
91
if (path.endsWith("/")) { // NOI18N
92
s = path.substring(0, path.length() - 1);
97
org.openide.loaders.FolderLookup l;
98
l = new org.openide.loaders.FolderLookup(DataFolder.findFolder(fo), s);
99
return new Lookup[] { l.getLookup(), Lookups.metaInfServices(allCL.iterator().next(), "META-INF/namedservices/" + path) }; // NOI18N
100
} catch (IOException ex) {
101
return new Lookup[] { Lookups.metaInfServices(allCL.iterator().next(), "META-INF/namedservices/" + path) }; // NOI18N
106
public void resultChanged(LookupEvent ev) {
107
setLookups(delegates(path));
109
} // end of OverObjects