Skip to content

Commit

Permalink
Fix NullFileSystem & LocalFileSystem getInstance() implementation
Browse files Browse the repository at this point in the history
getInstance() should always return same value, but that was not always
the case.

Public constructors of both file systems allowed clients to override
static instance field, causing potential and hard to find errors at at
runtime.

Fixes #1114
  • Loading branch information
iloveeclipse committed Jan 24, 2024
1 parent 2ef6ffd commit 951970a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@

import java.net.URI;
import java.util.HashMap;
import org.eclipse.core.filesystem.*;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.IFileSystem;
import org.eclipse.core.filesystem.provider.FileSystem;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionDelta;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IRegistryChangeEvent;
import org.eclipse.core.runtime.IRegistryChangeListener;
import org.eclipse.core.runtime.RegistryFactory;
import org.eclipse.osgi.util.NLS;

/**
Expand Down Expand Up @@ -72,9 +81,15 @@ public IFileSystem getFileSystem(String scheme) throws CoreException {
IConfigurationElement element = (IConfigurationElement) result;
FileSystem fs = (FileSystem) element.createExecutableExtension("run"); //$NON-NLS-1$
fs.initialize(scheme);
//store the file system instance so we don't have to keep recreating it
registry.put(scheme, fs);
return fs;
synchronized (this) {
result = registry.get(scheme);
if (result instanceof IFileSystem) {
return (IFileSystem) result;
}
//store the file system instance so we don't have to keep recreating it
registry.put(scheme, fs);
return fs;
}
} catch (CoreException e) {
//remove this invalid file system from the registry
registry.remove(scheme);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,22 @@ public class NullFileSystem extends FileSystem {
/**
* The singleton instance of this file system.
*/
private static IFileSystem instance;
private static final IFileSystem INSTANCE = EFS.getNullFileSystem();

/**
* Returns the instance of this file system
*
* @return The instance of this file system.
*/
public static IFileSystem getInstance() {
return instance;
return INSTANCE;
}

/**
* Creates the null file system.
*/
public NullFileSystem() {
super();
instance = this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import java.io.File;
import java.net.URI;
import org.eclipse.core.filesystem.*;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.IFileSystem;
import org.eclipse.core.filesystem.provider.FileSystem;
import org.eclipse.core.runtime.IPath;
import org.eclipse.osgi.service.environment.Constants;
Expand Down Expand Up @@ -47,15 +49,15 @@ public class LocalFileSystem extends FileSystem {
/**
* The singleton instance of this file system.
*/
private static IFileSystem instance;
private static final IFileSystem INSTANCE = EFS.getLocalFileSystem();

/**
* Returns the instance of this file system
*
* @return The instance of this file system.
*/
public static IFileSystem getInstance() {
return instance;
return INSTANCE;
}

/**
Expand All @@ -71,7 +73,6 @@ static String getOS() {
*/
public LocalFileSystem() {
super();
instance = this;
}

@Override
Expand Down

0 comments on commit 951970a

Please sign in to comment.