This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 98426
Collapse All | Expand All

(-)openide/util/apichanges.xml (+33 lines)
Lines 26-31 Link Here
26
    <apidef name="actions">Actions API</apidef>
26
    <apidef name="actions">Actions API</apidef>
27
</apidefs>
27
</apidefs>
28
<changes>
28
<changes>
29
    
30
    <change id="Lookups.getLookupForPath">
31
        <api name="util"/>
32
        <summary>Added <code>Lookups.forPath(String)</code> method</summary>
33
        <version major="7" minor="7"/>
34
        <date day="11" month="1" year="2007"/>
35
        <author login="tboudreau"/>
36
        <compatibility addition="yes"/>
37
        <description>
38
            <p>
39
                Frequently modules create registries of objects in folders the system
40
                filesystem.  The process of creating a Lookup that contains all
41
                objects registered there has so far been rather arduous:
42
                <code>new FolderLookup (DataFolder.findFolder(
43
                Repository.getDefault().getDefaultFileSystem().getRoot().getFileObject(
44
                &quot;com/foo/bar&quot;))).getLookup()</code>
45
            </p>
46
            <p>
47
                Added the method <code>Lookups.forPath(String)</code>
48
                which simply does the same thing as the above code if
49
                the DataSystems API is on the path.  Added the interface
50
                org.openide.util.lookup.PathBasedLookupProvider, which the
