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