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 169338
Collapse All | Expand All

(-)a/openide.filesystems/apichanges.xml (+18 lines)
Lines 49-54 Link Here
49
        <apidef name="filesystems">Filesystems API</apidef>
49
        <apidef name="filesystems">Filesystems API</apidef>
50
    </apidefs>
50
    </apidefs>
51
    <changes>
51
    <changes>
52
        <change id="getConfigObject">
53
            <api name="filesystems"/>
54
            <summary>FileUtil.getConfigObject</summary>
55
            <version major="7" minor="49"/>
56
            <date day="25" month="6" year="2011"/>
57
            <author login="jtulach"/>
58
            <compatibility addition="yes"/>
59
            <description>
60
                <p>
61
                    One can convert files in SystemFileSystem to Object with
62
                    a 
63
                    <a href="@TOP@org/openide/filesystems/FileUtil.html#getConfigObject(java.lang.String,%20java.lang.Class)">
64
                    single utility method</a>.
65
                </p>
66
            </description>
67
            <class package="org.openide.filesystems" name="FileUtil"/>
68
            <issue number="169338"/>
69
        </change>
52
        <change id="to.parent">
70
        <change id="to.parent">
53
            <api name="filesystems"/>
71
            <api name="filesystems"/>
54
            <summary>FileObject.getFileObject accepts ".."</summary>
72
            <summary>FileObject.getFileObject accepts ".."</summary>
(-)a/openide.filesystems/manifest.mf (-1 / +1 lines)
Lines 2-6 Link Here
2
OpenIDE-Module: org.openide.filesystems
2
OpenIDE-Module: org.openide.filesystems
3
OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties
4
OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml
4
OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml
5
OpenIDE-Module-Specification-Version: 7.48
5
OpenIDE-Module-Specification-Version: 7.49
6
6
(-)a/openide.filesystems/nbproject/project.xml (-1 / +1 lines)
Lines 62-68 Link Here
62
                    <build-prerequisite/>
62
                    <build-prerequisite/>
63
                    <compile-dependency/>
63
                    <compile-dependency/>
64
                    <run-dependency>
64
                    <run-dependency>
65
                        <specification-version>8.1</specification-version>
65
                        <specification-version>8.10</specification-version>
66
                    </run-dependency>
66
                    </run-dependency>
67
                </dependency>
67
                </dependency>
68
            </module-dependencies>
68
            </module-dependencies>
(-)a/openide.filesystems/src/org/netbeans/modules/openide/filesystems/RecognizeInstanceFiles.java (-16 / +35 lines)
Lines 71-79 Link Here
71
    private static final Logger LOG = Logger.getLogger(RecognizeInstanceFiles.class.getName());
71
    private static final Logger LOG = Logger.getLogger(RecognizeInstanceFiles.class.getName());
72
    
72
    
73
    
73
    
74
    @Override
74
    public Lookup create(String path) {
75
    public Lookup create(String path) {
75
        return new OverFiles(path);
76
        return new OverFiles(path);
76
    }        
77
    }        
78
    @Override
79
    public <T> T lookupObject(String path, Class<T> type) {
80
        FileObject fo = FileUtil.getConfigFile(path);
81
        if (fo != null && fo.isData()) {
82
            Object res = FOItem.createInstanceFor(fo, Object.class);
83
            return type.isInstance(res) ? type.cast(res) : null;
84
        }
85
        return null;
86
    }        
77
    
87
    
78
    
88
    
79
    private static final class OverFiles extends ProxyLookup 
89
    private static final class OverFiles extends ProxyLookup 
