# HG changeset patch # Parent 32dda4ed3d728eb17c08cf0fcc692bdc0bbadf18 diff -r 32dda4ed3d72 editor/src/org/netbeans/modules/editor/NbEditorUtilities.java --- a/editor/src/org/netbeans/modules/editor/NbEditorUtilities.java Tue Mar 27 01:33:44 2012 +0000 +++ b/editor/src/org/netbeans/modules/editor/NbEditorUtilities.java Wed Apr 25 11:23:56 2012 +0300 @@ -113,11 +113,8 @@ /** Get the fileobject from the document's StreamDescriptionProperty property. */ public static FileObject getFileObject(Document doc) { Object sdp = doc.getProperty(Document.StreamDescriptionProperty); - if (sdp instanceof FileObject) { - return (FileObject)sdp; - } - if (sdp instanceof DataObject) { - return ((DataObject)sdp).getPrimaryFile(); + if (sdp instanceof Lookup.Provider) { + return ((Lookup.Provider) sdp).getLookup().lookup(FileObject.class); } return null; } diff -r 32dda4ed3d72 openide.filesystems/apichanges.xml --- a/openide.filesystems/apichanges.xml Tue Mar 27 01:33:44 2012 +0000 +++ b/openide.filesystems/apichanges.xml Wed Apr 25 11:23:56 2012 +0300 @@ -49,6 +49,21 @@ Filesystems API + + + FileObject is a lookup provider + + + + + +

+ Added FileObject.getLookup. +

