Skip to content
Snippets Groups Projects
Commit 89a062ce authored by Alexander Diewald's avatar Alexander Diewald
Browse files

Merge branch '3993' into 'master'

3993

See merge request !111
parents f0d887a8 2ef7d61f
No related branches found
No related tags found
1 merge request!1113993
Showing
with 224 additions and 66 deletions
ICommandLineSwitchHandler.java 1df397bf5b1a2af7040f5e73bfe7f3bd239d47c5 GREEN
IConnectionCompositor.java 82188750593a08df75a5f21fd91d4b41f72593fd GREEN
IConstraintChecker.java a6d76e332ece919adb990397dd5ef6aaa542ea7d GREEN
IEclipseResourcePostLoadProvider.java e842bb7485ef27917092ffc60af8a57e475d01d6 GREEN
......
/*-------------------------------------------------------------------------+
| Copyright 2020 fortiss GmbH |
| |
| Licensed 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.fortiss.tooling.kernel.extension;
/**
* Defines an entry point to process command line arguments following a switch. Each implementing
* class must be accompanied with an extension point definition that defines the (string) identifier
* of the switch. The registered classes are executed after all services are started.
*
* @author diewald
*/
public interface ICommandLineSwitchHandler {
/** Indicates whether the handler expects an additional argument after the registered switch. */
boolean hasAdditionalArgument();
/**
* Entry point to handle the given {@code argument} given at the command line.
*
* @param argument
* String argument following a switch's identifier .
*/
void handleCLISwitch(String argument);
}
CommandLineInterfaceService.java 6b5c94c52702f773c60b181eff52204ab379b248 GREEN
CommandStackService.java 957bda69b5feb91f002aed4d25ed334e92801e7e GREEN
ConnectionCompositorService.java d69a60cd7a3d06e91d24fd32b9c00125ea71e0dd GREEN
ConstraintCheckerService.java 459b5eb717598e7e8bb71a0c87e57ea85cb00e4b GREEN
......@@ -7,7 +8,7 @@ LibraryPrototypeProvider.java b77eddbdca78f561ffb1233e98817be361c690ae GREEN
LibraryService.java d22671ba820466062852c15873698adf28960d94 GREEN
LoggingService.java da784259f7b456b54bf75c41ec268f64919ce78d GREEN
MigrationService.java 2f800eac9793aa736089a802bbfc2c4c1c09770d GREEN
PersistencyService.java 103eef642c038ef63fa49b743d803aaa3fea2724 GREEN
PersistencyService.java 2e3936012a9b7217a293cbe8272ffa5f02518045 GREEN
PrototypeService.java cf8e6fa96ba9c2f65b24400054ed68e93238a975 GREEN
ToolingKernelInternal.java f6e7114825748683c7f1d040b41ab854a6c4d79b GREEN
TransformationService.java 3cdb86fe920158f93cd9466c6ef9697b2dd8ca7f GREEN
......
/*-------------------------------------------------------------------------+
| Copyright 2020 fortiss GmbH |
| |
| Licensed 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.fortiss.tooling.kernel.internal;
import static org.fortiss.tooling.kernel.utils.LoggingUtils.error;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.Platform;
import org.fortiss.tooling.kernel.ToolingKernelActivator;
import org.fortiss.tooling.kernel.extension.ICommandLineSwitchHandler;
import org.fortiss.tooling.kernel.service.ICommandLineInterfaceService;
/**
* Implementation of the {@link ICommandLineInterfaceService}.
*
* @author diewald
*/
public class CommandLineInterfaceService implements ICommandLineInterfaceService {
/** Singleton instance of the {@link ICommandLineInterfaceService} service. */
private static CommandLineInterfaceService INSTANCE = new CommandLineInterfaceService();
/** Guard for executing the CLI switch only once during the applications lifetime. */
private boolean handlersExecuted = false;
/** Associates CLI switches with their handlers. */
private Map<String, ICommandLineSwitchHandler> switchHandlerMap = new HashMap<>();
/** Hiding constructor. */
private CommandLineInterfaceService() {
// Prevent external instantiation.
}
/** Returns the {@link CommandLineInterfaceService} singleton. */
public static synchronized CommandLineInterfaceService getInstance() {
return INSTANCE;
}
/** {@inheritDoc} */
@Override
public void executeHandlers() {
if(handlersExecuted) {
error(ToolingKernelActivator.getDefault(), "The " +
CommandLineInterfaceService.class.getSimpleName() +
" was asked more than one to process command line switches. The subsequent " +
"calls were NOT executed. Please report this issue to the developers.");
return;
}
handlersExecuted = true;
String[] args = Platform.getApplicationArgs();
for(int cliIdx = 0; cliIdx < args.length; ++cliIdx) {
String cliSwitch = args[cliIdx];
ICommandLineSwitchHandler cliHandler = switchHandlerMap.get(cliSwitch);
if(cliHandler != null) {
String argument =
cliHandler.hasAdditionalArgument() ? argument = args[++cliIdx] : null;
cliHandler.handleCLISwitch(argument);
} else {
error(ToolingKernelActivator.getDefault(),
"Could not parse the argument " + cliSwitch + ".");
}
}
}
/** {@inheritDoc} */
@Override
public void registerHandler(String cliSwitch, ICommandLineSwitchHandler cliHandler) {
if(switchHandlerMap.containsKey(cliSwitch)) {
ICommandLineSwitchHandler activeHandler = switchHandlerMap.get(cliSwitch);
error(ToolingKernelActivator.getDefault(),
"The CLI switch " + cliSwitch + " is already registered with the handler " +
activeHandler.getClass().getSimpleName() + " and won't be replaced.");
return;
}
switchHandlerMap.put(cliSwitch, cliHandler);
}
}
......@@ -246,21 +246,21 @@ public class PersistencyService implements IPersistencyService, IIntrospectiveKe
/** Notifies listener about top-level element loading (startup). */
private synchronized void notifyListenersAboutLoad(ITopLevelElement top) {
for(IPersistencyServiceListener listener : listeners) {
for(IPersistencyServiceListener listener : new ArrayList<>(listeners)) {
listener.topLevelElementLoaded(top);
}
}
/** Notifies listener about top-level element adding. */
private synchronized void notifyListenersAboutAdd(ITopLevelElement top) {
for(IPersistencyServiceListener listener : listeners) {
for(IPersistencyServiceListener listener : new ArrayList<>(listeners)) {
listener.topLevelElementAdded(top);
}
}
/** Notifies listener about top-level element removal. */
private synchronized void notifyListenersAboutRemove(ITopLevelElement top) {
for(IPersistencyServiceListener listener : listeners) {
for(IPersistencyServiceListener listener : new ArrayList<>(listeners)) {
listener.topLevelElementRemoved(top);
}
}
......
ICommandLineInterfaceService.java c3e3ba08b2a1b8125b43abd1c29b7dc0a0be2b80 GREEN
ICommandStackService.java 678dcd1a6ab435ed0870fa2a9ec48ce47f25a187 GREEN
IConnectionCompositorService.java 0cdf4568b2cd3e95ea195df90a84699eff36442b GREEN
IConstraintCheckerService.java 291e53297aaea213e07e78f63350938ee2c7b155 GREEN
......
/*-------------------------------------------------------------------------+
| Copyright 2020 fortiss GmbH |
| |
| Licensed 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.fortiss.tooling.kernel.service;
import org.fortiss.tooling.kernel.extension.ICommandLineSwitchHandler;
import org.fortiss.tooling.kernel.internal.CommandLineInterfaceService;
/**
* Service to register handlers for command line switches and their arguments. The handlers are
* executed after the startup of all other service to ensure the presence of the full feature set
* when this service is executed.
*
* @author diewald
*/
public interface ICommandLineInterfaceService {
/** Returns the internal implementation of the {@link ICommandLineInterfaceService}. */
public static ICommandLineInterfaceService getInstance() {
return CommandLineInterfaceService.getInstance();
}
/**
* Executes the registered {@link ICommandLineSwitchHandler}s for the switches that have been
* passed to the program executable. This method can be executed only once during the
* application lifecycle, otherwise it degenerates to a no-op.
*/
void executeHandlers();
/**
* Registers an {@link ICommandLineSwitchHandler} that is executed if the given switch has
* been passed to the program executable.
*/
void registerHandler(String cliSwitch, ICommandLineSwitchHandler cliHandler);
}
CompositionUtils.java 34c0a191bd0fb4176c94b4d61abb5c88a679d5e8 GREEN
EMFResourceUtils.java 979d0e1f4f66a2b3e715d2da0ebef6493f547fd7 GREEN
EMFResourceUtils.java 68e6712a52349548bf85346900b17aa65b5f0ea9 GREEN
EcoreSerializerBase.java 0a0c2969d793d2e68094c55c8f7b0a662ef6e5d5 GREEN
EcoreUtils.java 18416b5c214410a02eb35596fd807a1cc27d6b35 GREEN
ExtensionPointUtils.java 7ce63242b49eb9a7cd4eaadd223f5ebce1dfd75b GREEN
......
......@@ -15,6 +15,7 @@
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.utils;
import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;
import static org.fortiss.tooling.kernel.utils.LoggingUtils.error;
import java.io.IOException;
......@@ -23,6 +24,7 @@ import java.util.Map;
import org.conqat.lib.commons.collections.Pair;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
......@@ -37,6 +39,7 @@ import org.fortiss.tooling.kernel.ToolingKernelActivator;
* Utility methods used for Eclipse resource storage.
*
* @author hoelzl
* @author diewald
*/
public final class EMFResourceUtils {
/**
......@@ -55,6 +58,34 @@ public final class EMFResourceUtils {
r.save(buildOptionsMap());
}
/**
* Loads the root model element from the given {@link URI}. Logs errors to the console and
* returns <code>null</code>.
*
* @param uri
* the resource {@link URI}.
* @return the root {@link EObject}
*/
public static EObject loadModelFromFile(URI uri) {
try {
return loadModelFromFileWithUnknownFeatures(uri).getFirst();
} catch(Exception ex) {
error(ToolingKernelActivator.getDefault(),
"Failed to load model from " + uri.toString());
}
return null;
}
/** Refresh the workspace. */
public static void refreshWorkspace() {
try {
// refresh the workspace
getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
} catch(Exception e) {
error(ToolingKernelActivator.getDefault(), "Cannot refresh workspace!", e);
}
}
/**
* Build the map of options used for storage of EMF models.
*
......@@ -83,45 +114,9 @@ public final class EMFResourceUtils {
* the resource {@link URI}.
* @return the root {@link EObject}.
*/
public static Pair<EObject, Map<EObject, AnyType>>
private static Pair<EObject, Map<EObject, AnyType>>
loadModelFromFileWithUnknownFeatures(URI uri) {
return loadModelFromFileWithUnknownFeatures(uri, true);
}
/**
* Loads the root model element from the given {@link URI}. Returns {@code null} in case of
* errors, and optionally logs errors to the console.
*
* @param uri
* the resource {@link URI}.
* @param logError
* Whether to log errors.
* @return the root {@link EObject}.
*/
public static Pair<EObject, Map<EObject, AnyType>> loadModelFromFileWithUnknownFeatures(URI uri,
boolean logError) {
try {
ResourceSet rset = new ResourceSetImpl();
Resource r;
r = rset.createResource(uri);
r.load(EMFResourceUtils.buildOptionsMap());
Map<EObject, AnyType> unknownFeatures = r instanceof XMIResource
? ((XMIResource)r).getEObjectToExtensionMap() : new HashMap<EObject, AnyType>();
if(!r.getContents().isEmpty()) {
// get(0) because the list is not empty by the check above and because we do not
// know which element is relevant, so we just take the first one.
return new Pair<EObject, Map<EObject, AnyType>>(r.getContents().get(0),
unknownFeatures);
}
} catch(IOException ex) {
if(logError) {
error(ToolingKernelActivator.getDefault(),
"Failed to load model from " + uri.toString(), ex);
}
} catch(Throwable t) {
return null;
}
return null;
return loadModelFromFileWithUnknownFeatures(uri, EObject.class);
}
/**
......@@ -135,20 +130,20 @@ public final class EMFResourceUtils {
* @return the root {@link EObject} of class type topLevelClass (first root which is found of
* this type is returned)
*/
public static Pair<EObject, Map<EObject, AnyType>> loadModelFromFileWithUnknownFeatures(URI uri,
Class<?> topLevelClass) {
private static Pair<EObject, Map<EObject, AnyType>>
loadModelFromFileWithUnknownFeatures(URI uri, Class<?> topLevelClass) {
try {
ResourceSet rset = new ResourceSetImpl();
Resource r;
r = rset.createResource(uri);
r.load(EMFResourceUtils.buildOptionsMap());
r.load(buildOptionsMap());
Map<EObject, AnyType> unknownFeatures = r instanceof XMIResource
? ((XMIResource)r).getEObjectToExtensionMap() : new HashMap<EObject, AnyType>();
if(!r.getContents().isEmpty()) {
for(EObject content : r.getContents()) {
if(topLevelClass.isAssignableFrom(content.getClass()))
if(topLevelClass.isAssignableFrom(content.getClass())) {
return new Pair<EObject, Map<EObject, AnyType>>(content, unknownFeatures);
}
}
}
} catch(IOException ex) {
......@@ -157,22 +152,4 @@ public final class EMFResourceUtils {
}
return null;
}
/**
* Loads the root model element from the given {@link URI}. Logs errors to the console and
* returns <code>null</code>.
*
* @param uri
* the resource {@link URI}.
* @return the root {@link EObject}
*/
public static EObject loadModelFromFile(URI uri) {
try {
return loadModelFromFileWithUnknownFeatures(uri).getFirst();
} catch(Exception ex) {
error(ToolingKernelActivator.getDefault(),
"Failed to load model from " + uri.toString());
}
return null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment