1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.application.db;
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;
21 * A headless (non-ui) application base class for one-shot connection to and
22 * usage of a Simantics database.
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
32 * The application expects at least the {@link SimanticsArguments#SERVER} and
33 * supports the {@link SimanticsArguments#CHECKOUT}.
36 * @see TemplateDatabaseApplication for a usage example
38 public abstract class HeadlessDatabaseApplication implements IApplication {
40 private DatabaseHandle database;
43 public Object start(IApplicationContext context) throws Exception {
45 beforeConnect(context);
46 connectToServer(context);
49 return IApplication.EXIT_OK;
51 if (database != null) {
54 } catch (Throwable t) {
63 // TODO: force the application to stop
66 protected IDatabaseHandle connectToServer(IApplicationContext context) throws Exception {
67 String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
68 database = new DatabaseHandle(args, deleteLocalCopyAtExit());
72 protected Session getSession() {
74 throw new IllegalStateException("null database session");
75 return database.getSession();
79 * Override this method to perform actions <em>before</em> the application
80 * attempts to connect to the database specified by the command line
83 * @param context the application startup context
85 protected void beforeConnect(IApplicationContext context) {
89 * Override this method to perform actions <em>after</em> the application
90 * has connected to the database specified by the command line arguments.
92 protected void afterConnect() {
95 protected boolean deleteLocalCopyAtExit() {