+
+ + +
File System can create temporary file diff -r 32dda4ed3d72 openide.filesystems/arch.xml --- a/openide.filesystems/arch.xml Tue Mar 27 01:33:44 2012 +0000 +++ b/openide.filesystems/arch.xml Wed Apr 25 11:23:56 2012 +0300 @@ -533,6 +533,8 @@ --> Implementations of MIMEResolver and URLMapper are looked up . + +The FileObjectLookupFactory is also looked up in the global Lookup. diff -r 32dda4ed3d72 openide.filesystems/src/org/openide/filesystems/FileObject.java --- a/openide.filesystems/src/org/openide/filesystems/FileObject.java Tue Mar 27 01:33:44 2012 +0000 +++ b/openide.filesystems/src/org/openide/filesystems/FileObject.java Wed Apr 25 11:23:56 2012 +0300 @@ -65,7 +65,8 @@ import java.util.concurrent.Callable; import java.util.logging.Level; import org.openide.util.Enumerations; -import org.openide.util.UserQuestionException; +import org.openide.util.Lookup; +import org.openide.util.lookup.Lookups; /** This is the base for all implementations of file objects on a filesystem. * Provides basic information about the object (its name, parent, @@ -73,7 +74,7 @@ * * @author Jaroslav Tulach, Petr Hamernik, Ian Formanek */ -public abstract class FileObject extends Object implements Serializable { +public abstract class FileObject extends Object implements Serializable, Lookup.Provider { /** * Name of default line separator attribute. * File object can provide default line separator if it differs from @@ -618,6 +619,33 @@ */ public abstract InputStream getInputStream() throws FileNotFoundException; + /** + * A singleton lookup containing this file. This field exists only so we + * return the exact same instance in getLookup() when no + * FileObjectLookupFactory is present. There is no actual need to return the + * exact same instance since SingletonLookup is cheap but it helps with the + * testing. + */ + private final Lookup selfLookup = Lookups.singleton(this); + + /** + * @return lookup representing this file + */ + @Override + public Lookup getLookup() { + FileObjectLookupFactory f = Lookup.getDefault().lookup(FileObjectLookupFactory.class); + if (f != null) { + Lookup l = f.getLookup(this); + if (l != null) { + assert l.lookup(FileObject.class) != null : "At least a FileObject should be provided in the lookup"; //NOI18N + + return l; + } + } + //fallback + return selfLookup; + } + /** Reads the full content of the file object and returns it as array of * bytes. * @return array of bytes diff -r 32dda4ed3d72 openide.filesystems/src/org/openide/filesystems/FileObjectLookupFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/openide.filesystems/src/org/openide/filesystems/FileObjectLookupFactory.java Wed Apr 25 11:23:56 2012 +0300 @@ -0,0 +1,65 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): Emilian Bold + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.openide.filesystems; + +import org.openide.util.Lookup; + +/** + * A private service representing a factory that produces a FileObject's + * Lookup. + */ +public abstract class FileObjectLookupFactory { + + protected FileObjectLookupFactory() { + if (!getClass().getName().startsWith("org.openide")) { // NOI18N + throw new IllegalStateException(); + } + } + + /** + * Produce the lookup for the given file. + * + * @param fo any FileObject + * @return the FileObject's Lookup or null if unknown. + */ + public abstract Lookup getLookup(FileObject fo); +} \ No newline at end of file diff -r 32dda4ed3d72 openide.filesystems/test/unit/src/org/openide/filesystems/FileObjectLookupTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/openide.filesystems/test/unit/src/org/openide/filesystems/FileObjectLookupTest.java Wed Apr 25 11:23:56 2012 +0300 @@ -0,0 +1,118 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): Emilian Bold + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.openide.filesystems; + +import java.io.File; +import java.io.IOException; +import org.netbeans.junit.NbTestCase; +import org.openide.util.Lookup; + +public class FileObjectLookupTest extends NbTestCase { + + public FileObjectLookupTest(String testName) { + super(testName); + } + LocalFileSystem lfs; + + @Override + protected void setUp() throws Exception { + clearWorkDir(); + + File f = new File(getWorkDir(), "folder"); + assertTrue("Folder created", f.mkdir()); + File a = new File(f, "file1.txt"); + assertTrue("File created", a.createNewFile()); + + + lfs = new LocalFileSystem(); + lfs.setRootDirectory(getWorkDir()); + } + + @Override + protected void tearDown() throws Exception { + lfs.setReadOnly(true); + clearWorkDir(); + } + + public void testHasFileObject() throws IOException { + FileObject fo = lfs.findResource("folder/file1.txt"); + + FileObject primary = fo.getLookup().lookup(FileObject.class); + + assertNotNull("It should have a FileObject in the lookup (but the primary one!)", primary); + + assertEquals("Since we have no DataLoaders, the same file should be in the lookup", fo, primary); + } + + public void testSameLookupAfterMove() throws IOException { + FileObject fo = lfs.findResource("folder/file1.txt"); + + assertNotNull(fo); + + Lookup orig = fo.getLookup(); + + assertNotNull(orig); + + FileLock lock = fo.lock(); + + try { + fo = fo.move(lock, lfs.findResource("folder"), "movedfile", "txt"); + } finally { + lock.releaseLock(); + } + + Lookup move = fo.getLookup(); + + assertTrue("The lookup should remain the same after a move", orig == move); + + lock = fo.lock(); + try { + fo.rename(lock, "renamedfile", "txt"); + } finally { + lock.releaseLock(); + } + + Lookup rename = fo.getLookup(); + + assertTrue("The lookup should remain the same after a rename", orig == rename); + } +} diff -r 32dda4ed3d72 openide.loaders/arch.xml --- a/openide.loaders/arch.xml Tue Mar 27 01:33:44 2012 +0000 +++ b/openide.loaders/arch.xml Wed Apr 25 11:23:56 2012 +0300 @@ -711,6 +711,7 @@
  • DataLoaderPool.class
  • Environment.Provider.class
  • RepositoryNodeFactory.class
  • +
  • FileObjectLookupFactory.class
  • is done in core in META-INF/services. diff -r 32dda4ed3d72 openide.loaders/src/org/openide/loaders/FileObjectDataObjectBridge.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/openide.loaders/src/org/openide/loaders/FileObjectDataObjectBridge.java Wed Apr 25 11:23:56 2012 +0300 @@ -0,0 +1,69 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): Emilian Bold + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.openide.loaders; + +import java.util.logging.Level; +import java.util.logging.Logger; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileObjectLookupFactory; +import org.openide.util.Lookup; +import org.openide.util.lookup.ServiceProvider; + +@ServiceProvider(service = FileObjectLookupFactory.class) +public class FileObjectDataObjectBridge extends FileObjectLookupFactory { + private static final Logger log = Logger.getLogger(FileObjectDataObjectBridge.class.getName()); + + public FileObjectDataObjectBridge(){ + super(); + } + + @Override + public Lookup getLookup(FileObject fo) { + try { + return DataObject.find(fo).getLookup(); + } catch (DataObjectNotFoundException donfe) { + log.log(Level.WARNING, "Unexpected exception", donfe); + + return null; + } + } +} \ No newline at end of file diff -r 32dda4ed3d72 openide.loaders/test/unit/src/org/openide/loaders/FileObjectLookupTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/openide.loaders/test/unit/src/org/openide/loaders/FileObjectLookupTest.java Wed Apr 25 11:23:56 2012 +0300 @@ -0,0 +1,119 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): Emilian Bold + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.openide.loaders; + +import java.io.File; +import java.io.IOException; +import org.netbeans.junit.NbTestCase; +import org.openide.filesystems.FileLock; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.LocalFileSystem; +import org.openide.util.Lookup; + +public class FileObjectLookupTest extends NbTestCase { + + public FileObjectLookupTest(String testName) { + super(testName); + } + LocalFileSystem lfs; + + @Override + protected void setUp() throws Exception { + clearWorkDir(); + + File f = new File(getWorkDir(), "folder"); + assertTrue("Folder created", f.mkdir()); + File a = new File(f, "file1.txt"); + assertTrue("File created", a.createNewFile()); + + + lfs = new LocalFileSystem(); + lfs.setRootDirectory(getWorkDir()); + } + + @Override + protected void tearDown() throws Exception { + lfs.setReadOnly(true); + clearWorkDir(); + } + + public void testHasFileObject() throws IOException { + FileObject fo = lfs.findResource("folder/file1.txt"); + + FileObject primary = fo.getLookup().lookup(FileObject.class); + + assertNotNull("It should have a FileObject in the lookup (but the primary one!)", primary); + } + + public void testSameLookupAfterMove() throws IOException { + FileObject fo = lfs.findResource("folder/file1.txt"); + + assertNotNull(fo); + + Lookup orig = fo.getLookup(); + + assertNotNull(orig); + + FileLock lock = fo.lock(); + + try { + fo = fo.move(lock, lfs.findResource("folder"), "movedfile", "txt"); + } finally { + lock.releaseLock(); + } + + Lookup move = fo.getLookup(); + + assertTrue("The lookup should remain the same after a move", orig == move); + + lock = fo.lock(); + try { + fo.rename(lock, "renamedfile", "txt"); + } finally { + lock.releaseLock(); + } + + Lookup rename = fo.getLookup(); + + assertTrue("The lookup should remain the same after a rename", orig == rename); + } +}