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

(-)a/openide.filesystems/src/org/openide/filesystems/MultiFileObject.java (-7 / +56 lines)
Lines 60-65 Link Here
60
import java.util.Properties;
60
import java.util.Properties;
61
import java.util.Set;
61
import java.util.Set;
62
import java.util.WeakHashMap;
62
import java.util.WeakHashMap;
63
import java.util.logging.Logger;
63
64
64
/** Implementation of the file object for multi file system.
65
/** Implementation of the file object for multi file system.
65
*
66
*
Lines 74-79 Link Here
74
75
75
    /** default path separator */
76
    /** default path separator */
76
    private static final char PATH_SEP = '/';
77
    private static final char PATH_SEP = '/';
78
    private static final String WEIGHT_ATTRIBUTE = "weight"; // NOI18N
77
    private static final FileSystem.AtomicAction markAtomicAction = new FileSystem.AtomicAction() {
79
    private static final FileSystem.AtomicAction markAtomicAction = new FileSystem.AtomicAction() {
78
            public void run() {
80
            public void run() {
79
            }
81
            }
Lines 164-172 Link Here
164
166
165
        Set now = (delegates == null) ? Collections.EMPTY_SET : delegates;
167
        Set now = (delegates == null) ? Collections.EMPTY_SET : delegates;
166
        Set<FileObject> del = new HashSet<FileObject>(arr.length * 2);
168
        Set<FileObject> del = new HashSet<FileObject>(arr.length * 2);
169
        Number maxWeight = 0;
167
        FileObject led = null;
170
        FileObject led = null;
168
171
169
        String name = getPath();
172
        String name = getPath();
173
        FileSystem writable = mfs.writableLayer(name);
170
174
171
        for (int i = 0; i < arr.length; i++) {
175
        for (int i = 0; i < arr.length; i++) {
172
            if (arr[i] != null) {
176
            if (arr[i] != null) {
Lines 180-187 Link Here
180
                        fo.addFileChangeListener(weakL);
184
                        fo.addFileChangeListener(weakL);
181
                    }
185
                    }
182
186
183
                    if ((led == null) && fo.isValid()) {
187
                    if (fo.isValid()) {
184
                        led = fo;
188
                        Number weight = weightOf(fo, writable);
189
                        if (led == null || weight.doubleValue() > maxWeight.doubleValue()) {
190
                            led = fo;
191
                            maxWeight = weight;
192
                        }
185
                    }
193
                    }
186
                }
194
                }
187
            }
195
            }
