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

(-)a/editor/src/org/netbeans/modules/editor/NbEditorUtilities.java (-5 / +2 lines)
Lines 113-123 Link Here
113
    /** Get the fileobject from the document's StreamDescriptionProperty property. */
113
    /** Get the fileobject from the document's StreamDescriptionProperty property. */
114
    public static FileObject getFileObject(Document doc) {
114
    public static FileObject getFileObject(Document doc) {
115
        Object sdp = doc.getProperty(Document.StreamDescriptionProperty);
115
        Object sdp = doc.getProperty(Document.StreamDescriptionProperty);
116
        if (sdp instanceof FileObject) {
116
        if (sdp instanceof Lookup.Provider) {
117
            return (FileObject)sdp;
117
            return ((Lookup.Provider) sdp).getLookup().lookup(FileObject.class);
118
        }
119
        if (sdp instanceof DataObject) {
120
            return ((DataObject)sdp).getPrimaryFile();
121
        }
118
        }
122
        return null;
119
        return null;
123
    }
120
    }
(-)a/openide.filesystems/apichanges.xml (+15 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="FileObject.getLookup">
53
            <api name="filesystems"/>
54
            <summary>FileObject is a lookup provider</summary>
55
            <version major="7" minor="60"/>
56
            <date year="2012" month="4" day="24"/>
57
            <author login="emi"/>
58
            <compatibility addition="yes" deprecation="no"/>
59
            <description>
60
                <p>
61
                    Added <code>FileObject.getLookup</code>.
62
                </p>
63
            </description>
64
            <class package="org.openide.filesystems" name="FileObject"/>
65
            <issue number="209231"/>
66
        </change>
52
        <change id="FileSystem.createTemporaryFO">
67
        <change id="FileSystem.createTemporaryFO">
53
            <api name="filesystems"/>
68
            <api name="filesystems"/>
54
            <summary>File System can create temporary file</summary>
69
            <summary>File System can create temporary file</summary>
(-)a/openide.filesystems/arch.xml (+2 lines)
Lines 533-538 Link Here
533
-->
533
-->
534
<answer id="lookup-lookup">
534
<answer id="lookup-lookup">
535
Implementations of MIMEResolver and URLMapper are looked up .
535
Implementations of MIMEResolver and URLMapper are looked up .
536
537
The FileObjectLookupFactory is also looked up in the global Lookup.
536
</answer>
538
</answer>
537
539
538
540
(-)a/openide.filesystems/src/org/openide/filesystems/FileObject.java (-2 / +30 lines)
Lines 65-71 Link Here
65
import java.util.concurrent.Callable;
65
import java.util.concurrent.Callable;
66
import java.util.logging.Level;
66
import java.util.logging.Level;
67
import org.openide.util.Enumerations;
67
import org.openide.util.Enumerations;
68
import org.openide.util.UserQuestionException;
68
import org.openide.util.Lookup;
69
import org.openide.util.lookup.Lookups;
69
70
70
/** This is the base for all implementations of file objects on a filesystem.
71
/** This is the base for all implementations of file objects on a filesystem.
71
* Provides basic information about the object (its name, parent,
72
* Provides basic information about the object (its name, parent,
Lines 73-79 Link Here
73
*
74
*
74
* @author Jaroslav Tulach, Petr Hamernik, Ian Formanek
75
* @author Jaroslav Tulach, Petr Hamernik, Ian Formanek
75
*/
76
*/
76
public abstract class FileObject extends Object implements Serializable {
77
public abstract class FileObject extends Object implements Serializable, Lookup.Provider {
77
    /**
78
    /**
78
     * Name of default line separator attribute.
79
     * Name of default line separator attribute.
79
     * File object can provide default line separator if it differs from
80
     * File object can provide default line separator if it differs from
Lines 618-623 Link Here
618
    */
619
    */
619
    public abstract InputStream getInputStream() throws FileNotFoundException;
620
    public abstract InputStream getInputStream() throws FileNotFoundException;
620
621
622
    /**
623
     * A singleton lookup containing this file. This field exists only so we
624
     * return the exact same instance in getLookup() when no
625
     * FileObjectLookupFactory is present. There is no actual need to return the
626
     * exact same instance since SingletonLookup is cheap but it helps with the
627
     * testing.
628
     */
629
    private final Lookup selfLookup = Lookups.singleton(this);
630
    
631
    /**
632
     * @return lookup representing this file
633
     */
634
    @Override
635
    public Lookup getLookup() {
636
        FileObjectLookupFactory f = Lookup.getDefault().lookup(FileObjectLookupFactory.class);
637
        if (f != null) {
638
            Lookup l = f.getLookup(this);
639
            if (l != null) {
640
                assert l.lookup(FileObject.class) != null : "At least a FileObject should be provided in the lookup"; //NOI18N
641
642
                return l;
643
            }
644
        }
645
        //fallback
646
        return selfLookup;
647
    }
648
    
621
    /** Reads the full content of the file object and returns it as array of
649
    /** Reads the full content of the file object and returns it as array of
622
     * bytes.
650
     * bytes.
623
     * @return array of bytes
651
     * @return array of bytes
(-)32dda4ed3d72 (+65 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s): Emilian Bold <emilian.bold@gmail.com>
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.openide.filesystems;
43
44
import org.openide.util.Lookup;
45
46
/**
47
 * A private service representing a factory that produces a FileObject's
48
 * Lookup.
49
 */
50
public abstract class FileObjectLookupFactory {
51
    
52
    protected FileObjectLookupFactory() {
53
        if (!getClass().getName().startsWith("org.openide")) { // NOI18N
54
            throw new IllegalStateException();
55
        }
56
    }
57
58
    /**
59
     * Produce the lookup for the given file.
60
     * 
61
     * @param fo any FileObject
62
     * @return the FileObject's Lookup or null if unknown.
63
     */
64
    public abstract Lookup getLookup(FileObject fo);
65
}
(-)32dda4ed3d72 (+118 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s): Emilian Bold <emilian.bold@gmail.com>
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.openide.filesystems;
43
44
import java.io.File;
45
import java.io.IOException;
46
import org.netbeans.junit.NbTestCase;
47
import org.openide.util.Lookup;
48
49
public class FileObjectLookupTest extends NbTestCase {
50
51
    public FileObjectLookupTest(String testName) {
52
        super(testName);
53
    }
54
    LocalFileSystem lfs;
55
56
    @Override
57
    protected void setUp() throws Exception {
58
        clearWorkDir();
59
60
        File f = new File(getWorkDir(), "folder");
61
        assertTrue("Folder created", f.mkdir());
62
        File a = new File(f, "file1.txt");
63
        assertTrue("File created", a.createNewFile());
64
65
66
        lfs = new LocalFileSystem();
67
        lfs.setRootDirectory(getWorkDir());
68
    }
69
70
    @Override
71
    protected void tearDown() throws Exception {
72
        lfs.setReadOnly(true);
73
        clearWorkDir();
74
    }
75
76
    public void testHasFileObject() throws IOException {
77
        FileObject fo = lfs.findResource("folder/file1.txt");
78
79
        FileObject primary = fo.getLookup().lookup(FileObject.class);
80
81
        assertNotNull("It should have a FileObject in the lookup (but the primary one!)", primary);
82
        
83
        assertEquals("Since we have no DataLoaders, the same file should be in the lookup", fo, primary);
84
    }
85
86
    public void testSameLookupAfterMove() throws IOException {
87
        FileObject fo = lfs.findResource("folder/file1.txt");
88
89
        assertNotNull(fo);
90
91
        Lookup orig = fo.getLookup();
92
93
        assertNotNull(orig);
94
95
        FileLock lock = fo.lock();
96
97
        try {
98
            fo = fo.move(lock, lfs.findResource("folder"), "movedfile", "txt");
99
        } finally {
100
            lock.releaseLock();
101
        }
102
103
        Lookup move = fo.getLookup();
104
105
        assertTrue("The lookup should remain the same after a move", orig == move);
106
107
        lock = fo.lock();
108
        try {
109
            fo.rename(lock, "renamedfile", "txt");
110
        } finally {
111
            lock.releaseLock();
112
        }
113
114
        Lookup rename = fo.getLookup();
115
116
        assertTrue("The lookup should remain the same after a rename", orig == rename);
117
    }
118
}
(-)a/openide.loaders/arch.xml (+1 lines)
Lines 711-716 Link Here
711
<li>DataLoaderPool.class</li>
711
<li>DataLoaderPool.class</li>
712
<li>Environment.Provider.class</li>
712
<li>Environment.Provider.class</li>
713
<li>RepositoryNodeFactory.class</li>
713
<li>RepositoryNodeFactory.class</li>
714
<li>FileObjectLookupFactory.class</li>
714
</ul>
715
</ul>
715
is done in core in META-INF/services.
716
is done in core in META-INF/services.
716
</answer>
717
</answer>
(-)32dda4ed3d72 (+69 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s): Emilian Bold <emilian.bold@gmail.com>
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.openide.loaders;
43
44
import java.util.logging.Level;
45
import java.util.logging.Logger;
46
import org.openide.filesystems.FileObject;
47
import org.openide.filesystems.FileObjectLookupFactory;
48
import org.openide.util.Lookup;
49
import org.openide.util.lookup.ServiceProvider;
50
51
@ServiceProvider(service = FileObjectLookupFactory.class)
52
public class FileObjectDataObjectBridge extends FileObjectLookupFactory {
53
    private static final Logger log = Logger.getLogger(FileObjectDataObjectBridge.class.getName());
54
55
    public FileObjectDataObjectBridge(){
56
        super();
57
    }
58
    
59
    @Override
60
    public Lookup getLookup(FileObject fo) {
61
        try {
62
            return DataObject.find(fo).getLookup();
63
        } catch (DataObjectNotFoundException donfe) {
64
            log.log(Level.WARNING, "Unexpected exception", donfe);
65
            
66
            return null;
67
        }
68
    }
69
}
(-)32dda4ed3d72 (+119 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s): Emilian Bold <emilian.bold@gmail.com>
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.openide.loaders;
43
44
import java.io.File;
45
import java.io.IOException;
46
import org.netbeans.junit.NbTestCase;
47
import org.openide.filesystems.FileLock;
48
import org.openide.filesystems.FileObject;
49
import org.openide.filesystems.LocalFileSystem;
50
import org.openide.util.Lookup;
51
52
public class FileObjectLookupTest extends NbTestCase {
53
54
    public FileObjectLookupTest(String testName) {
55
        super(testName);
56
    }
57
    LocalFileSystem lfs;
58
59
    @Override
60
    protected void setUp() throws Exception {
61
        clearWorkDir();
62
63
        File f = new File(getWorkDir(), "folder");
64
        assertTrue("Folder created", f.mkdir());
65
        File a = new File(f, "file1.txt");
66
        assertTrue("File created", a.createNewFile());
67
68
69
        lfs = new LocalFileSystem();
70
        lfs.setRootDirectory(getWorkDir());
71
    }
72
73
    @Override
74
    protected void tearDown() throws Exception {
75
        lfs.setReadOnly(true);
76
        clearWorkDir();
77
    }
78
79
    public void testHasFileObject() throws IOException {
80
        FileObject fo = lfs.findResource("folder/file1.txt");
81
82
        FileObject primary = fo.getLookup().lookup(FileObject.class);
83
84
        assertNotNull("It should have a FileObject in the lookup (but the primary one!)", primary);
85
    }
86
87
    public void testSameLookupAfterMove() throws IOException {
88
        FileObject fo = lfs.findResource("folder/file1.txt");
89
90
        assertNotNull(fo);
91
92
        Lookup orig = fo.getLookup();
93
94
        assertNotNull(orig);
95
96
        FileLock lock = fo.lock();
97
98
        try {
99
            fo = fo.move(lock, lfs.findResource("folder"), "movedfile", "txt");
100
        } finally {
101
            lock.releaseLock();
102
        }
103
104
        Lookup move = fo.getLookup();
105
106
        assertTrue("The lookup should remain the same after a move", orig == move);
107
108
        lock = fo.lock();
109
        try {
110
            fo.rename(lock, "renamedfile", "txt");
111
        } finally {
112
            lock.releaseLock();
113
        }
114
115
        Lookup rename = fo.getLookup();
116
117
        assertTrue("The lookup should remain the same after a rename", orig == rename);
118
    }
119
}

Return to bug 209231