From 2149abc004d8b1965ad542dcde56574571f6fcfa Mon Sep 17 00:00:00 2001
From: Nikolaus Huber <nikolaus.huber@uni-wuerzburg.de>
Date: Mon, 16 Sep 2013 14:26:02 +0000
Subject: [PATCH] splitting project into several packages

git-svn-id: https://se1.informatik.uni-wuerzburg.de/usvn/svn/code/code/DMM/trunk@13065 9e42b895-fcda-4063-8a3b-11be15eb1bbd
---
 .../.classpath                                |   7 +
 .../.project                                  |  28 ++++
 .../.settings/org.eclipse.jdt.core.prefs      |   7 +
 .../.settings/org.eclipse.pde.core.prefs      |   4 +
 .../META-INF/MANIFEST.MF                      |  28 ++++
 .../build.properties                          |   4 +
 .../repository/AbstractEcoreModelLoader.java  | 124 ++++++++++++++++++
 .../model/repository/Activator.java           |  30 +++++
 .../dmm/AdaptationProcessModelLoader.java     | 111 ++++++++++++++++
 9 files changed, 343 insertions(+)
 create mode 100644 edu.kit.ipd.descartes.adaptation.model.repository/.classpath
 create mode 100644 edu.kit.ipd.descartes.adaptation.model.repository/.project
 create mode 100644 edu.kit.ipd.descartes.adaptation.model.repository/.settings/org.eclipse.jdt.core.prefs
 create mode 100644 edu.kit.ipd.descartes.adaptation.model.repository/.settings/org.eclipse.pde.core.prefs
 create mode 100644 edu.kit.ipd.descartes.adaptation.model.repository/META-INF/MANIFEST.MF
 create mode 100644 edu.kit.ipd.descartes.adaptation.model.repository/build.properties
 create mode 100644 edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/AbstractEcoreModelLoader.java
 create mode 100644 edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/Activator.java
 create mode 100644 edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/dmm/AdaptationProcessModelLoader.java

