diff -r 2fd4ee47e7fa openide.filesystems/apichanges.xml --- a/openide.filesystems/apichanges.xml Fri Jun 17 07:20:12 2011 +0200 +++ b/openide.filesystems/apichanges.xml Sat Jun 18 17:17:41 2011 +0200 @@ -49,6 +49,24 @@ Filesystems API + + + FileUtil.getConfigObject + + + + + +

+ One can convert files in SystemFileSystem to Object with + a + + single utility method. +

+
+ + +
FileObject.getFileObject accepts ".." diff -r 2fd4ee47e7fa openide.filesystems/manifest.mf --- a/openide.filesystems/manifest.mf Fri Jun 17 07:20:12 2011 +0200 +++ b/openide.filesystems/manifest.mf Sat Jun 18 17:17:41 2011 +0200 @@ -2,5 +2,5 @@ OpenIDE-Module: org.openide.filesystems OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml -OpenIDE-Module-Specification-Version: 7.48 +OpenIDE-Module-Specification-Version: 7.49 diff -r 2fd4ee47e7fa openide.filesystems/nbproject/project.xml --- a/openide.filesystems/nbproject/project.xml Fri Jun 17 07:20:12 2011 +0200 +++ b/openide.filesystems/nbproject/project.xml Sat Jun 18 17:17:41 2011 +0200 @@ -62,7 +62,7 @@ - 8.1 + 8.10 diff -r 2fd4ee47e7fa openide.filesystems/src/org/netbeans/modules/openide/filesystems/RecognizeInstanceFiles.java --- a/openide.filesystems/src/org/netbeans/modules/openide/filesystems/RecognizeInstanceFiles.java Fri Jun 17 07:20:12 2011 +0200 +++ b/openide.filesystems/src/org/netbeans/modules/openide/filesystems/RecognizeInstanceFiles.java Sat Jun 18 17:17:41 2011 +0200 @@ -71,9 +71,19 @@ private static final Logger LOG = Logger.getLogger(RecognizeInstanceFiles.class.getName()); + @Override public Lookup create(String path) { return new OverFiles(path); } + @Override + public T lookupObject(String path, Class type) { + FileObject fo = FileUtil.getConfigFile(path); + if (fo != null && fo.isData()) { + Object res = FOItem.createInstanceFor(fo, Object.class); + return type.isInstance(res) ? type.cast(res) : null; + } + return null; + } private static final class OverFiles extends ProxyLookup @@ -216,21 +226,7 @@ public synchronized Object getInstance() { Object r = ref.get(); if (r == null) { - r = fo.getAttribute("instanceCreate"); - if (r == null) { - try { - Class type = getType(); - if (SharedClassObject.class.isAssignableFrom(type)) { - r = SharedClassObject.findObject(type.asSubclass(SharedClassObject.class), true); - } else { - r = type.newInstance(); - } - } catch (InstantiationException ex) { - Exceptions.printStackTrace(ex); - } catch (IllegalAccessException ex) { - Exceptions.printStackTrace(ex); - } - } + r = createInstanceFor(fo, Object.class); if (r != null) { ref = new WeakReference(r); } @@ -238,13 +234,36 @@ return r; } + static T createInstanceFor(FileObject f, Class resultType) { + Object r = f.getAttribute("instanceCreate"); + if (r == null) { + try { + Class type = findTypeFor(f); + if (SharedClassObject.class.isAssignableFrom(type)) { + r = SharedClassObject.findObject(type.asSubclass(SharedClassObject.class), true); + } else { + r = type.newInstance(); + } + } catch (InstantiationException ex) { + Exceptions.printStackTrace(ex); + } catch (IllegalAccessException ex) { + Exceptions.printStackTrace(ex); + } + } + return resultType.isInstance(r) ? resultType.cast(r) : null; + } + public Class getType() { + return findTypeFor(fo); + } + + private static Class findTypeFor(FileObject f) { ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class); if (l == null) { l = FOItem.class.getClassLoader(); } try { - return Class.forName(getClassName(fo), false, l); + return Class.forName(getClassName(f), false, l); } catch (ClassNotFoundException ex) { LOG.log(Level.INFO, ex.getMessage(), ex); return Object.class; diff -r 2fd4ee47e7fa openide.filesystems/src/org/openide/filesystems/FileUtil.java --- a/openide.filesystems/src/org/openide/filesystems/FileUtil.java Fri Jun 17 07:20:12 2011 +0200 +++ b/openide.filesystems/src/org/openide/filesystems/FileUtil.java Sat Jun 18 17:17:41 2011 +0200 @@ -91,6 +91,7 @@ import org.openide.util.RequestProcessor; import org.openide.util.Utilities; import org.openide.util.WeakListeners; +import org.openide.util.lookup.implspi.NamedServicesProvider; /** Common utilities for handling files. * This is a dummy class; all methods are static. @@ -2215,6 +2216,25 @@ Parameters.notNull("path", path); //NOI18N return Repository.getDefault().getDefaultFileSystem().findResource(path); } + + /** Finds a config object under given path. The path contains the extension + * of a file e.g.: + *
+     * Actions/Edit/org-openide-actions-CopyAction.instance
+     * Services/Browsers/swing-browser.settings
+     * 
+ * @param filePath path to .instance or .settings file + * @param type the requested type for given object + * @return either null or instance of requrested type + * @since 7.49 + */ + public static T getConfigObject(String path, Class type) { + FileObject fo = getConfigFile(path); + if (fo == null || fo.isFolder()) { + return null; + } + return NamedServicesProvider.getConfigObject(path, type); + } /** * Returns the root of the NetBeans default (system, configuration) diff -r 2fd4ee47e7fa openide.filesystems/test/unit/src/org/netbeans/modules/openide/filesystems/RecognizeInstanceFilesTest.java --- a/openide.filesystems/test/unit/src/org/netbeans/modules/openide/filesystems/RecognizeInstanceFilesTest.java Fri Jun 17 07:20:12 2011 +0200 +++ b/openide.filesystems/test/unit/src/org/netbeans/modules/openide/filesystems/RecognizeInstanceFilesTest.java Sat Jun 18 17:17:41 2011 +0200 @@ -230,9 +230,17 @@ public void testSharedClassObject() throws Exception { Shared instance = SharedClassObject.findObject(Shared.class, true); - FileUtil.createData(root, "dir/" + Shared.class.getName().replace('.', '-') + ".instance"); + FileObject data = FileUtil.createData(root, "dir/" + Shared.class.getName().replace('.', '-') + ".instance"); Lookup l = Lookups.forPath("dir"); assertSame(instance, l.lookup(Shared.class)); + + Shared created = FileUtil.getConfigObject(data.getPath(), Shared.class); + assertSame("Config file found", instance, created); + } + public void testNullForFolders() throws Exception { + FileObject data = FileUtil.createFolder(root, "dir/" + Shared.class.getName().replace('.', '-') + ".instance"); + Shared nul = FileUtil.getConfigObject(data.getPath(), Shared.class); + assertNull("No object for folders", nul); } public static final class Shared extends SharedClassObject {} diff -r 2fd4ee47e7fa openide.util.lookup/manifest.mf --- a/openide.util.lookup/manifest.mf Fri Jun 17 07:20:12 2011 +0200 +++ b/openide.util.lookup/manifest.mf Sat Jun 18 17:17:41 2011 +0200 @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.util.lookup OpenIDE-Module-Localizing-Bundle: org/openide/util/lookup/Bundle.properties -OpenIDE-Module-Specification-Version: 8.9 +OpenIDE-Module-Specification-Version: 8.10 diff -r 2fd4ee47e7fa openide.util.lookup/src/org/openide/util/lookup/implspi/NamedServicesProvider.java --- a/openide.util.lookup/src/org/openide/util/lookup/implspi/NamedServicesProvider.java Fri Jun 17 07:20:12 2011 +0200 +++ b/openide.util.lookup/src/org/openide/util/lookup/implspi/NamedServicesProvider.java Sat Jun 18 17:17:41 2011 +0200 @@ -105,6 +105,17 @@ namedServicesProviders.put(path, new WeakReference(lkp)); return lkp; } + + /** Finds a config object under given path. + * @param filePath path to .instance or .settings file + * @param type the requested type for given object + * @return either null or instance of requested type + * @since 8.10 + */ + public static T getConfigObject(String filePath, Class type) { + NamedServicesProvider prov = Lookup.getDefault().lookup(NamedServicesProvider.class); + return prov != null ? prov.lookupObject(filePath, type) : null; + } /** Throws an exception. Prevents unwanted instantiation of this class * by unknown subclasses. @@ -137,4 +148,13 @@ */ protected abstract Lookup create(String path); + /** Finds a config object under given path. Called from {@link FileUtil#getConfigObject}. + * @param filePath path to .instance or .settings file + * @param type the requested type for given object + * @return either null or instance of requested type + * @since 8.10 + */ + protected T lookupObject(String path, Class type) { + return create(path).lookup(type); + } } diff -r 2fd4ee47e7fa settings/nbproject/project.xml --- a/settings/nbproject/project.xml Fri Jun 17 07:20:12 2011 +0200 +++ b/settings/nbproject/project.xml Sat Jun 18 17:17:41 2011 +0200 @@ -88,7 +88,7 @@ - 7.19 + 7.49 @@ -126,7 +126,7 @@ - 8.1 + 8.10 diff -r 2fd4ee47e7fa settings/src/org/netbeans/modules/settings/RecognizeInstanceObjects.java --- a/settings/src/org/netbeans/modules/settings/RecognizeInstanceObjects.java Fri Jun 17 07:20:12 2011 +0200 +++ b/settings/src/org/netbeans/modules/settings/RecognizeInstanceObjects.java Sat Jun 18 17:17:41 2011 +0200 @@ -34,10 +34,12 @@ package org.netbeans.modules.settings; +import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.logging.Level; import java.util.logging.Logger; +import org.openide.cookies.InstanceCookie; import org.openide.filesystems.FileAttributeEvent; import org.openide.filesystems.FileChangeListener; import org.openide.filesystems.FileEvent; @@ -47,6 +49,7 @@ import org.openide.filesystems.FileSystem; import org.openide.filesystems.FileUtil; import org.openide.loaders.DataFolder; +import org.openide.loaders.DataObject; import org.openide.util.Lookup; import org.openide.util.LookupEvent; import org.openide.util.LookupListener; @@ -68,6 +71,22 @@ public final class RecognizeInstanceObjects extends NamedServicesProvider { private static final Logger LOG = Logger.getLogger(RecognizeInstanceObjects.class.getName()); + @Override + public T lookupObject(String path, Class type) { + FileObject fo = FileUtil.getConfigFile(path); + if (fo != null && fo.isData()) { + try { + InstanceCookie ic = DataObject.find(fo).getLookup().lookup(InstanceCookie.class); + Object obj = ic != null ? ic.instanceCreate() : null; + return type.isInstance(obj) ? type.cast(obj) : null; + } catch (IOException ex) { + LOG.log(Level.INFO, "Cannot create instance for " + path, ex); + } catch (ClassNotFoundException ex) { + LOG.log(Level.INFO, "Cannot create instance for " + path, ex); + } + } + return null; + } @Override public Lookup create(String path) {