51
                DataSystems API now implements (that's where FolderLookup lives).
52
                This should considerably simplify creation of registries of
53
                objects in the system filesystem, and also not require a hard
54
                dependency on the DataSystems API for anyone who wants to do
55
                such a thing.
56
            </p>
57
        </description>
58
        <class package="org.openide.util.lookup" name="Lookups"/>
59
        <class package="org.openide.util.lookup" name="PathBasedLookupProvider"/>
60
        <issue number="98426"/>
61
    </change>
29
62
30
    <change id="Utilities.isMac">
63
    <change id="Utilities.isMac">
31
        <api name="util"/>
64
        <api name="util"/>
(-)openide/util/nbproject/project.properties (-1 / +1 lines)
Lines 19-25 Link Here
19
javac.source=1.5
19
javac.source=1.5
20
module.jar.dir=lib
20
module.jar.dir=lib
21
21
22
spec.version.base=7.7.0
22
spec.version.base=7.8
23
23
24
# For XMLSerializer, needed for XMLUtil.write to work w/ namespaces under JDK 1.4:
24
# For XMLSerializer, needed for XMLUtil.write to work w/ namespaces under JDK 1.4:
25
25
(-)openide/util/src/org/openide/util/lookup/Lookups.java (+29 lines)
Lines 21-26 Link Here
21
21
22
import java.util.Arrays;
22
import java.util.Arrays;
23
import java.util.Collections;
23
import java.util.Collections;
24
import java.util.logging.Level;
25
import java.util.logging.Logger;
24
import org.openide.util.Lookup;
26
import org.openide.util.Lookup;
25
27
26
/**
28
/**
Lines 135-140 Link Here
135
     */
137
     */
136
    public static Lookup metaInfServices(ClassLoader classLoader) {
138
    public static Lookup metaInfServices(ClassLoader classLoader) {
137
        return new MetaInfServicesLookup(classLoader);
139
        return new MetaInfServicesLookup(classLoader);
140
    }
141
    
142
    /**
143
     * Get a Lookup over a folder in the system filesystem which may have
144
     * objects registered in it (as <code>.instance</code>, <code>.settings</code>,
145
     * or any other file type whose DataObject provides an InstanceCookie).  
146
     * If your module defines a system filesystem folder which other modules
147
     * will register objects into, this is a convenient way of getting a 
148
     * Lookup containing all registered objects.
149
     * <p>
150
     * Note that this method is not entirely tied
151
     * to the system filesystem - whatever implements 
152
     * <a href="PathBasedLookupProvider.html">PathBasedLookupProvider</a>
153
     * provides what is returned by this method.
154
     * 
155
     * @since 7.8
156
     */ 
157
    public static Lookup forPath (String path) {
158
        PathBasedLookupProvider provider = Lookup.getDefault().lookup (
159
                PathBasedLookupProvider.class);
160
        if (provider == null) {
161
            Logger.getLogger(Lookups.class.getPackage().getName()).log(
162
                    Level.SEVERE,
163
                    "No PathBasedLookupProvider registered.  Probably " + //NOI18N
164
                    "the DataSystems API is not on the classpath"); //NOI18N
165
        }
166
        return provider == null ? Lookup.EMPTY : provider.getLookup(path);
138
    }
167
    }
139
168
140
    /** Creates a lookup that wraps another one and filters out instances
169
    /** Creates a lookup that wraps another one and filters out instances
(-)openide/util/src/org/openide/util/lookup/PathBasedLookupProvider.java (+43 lines)
Added Link Here
1
/*
2
 * PathBasedLookupProvider.java
3
 *
4
 * Created on March 20, 2007, 10:06 PM
5
 *
6
 * To change this template, choose Tools | Template Manager
7
 * and open the template in the editor.
8
 */
9
10
package org.openide.util.lookup;
11
12
import org.openide.util.Lookup;
13
14
/**
15
 * Interface for the core system to install a factory for Lookups over the
16
 * system filesystem or other hierarchical namespace.  
17
 * <p>
18
 * <b>If you are implementing this interface, you are probably doing something
19
 * wrong.</b>
20
 * It is very unusual to need to implement this interface - 
21
 * by default the implementation is the DataSystems API module, and uses
22
 * FolderLookup over the system filesystem.  Implement only if you plan
23
 * to place it in the default lookup to entirely replace the backing storage
24
 * for registries of objects provided by the system filesystem (at least for
25
 * those modules which use <code>Lookups.forPath()</code>).
26
 * 
27
 * @since 7.8
28
 * @see org.openide.util.lookup.Lookups#forPath(java.lang.String)
29
 *
30
 * @author Tim Boudreau
31
 */
32
public interface PathBasedLookupProvider {
33
    /**
34
     * Get a Lookup over a named &quot;folder&quot; in a hierarchical
35
     * registry things can be registered into.  The default implementation
36
     * provides a Lookup over the specified folder in the system filesystem.
37
     * The specified path uses / for separators with no leading or trailing
38
     * /'s.
39
     * @param path A string path, such as <code>com/foo/bar</code>
40
     * @return A Lookup instance over that folder.
41
     */ 
42
    public Lookup getLookup (String path);
43
}
(-)openide/util/test/unit/src/org/openide/util/lookup/PathBasedTest.java (+70 lines)
Added Link Here
1
/*
2
 * PathBasedTest.java
3
 *
4
 * Created on March 20, 2007, 10:46 PM
5
 *
6
 * To change this template, choose Tools | Template Manager
7
 * and open the template in the editor.
8
 */
9
10
package org.openide.util.lookup;
11
12
import java.util.HashMap;
13
import java.util.Map;
14
import org.netbeans.junit.MockServices;
15
import org.netbeans.junit.NbTestCase;
16
import org.openide.util.Lookup;
17
18
/**
19
 * This is a class
20
 *
21
 * @author Tim Boudreau
22
 */
23
public class PathBasedTest extends NbTestCase {
24
    
25
    public PathBasedTest(String name) {
26
        super (name);
27
    }
28
    
29
    public void setUp() {
30
        MockServices.setServices (PBImpl.class);
31
    }
32
    
33
    public void testFindImpl() {
34
        assertNotNull(Lookup.getDefault().lookup (PathBasedLookupProvider.class));
35
    }
36
    
37
    public void testFindLookup() {
38
        String path = "com/foo/bar";
39
        Lookup lkp = Lookups.forPath(path);
40
        Lookup lkp2 = Lookups.forPath (path);
41
        assertSame (lkp, lkp2);
42
        InstanceContent ic = impl.contentFor(lkp);
43
        assertNotNull (ic);
44
    }
45
    
46
    static PBImpl impl = null;
47
    public static final class PBImpl implements PathBasedLookupProvider {
48
        Map <String, Lookup> m = new HashMap <String, Lookup> ();
49
        Map <Lookup, InstanceContent> l2ic = new HashMap<Lookup, InstanceContent>();
50
        
51
        public PBImpl() {
52
            PathBasedTest.impl = this;
53
        }
54
        
55
        InstanceContent contentFor (Lookup lkp) {
56
            return l2ic.get(lkp);
57
        }
58
    
59
        public Lookup getLookup(String path) {
60
            Lookup result = m.get (path);
61
            if (result == null) {
62
                InstanceContent content = new InstanceContent();
63
                result = new AbstractLookup(content);
64
                l2ic.put (result, content);
65
                m.put (path, result);
66
            }
67
            return result;
68
        }
69
    }
70
}
(-)openide/loaders/manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.openide.loaders
2
OpenIDE-Module: org.openide.loaders
3
OpenIDE-Module-Specification-Version: 6.1
3
OpenIDE-Module-Specification-Version: 6.2
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
5
5
(-)openide/loaders/nbproject/project.xml (-2 / +2 lines)
Lines 69-75 Link Here
69
                    <build-prerequisite/>
69
                    <build-prerequisite/>
70
                    <compile-dependency/>
70
                    <compile-dependency/>
71
                    <run-dependency>
71
                    <run-dependency>
72
                        <specification-version>6.2</specification-version>
72
                        <specification-version>7.1</specification-version>
73
                    </run-dependency>
73
                    </run-dependency>
74
                </dependency>
74
                </dependency>
75
                <dependency>
75
                <dependency>
Lines 101-107 Link Here
101
                    <build-prerequisite/>
101
                    <build-prerequisite/>
102
                    <compile-dependency/>
102
                    <compile-dependency/>
103
                    <run-dependency>
103
                    <run-dependency>
104
                        <specification-version>7.3</specification-version>
104
                        <specification-version>7.8</specification-version>
105
                    </run-dependency>
105
                    </run-dependency>
106
                </dependency>
106
                </dependency>
107
                <dependency>
107
                <dependency>
(-)openide/loaders/src/META-INF/services/org.openide.util.lookup.PathBasedLookupProvider (+1 lines)
Added Link Here
1
org.netbeans.modules.openide.loaders.PathLookupProvider
(-)openide/loaders/src/org/netbeans/modules/openide/loaders/PathLookupProvider.java (+40 lines)
Added Link Here
1
/*
2
 * PathLookupProvider.java
3
 *
4
 * Created on March 20, 2007, 10:10 PM
5
 *
6
 * To change this template, choose Tools | Template Manager
7
 * and open the template in the editor.
8
 */
9
10
package org.netbeans.modules.openide.loaders;
11
12
import org.openide.filesystems.FileObject;
13
import org.openide.filesystems.FileUtil;
14
import org.openide.loaders.DataFolder;
15
import org.openide.loaders.FolderLookup;
16
import org.openide.util.Lookup;
17
import org.openide.util.lookup.Lookups;
18
import org.openide.util.lookup.PathBasedLookupProvider;
19
20
/**
21
 * This is a class
22
 *
23
 * @author Tim Boudreau
24
 */
25
public class PathLookupProvider implements PathBasedLookupProvider {
26
    
27
    public PathLookupProvider() {
28
    }
29
    
30
    public Lookup getLookup(String path) {
31
        Lookup result = null;
32
        FileObject ob = FileUtil.getConfigurationFolder(path, false);
33
        if (ob != null) {
34
            DataFolder fld = DataFolder.findFolder (ob);
35
            FolderLookup flookup = new FolderLookup (fld);
36
            result = fld.getLookup();
37
        }
38
        return result == null ? Lookup.EMPTY : result;
39
    }
40
}
(-)openide/loaders/test/unit/src/org/openide/loaders/PathLookupProviderTest.java (+81 lines)
Added Link Here
1
/*
2
 * PathLookupProviderTest.java
3
 *
4
 * Created on March 20, 2007, 10:58 PM
5
 *
6
 * To change this template, choose Tools | Template Manager
7
 * and open the template in the editor.
8
 */
9
10
package org.openide.loaders;
11
12
import java.io.IOException;
13
import java.util.Collection;
14
import org.netbeans.junit.MockServices;
15
import org.netbeans.junit.NbTestCase;
16
import org.openide.filesystems.FileObject;
17
import org.openide.filesystems.FileSystem;
18
import org.openide.filesystems.FileUtil;
19
import org.openide.filesystems.Repository;
20
import org.openide.loaders.PathLookupProviderTest.O;
21
import org.openide.util.Lookup;
22
import org.openide.util.lookup.Lookups;
23
24
/**
25
 * This is a class
26
 *
27
 * @author Tim Boudreau
28
 */
29
public class PathLookupProviderTest extends NbTestCase {
30
    
31
    /** Creates a new instance of PathLookupProviderTest */
32
    public PathLookupProviderTest(String name) {
33
        super (name);
34
    }
35
    
36
    FileObject data;
37
    static boolean objCreated = false;
38
    public void setUp() throws IOException {
39
        objCreated = false;
40
        MockServices.setServices (MyRepo.class);
41
        FileObject fld = FileUtil.getConfigurationFolder("somewhere/or/other", true);
42
        assertNotNull (fld);
43
        data = fld.createData ("org-openide-loaders-PathLookupProviderTest$O.instance");
44
    }
45
    
46
    public void testInstanceIsFound() {
47
        System.out.println("testInstanceIsFound");
48
        Lookup lkp = Lookups.forPath ("somewhere/or/other");
49
        assertNotNull (lkp);
50
        assertNotSame (Lookup.EMPTY, lkp);
51
        Collection <? extends O> c = lkp.lookupAll(O.class);
52
        assertTrue (objCreated);
53
        assertFalse (c.isEmpty());
54
    }
55
    
56
    public static class MyRepo extends Repository {
57
        static FileSystem sysfs;
58
        public MyRepo () {
59
            super(sysfs = FileUtil.createMemoryFileSystem());
60
        }
61
    }
62
63
64
    static char ch = 'A';
65
    public static final class O {
66
        private final String s;
67
        public O () {
68
            this ("" + (ch++));
69
        }
70
        
71
        O (String s) {
72
            this.s = s;
73
            objCreated = true;
74
        }
75
        
76
        public String toString() {
77
            return s;
78
        }
79
    }
80
    
81
}

Return to bug 98426