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 extends Object> getType() {
+ return findTypeFor(fo);
+ }
+
+ private static Class extends Object> 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;
--- a/openide.filesystems/src/org/openide/filesystems/FileUtil.java Fri Jun 17 07:20:12 2011 +0200
+++ a/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)
--- a/openide.filesystems/test/unit/src/org/netbeans/modules/openide/filesystems/RecognizeInstanceFilesTest.java Fri Jun 17 07:20:12 2011 +0200
+++ a/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 {}
--- a/openide.util.lookup/manifest.mf Fri Jun 17 07:20:12 2011 +0200
+++ a/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
--- a/openide.util.lookup/src/org/openide/util/lookup/implspi/NamedServicesProvider.java Fri Jun 17 07:20:12 2011 +0200
+++ a/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);
+ }
}
--- a/settings/nbproject/project.xml Fri Jun 17 07:20:12 2011 +0200
+++ a/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
--- a/settings/src/org/netbeans/modules/settings/RecognizeInstanceObjects.java Fri Jun 17 07:20:12 2011 +0200
+++ a/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) {