]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.application/src/org/simantics/application/db/HeadlessDatabaseApplication.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.application / src / org / simantics / application / db / HeadlessDatabaseApplication.java
diff --git a/bundles/org.simantics.application/src/org/simantics/application/db/HeadlessDatabaseApplication.java b/bundles/org.simantics.application/src/org/simantics/application/db/HeadlessDatabaseApplication.java
new file mode 100644 (file)
index 0000000..2f13280
--- /dev/null
@@ -0,0 +1,99 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.application.db;\r
+\r
+import org.eclipse.equinox.app.IApplication;\r
+import org.eclipse.equinox.app.IApplicationContext;\r
+import org.simantics.application.arguments.SimanticsArguments;\r
+import org.simantics.application.template.TemplateDatabaseApplication;\r
+import org.simantics.db.Session;\r
+\r
+/**\r
+ * A headless (non-ui) application base class for one-shot connection to and\r
+ * usage of a Simantics database.\r
+ * \r
+ * <p>\r
+ * The simplest way to use this application class is to override the\r
+ * {@link #afterConnect(DatabaseHandle)} method to do your own stuff with the\r
+ * database in question. Use {@link #getSession()} to get a hold of the database\r
+ * session.\r
+ * </p>\r
+ * \r
+ * <p>\r
+ * The application expects at least the {@link SimanticsArguments#SERVER} and\r
+ * supports the {@link SimanticsArguments#CHECKOUT}.\r
+ * </p>\r
+ * \r
+ * @see TemplateDatabaseApplication for a usage example\r
+ */\r
+public abstract class HeadlessDatabaseApplication implements IApplication {\r
+\r
+    private DatabaseHandle database;\r
+\r
+    @Override\r
+    public Object start(IApplicationContext context) throws Exception {\r
+        try {\r
+            beforeConnect(context);\r
+            connectToServer(context);\r
+            afterConnect();\r
+            \r
+            return IApplication.EXIT_OK;\r
+        } finally {\r
+            if (database != null) {\r
+                try {\r
+                    database.dispose();\r
+                } catch (Throwable t) {\r
+                    t.printStackTrace();\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void stop() {\r
+        // TODO: force the application to stop\r
+    }\r
+\r
+    protected IDatabaseHandle connectToServer(IApplicationContext context) throws Exception {\r
+        String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);\r
+        database = new DatabaseHandle(args, deleteLocalCopyAtExit());\r
+        return database;\r
+    }\r
+\r
+    protected Session getSession() {\r
+        if (database == null)\r
+            throw new IllegalStateException("null database session");\r
+        return database.getSession();\r
+    }\r
+\r
+    /**\r
+     * Override this method to perform actions <em>before</em> the application\r
+     * attempts to connect to the database specified by the command line\r
+     * arguments.\r
+     * \r
+     * @param context the application startup context\r
+     */\r
+    protected void beforeConnect(IApplicationContext context) {\r
+    }\r
+\r
+    /**\r
+     * Override this method to perform actions <em>after</em> the application\r
+     * has connected to the database specified by the command line arguments.\r
+     */\r
+    protected void afterConnect() {\r
+    }\r
+\r
+    protected boolean deleteLocalCopyAtExit() {\r
+        return false;\r
+    }\r
+\r
+}\r