Skip to content
Snippets Groups Projects
Commit 4e692f9d authored by Nikolaus Huber's avatar Nikolaus Huber
Browse files

Added Exception handling for the case when an referenced 'lastImpact' cannot be handled

git-svn-id: https://se1.informatik.uni-wuerzburg.de/usvn/svn/code/DMM/trunk@16282 9e42b895-fcda-4063-8a3b-11be15eb1bbd
parent 840d9912
No related branches found
No related tags found
No related merge requests found
...@@ -9,31 +9,35 @@ import org.eclipse.core.resources.IProject; ...@@ -9,31 +9,35 @@ import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.util.URI; import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.UnresolvedReferenceException;
import org.eclipse.emf.ecore.xmi.XMIResource; import org.eclipse.emf.ecore.xmi.XMIResource;
public abstract class AbstractEcoreModelLoader { public abstract class AbstractEcoreModelLoader {
private static Logger logger = Logger.getLogger(AbstractEcoreModelLoader.class); private static Logger logger = Logger
private ResourceSet resourceSet = null; .getLogger(AbstractEcoreModelLoader.class);
private URI currentUri = null; private ResourceSet resourceSet = null;
private EObject loadedModel; private URI currentUri = null;
private EObject loadedModel;
protected ResourceSet getResourceSet() { protected ResourceSet getResourceSet() {
if (resourceSet == null) if (resourceSet == null)
throw new NullPointerException("INTITIALIZE RESOURCE SET FIRST! "); throw new NullPointerException("INTITIALIZE RESOURCE SET FIRST! ");
return resourceSet; return resourceSet;
} }
protected void setResourceSet(ResourceSet resourceSet) { protected void setResourceSet(ResourceSet resourceSet) {
this.resourceSet = resourceSet; this.resourceSet = resourceSet;
} }
/**Implement this method to initialize your {@link ResourceSet}, e.g., /**
* if you load your models in a standalone application. * Implement this method to initialize your {@link ResourceSet}, e.g., if
* you load your models in a standalone application.
* *
* @param resourceSet * @param resourceSet
*/ */
...@@ -46,79 +50,95 @@ public abstract class AbstractEcoreModelLoader { ...@@ -46,79 +50,95 @@ public abstract class AbstractEcoreModelLoader {
* @return * @return
* @throws IOException * @throws IOException
*/ */
public EObject load(URI uri) { public EObject load(URI uri) {
if (null == uri) if (null == uri)
throw new IllegalArgumentException("Missing file URI."); throw new IllegalArgumentException("Missing file URI.");
if (uri.equals(currentUri) && loadedModel != null) if (uri.equals(currentUri) && loadedModel != null)
return loadedModel; return loadedModel;
// Resource resource = resourceSet.createResource(fileURI); // Resource resource = resourceSet.createResource(fileURI);
Resource resource = resourceSet.getResource(uri, true); Resource resource = null;
try { try {
resource.load(null); resource = resourceSet.getResource(uri, true);
logger.info("Model from URI " + uri.toFileString() + " loaded."); } catch (WrappedException e) {
} catch (IOException e) { if (e.exception() instanceof UnresolvedReferenceException) {
logger.error("Error while loading model " + uri.toFileString(), e); logger.error("Could not load model! Reason: "
e.printStackTrace(); + e.exception().getMessage());
} logger.error("This is intended since currently a PerformanceDataRepository is used!");
currentUri = uri; logger.error("To fix this issue, please remove any 'lastImpact' references from your adaptation process model");
EcoreUtil.resolveAll(resourceSet); }
loadedModel = resource.getContents().get(0); throw e;
return resource.getContents().get(0); }
}
try {
/** resource.load(null);
* Persists the specified <code>obj</code> at the given logger.info("Model from URI " + uri.toFileString() + " loaded.");
* <code>uri</code>. } catch (IOException e) {
* logger.error("Error while loading model " + uri.toFileString(), e);
* @param uri e.printStackTrace();
* @param obj }
* @throws IOException currentUri = uri;
*/ EcoreUtil.resolveAll(resourceSet);
loadedModel = resource.getContents().get(0);
return resource.getContents().get(0);
}
/**
* Persists the specified <code>obj</code> at the given <code>uri</code>.
*
* @param uri
* @param obj
* @throws IOException
*/
public void save(URI uri, EObject obj) throws IOException { public void save(URI uri, EObject obj) throws IOException {
if (null == uri) { if (null == uri) {
throw new IllegalArgumentException("Missing file URI."); throw new IllegalArgumentException("Missing file URI.");
} }
Resource resource = resourceSet.createResource(uri); Resource resource = resourceSet.createResource(uri);
resource.getContents().add(obj); resource.getContents().add(obj);
Map<String, Boolean> options = new HashMap<String, Boolean>(); Map<String, Boolean> options = new HashMap<String, Boolean>();
options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
options.put(XMIResource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER, Boolean.TRUE); options.put(XMIResource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER,
resource.save(options); Boolean.TRUE);
} resource.save(options);
}
/** Convenience method to create an URI.
/**
* Convenience method to create an URI.
* *
* @param projectname * @param projectname
* @param filename * @param filename
* @param filename_extension * @param filename_extension
* @return * @return
*/ */
public static URI createFileURI(String projectname, String filename, String filename_extension) { public static URI createFileURI(String projectname, String filename,
if (null == projectname) { String filename_extension) {
throw new IllegalArgumentException("Missing project name."); if (null == projectname) {
} throw new IllegalArgumentException("Missing project name.");
if (null == filename) { }
throw new IllegalArgumentException("Missing file name."); if (null == filename) {
} throw new IllegalArgumentException("Missing file name.");
if (null == filename_extension) { }
throw new IllegalArgumentException("Missing file name extension."); if (null == filename_extension) {
} throw new IllegalArgumentException("Missing file name extension.");
/* TODO check for null pointer exceptions */ }
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); /* TODO check for null pointer exceptions */
IProject project = root.getProject(projectname); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
String projectPath = project.getLocationURI().getPath(); IProject project = root.getProject(projectname);
URI fileURI = URI.createFileURI(projectPath); String projectPath = project.getLocationURI().getPath();
return fileURI.appendSegment(filename).appendFileExtension(filename_extension); URI fileURI = URI.createFileURI(projectPath);
} return fileURI.appendSegment(filename).appendFileExtension(
filename_extension);
/** }
* Convenience method to determine the current model location.
* @return /**
*/ * Convenience method to determine the current model location.
public URI getCurrentModelUri() { *
return currentUri; * @return
} */
public URI getCurrentModelUri() {
return currentUri;
}
} }
...@@ -93,7 +93,7 @@ public class DmlModelLoader extends AbstractEcoreModelLoader { ...@@ -93,7 +93,7 @@ public class DmlModelLoader extends AbstractEcoreModelLoader {
@Override @Override
public AdaptationProcess load(URI uri) { public AdaptationProcess load(URI uri) {
return (AdaptationProcess) super.load(uri); return (AdaptationProcess) super.load(uri);
} }
public static void startRecording() { public static void startRecording() {
......
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