Lines 293-298 Link Here
293
    private FileObject findLeader(FileSystem[] fs, String path) {
301
    private FileObject findLeader(FileSystem[] fs, String path) {
294
        MultiFileSystem mfs = getMultiFileSystem();
302
        MultiFileSystem mfs = getMultiFileSystem();
295
303
304
        Number maxWeight = 0;
305
        FileObject _leader = null;
306
        FileSystem writable = mfs.writableLayer(path);
307
        
296
        for (FileSystem f : fs) {
308
        for (FileSystem f : fs) {
297
            if (f == null) {
309
            if (f == null) {
298
                continue;
310
                continue;
Lines 300-310 Link Here
300
            FileObject fo = mfs.findResourceOn(f, path);
312
            FileObject fo = mfs.findResourceOn(f, path);
301
313
302
            if (fo != null) {
314
            if (fo != null) {
303
                return fo;
315
                Number weight = weightOf(fo, writable);
316
                if (_leader == null || weight.doubleValue() > maxWeight.doubleValue()) {
317
                    _leader = fo;
318
                    maxWeight = weight;
319
                }
304
            }
320
            }
305
        }
321
        }
306
322
307
        return null;
323
        return _leader;
324
    }
325
326
    private static Number weightOf(FileObject f, FileSystem writable) {
327
        try {
328
            if (f.getFileSystem() == writable) {
329
                return Double.MAX_VALUE;
330
            }
331
        } catch (FileStateInvalidException x) {/* ignore */}
332
        Object weight = f.getAttribute(WEIGHT_ATTRIBUTE);
333
        if (weight instanceof Number) {
334
            return (Number) weight;
335
        } else if (weight == null) {
336
            return 0;
337
        } else {
338
            try {
339
                Logger.getLogger(MultiFileObject.class.getName()).warning(
340
                        "File " + f.getPath() + " in " + f.getFileSystem() +
341
                        " has nonnumeric weight " + weight + " of type " + weight.getClass().getName());
342
            } catch (FileStateInvalidException x) {/* ignore */}
343
            return 0;
344
        }
308
    }
345
    }
309
346
310
    /** Getter for the right file system */
347
    /** Getter for the right file system */
Lines 767-772 Link Here
767
804
768
        FileSystem[] systems = getMultiFileSystem().getDelegates();
805
        FileSystem[] systems = getMultiFileSystem().getDelegates();
769
806
807
        Number maxWeight = 0;
808
        Object attr = null;
809
        FileSystem writable = getMultiFileSystem().writableLayer(path);
810
770
        //        boolean isLoaderAttr = /* DataObject.EA_ASSIGNED_LOADER */ "NetBeansAttrAssignedLoader".equals (attrName); // NOI18N                
811
        //        boolean isLoaderAttr = /* DataObject.EA_ASSIGNED_LOADER */ "NetBeansAttrAssignedLoader".equals (attrName); // NOI18N                
771
        for (int i = 0; i < systems.length; i++) {
812
        for (int i = 0; i < systems.length; i++) {
772
            if (systems[i] == null) {
813
            if (systems[i] == null) {
Lines 786-792 Link Here
786
                Object o = getAttribute(fo, attrName, fo.getPath()); // Performance tricks:                
827
                Object o = getAttribute(fo, attrName, fo.getPath()); // Performance tricks:                
787
828
788
                if (o != null) {
829
                if (o != null) {
789
                    return devoidify(o);
830
                    Number weight = weightOf(fo, writable);
831
                    if (attr == null || weight.doubleValue() > maxWeight.doubleValue()) {
832
                        attr = o;
833
                        maxWeight = weight;
834
                    }
790
                }
835
                }
791
            }
836
            }
792
837
Lines 800-811 Link Here
800
                Object o = getAttribute(fo, prefixattr, ""); // NOI18N
845
                Object o = getAttribute(fo, prefixattr, ""); // NOI18N
801
846
802
                if (o != null) {
847
                if (o != null) {
803
                    return devoidify(o);
848
                    Number weight = weightOf(fo, writable);
849
                    if (attr == null || weight.doubleValue() > maxWeight.doubleValue()) {
850
                        attr = o;
851
                        maxWeight = weight;
852
                    }
804
                }
853
                }
805
            }
854
            }
806
        }
855
        }
807
856
808
        return null;
857
        return devoidify(attr);
809
    }
858
    }
810
859
811
    private Object getAttribute(FileObject fo, String attrName, String path) {
860
    private Object getAttribute(FileObject fo, String attrName, String path) {
(-)a/openide.filesystems/src/org/openide/filesystems/MultiFileSystem.java (+10 lines)
Lines 447-452 Link Here
447
        return systems[WRITE_SYSTEM_INDEX];
447
        return systems[WRITE_SYSTEM_INDEX];
448
    }
448
    }
449
449
450
    FileSystem writableLayer(String path) {
451
        try {
452
            return createWritableOn(path);
453
        } catch (IOException x) {
454
            // ignore
455
            return systems.length > WRITE_SYSTEM_INDEX ? systems[WRITE_SYSTEM_INDEX] : null;
456
        }
457
    }
458
450
    /** Special case of createWritableOn (@see #createWritableOn).
459
    /** Special case of createWritableOn (@see #createWritableOn).
451
    *
460
    *
452
    * @param oldName original name of the file (full)
461
    * @param oldName original name of the file (full)
Lines 554-559 Link Here
554
    Enumeration<FileObject> delegates(final String name) {
563
    Enumeration<FileObject> delegates(final String name) {
555
        Enumeration<FileSystem> en = Enumerations.array(systems);
564
        Enumeration<FileSystem> en = Enumerations.array(systems);
556
565
566
        // XXX order (stably) by weight
557
        class Resources implements Enumerations.Processor<FileSystem, FileObject> {
567
        class Resources implements Enumerations.Processor<FileSystem, FileObject> {
558
            public FileObject process(FileSystem fs, Collection<FileSystem> ignore) {
568
            public FileObject process(FileSystem fs, Collection<FileSystem> ignore) {
559
                if (fs == null) {
569
                if (fs == null) {
(-)a/openide.filesystems/test/unit/src/org/openide/filesystems/MultiFileSystemMaskTest.java (+60 lines)
Lines 41-51 Link Here
41
41
42
package org.openide.filesystems;
42
package org.openide.filesystems;
43
43
44
import java.beans.PropertyVetoException;
44
import java.util.ArrayList;
45
import java.util.ArrayList;
45
import java.util.Collections;
46
import java.util.Collections;
46
import java.util.Iterator;
47
import java.util.Iterator;
47
import java.util.List;
48
import java.util.List;
48
import org.netbeans.junit.NbTestCase;
49
import org.netbeans.junit.NbTestCase;
50
import org.openide.filesystems.test.TestFileUtils;
49
51
50
// XXX should only *unused* mask files be listed when propagateMasks?
52
// XXX should only *unused* mask files be listed when propagateMasks?
51
// XXX write similar test for ParsingLayerCacheManager (simulate propagateMasks)
53
// XXX write similar test for ParsingLayerCacheManager (simulate propagateMasks)
Lines 249-252 Link Here
249
    
251
    
250
    // XXX test create -> mask -> recreate in same MFS
252
    // XXX test create -> mask -> recreate in same MFS
251
    
253
    
254
    @SuppressWarnings("deprecation") // for debugging only
255
    private static void setSystemName(FileSystem fs, String s) throws PropertyVetoException {
256
        fs.setSystemName(s);
257
    }
258
    public void testWeightedOverrides() throws Exception { // #141925
259
        FileSystem wr = FileUtil.createMemoryFileSystem();
260
        setSystemName(wr, "wr");
261
        FileSystem fs1 = FileUtil.createMemoryFileSystem();
262
        setSystemName(fs1, "fs1");
263
        FileObject f = TestFileUtils.writeFile(fs1.getRoot(), "d/f", "1");
264
        f.setAttribute("a", 1);
265
        FileSystem fs2 = FileUtil.createMemoryFileSystem();
266
        setSystemName(fs2, "fs2");
267
        f = TestFileUtils.writeFile(fs2.getRoot(), "d/f", "2");
268
        f.setAttribute("a", 2);
269
        // Test behavior with no weights: first layer wins.
270
        FileSystem mfs = new MultiFileSystem(new FileSystem[] {wr, fs1, fs2});
271
        f = mfs.findResource("d/f");
272
        assertEquals(1, f.getAttribute("a"));
273
        assertEquals("1", TestFileUtils.readFile(f));
274
        mfs = new MultiFileSystem(new FileSystem[] {wr, fs2, fs1});
275
        f = mfs.findResource("d/f");
276
        assertEquals(2, f.getAttribute("a"));
277
        assertEquals("2", TestFileUtils.readFile(f));
278
        // Now test that weighted layer wins over unweighted regardless of order.
279
        fs2.findResource("d/f").setAttribute("weight", 100);
280
        mfs = new MultiFileSystem(new FileSystem[] {wr, fs1, fs2});
281
        f = mfs.findResource("d/f");
282
        assertEquals(2, f.getAttribute("a"));
283
        assertEquals("2", TestFileUtils.readFile(f));
284
        mfs = new MultiFileSystem(new FileSystem[] {wr, fs2, fs1});
285
        f = mfs.findResource("d/f");
286
        assertEquals(2, f.getAttribute("a"));
287
        assertEquals("2", TestFileUtils.readFile(f));
288
        // And that a higher weight beats a lower weight.
289
        fs1.findResource("d/f").setAttribute("weight", 200);
290
        mfs = new MultiFileSystem(new FileSystem[] {wr, fs1, fs2});
291
        f = mfs.findResource("d/f");
292
        assertEquals(1, f.getAttribute("a"));
293
        assertEquals("1", TestFileUtils.readFile(f));
294
        mfs = new MultiFileSystem(new FileSystem[] {wr, fs2, fs1});
295
        f = mfs.findResource("d/f");
296
        assertEquals(1, f.getAttribute("a"));
297
        assertEquals("1", TestFileUtils.readFile(f));
298
        // Now test writable layer which should always win regardless of weights.
299
        mfs = new MultiFileSystem(new FileSystem[] {wr, fs1, fs2});
300
        f = mfs.findResource("d/f");
301
        f.setAttribute("a", 0);
302
        TestFileUtils.writeFile(mfs.getRoot(), "d/f", "0");
303
        f = wr.findResource("d/f");
304
        // Oddly, it is null: assertEquals(0, f.getAttribute("a"));
305
        assertEquals("0", TestFileUtils.readFile(f));
306
        mfs = new MultiFileSystem(new FileSystem[] {wr, fs1, fs2});
307
        f = mfs.findResource("d/f");
308
        assertEquals(0, f.getAttribute("a"));
309
        assertEquals("0", TestFileUtils.readFile(f));
310
    }
311
252
}
312
}

Return to bug 141925