Lines 216-236 Link Here
216
        public synchronized Object getInstance() {
226
        public synchronized Object getInstance() {
217
            Object r = ref.get();
227
            Object r = ref.get();
218
            if (r == null) {
228
            if (r == null) {
219
                r = fo.getAttribute("instanceCreate");
229
                r = createInstanceFor(fo, Object.class);
220
                if (r == null) {
221
                    try {
222
                        Class<?> type = getType();
223
                        if (SharedClassObject.class.isAssignableFrom(type)) {
224
                            r = SharedClassObject.findObject(type.asSubclass(SharedClassObject.class), true);
225
                        } else {
226
                            r = type.newInstance();
227
                        }
228
                    } catch (InstantiationException ex) {
229
                        Exceptions.printStackTrace(ex);
230
                    } catch (IllegalAccessException ex) {
231
                        Exceptions.printStackTrace(ex);
232
                    }
233
                }
234
                if (r != null) {
230
                if (r != null) {
235
                    ref = new WeakReference<Object>(r);
231
                    ref = new WeakReference<Object>(r);
236
                }
232
                }
Lines 238-250 Link Here
238
            return r;
234
            return r;
239
        }
235
        }
240
236
237
        static <T> T createInstanceFor(FileObject f, Class<T> resultType) {
238
            Object r = f.getAttribute("instanceCreate");
239
            if (r == null) {
240
                try {
241
                    Class<?> type = findTypeFor(f);
242
                    if (SharedClassObject.class.isAssignableFrom(type)) {
243
                        r = SharedClassObject.findObject(type.asSubclass(SharedClassObject.class), true);
244
                    } else {
245
                        r = type.newInstance();
246
                    }
247
                } catch (InstantiationException ex) {
248
                    Exceptions.printStackTrace(ex);
249
                } catch (IllegalAccessException ex) {
250
                    Exceptions.printStackTrace(ex);
251
                }
252
            }
253
            return resultType.isInstance(r) ? resultType.cast(r) : null;
254
        }
255
241
        public Class<? extends Object> getType() {
256
        public Class<? extends Object> getType() {
257
            return findTypeFor(fo);
258
        }
259
260
        private static Class<? extends Object> findTypeFor(FileObject f) {
242
            ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
261
            ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
243
            if (l == null) {
262
            if (l == null) {
244
                l = FOItem.class.getClassLoader();
263
                l = FOItem.class.getClassLoader();
245
            }
264
            }
246
            try {
265
            try {
247
                return Class.forName(getClassName(fo), false, l);
266
                return Class.forName(getClassName(f), false, l);
248
            } catch (ClassNotFoundException ex) {
267
            } catch (ClassNotFoundException ex) {
249
                LOG.log(Level.INFO, ex.getMessage(), ex);
268
                LOG.log(Level.INFO, ex.getMessage(), ex);
250
                return Object.class;
269
                return Object.class;
(-)a/openide.filesystems/src/org/openide/filesystems/FileUtil.java (+20 lines)
Lines 91-96 Link Here
91
import org.openide.util.RequestProcessor;
91
import org.openide.util.RequestProcessor;
92
import org.openide.util.Utilities;
92
import org.openide.util.Utilities;
93
import org.openide.util.WeakListeners;
93
import org.openide.util.WeakListeners;
94
import org.openide.util.lookup.implspi.NamedServicesProvider;
94
95
95
/** Common utilities for handling files.
96
/** Common utilities for handling files.
96
 * This is a dummy class; all methods are static.
97
 * This is a dummy class; all methods are static.
Lines 2215-2220 Link Here
2215
        Parameters.notNull("path", path);  //NOI18N
2216
        Parameters.notNull("path", path);  //NOI18N
2216
        return Repository.getDefault().getDefaultFileSystem().findResource(path);
2217
        return Repository.getDefault().getDefaultFileSystem().findResource(path);
2217
    }
2218
    }
2219
    
2220
    /** Finds a config object under given path. The path contains the extension
2221
     * of a file e.g.:
2222
     * <pre>
2223
     * Actions/Edit/org-openide-actions-CopyAction.instance
2224
     * Services/Browsers/swing-browser.settings
2225
     * </pre>
2226
     * @param filePath path to .instance or .settings file
2227
     * @param type the requested type for given object
2228
     * @return either null or instance of requrested type
2229
     * @since 7.49 
2230
     */
2231
    public static <T> T getConfigObject(String path, Class<T> type) {
2232
        FileObject fo = getConfigFile(path);
2233
        if (fo == null || fo.isFolder()) {
2234
            return null;
2235
        }
2236
        return NamedServicesProvider.getConfigObject(path, type);
2237
    }
2218
2238
2219
    /**
2239
    /**
2220
     * Returns the root of the NetBeans default (system, configuration)
2240
     * Returns the root of the NetBeans default (system, configuration)
(-)a/openide.filesystems/test/unit/src/org/netbeans/modules/openide/filesystems/RecognizeInstanceFilesTest.java (-1 / +9 lines)
Lines 230-238 Link Here
230
230
231
    public void testSharedClassObject() throws Exception {
231
    public void testSharedClassObject() throws Exception {
232
        Shared instance = SharedClassObject.findObject(Shared.class, true);
232
        Shared instance = SharedClassObject.findObject(Shared.class, true);
233
        FileUtil.createData(root, "dir/" + Shared.class.getName().replace('.', '-') + ".instance");
233
        FileObject data = FileUtil.createData(root, "dir/" + Shared.class.getName().replace('.', '-') + ".instance");
234
        Lookup l = Lookups.forPath("dir");
234
        Lookup l = Lookups.forPath("dir");
235
        assertSame(instance, l.lookup(Shared.class));
235
        assertSame(instance, l.lookup(Shared.class));
236
        
237
        Shared created = FileUtil.getConfigObject(data.getPath(), Shared.class);
238
        assertSame("Config file found", instance, created);
239
    }
240
    public void testNullForFolders() throws Exception {
241
        FileObject data = FileUtil.createFolder(root, "dir/" + Shared.class.getName().replace('.', '-') + ".instance");
242
        Shared nul = FileUtil.getConfigObject(data.getPath(), Shared.class);
243
        assertNull("No object for folders", nul);
236
    }
244
    }
237
245
238
    public static final class Shared extends SharedClassObject {}
246
    public static final class Shared extends SharedClassObject {}
(-)a/openide.util.lookup/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.util.lookup
2
OpenIDE-Module: org.openide.util.lookup
3
OpenIDE-Module-Localizing-Bundle: org/openide/util/lookup/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/openide/util/lookup/Bundle.properties
4
OpenIDE-Module-Specification-Version: 8.9
4
OpenIDE-Module-Specification-Version: 8.10
5
5
(-)a/openide.util.lookup/src/org/openide/util/lookup/implspi/NamedServicesProvider.java (+20 lines)
Lines 105-110 Link Here
105
        namedServicesProviders.put(path, new WeakReference<Lookup>(lkp));
105
        namedServicesProviders.put(path, new WeakReference<Lookup>(lkp));
106
        return lkp;
106
        return lkp;
107
    }
107
    }
108
    
109
    /** Finds a config object under given path.
110
     * @param filePath path to .instance or .settings file
111
     * @param type the requested type for given object
112
     * @return either null or instance of requested type
113
     * @since 8.10 
114
     */
115
    public static <T> T getConfigObject(String filePath, Class<T> type) {
116
        NamedServicesProvider prov = Lookup.getDefault().lookup(NamedServicesProvider.class);
117
        return prov != null ? prov.lookupObject(filePath, type) : null;
118
    }
108
119
109
    /** Throws an exception. Prevents unwanted instantiation of this class
120
    /** Throws an exception. Prevents unwanted instantiation of this class
110
     * by unknown subclasses.
121
     * by unknown subclasses.
Lines 137-140 Link Here
137
     */
148
     */
138
    protected abstract Lookup create(String path);
149
    protected abstract Lookup create(String path);
139
    
150
    
151
    /** Finds a config object under given path. Called from {@link FileUtil#getConfigObject}.
152
     * @param filePath path to .instance or .settings file
153
     * @param type the requested type for given object
154
     * @return either null or instance of requested type
155
     * @since 8.10 
156
     */
157
    protected <T> T lookupObject(String path, Class<T> type) {
158
        return create(path).lookup(type);
159
    }
140
}
160
}
(-)a/settings/nbproject/project.xml (-2 / +2 lines)
Lines 88-94 Link Here
88
                    <build-prerequisite/>
88
                    <build-prerequisite/>
89
                    <compile-dependency/>
89
                    <compile-dependency/>
90
                    <run-dependency>
90
                    <run-dependency>
91
                        <specification-version>7.19</specification-version>
91
                        <specification-version>7.49</specification-version>
92
                    </run-dependency>
92
                    </run-dependency>
93
                </dependency>
93
                </dependency>
94
                <dependency>
94
                <dependency>
Lines 126-132 Link Here
126
                    <build-prerequisite/>
126
                    <build-prerequisite/>
127
                    <compile-dependency/>
127
                    <compile-dependency/>
128
                    <run-dependency>
128
                    <run-dependency>
129
                        <specification-version>8.1</specification-version>
129
                        <specification-version>8.10</specification-version>
130
                    </run-dependency>
130
                    </run-dependency>
131
                </dependency>
131
                </dependency>
132
                <dependency>
132
                <dependency>
(-)a/settings/src/org/netbeans/modules/settings/RecognizeInstanceObjects.java (+19 lines)
Lines 34-43 Link Here
34
34
35
package org.netbeans.modules.settings;
35
package org.netbeans.modules.settings;
36
36
37
import java.io.IOException;
37
import java.util.Collection;
38
import java.util.Collection;
38
import java.util.Collections;
39
import java.util.Collections;
39
import java.util.logging.Level;
40
import java.util.logging.Level;
40
import java.util.logging.Logger;
41
import java.util.logging.Logger;
42
import org.openide.cookies.InstanceCookie;
41
import org.openide.filesystems.FileAttributeEvent;
43
import org.openide.filesystems.FileAttributeEvent;
42
import org.openide.filesystems.FileChangeListener;
44
import org.openide.filesystems.FileChangeListener;
43
import org.openide.filesystems.FileEvent;
45
import org.openide.filesystems.FileEvent;
Lines 47-52 Link Here
47
import org.openide.filesystems.FileSystem;
49
import org.openide.filesystems.FileSystem;
48
import org.openide.filesystems.FileUtil;
50
import org.openide.filesystems.FileUtil;
49
import org.openide.loaders.DataFolder;
51
import org.openide.loaders.DataFolder;
52
import org.openide.loaders.DataObject;
50
import org.openide.util.Lookup;
53
import org.openide.util.Lookup;
51
import org.openide.util.LookupEvent;
54
import org.openide.util.LookupEvent;
52
import org.openide.util.LookupListener;
55
import org.openide.util.LookupListener;
Lines 68-73 Link Here
68
public final class RecognizeInstanceObjects extends NamedServicesProvider {
71
public final class RecognizeInstanceObjects extends NamedServicesProvider {
69
    private static final Logger LOG = Logger.getLogger(RecognizeInstanceObjects.class.getName());
72
    private static final Logger LOG = Logger.getLogger(RecognizeInstanceObjects.class.getName());
70
    
73
    
74
    @Override
75
    public <T> T lookupObject(String path, Class<T> type) {
76
        FileObject fo = FileUtil.getConfigFile(path);
77
        if (fo != null && fo.isData()) {
78
            try {
79
                InstanceCookie ic = DataObject.find(fo).getLookup().lookup(InstanceCookie.class);
80
                Object obj = ic != null ? ic.instanceCreate() : null;
81
                return type.isInstance(obj) ? type.cast(obj) : null;
82
            } catch (IOException ex) {
83
                LOG.log(Level.INFO, "Cannot create instance for " + path, ex);
84
            } catch (ClassNotFoundException ex) {
85
                LOG.log(Level.INFO, "Cannot create instance for " + path, ex);
86
            }
87
        }
88
        return null;
89
    }
71
    
90
    
72
    @Override
91
    @Override
73
    public Lookup create(String path) {
92
    public Lookup create(String path) {

Return to bug 169338