]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.application.db;\r
13 \r
14 import org.eclipse.equinox.app.IApplication;\r
15 import org.eclipse.equinox.app.IApplicationContext;\r
16 import org.simantics.application.arguments.SimanticsArguments;\r
17 import org.simantics.application.template.TemplateDatabaseApplication;\r
18 import org.simantics.db.Session;\r
19 \r
20 /**\r
21  * A headless (non-ui) application base class for one-shot connection to and\r
22  * usage of a Simantics database.\r
23  * \r
24  * <p>\r
25  * The simplest way to use this application class is to override the\r
26  * {@link #afterConnect(DatabaseHandle)} method to do your own stuff with the\r
27  * database in question. Use {@link #getSession()} to get a hold of the database\r
28  * session.\r
29  * </p>\r
30  * \r
31  * <p>\r
32  * The application expects at least the {@link SimanticsArguments#SERVER} and\r
33  * supports the {@link SimanticsArguments#CHECKOUT}.\r
34  * </p>\r
35  * \r
36  * @see TemplateDatabaseApplication for a usage example\r
37  */\r
38 public abstract class HeadlessDatabaseApplication implements IApplication {\r
39 \r
40     private DatabaseHandle database;\r
41 \r
42     @Override\r
43     public Object start(IApplicationContext context) throws Exception {\r
44         try {\r
45             beforeConnect(context);\r
46             connectToServer(context);\r
47             afterConnect();\r
48             \r
49             return IApplication.EXIT_OK;\r
50         } finally {\r
51             if (database != null) {\r
52                 try {\r
53                     database.dispose();\r
54                 } catch (Throwable t) {\r
55                     t.printStackTrace();\r
56                 }\r
57             }\r
58         }\r
59     }\r
60 \r
61     @Override\r
62     public void stop() {\r
63         // TODO: force the application to stop\r
64     }\r
65 \r
66     protected IDatabaseHandle connectToServer(IApplicationContext context) throws Exception {\r
67         String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);\r
68         database = new DatabaseHandle(args, deleteLocalCopyAtExit());\r
69         return database;\r
70     }\r
71 \r
72     protected Session getSession() {\r
73         if (database == null)\r
74             throw new IllegalStateException("null database session");\r
75         return database.getSession();\r
76     }\r
77 \r
78     /**\r
79      * Override this method to perform actions <em>before</em> the application\r
80      * attempts to connect to the database specified by the command line\r
81      * arguments.\r
82      * \r
83      * @param context the application startup context\r
84      */\r
85     protected void beforeConnect(IApplicationContext context) {\r
86     }\r
87 \r
88     /**\r
89      * Override this method to perform actions <em>after</em> the application\r
90      * has connected to the database specified by the command line arguments.\r
91      */\r
92     protected void afterConnect() {\r
93     }\r
94 \r
95     protected boolean deleteLocalCopyAtExit() {\r
96         return false;\r
97     }\r
98 \r
99 }\r