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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.application.db;
\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
21 * A headless (non-ui) application base class for one-shot connection to and
\r
22 * usage of a Simantics database.
\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
32 * The application expects at least the {@link SimanticsArguments#SERVER} and
\r
33 * supports the {@link SimanticsArguments#CHECKOUT}.
\r
36 * @see TemplateDatabaseApplication for a usage example
\r
38 public abstract class HeadlessDatabaseApplication implements IApplication {
\r
40 private DatabaseHandle database;
\r
43 public Object start(IApplicationContext context) throws Exception {
\r
45 beforeConnect(context);
\r
46 connectToServer(context);
\r
49 return IApplication.EXIT_OK;
\r
51 if (database != null) {
\r
54 } catch (Throwable t) {
\r
55 t.printStackTrace();
\r
62 public void stop() {
\r
63 // TODO: force the application to stop
\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
72 protected Session getSession() {
\r
73 if (database == null)
\r
74 throw new IllegalStateException("null database session");
\r
75 return database.getSession();
\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
83 * @param context the application startup context
\r
85 protected void beforeConnect(IApplicationContext context) {
\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
92 protected void afterConnect() {
\r
95 protected boolean deleteLocalCopyAtExit() {
\r