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.
+
.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 --- /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
--- /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