From 26c1b8068623e1ca4ae711349c2eecc62bf7becd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Tue, 16 Aug 2016 13:23:50 +0300 Subject: [PATCH] added backup.db and backup.ontology plugins --- bundles/org.simantics.backup.db/.classpath | 7 + bundles/org.simantics.backup.db/.project | 33 +++++ .../.settings/org.eclipse.jdt.core.prefs | 7 + .../META-INF/MANIFEST.MF | 12 ++ .../OSGI-INF/component.xml | 7 + .../org.simantics.backup.db/build.properties | 6 + .../scl/Simantics/Backup.scl | 15 ++ .../org/simantics/backup/db/Activator.java | 30 ++++ .../simantics/backup/db/ModelledBackup.java | 82 +++++++++++ .../backup/db/ModelledBackupProvider.java | 133 ++++++++++++++++++ .../org.simantics.backup.ontology/.classpath | 7 + .../org.simantics.backup.ontology/.project | 34 +++++ .../.settings/org.eclipse.jdt.core.prefs | 7 + .../META-INF/MANIFEST.MF | 12 ++ .../build.properties | 6 + .../org.simantics.backup.ontology/graph.tg | Bin 0 -> 2565 bytes .../graph/Backup.pgraph | 21 +++ .../graph/scl/SCLMain.scl | 1 + .../simantics/backup/ontology/Activator.java | 30 ++++ .../backup/ontology/BackupResource.java | 70 +++++++++ 20 files changed, 520 insertions(+) create mode 100644 bundles/org.simantics.backup.db/.classpath create mode 100644 bundles/org.simantics.backup.db/.project create mode 100644 bundles/org.simantics.backup.db/.settings/org.eclipse.jdt.core.prefs create mode 100644 bundles/org.simantics.backup.db/META-INF/MANIFEST.MF create mode 100644 bundles/org.simantics.backup.db/OSGI-INF/component.xml create mode 100644 bundles/org.simantics.backup.db/build.properties create mode 100644 bundles/org.simantics.backup.db/scl/Simantics/Backup.scl create mode 100644 bundles/org.simantics.backup.db/src/org/simantics/backup/db/Activator.java create mode 100644 bundles/org.simantics.backup.db/src/org/simantics/backup/db/ModelledBackup.java create mode 100644 bundles/org.simantics.backup.db/src/org/simantics/backup/db/ModelledBackupProvider.java create mode 100644 bundles/org.simantics.backup.ontology/.classpath create mode 100644 bundles/org.simantics.backup.ontology/.project create mode 100644 bundles/org.simantics.backup.ontology/.settings/org.eclipse.jdt.core.prefs create mode 100644 bundles/org.simantics.backup.ontology/META-INF/MANIFEST.MF create mode 100644 bundles/org.simantics.backup.ontology/build.properties create mode 100644 bundles/org.simantics.backup.ontology/graph.tg create mode 100644 bundles/org.simantics.backup.ontology/graph/Backup.pgraph create mode 100644 bundles/org.simantics.backup.ontology/graph/scl/SCLMain.scl create mode 100644 bundles/org.simantics.backup.ontology/src/org/simantics/backup/ontology/Activator.java create mode 100644 bundles/org.simantics.backup.ontology/src/org/simantics/backup/ontology/BackupResource.java diff --git a/bundles/org.simantics.backup.db/.classpath b/bundles/org.simantics.backup.db/.classpath new file mode 100644 index 000000000..b862a296d --- /dev/null +++ b/bundles/org.simantics.backup.db/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/bundles/org.simantics.backup.db/.project b/bundles/org.simantics.backup.db/.project new file mode 100644 index 000000000..7dca7f136 --- /dev/null +++ b/bundles/org.simantics.backup.db/.project @@ -0,0 +1,33 @@ + + + org.simantics.backup.db + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + org.eclipse.pde.ds.core.builder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/bundles/org.simantics.backup.db/.settings/org.eclipse.jdt.core.prefs b/bundles/org.simantics.backup.db/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..295926d96 --- /dev/null +++ b/bundles/org.simantics.backup.db/.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.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/bundles/org.simantics.backup.db/META-INF/MANIFEST.MF b/bundles/org.simantics.backup.db/META-INF/MANIFEST.MF new file mode 100644 index 000000000..27255bf14 --- /dev/null +++ b/bundles/org.simantics.backup.db/META-INF/MANIFEST.MF @@ -0,0 +1,12 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Modelled Backup Provider +Bundle-SymbolicName: org.simantics.backup.db +Bundle-Version: 1.0.0.qualifier +Bundle-Activator: org.simantics.backup.db.Activator +Require-Bundle: org.simantics, + org.simantics.backup.ontology, + org.simantics.backup +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Bundle-ActivationPolicy: lazy +Service-Component: OSGI-INF/component.xml diff --git a/bundles/org.simantics.backup.db/OSGI-INF/component.xml b/bundles/org.simantics.backup.db/OSGI-INF/component.xml new file mode 100644 index 000000000..6b299801b --- /dev/null +++ b/bundles/org.simantics.backup.db/OSGI-INF/component.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/bundles/org.simantics.backup.db/build.properties b/bundles/org.simantics.backup.db/build.properties new file mode 100644 index 000000000..5a2708a18 --- /dev/null +++ b/bundles/org.simantics.backup.db/build.properties @@ -0,0 +1,6 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + scl/,\ + OSGI-INF/component.xml diff --git a/bundles/org.simantics.backup.db/scl/Simantics/Backup.scl b/bundles/org.simantics.backup.db/scl/Simantics/Backup.scl new file mode 100644 index 000000000..1fafb0113 --- /dev/null +++ b/bundles/org.simantics.backup.db/scl/Simantics/Backup.scl @@ -0,0 +1,15 @@ +import "Simantics/DB" + +importJava "org.simantics.backup.IBackupProvider" where + data IBackupProvider + +importJava "org.simantics.backup.db.ModelledBackup" where + modelledBackup :: + ( ()) -> + (String -> Integer -> (() -> Maybe String)) -> + ( ()) -> + (String -> Integer -> ()) -> + IBackupProvider + +importJava "org.simantics.backup.BackupProviderService" where + backup :: String -> Integer -> () \ No newline at end of file diff --git a/bundles/org.simantics.backup.db/src/org/simantics/backup/db/Activator.java b/bundles/org.simantics.backup.db/src/org/simantics/backup/db/Activator.java new file mode 100644 index 000000000..8f87f9a30 --- /dev/null +++ b/bundles/org.simantics.backup.db/src/org/simantics/backup/db/Activator.java @@ -0,0 +1,30 @@ +package org.simantics.backup.db; + +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/bundles/org.simantics.backup.db/src/org/simantics/backup/db/ModelledBackup.java b/bundles/org.simantics.backup.db/src/org/simantics/backup/db/ModelledBackup.java new file mode 100644 index 000000000..258915ce8 --- /dev/null +++ b/bundles/org.simantics.backup.db/src/org/simantics/backup/db/ModelledBackup.java @@ -0,0 +1,82 @@ +package org.simantics.backup.db; + +import java.nio.file.Path; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; + +import org.simantics.backup.BackupException; +import org.simantics.backup.IBackupProvider; +import org.simantics.scl.runtime.function.Function1; +import org.simantics.scl.runtime.function.Function2; +import org.simantics.scl.runtime.tuple.Tuple0; + +public class ModelledBackup implements IBackupProvider { + + private final Function1 lockFunc; + private final Function2> backupFunc; + private final Function1 unlockFunc; + private final Function2 restoreFunc; + + private ModelledBackup( + Function1 lockFunc, + Function2> backupFunc, + Function1 unlockFunc, + Function2 restoreFunc) { + this.lockFunc = lockFunc; + this.backupFunc = backupFunc; + this.unlockFunc = unlockFunc; + this.restoreFunc = restoreFunc; + } + + @Override + public void lock() { + lockFunc.apply(Tuple0.INSTANCE); + } + + @Override + public Future backup(Path targetPath, int revision) { + + final Function1 fn = backupFunc.apply(targetPath.toString(), revision); + + final FutureTask futureTask = new FutureTask(new Callable() { + @Override + public BackupException call() throws Exception { + try { + String error = fn.apply(Tuple0.INSTANCE); + if (error != null) { + return new BackupException(error); + } else { + return null; + } + } catch (Throwable t) { + return new BackupException(t); + } + } + }); + + Thread thread = new Thread(futureTask, "Backup Provider Thread " + backupFunc.toString()); + thread.start(); + + return futureTask; + } + + @Override + public void unlock() { + unlockFunc.apply(Tuple0.INSTANCE); + } + + @Override + public void restore(Path fromPath, int revision) { + restoreFunc.apply(fromPath.toString(), revision); + } + + public static IBackupProvider modelledBackup( + Function1 lockFunc, + Function2> backupFunc, + Function1 unlockFunc, + Function2 restoreFunc) { + + return new ModelledBackup(lockFunc, backupFunc, unlockFunc, restoreFunc); + } +} diff --git a/bundles/org.simantics.backup.db/src/org/simantics/backup/db/ModelledBackupProvider.java b/bundles/org.simantics.backup.db/src/org/simantics/backup/db/ModelledBackupProvider.java new file mode 100644 index 000000000..3aaefabbc --- /dev/null +++ b/bundles/org.simantics.backup.db/src/org/simantics/backup/db/ModelledBackupProvider.java @@ -0,0 +1,133 @@ +package org.simantics.backup.db; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.MultiStatus; +import org.eclipse.core.runtime.Status; +import org.simantics.Simantics; +import org.simantics.backup.Activator; +import org.simantics.backup.BackupException; +import org.simantics.backup.Backups; +import org.simantics.backup.IBackupProvider; +import org.simantics.backup.ontology.BackupResource; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.Instances; +import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; + +public class ModelledBackupProvider implements IBackupProvider { + + List modelledBackups = new ArrayList<>(); + + public ModelledBackupProvider() { + // Should this be on the constructor or can there be dynamic ontologies? + this.modelledBackups = getModelledBackups(); + } + + @Override + public void lock() throws BackupException { + Backups.lock(modelledBackups); + } + + @Override + public Future backup(Path targetPath, int revision) throws BackupException { + final List> backups = new ArrayList<>(); + final List exceptions = new ArrayList<>(backups.size()); + for (IBackupProvider modelledBackup : modelledBackups) { + try { + Future future = modelledBackup.backup(targetPath, revision); + backups.add(future); + } catch (BackupException e) { + exceptions.add(e); + } + } + FutureTask task = new FutureTask<>(new Callable() { + + @Override + public BackupException call() throws Exception { + for (Future f : backups) { + try { + Exception exception = f.get(); + if (exception != null) + exceptions.add(exception); + } catch (InterruptedException | ExecutionException e) { + exceptions.add(e); + } + } + BackupException problem = null; + // Throw BackupException if any of the backup operations failed. + if (!exceptions.isEmpty()) { + IStatus[] ss = exceptions.stream() + .map(e -> new Status(IStatus.ERROR, Activator.BUNDLE_ID, e.getMessage(), e)) + .toArray(IStatus[]::new); + problem = new BackupException(new CoreException(new MultiStatus(Activator.BUNDLE_ID, 0, ss, + "Backup operation(s) failed to complete.", null))); + } + return problem; + } + }); + new Thread(task).run(); + return task; + } + + @Override + public void unlock() throws BackupException { + Backups.unlock(modelledBackups); + } + + @Override + public void restore(Path fromPath, int revision) throws BackupException { + for (IBackupProvider modelledBackup : modelledBackups) { + modelledBackup.restore(fromPath, revision); + } + } + + + private List getModelledBackups() { + List modelledProviders = new ArrayList<>(0); + try { + modelledProviders = Simantics.getSession().syncRequest(new UniqueRead>() { + + @Override + public List perform(ReadGraph graph) throws DatabaseException { + BackupResource BACKUP = BackupResource.getInstance(graph); + Instances query = graph.adapt(BACKUP.ModelledBackupProvider, Instances.class); + + HashSet providers = new HashSet<>(); + + List ontologies = Layer0Utils.listOntologies(graph); + for (Resource ontology : ontologies) { + for(Resource provider : query.find(graph, ontology)) { + providers.add(provider); + } + } + List modelledBackups = new ArrayList<>(); + for (Resource provider : providers) { + Variable variable = Variables.getVariable(graph, provider); + IBackupProvider modelledBackup = variable.getPropertyValue(graph, "instance"); + modelledBackups.add(modelledBackup); + } + + return modelledBackups; + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + return modelledProviders; + } + +} diff --git a/bundles/org.simantics.backup.ontology/.classpath b/bundles/org.simantics.backup.ontology/.classpath new file mode 100644 index 000000000..b1dabee38 --- /dev/null +++ b/bundles/org.simantics.backup.ontology/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/bundles/org.simantics.backup.ontology/.project b/bundles/org.simantics.backup.ontology/.project new file mode 100644 index 000000000..af5199b11 --- /dev/null +++ b/bundles/org.simantics.backup.ontology/.project @@ -0,0 +1,34 @@ + + + org.simantics.backup.ontology + + + + + + org.simantics.graph.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + org.simantics.graph.nature + + diff --git a/bundles/org.simantics.backup.ontology/.settings/org.eclipse.jdt.core.prefs b/bundles/org.simantics.backup.ontology/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..11f6e462d --- /dev/null +++ b/bundles/org.simantics.backup.ontology/.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.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/bundles/org.simantics.backup.ontology/META-INF/MANIFEST.MF b/bundles/org.simantics.backup.ontology/META-INF/MANIFEST.MF new file mode 100644 index 000000000..94a4bfa73 --- /dev/null +++ b/bundles/org.simantics.backup.ontology/META-INF/MANIFEST.MF @@ -0,0 +1,12 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Simantics Backup Ontology +Bundle-SymbolicName: org.simantics.backup.ontology +Bundle-Version: 1.0.0.qualifier +Bundle-Activator: org.simantics.backup.ontology.Activator +Require-Bundle: org.eclipse.core.runtime, + org.simantics.layer0, + org.simantics.structural.ontology;bundle-version="1.2.0" +Bundle-RequiredExecutionEnvironment: JavaSE-1.7 +Bundle-ActivationPolicy: lazy +Export-Package: org.simantics.backup.ontology diff --git a/bundles/org.simantics.backup.ontology/build.properties b/bundles/org.simantics.backup.ontology/build.properties new file mode 100644 index 000000000..022de1735 --- /dev/null +++ b/bundles/org.simantics.backup.ontology/build.properties @@ -0,0 +1,6 @@ +source.. = src/ +output.. = bin/ +bin.includes = plugin.xml,\ + META-INF/,\ + .,\ + graph.tg diff --git a/bundles/org.simantics.backup.ontology/graph.tg b/bundles/org.simantics.backup.ontology/graph.tg new file mode 100644 index 0000000000000000000000000000000000000000..bcb6d85622b9a98b77b9f2afc83ec82e386dc77f GIT binary patch literal 2565 zcmeHJYg5}s6g5V&W!c7OQD~zYw6mb#jJRo)>HG?`*6=bSE#|EaU>C)6bo_Ct2#e$z_)J)45UU4zp)c| zUC#uIDXTfWyu2Le@l0W1qI{fY=dkdW@mdOhRMGF{eE-e(5byyxZ!}A%I!W}j(tXI% z3oL2|RVe!QEB|9fITR4#_nKlX9-JOeczqdh!|EKont;swo1+h7;@x*I}~hv`jcw zk5o6j-gm&)(E8+_7^Kr}fI|SCyYC zbI!3}l391gc_k)YnG8>r_&pOc#F}G@#g%aPgzHbrvxPZ76gv2*XLGCshqzxw#Gti5 z$gQuWW4TXq%Sk2>t5xP(U$#W$oa_NLfn&NYrW-J=17fLgKX9gLzen}9WABT7cx=yr zOld{9*4Mpi3tl$*63&kW_`leK@SVUe#`{SX>LXSPHk(4O{al);x>=|8I6&7DU2tNG zG)d37K_g*?r}~dFMhW=D%Vh(-plal#yCb&UDzzW7OfkSCA;Tlon>a6WnFEW}bI3gi zFV8c`6Nvv89{lb=b{+f}-wz<$kUa=(iMJs85ZV&g{txke*TF0J-hhlCFCf$rKZ1-Q z%!Rnl=f1^tyiJRr;+I1EA!Hr$5<*|%HOMQ-9gFMu)UQJ7c+97N1BVV~O(kY|Vo9Pc zi?!?T0kcdWSRHXa9@oOf%+1A&OYd$5T_F#u!p9AHB27dLftu~SJo`;Y1^Uk%2Z;>`^sd_i{%==U5&PU|xQp$I=Zp`} zRG%H)S<%LFqh4yUsb2rm%{tuqTe?U{z7t^M< +STR = + +BACKUP = : L0.Ontology + @L0.new + L0.HasResourceClass "org.simantics.backup.ontology.BackupResource" + +BACKUP.SCLMain : L0.SCLModule + L0.SCLModule.definition _ : L0.String + @L0.loadString "scl/SCLMain.scl" + +BACKUP.ModelledBackupProvider BACKUP.ModelledBackupProvider.instance ==> "IBackupProvider" () { + public BackupResource perform(ReadGraph graph) throws DatabaseException { + QueryControl qc = graph.getService(QueryControl.class); + return new BackupResource(qc.getIndependentGraph(graph)); + } + }); + session.registerService(BackupResource.class, ret); + } + return ret; + } + +} + -- 2.43.2