Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.netbeans.junit.NbTestSuite;
import org.openide.filesystems.AttributesTestHidden;
import org.openide.filesystems.FileObjectTestHid;
import org.openide.filesystems.PathTestHid;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileSystemFactoryHid;
import org.openide.filesystems.FileSystemTestHid;
Expand All @@ -49,6 +50,7 @@ public static Test suite() {
NbTestSuite suite = new NbTestSuite();
suite.addTestSuite(FileSystemTestHid.class);
suite.addTestSuite(FileObjectTestHid.class);
suite.addTestSuite(PathTestHid.class);
suite.addTestSuite(AttributesTestHidden.class);
suite.addTestSuite(XMLFileSystemTestHid.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.netbeans.junit.NbTestSuite;
import org.openide.filesystems.AttributesTestHidden;
import org.openide.filesystems.FileObjectTestHid;
import org.openide.filesystems.PathTestHid;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileSystemFactoryHid;
import org.openide.filesystems.FileSystemTestHid;
Expand All @@ -47,6 +48,7 @@ public static Test suite() {
NbTestSuite suite = new NbTestSuite();
suite.addTestSuite(FileSystemTestHid.class);
suite.addTestSuite(FileObjectTestHid.class);
suite.addTestSuite(PathTestHid.class);
suite.addTestSuite(AttributesTestHidden.class);
suite.addTestSuite(XMLFileSystemTestHid.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static NbTestSuite suite(boolean created) {
NbTestSuite suite = new NbTestSuite();
suite.addTestSuite(FileSystemTestHid.class);
suite.addTestSuite(FileObjectTestHid.class);
suite.addTestSuite(PathTestHid.class);
suite.addTestSuite(URLMapperTestHidden.class);
suite.addTestSuite(FileUtilTestHidden.class);
suite.addTestSuite(FileUtilJavaIOFileHidden.class);
Expand Down
16 changes: 15 additions & 1 deletion platform/openide.filesystems/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@
under the License.

-->
<!DOCTYPE apichanges PUBLIC "-//NetBeans//DTD API changes list 1.0//EN" "../../nbbuild/javadoctools/apichanges.dtd">
<!DOCTYPE apichanges PUBLIC "-//NetBeans//DTD API changes list 1.1//EN" "../../nbbuild/javadoctools/apichanges-1.1.dtd">
<apichanges>
<apidefs>
<apidef name="filesystems">Filesystems API</apidef>
</apidefs>
<changes>
<change id="path4all.file.objects">
<api name="filesystems"/>
<summary>FileUtil.toPath works for every file object</summary>
<version major="9" minor="50"/>
<date day="1" month="12" year="2026"/>
<author login="jtulach"/>
<compatibility addition="yes" semantic="compatible"/>
<description>
<a href="@TOP@/org/openide/filesystems/FileUtil.html#toPath(org.openide.filesystems.FileObject)">FileUtil.toPath</a>
now returns a non-<code>null</code> <code>Path</code> instance
for all file objects, even for virtual ones.
</description>
<class name="FileUtil" package="org.openide.filesystems"/>
</change>
<change id="fileutil.copyposixperms">
<api name="filesystems"/>
<summary>FileObject copy preserves source posix permissions.</summary>
Expand Down
2 changes: 1 addition & 1 deletion platform/openide.filesystems/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ Manifest-Version: 1.0
OpenIDE-Module: org.openide.filesystems
OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties
OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml
OpenIDE-Module-Specification-Version: 9.48
OpenIDE-Module-Specification-Version: 9.50


Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.openide.filesystems.declmime;

import java.nio.file.Path;
import java.nio.file.spi.FileTypeDetector;

import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service = FileTypeDetector.class, position = 100)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files.probeContentType loads installed detectors with ServiceLoader.load(FileTypeDetector.class, ClassLoader.getSystemClassLoader()) (checked in the JDK 21 sources). In unit tests the module is on the system classpath, so this works. In the running IDE, openide.filesystems lives in platform/core and is loaded by MainImpl.BootClassLoader, not the system classloader, so I'd expect this detector never to be found there. Also, it's registered globally, so it would be asked about every Path in the JVM, not just the virtual ones; returning null early for non-NioPathForFileObject paths would keep it out of the way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Interesting observation
  • how do we want to address it?
  • shell we register such an implementation in openide.util.lookup or openide.util and ...
  • ... let the implementation delegate to all FileTypeDetectors found in the Lookup.getDefault()?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion sounds right to me. openide.util and openide.util.lookup both have module.jar.dir=lib, so they are on the launcher classpath, which is what ClassLoader.getSystemClassLoader() sees. A detector registered there via META-INF/services would be found by the JDK. Since it would query Lookup.getDefault() on every call, it also doesn't matter that the JDK caches the list of installed detectors on first use: modules enabled later would still be reached. Then NioFileTypeDetector itself would only need a @ServiceProvider registration, and it could return null early for paths that aren't NioPathForFileObjects.

public final class NioFileTypeDetector extends FileTypeDetector {
@Override
public String probeContentType(Path path) {
FileObject fileObject = FileUtil.toFileObject(path);
return fileObject == null ? null : fileObject.getMIMEType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public String annotateNameHtml(String name, Set<? extends FileObject> files) {
private transient ListenerList<FileStatusListener> fileStatusList;
private transient ListenerList<VetoableChangeListener> vetoableChangeList;
private transient PropertyChangeSupport changeSupport;
private transient NioFsForFileSystem nioFs;

/** Default constructor. */
public FileSystem() {
Expand Down Expand Up @@ -737,6 +738,13 @@ public final void removeFileChangeListener(FileChangeListener fcl) {
void waitRefreshed() {
}

final synchronized NioFsForFileSystem getNioFs() {
if (nioFs == null) {
nioFs = new NioFsForFileSystem(this);
}
return nioFs;
}

/** An action that it is to be called atomically with respect to filesystem event notification.
* During its execution (via {@link FileSystem#runAtomicAction runAtomicAction})
* no events about changes in filesystems are fired.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,16 +833,30 @@ public static File toFile(FileObject fo) {
return retVal;
}

/** Finds appropriate java.nio.file.Path to FileObject if possible.
* If not possible then null is returned.
* This is the inverse operation of {@link #toFileObject}.
* @param fo FileObject whose corresponding Path will be looked for
* @return java.nio.file.Path or null if no corresponding File exists.
/** Finds appropriate {@link Path} for given {@code FileObject}.
* Since version <em>9.50</em> it is guaranteed that the return value
* <em>will never be {@code null}</em> for an object with valid {@link FileObject#getFileSystem()}.
* A fully functional {@link Path}
* instance is created even for fully virtual {@code FileObject}s.
*
* This is the inverse operation of {@link #toFileObject(java.nio.file.Path) }.
* @param fo object to find corresponding Path for
* @return {@link Path} for the {@code FileObject} never {@code null}
*
* @since 9.32
*/
public static Path toPath(FileObject fo) {
Parameters.notNull("fo", fo);
File f = toFile(fo);
return f != null ? f.toPath() : null;
if (f != null) {
return f.toPath();
}
try {
var nioFs = fo.getFileSystem().getNioFs();
return NioPathForFileObject.create(fo, nioFs);
} catch (FileStateInvalidException ex) {
throw new IllegalStateException("FileObject has no valid filesystem", ex);
}
}

/**
Expand Down Expand Up @@ -917,8 +931,12 @@ public static FileObject toFileObject(File file) {
* @param path the {@link Path} to be converted
* @return the {@link FileObject} representing the {@code path} or {@code null}
* @since 9.32
* @see #toPath
*/
public static FileObject toFileObject(Path path) {
if (path instanceof NioPathForFileObject nioPath) {
return nioPath.getFileObject();
}
try {
return toFileObject(path.toFile());
} catch (UnsupportedOperationException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.openide.filesystems;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.WatchService;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;
import java.util.Collections;
import java.util.Set;

final class NioFsForFileSystem extends FileSystem {
private final org.openide.filesystems.FileSystem fileSystem;
private final NioPathProvider provider;

NioFsForFileSystem(org.openide.filesystems.FileSystem fileSystem) {
this.fileSystem = fileSystem;
this.provider = new NioPathProvider(this);
}

org.openide.filesystems.FileSystem getFileSystem() {
return fileSystem;
}

@Override
public FileSystemProvider provider() {
return provider;
}

@Override
public void close() {
}

@Override
public boolean isOpen() {
return true;
}

@Override
public boolean isReadOnly() {
return fileSystem.isReadOnly();
}

@Override
public String getSeparator() {
return "/";
}

@Override
public Iterable<Path> getRootDirectories() {
return Collections.singletonList(NioPathForFileObject.create(fileSystem.getRoot(), this));
}

@Override
public Iterable<FileStore> getFileStores() {
return Collections.emptyList();
}

@Override
public Set<String> supportedFileAttributeViews() {
return Collections.singleton("basic");
}

@Override
public Path getPath(String first, String... more) {
StringBuilder value = new StringBuilder(first);
for (String part : more) {
if (!value.isEmpty() && value.charAt(value.length() - 1) != '/') {
value.append('/');
}
value.append(part);
}
String normalized = normalizedPath(value.toString());
FileObject fo = fileSystem.findResource(normalized);
String name = normalizeName(value.toString());
if (!value.toString().startsWith("/")) {
return NioPathForFileObject.relative(fo, this, name);
}
return NioPathForFileObject.create(fo, this, name);
}

private String normalizedPath(String value) {
if (value.isEmpty()) {
return "";
}
String result = value.replace('\\', '/');
while (result.startsWith("/")) {
result = result.substring(1);
}
return result;
}

private String normalizeName(String value) {
String result = value.replace('\\', '/');
while (result.startsWith("/")) {
result = result.substring(1);
}
return result;
}

@Override
public PathMatcher getPathMatcher(String syntaxAndPattern) {
throw new UnsupportedOperationException("PathMatcher is not supported");
}

@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
throw new UnsupportedOperationException("User lookup is not supported");
}

@Override
public WatchService newWatchService() throws IOException {
return new NioWatchService();
}
}
Loading
Loading