? openide/util/enum/api Index: openide/util/apichanges.xml =================================================================== RCS file: /cvs/openide/util/apichanges.xml,v retrieving revision 1.22 diff -u -r1.22 apichanges.xml --- openide/util/apichanges.xml 11 Jan 2007 14:08:21 -0000 1.22 +++ openide/util/apichanges.xml 21 Mar 2007 06:40:54 -0000 @@ -26,6 +26,39 @@ Actions API + + + + Added Lookups.forPath(String) method + + + + + +

+ Frequently modules create registries of objects in folders the system + filesystem. The process of creating a Lookup that contains all + objects registered there has so far been rather arduous: + new FolderLookup (DataFolder.findFolder( + Repository.getDefault().getDefaultFileSystem().getRoot().getFileObject( + "com/foo/bar"))).getLookup() +

+

+ Added the method Lookups.forPath(String) + which simply does the same thing as the above code if + the DataSystems API is on the path. Added the interface + org.openide.util.lookup.PathBasedLookupProvider, which the + DataSystems API now implements (that's where FolderLookup lives). + This should considerably simplify creation of registries of + objects in the system filesystem, and also not require a hard + dependency on the DataSystems API for anyone who wants to do + such a thing. +

+
+ + + +
Index: openide/util/nbproject/project.properties =================================================================== RCS file: /cvs/openide/util/nbproject/project.properties,v retrieving revision 1.25 diff -u -r1.25 project.properties --- openide/util/nbproject/project.properties 11 Jan 2007 14:08:22 -0000 1.25 +++ openide/util/nbproject/project.properties 21 Mar 2007 06:40:54 -0000 @@ -19,7 +19,7 @@ javac.source=1.5 module.jar.dir=lib -spec.version.base=7.7.0 +spec.version.base=7.8 # For XMLSerializer, needed for XMLUtil.write to work w/ namespaces under JDK 1.4: Index: openide/util/src/org/openide/util/lookup/Lookups.java =================================================================== RCS file: /cvs/openide/util/src/org/openide/util/lookup/Lookups.java,v retrieving revision 1.7 diff -u -r1.7 Lookups.java --- openide/util/src/org/openide/util/lookup/Lookups.java 1 Nov 2006 11:16:42 -0000 1.7 +++ openide/util/src/org/openide/util/lookup/Lookups.java 21 Mar 2007 06:40:55 -0000 @@ -21,6 +21,8 @@ import java.util.Arrays; import java.util.Collections; +import java.util.logging.Level; +import java.util.logging.Logger; import org.openide.util.Lookup; /** @@ -135,6 +137,33 @@ */ public static Lookup metaInfServices(ClassLoader classLoader) { return new MetaInfServicesLookup(classLoader); + } + + /** + * Get a Lookup over a folder in the system filesystem which may have + * objects registered in it (as .instance, .settings, + * or any other file type whose DataObject provides an InstanceCookie). + * If your module defines a system filesystem folder which other modules + * will register objects into, this is a convenient way of getting a + * Lookup containing all registered objects. + *

+ * Note that this method is not entirely tied + * to the system filesystem - whatever implements + * PathBasedLookupProvider + * provides what is returned by this method. + * + * @since 7.8 + */ + public static Lookup forPath (String path) { + PathBasedLookupProvider provider = Lookup.getDefault().lookup ( + PathBasedLookupProvider.class); + if (provider == null) { + Logger.getLogger(Lookups.class.getPackage().getName()).log( + Level.SEVERE, + "No PathBasedLookupProvider registered. Probably " + //NOI18N + "the DataSystems API is not on the classpath"); //NOI18N + } + return provider == null ? Lookup.EMPTY : provider.getLookup(path); } /** Creates a lookup that wraps another one and filters out instances Index: openide/util/src/org/openide/util/lookup/PathBasedLookupProvider.java =================================================================== RCS file: openide/util/src/org/openide/util/lookup/PathBasedLookupProvider.java diff -N openide/util/src/org/openide/util/lookup/PathBasedLookupProvider.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/util/src/org/openide/util/lookup/PathBasedLookupProvider.java 21 Mar 2007 06:40:55 -0000 @@ -0,0 +1,43 @@ +/* + * PathBasedLookupProvider.java + * + * Created on March 20, 2007, 10:06 PM + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package org.openide.util.lookup; + +import org.openide.util.Lookup; + +/** + * Interface for the core system to install a factory for Lookups over the + * system filesystem or other hierarchical namespace. + *

+ * If you are implementing this interface, you are probably doing something + * wrong. + * It is very unusual to need to implement this interface - + * by default the implementation is the DataSystems API module, and uses + * FolderLookup over the system filesystem. Implement only if you plan + * to place it in the default lookup to entirely replace the backing storage + * for registries of objects provided by the system filesystem (at least for + * those modules which use Lookups.forPath()). + * + * @since 7.8 + * @see org.openide.util.lookup.Lookups#forPath(java.lang.String) + * + * @author Tim Boudreau + */ +public interface PathBasedLookupProvider { + /** + * Get a Lookup over a named "folder" in a hierarchical + * registry things can be registered into. The default implementation + * provides a Lookup over the specified folder in the system filesystem. + * The specified path uses / for separators with no leading or trailing + * /'s. + * @param path A string path, such as com/foo/bar + * @return A Lookup instance over that folder. + */ + public Lookup getLookup (String path); +} Index: openide/util/test/unit/src/org/openide/util/lookup/PathBasedTest.java =================================================================== RCS file: openide/util/test/unit/src/org/openide/util/lookup/PathBasedTest.java diff -N openide/util/test/unit/src/org/openide/util/lookup/PathBasedTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/util/test/unit/src/org/openide/util/lookup/PathBasedTest.java 21 Mar 2007 06:40:55 -0000 @@ -0,0 +1,70 @@ +/* + * PathBasedTest.java + * + * Created on March 20, 2007, 10:46 PM + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package org.openide.util.lookup; + +import java.util.HashMap; +import java.util.Map; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.openide.util.Lookup; + +/** + * This is a class + * + * @author Tim Boudreau + */ +public class PathBasedTest extends NbTestCase { + + public PathBasedTest(String name) { + super (name); + } + + public void setUp() { + MockServices.setServices (PBImpl.class); + } + + public void testFindImpl() { + assertNotNull(Lookup.getDefault().lookup (PathBasedLookupProvider.class)); + } + + public void testFindLookup() { + String path = "com/foo/bar"; + Lookup lkp = Lookups.forPath(path); + Lookup lkp2 = Lookups.forPath (path); + assertSame (lkp, lkp2); + InstanceContent ic = impl.contentFor(lkp); + assertNotNull (ic); + } + + static PBImpl impl = null; + public static final class PBImpl implements PathBasedLookupProvider { + Map m = new HashMap (); + Map l2ic = new HashMap(); + + public PBImpl() { + PathBasedTest.impl = this; + } + + InstanceContent contentFor (Lookup lkp) { + return l2ic.get(lkp); + } + + public Lookup getLookup(String path) { + Lookup result = m.get (path); + if (result == null) { + InstanceContent content = new InstanceContent(); + result = new AbstractLookup(content); + l2ic.put (result, content); + m.put (path, result); + } + return result; + } + } +} Index: openide/loaders/manifest.mf =================================================================== RCS file: /cvs/openide/loaders/manifest.mf,v retrieving revision 1.30 diff -u -r1.30 manifest.mf --- openide/loaders/manifest.mf 7 Feb 2007 10:02:42 -0000 1.30 +++ openide/loaders/manifest.mf 21 Mar 2007 06:40:55 -0000 @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.loaders -OpenIDE-Module-Specification-Version: 6.1 +OpenIDE-Module-Specification-Version: 6.2 OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties Index: openide/loaders/nbproject/project.xml =================================================================== RCS file: /cvs/openide/loaders/nbproject/project.xml,v retrieving revision 1.26 diff -u -r1.26 project.xml --- openide/loaders/nbproject/project.xml 20 Feb 2007 15:13:51 -0000 1.26 +++ openide/loaders/nbproject/project.xml 21 Mar 2007 06:40:55 -0000 @@ -69,7 +69,7 @@ - 6.2 + 7.1 @@ -101,7 +101,7 @@ - 7.3 + 7.8 Index: openide/loaders/src/META-INF/services/org.openide.util.lookup.PathBasedLookupProvider =================================================================== RCS file: openide/loaders/src/META-INF/services/org.openide.util.lookup.PathBasedLookupProvider diff -N openide/loaders/src/META-INF/services/org.openide.util.lookup.PathBasedLookupProvider --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/loaders/src/META-INF/services/org.openide.util.lookup.PathBasedLookupProvider 21 Mar 2007 06:40:55 -0000 @@ -0,0 +1 @@ +org.netbeans.modules.openide.loaders.PathLookupProvider Index: openide/loaders/src/org/netbeans/modules/openide/loaders/PathLookupProvider.java =================================================================== RCS file: openide/loaders/src/org/netbeans/modules/openide/loaders/PathLookupProvider.java diff -N openide/loaders/src/org/netbeans/modules/openide/loaders/PathLookupProvider.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/loaders/src/org/netbeans/modules/openide/loaders/PathLookupProvider.java 21 Mar 2007 06:40:55 -0000 @@ -0,0 +1,40 @@ +/* + * PathLookupProvider.java + * + * Created on March 20, 2007, 10:10 PM + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package org.netbeans.modules.openide.loaders; + +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.loaders.DataFolder; +import org.openide.loaders.FolderLookup; +import org.openide.util.Lookup; +import org.openide.util.lookup.Lookups; +import org.openide.util.lookup.PathBasedLookupProvider; + +/** + * This is a class + * + * @author Tim Boudreau + */ +public class PathLookupProvider implements PathBasedLookupProvider { + + public PathLookupProvider() { + } + + public Lookup getLookup(String path) { + Lookup result = null; + FileObject ob = FileUtil.getConfigurationFolder(path, false); + if (ob != null) { + DataFolder fld = DataFolder.findFolder (ob); + FolderLookup flookup = new FolderLookup (fld); + result = fld.getLookup(); + } + return result == null ? Lookup.EMPTY : result; + } +} Index: openide/loaders/test/unit/src/org/openide/loaders/PathLookupProviderTest.java =================================================================== RCS file: openide/loaders/test/unit/src/org/openide/loaders/PathLookupProviderTest.java diff -N openide/loaders/test/unit/src/org/openide/loaders/PathLookupProviderTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/loaders/test/unit/src/org/openide/loaders/PathLookupProviderTest.java 21 Mar 2007 06:40:56 -0000 @@ -0,0 +1,81 @@ +/* + * PathLookupProviderTest.java + * + * Created on March 20, 2007, 10:58 PM + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package org.openide.loaders; + +import java.io.IOException; +import java.util.Collection; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileSystem; +import org.openide.filesystems.FileUtil; +import org.openide.filesystems.Repository; +import org.openide.loaders.PathLookupProviderTest.O; +import org.openide.util.Lookup; +import org.openide.util.lookup.Lookups; + +/** + * This is a class + * + * @author Tim Boudreau + */ +public class PathLookupProviderTest extends NbTestCase { + + /** Creates a new instance of PathLookupProviderTest */ + public PathLookupProviderTest(String name) { + super (name); + } + + FileObject data; + static boolean objCreated = false; + public void setUp() throws IOException { + objCreated = false; + MockServices.setServices (MyRepo.class); + FileObject fld = FileUtil.getConfigurationFolder("somewhere/or/other", true); + assertNotNull (fld); + data = fld.createData ("org-openide-loaders-PathLookupProviderTest$O.instance"); + } + + public void testInstanceIsFound() { + System.out.println("testInstanceIsFound"); + Lookup lkp = Lookups.forPath ("somewhere/or/other"); + assertNotNull (lkp); + assertNotSame (Lookup.EMPTY, lkp); + Collection c = lkp.lookupAll(O.class); + assertTrue (objCreated); + assertFalse (c.isEmpty()); + } + + public static class MyRepo extends Repository { + static FileSystem sysfs; + public MyRepo () { + super(sysfs = FileUtil.createMemoryFileSystem()); + } + } + + + static char ch = 'A'; + public static final class O { + private final String s; + public O () { + this ("" + (ch++)); + } + + O (String s) { + this.s = s; + objCreated = true; + } + + public String toString() { + return s; + } + } + +}