Reworked guards enabling Sphinx to be used in Java standalone
applications to enable it also to be used when an OSGi runtime is
available but not Eclipse workspace is used
diff --git a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/ecore/proxymanagement/ProxyHelperAdapterFactory.java b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/ecore/proxymanagement/ProxyHelperAdapterFactory.java
index 2950c76..f425f92 100644
--- a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/ecore/proxymanagement/ProxyHelperAdapterFactory.java
+++ b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/ecore/proxymanagement/ProxyHelperAdapterFactory.java
@@ -1,7 +1,7 @@
 /**

  * <copyright>

  * 

- * Copyright (c) 2008-2012 See4sys, BMW Car IT and others.

+ * Copyright (c) 2008-2019 See4sys, BMW Car IT and others.

  * All rights reserved. This program and the accompanying materials

  * are made available under the terms of the Eclipse Public License v1.0

  * which accompanies this distribution, and is available at

@@ -15,7 +15,6 @@
  */

 package org.eclipse.sphinx.emf.internal.ecore.proxymanagement;

 

-import org.eclipse.core.runtime.Platform;

 import org.eclipse.emf.common.notify.Adapter;

 import org.eclipse.emf.common.notify.AdapterFactory;

 import org.eclipse.emf.common.notify.Notifier;

@@ -26,6 +25,7 @@
 import org.eclipse.sphinx.emf.domain.factory.EditingDomainFactoryListenerRegistry;

 import org.eclipse.sphinx.emf.domain.factory.ITransactionalEditingDomainFactoryListener;

 import org.eclipse.sphinx.emf.metamodel.MetaModelDescriptorRegistry;

