~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to core/settings/src/org/netbeans/modules/settings/RecognizeInstanceObjects.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc.
 
28
 *
 
29
 * Portions Copyrighted 2007 Sun Microsystems, Inc.
 
30
 */
 
31
 
 
32
package org.netbeans.modules.settings;
 
33
 
 
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;
 
50
 
 
51
/** Use FolderLookup to find out intances of named services.
 
52
 *
 
53
 * @author Jaroslav Tulach
 
54
 */
 
55
public final class RecognizeInstanceObjects extends NamedServicesProvider {
 
56
    private static final Logger LOG = Logger.getLogger(RecognizeInstanceObjects.class.getName());
 
57
    
 
58
    
 
59
    public Lookup create(String path) {
 
60
        return new OverObjects(path);
 
61
    }        
 
62
    
 
63
    
 
64
    private static final class OverObjects extends ProxyLookup 
 
65
    implements LookupListener {
 
66
        private static Lookup.Result<ClassLoader> CL = Lookup.getDefault().lookupResult(ClassLoader.class);
 
67
        
 
68
        private final String path;
 
69
        
 
70
        public OverObjects(String path) {
 
71
            super(delegates(path));
 
72
            this.path = path;
 
73
            CL.addLookupListener(WeakListeners.create(LookupListener.class, this, CL));
 
74
        }
 
75
        
 
76
        @SuppressWarnings("deprecation")
 
77
        private static Lookup[] delegates(String path) {
 
78
            Collection<? extends ClassLoader> allCL = CL.allInstances();
 
79
            ClassLoader ccl = Thread.currentThread().getContextClassLoader();
 
80
            if (ccl != null) {
 
81
                allCL = Collections.singleton(ccl);
 
82
            } else {
 
83
                if (allCL.isEmpty()) {
 
84
                    allCL = Collections.singleton(RecognizeInstanceObjects.class.getClassLoader());
 
85
                }
 
86
            }
 
87
            try {
 
88
                FileObject fo = FileUtil.createFolder(Repository.getDefault().getDefaultFileSystem().getRoot(), path);
 
89
                
 
90
                String s;
 
91
                if (path.endsWith("/")) { // NOI18N
 
92
                    s = path.substring(0, path.length() - 1);
 
93
                } else {
 
94
                    s = path;
 
95
                }
 
96
                
 
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
 
102
            }
 
103
            
 
104
        }
 
105
    
 
106
        public void resultChanged(LookupEvent ev) {
 
107
            setLookups(delegates(path));
 
108
        }
 
109
    } // end of OverObjects
 
110
}