diff options
author | Ed Merks | 2020-01-27 13:41:43 +0000 |
---|---|---|
committer | Ed Merks | 2020-01-27 13:42:09 +0000 |
commit | efc120006f61eec01b4f1e9fd4f6315408f108da (patch) | |
tree | c891664b888b6e59485163f47503dcd6b42c91f7 /plugins/org.eclipse.emf.ecore/src/org/eclipse/emf | |
parent | 8c2fd9a987483a155cf166c21909b40f253aebb8 (diff) | |
download | org.eclipse.emf-efc120006f61eec01b4f1e9fd4f6315408f108da.tar.gz org.eclipse.emf-efc120006f61eec01b4f1e9fd4f6315408f108da.tar.xz org.eclipse.emf-efc120006f61eec01b4f1e9fd4f6315408f108da.zip |
[559317] Handle a not-yet-initialized instance location gracefully
Diffstat (limited to 'plugins/org.eclipse.emf.ecore/src/org/eclipse/emf')
3 files changed, 64 insertions, 24 deletions
diff --git a/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/ExtensibleURIConverterImpl.java b/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/ExtensibleURIConverterImpl.java index 3f3e51570..c42fe2c5f 100644 --- a/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/ExtensibleURIConverterImpl.java +++ b/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/ExtensibleURIConverterImpl.java @@ -20,11 +20,9 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; -import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.emf.common.util.BasicEList; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.plugin.EcorePlugin; import org.eclipse.emf.ecore.resource.ContentHandler; import org.eclipse.emf.ecore.resource.URIConverter; import org.eclipse.emf.ecore.resource.URIHandler; @@ -390,7 +388,6 @@ public class ExtensibleURIConverterImpl implements URIConverter getURIHandler(normalizedURI).setAttributes(normalizedURI, attributes, new OptionsMap(OPTION_URI_CONVERTER, this, options)); } - private static IWorkspaceRoot workspaceRoot = EcorePlugin.getWorkspaceRoot(); /** * Returns the normalized form of the URI. @@ -411,7 +408,7 @@ public class ExtensibleURIConverterImpl implements URIConverter String scheme = result.scheme(); if (scheme == null) { - if (workspaceRoot != null) + if (PlatformResourceURIHandlerImpl.getWorkspaceRoot() != null) { if (result.hasAbsolutePath()) { diff --git a/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/PlatformContentHandlerImpl.java b/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/PlatformContentHandlerImpl.java index 875077e46..e683ffed7 100644 --- a/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/PlatformContentHandlerImpl.java +++ b/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/PlatformContentHandlerImpl.java @@ -88,7 +88,7 @@ public class PlatformContentHandlerImpl extends ContentHandlerImpl IContentDescription contentDescription; try { - if (uri.isPlatformResource() && PlatformResourceURIHandlerImpl.workspaceRoot != null) + if (uri.isPlatformResource() && PlatformResourceURIHandlerImpl.getWorkspaceRoot() != null) { contentDescription = PlatformResourceURIHandlerImpl.WorkbenchHelper.getContentDescription(uri.toPlatformString(true), options); } @@ -175,7 +175,7 @@ public class PlatformContentHandlerImpl extends ContentHandlerImpl @Override protected String getCharset(URI uri, InputStream inputStream, Map<?, ?> options, Map<Object, Object> context) throws IOException { - if (uri.isPlatformResource() && PlatformResourceURIHandlerImpl.workspaceRoot != null) + if (uri.isPlatformResource() && PlatformResourceURIHandlerImpl.getWorkspaceRoot() != null) { return PlatformResourceURIHandlerImpl.WorkbenchHelper.getCharset(uri.toPlatformString(true), options); } @@ -193,7 +193,7 @@ public class PlatformContentHandlerImpl extends ContentHandlerImpl @Override protected String getLineDelimiter(URI uri, InputStream inputStream, Map<?, ?> options, Map<Object, Object> context) throws IOException { - if (uri.isPlatformResource() && PlatformResourceURIHandlerImpl.workspaceRoot != null) + if (uri.isPlatformResource() && PlatformResourceURIHandlerImpl.getWorkspaceRoot() != null) { return PlatformResourceURIHandlerImpl.WorkbenchHelper.getLineDelimiter(uri.toPlatformString(true), options); } diff --git a/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/PlatformResourceURIHandlerImpl.java b/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/PlatformResourceURIHandlerImpl.java index 368353990..15d8a9703 100644 --- a/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/PlatformResourceURIHandlerImpl.java +++ b/plugins/org.eclipse.emf.ecore/src/org/eclipse/emf/ecore/resource/impl/PlatformResourceURIHandlerImpl.java @@ -37,6 +37,7 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.content.IContentDescription; import org.eclipse.core.runtime.preferences.IScopeContext; import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.emf.common.EMFPlugin; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.plugin.EcorePlugin; import org.eclipse.emf.ecore.resource.Resource; @@ -161,7 +162,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl */ public static OutputStream createPlatformResourceOutputStream(String platformResourcePath, Map<?, ?> options) throws IOException { - IFile file = workspaceRoot.getFile(new Path(platformResourcePath)); + IFile file = getWorkspaceRoot().getFile(new Path(platformResourcePath)); @SuppressWarnings("unchecked") final Map<Object, Object> response = options == null ? null : (Map<Object, Object>)options.get(URIConverter.OPTION_RESPONSE); return @@ -197,7 +198,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl */ public static InputStream createPlatformResourceInputStream(String platformResourcePath, Map<?, ?> options) throws IOException { - IFile file = workspaceRoot.getFile(new Path(platformResourcePath)); + IFile file = getWorkspaceRoot().getFile(new Path(platformResourcePath)); try { if (!file.isSynchronized(IResource.DEPTH_ONE)) @@ -224,7 +225,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public static void delete(String platformResourcePath, Map<?, ?> options) throws IOException { - IFile file = workspaceRoot.getFile(new Path(platformResourcePath)); + IFile file = getWorkspaceRoot().getFile(new Path(platformResourcePath)); try { file.delete(true, null); @@ -237,13 +238,13 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public static boolean exists(String platformResourcePath, Map<?, ?> options) { - IResource resource = workspaceRoot.findMember(new Path(platformResourcePath)); + IResource resource = getWorkspaceRoot().findMember(new Path(platformResourcePath)); return resource != null && resource.getResourceAttributes() != null; } public static Map<String, ?> attributes(String platformResourcePath, Map<?, ?> options) { - IResource resource = workspaceRoot.findMember(new Path(platformResourcePath)); + IResource resource = getWorkspaceRoot().findMember(new Path(platformResourcePath)); Map<String, Object> result = new HashMap<String, Object>(); if (resource != null) { @@ -318,7 +319,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public static void updateAttributes(String platformResourcePath, Map<String, ?> attributes, Map<?, ?> options) throws IOException { - IResource resource = workspaceRoot.findMember(new Path(platformResourcePath)); + IResource resource = getWorkspaceRoot().findMember(new Path(platformResourcePath)); if (resource == null) { throw new FileNotFoundException("The resource " + platformResourcePath + " does not exist"); @@ -398,7 +399,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public static IContentDescription getContentDescription(String platformResourcePath, Map<?, ?> options) throws IOException { - IFile file = workspaceRoot.getFile(new Path(platformResourcePath)); + IFile file = getWorkspaceRoot().getFile(new Path(platformResourcePath)); try { return file.getContentDescription(); @@ -415,7 +416,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl */ public static String getCharset(String platformResourcePath, Map<?, ?> options) throws IOException { - IFile file = workspaceRoot.getFile(new Path(platformResourcePath)); + IFile file = getWorkspaceRoot().getFile(new Path(platformResourcePath)); try { return file.getCharset(); @@ -437,7 +438,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl */ public static String getLineDelimiter(String platformResourcePath, Map<?, ?> options) { - IFile file = workspaceRoot.getFile(new Path(platformResourcePath)); + IFile file = getWorkspaceRoot().getFile(new Path(platformResourcePath)); IProject project = file.getProject(); return Platform.getPreferencesService().getString @@ -449,10 +450,52 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl } /** - * The cached Eclipse workspace. + * The cached Eclipse workspace root. + * @deprecated use {@link #getWorkspaceRoot()} instead. */ - protected static IWorkspaceRoot workspaceRoot = EcorePlugin.getWorkspaceRoot(); + protected static IWorkspaceRoot workspaceRoot = getWorkspaceRoot(); + /** + * Whether {@link #cachedWorkspaceRoot} is initialized. + */ + private static boolean cachedWorkspaceRootInitialized; + + /** + * The cached Eclipse workspace root returned by {@link #getWorkspaceRoot()}. + */ + private static IWorkspaceRoot cachedWorkspaceRoot; + + /** + * Returns the Eclipse workspace root, + * except in the case that the resource bundle is not available or the platform has not yet initialized in instance location, + * in which case it returns {@code null}. + * @return the workspace root. + * @since 2.21 + */ + protected static IWorkspaceRoot getWorkspaceRoot() + { + if (!cachedWorkspaceRootInitialized) + { + try + { + // If the resource bundle isn't available, we will always return null for this method. + if (EMFPlugin.IS_RESOURCES_BUNDLE_AVAILABLE) + { + // This will throw an exception if the instance location is not yet initialized, + // i.e., when EMF is used by some component before the the user chooses the workspace location. + // In this case we will return null and try again later to initialize the cached workspace root instance. + EcorePlugin.getPlugin().getStateLocation(); + cachedWorkspaceRoot = workspaceRoot = EcorePlugin.getWorkspaceRoot(); + } + cachedWorkspaceRootInitialized = true; + } + catch (Exception exception) + { + // Ignore. + } + } + return cachedWorkspaceRoot; + } /** * Creates an instance. @@ -486,7 +529,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public OutputStream createOutputStream(URI uri, Map<?, ?> options) throws IOException { String platformResourcePath = uri.toPlatformString(true); - if (workspaceRoot != null) + if (getWorkspaceRoot() != null) { return WorkbenchHelper.createPlatformResourceOutputStream(platformResourcePath, options); } @@ -520,7 +563,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException { String platformResourcePath = uri.toPlatformString(true); - if (workspaceRoot != null) + if (getWorkspaceRoot() != null) { return WorkbenchHelper.createPlatformResourceInputStream(platformResourcePath, options); } @@ -540,7 +583,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public void delete(URI uri, Map<?, ?> options) throws IOException { String platformResourcePath = uri.toPlatformString(true); - if (workspaceRoot != null) + if (getWorkspaceRoot() != null) { WorkbenchHelper.delete(platformResourcePath, options); } @@ -562,7 +605,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public boolean exists(URI uri, Map<?, ?> options) { String platformResourcePath = uri.toPlatformString(true); - if (workspaceRoot != null) + if (getWorkspaceRoot() != null) { return WorkbenchHelper.exists(platformResourcePath, options); } @@ -577,7 +620,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public Map<String, ?> getAttributes(URI uri, Map<?, ?> options) { String platformResourcePath = uri.toPlatformString(true); - if (workspaceRoot != null) + if (getWorkspaceRoot() != null) { return WorkbenchHelper.attributes(platformResourcePath, options); } @@ -592,7 +635,7 @@ public class PlatformResourceURIHandlerImpl extends URIHandlerImpl public void setAttributes(URI uri, Map<String, ?> attributes, Map<?, ?> options) throws IOException { String platformResourcePath = uri.toPlatformString(true); - if (workspaceRoot != null) + if (getWorkspaceRoot() != null) { WorkbenchHelper.updateAttributes(platformResourcePath, attributes, options); } |