+import org.eclipse.sphinx.platform.util.ExtendedPlatform;

 

 /**

  * An {@link AdapterFactory adapter factory} for creating {@link ProxyHelper} adapters on {@link ResourceSet}s.

@@ -46,13 +46,13 @@
 	}

 

 	public void start() {

-		if (Platform.isRunning()) {

+		if (ExtendedPlatform.IS_PLATFORM_RUNNING) {

 			EditingDomainFactoryListenerRegistry.INSTANCE.addListener(MetaModelDescriptorRegistry.ANY_MM, null, this, null);

 		}

 	}

 

 	public void stop() {

-		if (Platform.isRunning()) {

+		if (ExtendedPlatform.IS_PLATFORM_RUNNING) {

 			EditingDomainFactoryListenerRegistry.INSTANCE.removeListener(this);

 		}

 	}

diff --git a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/ecore/proxymanagement/blacklist/MapModelIndex.java b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/ecore/proxymanagement/blacklist/MapModelIndex.java
index ba0fb5e..507533a 100644
--- a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/ecore/proxymanagement/blacklist/MapModelIndex.java
+++ b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/ecore/proxymanagement/blacklist/MapModelIndex.java
@@ -1,7 +1,7 @@
 /**

  * <copyright>

  *

- * Copyright (c) 2008-2014 See4sys, itemis and others.

+ * Copyright (c) 2008-2019 See4sys, itemis and others.

  * All rights reserved. This program and the accompanying materials

  * are made available under the terms of the Eclipse Public License v1.0

  * which accompanies this distribution, and is available at

@@ -28,7 +28,6 @@
 import org.eclipse.core.resources.IResourceDelta;

 import org.eclipse.core.resources.ResourcesPlugin;

 import org.eclipse.core.runtime.CoreException;

-import org.eclipse.core.runtime.Platform;

 import org.eclipse.emf.common.util.TreeIterator;

 import org.eclipse.emf.common.util.URI;

 import org.eclipse.emf.ecore.EClass;

@@ -38,6 +37,7 @@
 import org.eclipse.emf.ecore.resource.ResourceSet;

 import org.eclipse.sphinx.emf.Activator;

 import org.eclipse.sphinx.emf.resource.ExtendedResourceSet;

+import org.eclipse.sphinx.platform.util.ExtendedPlatform;

 import org.eclipse.sphinx.platform.util.PlatformLogUtil;

 

 /**

@@ -59,13 +59,13 @@
 	}

 

 	public void startListening() {

-		if (Platform.isRunning()) {

+		if (ExtendedPlatform.IS_PLATFORM_RUNNING && ExtendedPlatform.IS_WORKSPACE_AVAILABLE) {

 			ResourcesPlugin.getWorkspace().addResourceChangeListener(this);

 		}

 	}

 

 	public void stopListening() {

-		if (Platform.isRunning()) {

+		if (ExtendedPlatform.IS_PLATFORM_RUNNING && ExtendedPlatform.IS_WORKSPACE_AVAILABLE) {

 			ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);

 		}

 	}

@@ -97,7 +97,8 @@
 			// System.out.println("Added Proxy: " + proxyURIs.size() + " " + proxyURI); //$NON-NLS-1$ //$NON-NLS-2$

 			return true;

 		} else {

-			// System.out.println("Trying to add existing proxy: " + proxyURIs.size() + " " + proxyURI); //$NON-NLS-1$//$NON-NLS-2$

+			// System.out.println("Trying to add existing proxy: " + proxyURIs.size() + " " + proxyURI);

+			// //$NON-NLS-1$//$NON-NLS-2$

 			return false;

 		}

 	}

@@ -110,7 +111,8 @@
 			// System.out.println("Removed Proxy: " + proxyURIs.size() + " " + proxyURI); //$NON-NLS-1$ //$NON-NLS-2$

 			return true;

 		} else {

-			// System.out.println("Trying to remove proxy: " + proxyURIs.size() + " " + proxyURI); //$NON-NLS-1$ //$NON-NLS-2$

+			// System.out.println("Trying to remove proxy: " + proxyURIs.size() + " " + proxyURI); //$NON-NLS-1$

+			// //$NON-NLS-2$

 			return false;

 		}

 	}

diff --git a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/resource/ResourceProblemMarkerService.java b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/resource/ResourceProblemMarkerService.java
index 2aca172..5b0a109 100644
--- a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/resource/ResourceProblemMarkerService.java
+++ b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/internal/resource/ResourceProblemMarkerService.java
@@ -30,7 +30,6 @@
 import org.eclipse.core.runtime.Assert;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.OperationCanceledException;
-import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.emf.ecore.resource.Resource;
 import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
@@ -219,18 +218,20 @@
 			}
 		}
 
-		if (!resourcesWithProblems.isEmpty() && Platform.isRunning()) {
+		if (!resourcesWithProblems.isEmpty()) {
 			// Collect resources to update problem markers for in sets of resources per editing domain; tolerate
 			// resources that aren't in any editing domain
 			final Map<TransactionalEditingDomain, Collection<Resource>> resourcesToUpdate = new HashMap<TransactionalEditingDomain, Collection<Resource>>();
 			for (Resource resource : resourcesWithProblems) {
 				TransactionalEditingDomain editingDomain = TransactionUtil.getEditingDomain(resource);
-				Collection<Resource> resourcesToUpdateInEditingDomain = resourcesToUpdate.get(editingDomain);
-				if (resourcesToUpdateInEditingDomain == null) {
-					resourcesToUpdateInEditingDomain = new HashSet<Resource>();
-					resourcesToUpdate.put(editingDomain, resourcesToUpdateInEditingDomain);
+				if (editingDomain != null) {
+					Collection<Resource> resourcesToUpdateInEditingDomain = resourcesToUpdate.get(editingDomain);
+					if (resourcesToUpdateInEditingDomain == null) {
+						resourcesToUpdateInEditingDomain = new HashSet<Resource>();
+						resourcesToUpdate.put(editingDomain, resourcesToUpdateInEditingDomain);
+					}
+					resourcesToUpdateInEditingDomain.add(resource);
 				}
-				resourcesToUpdateInEditingDomain.add(resource);
 			}
 
 			// Update problem markers of resources in each editing domain
diff --git a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/metamodel/MetaModelDescriptorRegistry.java b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/metamodel/MetaModelDescriptorRegistry.java
index 0a5eb72..3999a94 100644
--- a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/metamodel/MetaModelDescriptorRegistry.java
+++ b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/metamodel/MetaModelDescriptorRegistry.java
@@ -1,7 +1,7 @@
 /**
  * <copyright>
  *
- * Copyright (c) 2008-2014 BMW Car IT, See4sys, itemis and others.
+ * Copyright (c) 2008-2019 BMW Car IT, See4sys, itemis and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -197,7 +197,7 @@
 	 */
 	private void readContributedDescriptors() {
 		IExtensionRegistry extensionRegistry = getExtensionRegistry();
-		if (extensionRegistry != null) {
+		if (extensionRegistry != null && extensionRegistry.getExtensionPoint(EXTP_META_MODEL_DESCRIPTORS) != null) {
 			IExtension[] extensions = extensionRegistry.getExtensionPoint(EXTP_META_MODEL_DESCRIPTORS).getExtensions();
 			for (IExtension extension : extensions) {
 				IConfigurationElement[] configElements = extension.getConfigurationElements();
@@ -236,7 +236,7 @@
 	 */
 	private void readAssociatedContentTypeIds() {
 		IExtensionRegistry extensionRegistry = getExtensionRegistry();
-		if (extensionRegistry != null) {
+		if (extensionRegistry != null && extensionRegistry.getExtensionPoint(EXTP_META_MODEL_DESCRIPTORS) != null) {
 			IExtension[] extensions = extensionRegistry.getExtensionPoint(EXTP_META_MODEL_DESCRIPTORS).getExtensions();
 			for (IExtension extension : extensions) {
 				IConfigurationElement[] configElements = extension.getConfigurationElements();
@@ -294,7 +294,7 @@
 	 */
 	private void readContributedTargetMetaModelDescriptorProviders() {
 		IExtensionRegistry extensionRegistry = getExtensionRegistry();
-		if (extensionRegistry != null) {
+		if (extensionRegistry != null && extensionRegistry.getExtensionPoint(EXTP_META_MODEL_DESCRIPTORS) != null) {
 			Set<String> overriddenIds = new HashSet<String>();
 			IExtension[] extensions = extensionRegistry.getExtensionPoint(EXTP_META_MODEL_DESCRIPTORS).getExtensions();
 			for (IExtension extension : extensions) {
diff --git a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/resource/ModelConverterRegistry.java b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/resource/ModelConverterRegistry.java
index 90cb8bf..237e262 100644
--- a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/resource/ModelConverterRegistry.java
+++ b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/resource/ModelConverterRegistry.java
@@ -1,7 +1,7 @@
 /**

  * <copyright>

  * 

- * Copyright (c) 2008-2010 See4sys, BMW Car IT and others.

+ * Copyright (c) 2008-2019 See4sys, BMW Car IT and others.

  * All rights reserved. This program and the accompanying materials

  * are made available under the terms of the Eclipse Public License v1.0

  * which accompanies this distribution, and is available at

@@ -29,6 +29,7 @@
 import org.eclipse.sphinx.emf.Activator;

 import org.eclipse.sphinx.emf.internal.resource.ModelConverterDescription;

 import org.eclipse.sphinx.emf.metamodel.IMetaModelDescriptor;

+import org.eclipse.sphinx.platform.util.ExtendedPlatform;

 import org.eclipse.sphinx.platform.util.PlatformLogUtil;

 

 /**

@@ -59,18 +60,20 @@
 	protected void init() {

 		fModelConverters = new ArrayList<IModelConverter>();

 		try {

-			IExtensionRegistry registry = Platform.getExtensionRegistry();

-			if (registry != null) {

-				String symbolicName = Activator.getPlugin().getSymbolicName();

-				IConfigurationElement[] configurationElements = registry.getConfigurationElementsFor(symbolicName, EXTP_MODEL_CONVERTERS);

-				for (IConfigurationElement element : configurationElements) {

-					try {

-						IModelConverter converter = (IModelConverter) element.createExecutableExtension(ATTR_CLASS);

-						IModelConverterDescription convDesc = new ModelConverterDescription(getDescription(element), getWarning(element));

-						fConverterDescriptions.put(converter, convDesc);

-						fModelConverters.add(converter);

-					} catch (CoreException ex) {

-						PlatformLogUtil.logAsError(Activator.getDefault(), ex);

+			if (ExtendedPlatform.IS_PLATFORM_RUNNING) {

+				IExtensionRegistry registry = Platform.getExtensionRegistry();

+				if (registry != null) {

+					String symbolicName = Activator.getPlugin().getSymbolicName();

+					IConfigurationElement[] configurationElements = registry.getConfigurationElementsFor(symbolicName, EXTP_MODEL_CONVERTERS);

+					for (IConfigurationElement element : configurationElements) {

+						try {

+							IModelConverter converter = (IModelConverter) element.createExecutableExtension(ATTR_CLASS);

+							IModelConverterDescription convDesc = new ModelConverterDescription(getDescription(element), getWarning(element));

+							fConverterDescriptions.put(converter, convDesc);

+							fModelConverters.add(converter);

+						} catch (CoreException ex) {

+							PlatformLogUtil.logAsError(Activator.getDefault(), ex);

+						}

 					}

 				}

 			}

diff --git a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/resource/ResourceHandlerRegistry.java b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/resource/ResourceHandlerRegistry.java
index f9e233e..764a63b 100644
--- a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/resource/ResourceHandlerRegistry.java
+++ b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/resource/ResourceHandlerRegistry.java
@@ -1,7 +1,7 @@
 /**

  * <copyright>

  * 

- * Copyright (c) 2008-2010 See4sys, BMW Car IT and others.

+ * Copyright (c) 2008-2019 See4sys, BMW Car IT and others.

  * All rights reserved. This program and the accompanying materials

  * are made available under the terms of the Eclipse Public License v1.0

  * which accompanies this distribution, and is available at

@@ -24,6 +24,7 @@
 import org.eclipse.core.runtime.Platform;

 import org.eclipse.emf.ecore.xmi.XMLResource.ResourceHandler;

 import org.eclipse.sphinx.emf.Activator;

+import org.eclipse.sphinx.platform.util.ExtendedPlatform;

 import org.eclipse.sphinx.platform.util.PlatformLogUtil;

 

 /**

@@ -56,15 +57,17 @@
 

 	private void readRegistry() {

 		try {

-			IExtensionRegistry registry = Platform.getExtensionRegistry();

-			if (registry != null) {

-				String symbolicName = Activator.getPlugin().getSymbolicName();

-				IConfigurationElement[] contributions = registry.getConfigurationElementsFor(symbolicName, EXTPOINT_RESOURCE_HANDLERS);

-				for (IConfigurationElement contribution : contributions) {

-					try {

-						readContribution(contribution);

-					} catch (Exception ex) {

-						PlatformLogUtil.logAsError(Activator.getPlugin(), ex);

+			if (ExtendedPlatform.IS_PLATFORM_RUNNING) {

+				IExtensionRegistry registry = Platform.getExtensionRegistry();

+				if (registry != null) {

+					String symbolicName = Activator.getPlugin().getSymbolicName();

+					IConfigurationElement[] contributions = registry.getConfigurationElementsFor(symbolicName, EXTPOINT_RESOURCE_HANDLERS);

+					for (IConfigurationElement contribution : contributions) {

+						try {

+							readContribution(contribution);

+						} catch (Exception ex) {

+							PlatformLogUtil.logAsError(Activator.getPlugin(), ex);

+						}

 					}

 				}

 			}

diff --git a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/util/EcorePlatformUtil.java b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/util/EcorePlatformUtil.java
index a3a5977..8ae5dfc 100644
--- a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/util/EcorePlatformUtil.java
+++ b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/util/EcorePlatformUtil.java
@@ -890,7 +890,7 @@
 	 * @return The file corresponding to the specified {@link URI uri}.
 	 */
 	public static IFile getFile(URI uri) {
-		if (uri != null && Platform.isRunning()) {
+		if (uri != null && ExtendedPlatform.IS_PLATFORM_RUNNING) {
 			// Create dummy resource transporting given URI
 			Resource resource = new ResourceImpl(uri);
 
@@ -913,7 +913,7 @@
 	 * @return The file corresponding to the specified <code>resource</code>.
 	 */
 	public static IFile getFile(final Resource resource) {
-		if (resource != null && Platform.isRunning()) {
+		if (resource != null && ExtendedPlatform.IS_PLATFORM_RUNNING) {
 			return WorkspaceSynchronizer.getFile(resource);
 		}
 		return null;
diff --git a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/util/EcoreResourceUtil.java b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/util/EcoreResourceUtil.java
index f752604..bad5f73 100644
--- a/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/util/EcoreResourceUtil.java
+++ b/plugins/org.eclipse.sphinx.emf/src/org/eclipse/sphinx/emf/util/EcoreResourceUtil.java
@@ -1,7 +1,7 @@
 /**

  * <copyright>

  *

- * Copyright (c) 2008-2016 BMW Car IT, See4sys, itemis and others.

+ * Copyright (c) 2008-2019 BMW Car IT, See4sys, itemis and others.

  * All rights reserved. This program and the accompanying materials

  * are made available under the terms of the Eclipse Public License v1.0

  * which accompanies this distribution, and is available at

@@ -53,7 +53,6 @@
 import org.eclipse.core.runtime.Assert;

 import org.eclipse.core.runtime.IPath;

 import org.eclipse.core.runtime.Path;

-import org.eclipse.core.runtime.Platform;

 import org.eclipse.emf.common.CommonPlugin;

 import org.eclipse.emf.common.notify.Notifier;

 import org.eclipse.emf.common.util.BasicEList;

@@ -130,7 +129,7 @@
 		} else {

 			uriConverter = new ExtensibleURIConverterImpl();

 		}

-		if (Platform.isRunning()) {

+		if (ExtendedPlatform.IS_PLATFORM_RUNNING && ExtendedPlatform.IS_WORKSPACE_AVAILABLE) {

 			// Initialize URI mappings

 			IPath workspaceRootPath = ResourcesPlugin.getWorkspace().getRoot().getFullPath().addTrailingSeparator();

 			URI workspaceRootURI = URI.createPlatformResourceURI(workspaceRootPath.toString(), true);

@@ -191,7 +190,7 @@
 		}

 

 		// Try to convert absolute file URIs to platform resource URIs

-		if (uri.isFile() && !uri.isRelative() && Platform.isRunning()) {

+		if (uri.isFile() && !uri.isRelative() && ExtendedPlatform.IS_PLATFORM_RUNNING && ExtendedPlatform.IS_WORKSPACE_AVAILABLE) {

 			/*

 			 * !! Important Note !! Use IWorkspaceRoot#getFileForLocation(IPath) rather than trying to match the given

 			 * URI against the workspace root location. This enables cases to be covered where the given URI references

@@ -906,7 +905,7 @@
 	 * <tt>null<tt> the method will return an empty list.

 	 *

 	 * &#64;param resource

-	 * @return The content of the given <code>resource</code> or an empty list if no Resource is provided.

+	 * &#64;return The content of the given <code>resource</code> or an empty list if no Resource is provided.

 	 */

 	public static EList<EObject> getResourceContents(Resource resource) {

 		if (resource != null) {

@@ -959,7 +958,7 @@
 	 * @param uri

 	 *            The URI of the concerned resource.

 	 * @return

-	 * 		<ul>

+	 *         <ul>

 	 *         <li><tt><b>true</b>&nbsp;&nbsp;</tt> if resource set contains the model with specified the URI;</li>

 	 *         <li><tt><b>false</b>&nbsp;</tt> otherwise.</li>

 	 *         </ul>

diff --git a/plugins/org.eclipse.sphinx.platform/src/org/eclipse/sphinx/platform/util/ExtendedPlatform.java b/plugins/org.eclipse.sphinx.platform/src/org/eclipse/sphinx/platform/util/ExtendedPlatform.java
index 6ff08e2..3227b19 100644
--- a/plugins/org.eclipse.sphinx.platform/src/org/eclipse/sphinx/platform/util/ExtendedPlatform.java
+++ b/plugins/org.eclipse.sphinx.platform/src/org/eclipse/sphinx/platform/util/ExtendedPlatform.java
@@ -1,7 +1,7 @@
 /**

  * <copyright>

  *

- * Copyright (c) 2008-2013 See4sys, BMW Car IT, Continental Engineering Services, itemis and others.

+ * Copyright (c) 2008-2019 See4sys, BMW Car IT, Continental Engineering Services, itemis and others.

  * All rights reserved. This program and the accompanying materials

  * are made available under the terms of the Eclipse Public License v1.0

  * which accompanies this distribution, and is available at

@@ -86,7 +86,7 @@
 	private ExtendedPlatform() {

 	}

 

-	public static final boolean IS_ECLIPSE_RUNNING;

+	public static final boolean IS_PLATFORM_RUNNING;

 	static {

 		boolean result = false;

 		try {

@@ -94,12 +94,12 @@
 		} catch (Throwable exception) {

 			// Assume that we aren't running

 		}

-		IS_ECLIPSE_RUNNING = result;

+		IS_PLATFORM_RUNNING = result;

 	}

 

-	public static final boolean IS_RESOURCES_BUNDLE_AVAILABLE;

+	public static final boolean IS_WORKSPACE_AVAILABLE;

 	static {

-		IS_RESOURCES_BUNDLE_AVAILABLE = isBundleAvailable(ResourcesPlugin.PI_RESOURCES);

+		IS_WORKSPACE_AVAILABLE = isBundleAvailable(ResourcesPlugin.PI_RESOURCES);

 	}

 

 	/**

@@ -131,7 +131,7 @@
 	}

 

 	public static boolean isBundleAvailable(String pluginId) {

-		if (IS_ECLIPSE_RUNNING) {

+		if (IS_PLATFORM_RUNNING) {

 			try {

 				Bundle resourcesBundle = Platform.getBundle(pluginId);

 				return resourcesBundle != null && (resourcesBundle.getState() & (Bundle.ACTIVE | Bundle.STARTING | Bundle.RESOLVED)) != 0;