diff --git a/edu.kit.ipd.descartes.adaptation.model.repository/.classpath b/edu.kit.ipd.descartes.adaptation.model.repository/.classpath
new file mode 100644
index 00000000..ad32c83a
--- /dev/null
+++ b/edu.kit.ipd.descartes.adaptation.model.repository/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/edu.kit.ipd.descartes.adaptation.model.repository/.project b/edu.kit.ipd.descartes.adaptation.model.repository/.project
new file mode 100644
index 00000000..4805af1e
--- /dev/null
+++ b/edu.kit.ipd.descartes.adaptation.model.repository/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>edu.kit.ipd.descartes.adaptation.model.repository</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.ManifestBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.SchemaBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.pde.PluginNature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
diff --git a/edu.kit.ipd.descartes.adaptation.model.repository/.settings/org.eclipse.jdt.core.prefs b/edu.kit.ipd.descartes.adaptation.model.repository/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 00000000..c537b630
--- /dev/null
+++ b/edu.kit.ipd.descartes.adaptation.model.repository/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
diff --git a/edu.kit.ipd.descartes.adaptation.model.repository/.settings/org.eclipse.pde.core.prefs b/edu.kit.ipd.descartes.adaptation.model.repository/.settings/org.eclipse.pde.core.prefs
new file mode 100644
index 00000000..e8ff8be0
--- /dev/null
+++ b/edu.kit.ipd.descartes.adaptation.model.repository/.settings/org.eclipse.pde.core.prefs
@@ -0,0 +1,4 @@
+eclipse.preferences.version=1
+pluginProject.equinox=false
+pluginProject.extensions=false
+resolve.requirebundle=false
diff --git a/edu.kit.ipd.descartes.adaptation.model.repository/META-INF/MANIFEST.MF b/edu.kit.ipd.descartes.adaptation.model.repository/META-INF/MANIFEST.MF
new file mode 100644
index 00000000..2d69bc81
--- /dev/null
+++ b/edu.kit.ipd.descartes.adaptation.model.repository/META-INF/MANIFEST.MF
@@ -0,0 +1,28 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: ModelRepository
+Bundle-SymbolicName: edu.kit.ipd.descartes.adaptation.model.repository
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: edu.kit.ipd.descartes.adaptation.model.repository.Activator
+Bundle-Vendor: Descartes Research Group
+Bundle-RequiredExecutionEnvironment: JavaSE-1.6
+Import-Package: edu.kit.ipd.descartes.core,
+ edu.kit.ipd.descartes.mm.adaptation,
+ edu.kit.ipd.descartes.mm.adaptationpoints,
+ edu.kit.ipd.descartes.mm.containerrepository,
+ edu.kit.ipd.descartes.mm.resourcelandscape,
+ org.apache.log4j;version="1.2.15",
+ org.eclipse.core.resources,
+ org.eclipse.core.runtime;version="3.4.0",
+ org.eclipse.emf.common.util,
+ org.eclipse.emf.ecore,
+ org.eclipse.emf.ecore.change,
+ org.eclipse.emf.ecore.change.util,
+ org.eclipse.emf.ecore.resource,
+ org.eclipse.emf.ecore.resource.impl,
+ org.eclipse.emf.ecore.util,
+ org.eclipse.emf.ecore.xmi,
+ org.eclipse.emf.ecore.xmi.impl,
+ org.osgi.framework;version="1.3.0"
+Export-Package: edu.kit.ipd.descartes.adaptation.model.repository,
+ edu.kit.ipd.descartes.adaptation.model.repository.dmm
diff --git a/edu.kit.ipd.descartes.adaptation.model.repository/build.properties b/edu.kit.ipd.descartes.adaptation.model.repository/build.properties
new file mode 100644
index 00000000..34d2e4d2
--- /dev/null
+++ b/edu.kit.ipd.descartes.adaptation.model.repository/build.properties
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .
diff --git a/edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/AbstractEcoreModelLoader.java b/edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/AbstractEcoreModelLoader.java
new file mode 100644
index 00000000..2057904a
--- /dev/null
+++ b/edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/AbstractEcoreModelLoader.java
@@ -0,0 +1,124 @@
+package edu.kit.ipd.descartes.adaptation.model.repository;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.xmi.XMIResource;
+
+public abstract class AbstractEcoreModelLoader {
+	
+    private static Logger logger = Logger.getLogger(AbstractEcoreModelLoader.class);
+    private ResourceSet resourceSet = null;
+    private URI currentUri = null;
+    private EObject loadedModel;
+    
+	protected ResourceSet getResourceSet() {
+	    if (resourceSet == null)
+	        throw new NullPointerException("INTITIALIZE RESOURCE SET FIRST! ");
+        return resourceSet;
+    }
+
+	protected void setResourceSet(ResourceSet resourceSet) {
+        this.resourceSet = resourceSet;
+    }
+
+	/**Implement this method to initialize your {@link ResourceSet}, e.g.,
+	 * if you load your models in a standalone application.
+	 * 
+	 * @param resourceSet
+	 */
+	public abstract void initializeResourceSet(ResourceSet resourceSet);
+
+	/**
+	 * Loads the model specified by <code>uri</code>.
+	 * 
+	 * @param uri
+	 * @return
+	 * @throws IOException
+	 */
+    public EObject load(URI uri) {
+        
+        if (null == uri)
+            throw new IllegalArgumentException("Missing file URI.");
+        
+        if (uri.equals(currentUri) && loadedModel != null)
+            return loadedModel;
+        
+        // Resource resource = resourceSet.createResource(fileURI);
+        Resource resource = resourceSet.getResource(uri, true);
+        try {
+            resource.load(null);
+            logger.info("Model from URI " + uri.toFileString() + " loaded.");
+        } catch (IOException e) {
+            logger.error("Error while loading model " + uri.toFileString(), e);
+            e.printStackTrace();
+        }
+        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 {
+        if (null == uri) {
+            throw new IllegalArgumentException("Missing file URI.");
+        }
+        Resource resource = resourceSet.createResource(uri);
+        resource.getContents().add(obj);
+        Map<String, Boolean> options = new HashMap<String, Boolean>();
+        options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
+        options.put(XMIResource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER, Boolean.TRUE);
+        resource.save(options);
+    }
+	
+	/** Convenience method to create an URI.
+	 *
+	 * @param projectname
+	 * @param filename
+	 * @param filename_extension
+	 * @return
+	 */
+    public static URI createFileURI(String projectname, String filename, String filename_extension) {
+        if (null == projectname) {
+            throw new IllegalArgumentException("Missing project name.");
+        }
+        if (null == filename) {
+            throw new IllegalArgumentException("Missing file name.");
+        }
+        if (null == filename_extension) {
+            throw new IllegalArgumentException("Missing file name extension.");
+        }
+        /* TODO check for null pointer exceptions */
+        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+        IProject project = root.getProject(projectname);
+        String projectPath = project.getLocationURI().getPath();
+        URI fileURI = URI.createFileURI(projectPath);
+        return fileURI.appendSegment(filename).appendFileExtension(filename_extension);
+    }
+    
+    /**
+     * Convenience method to determine the current model location.
+     * @return
+     */
+    public URI getCurrentModelUri() {
+        return currentUri;
+    }
+}
diff --git a/edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/Activator.java b/edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/Activator.java
new file mode 100644
index 00000000..1aa04c83
--- /dev/null
+++ b/edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/Activator.java
@@ -0,0 +1,30 @@
+package edu.kit.ipd.descartes.adaptation.model.repository;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+public class Activator implements BundleActivator {
+
+	private static BundleContext context;
+
+	static BundleContext getContext() {
+		return context;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+	 */
+	public void start(BundleContext bundleContext) throws Exception {
+		Activator.context = bundleContext;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+	 */
+	public void stop(BundleContext bundleContext) throws Exception {
+		Activator.context = null;
+	}
+
+}
diff --git a/edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/dmm/AdaptationProcessModelLoader.java b/edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/dmm/AdaptationProcessModelLoader.java
new file mode 100644
index 00000000..0200cd79
--- /dev/null
+++ b/edu.kit.ipd.descartes.adaptation.model.repository/src/edu/kit/ipd/descartes/adaptation/model/repository/dmm/AdaptationProcessModelLoader.java
@@ -0,0 +1,111 @@
+package edu.kit.ipd.descartes.adaptation.model.repository.dmm;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.change.ChangeDescription;
+import org.eclipse.emf.ecore.change.util.ChangeRecorder;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecore.xmi.DanglingHREFException;
+import org.eclipse.emf.ecore.xmi.XMIResource;
+import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
+
+import edu.kit.ipd.descartes.adaptation.model.repository.AbstractEcoreModelLoader;
+import edu.kit.ipd.descartes.mm.adaptation.AdaptationPackage;
+import edu.kit.ipd.descartes.mm.adaptation.AdaptationProcess;
+import edu.kit.ipd.descartes.mm.adaptationpoints.AdaptationpointsPackage;
+import edu.kit.ipd.descartes.mm.containerrepository.ContainerrepositoryPackage;
+import edu.kit.ipd.descartes.mm.resourcelandscape.DistributedDataCenter;
+import edu.kit.ipd.descartes.mm.resourcelandscape.ResourcelandscapePackage;
+
+public class AdaptationProcessModelLoader extends AbstractEcoreModelLoader {
+
+    public static final EPackage[] DMM_EPACKAGES = new EPackage[] { 
+        ResourcelandscapePackage.eINSTANCE,
+        AdaptationpointsPackage.eINSTANCE, 
+        ContainerrepositoryPackage.eINSTANCE, 
+        AdaptationPackage.eINSTANCE };
+
+    private static AdaptationProcessModelLoader instance = null;
+    private static ChangeRecorder recorder = null;
+
+    public static AdaptationProcessModelLoader getInstance() {
+        if (instance == null) {
+            instance = new AdaptationProcessModelLoader();
+        }
+        return instance;
+    }
+
+    private AdaptationProcessModelLoader() {
+        initializeResourceSet(new ResourceSetImpl());
+    }
+
+    public static void saveAll() throws IOException {
+
+        EList<Resource> resources = getInstance().getResourceSet().getResources();
+        Map<String, String> options = new HashMap<String, String>();
+        options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE.toString());
+        options.put(XMIResource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER, Boolean.TRUE.toString());
+        options.put(XMIResource.OPTION_PROCESS_DANGLING_HREF, XMIResource.OPTION_PROCESS_DANGLING_HREF_THROW);
+
+        for (Resource res : resources) {
+            try {
+                res.save(options);
+            } catch (IOException e) {
+                if (e.getCause() instanceof DanglingHREFException) {
+                    String newMessage = "Error while persisting " + res.getURI()
+                            + " because of dangling references. Probably not all changes have been saved.\n";
+                    throw new IOException(newMessage + e.getMessage(), e.getCause());
+                }
+            }
+        }
+    }
+
+    @Override
+    public void initializeResourceSet(ResourceSet resourceSet) {
+        setResourceSet(resourceSet);
+
+        /* Register the default resource factory -- only needed for stand-alone! */
+        getResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap()
+                .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
+
+        /* Register also the packages needed for the DMM meta model */
+        for (EPackage ePackage : DMM_EPACKAGES) {
+            getResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap().put(ePackage.getNsURI(), ePackage);
+        }
+    }
+
+    public static DistributedDataCenter getDistributedDataCenter() {
+        for (Iterator<Resource> iter = getInstance().getResourceSet().getResources().iterator(); iter.hasNext();) {
+            Resource resource = (Resource) iter.next();
+            if (resource.getContents().get(0).eClass().getClassifierID() == ResourcelandscapePackage.DISTRIBUTED_DATA_CENTER)
+                return (DistributedDataCenter) resource.getContents().get(0);
+        }
+        return null;
+    }
+
+    @Override
+    public AdaptationProcess load(URI uri) {
+        return (AdaptationProcess) super.load(uri);
+    }
+
+    public static void startRecording() {
+        recorder = new ChangeRecorder(getInstance().getResourceSet());
+    }
+    
+    public static ChangeDescription endRecording() {
+        return recorder.endRecording();
+    }
+
+    public static void endRecordingAndRollback() {
+        recorder.endRecording().apply();
+        
+    }
+}
-- 
GitLab