X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.application%2Fsrc%2Forg%2Fsimantics%2Fapplication%2Fdb%2FHeadlessDatabaseApplication.java;fp=bundles%2Forg.simantics.application%2Fsrc%2Forg%2Fsimantics%2Fapplication%2Fdb%2FHeadlessDatabaseApplication.java;h=2f13280ff86fd44d04075d659522bdd4c2cdae74;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..2f13280ff --- /dev/null +++ b/bundles/org.simantics.application/src/org/simantics/application/db/HeadlessDatabaseApplication.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.application.db; + +import org.eclipse.equinox.app.IApplication; +import org.eclipse.equinox.app.IApplicationContext; +import org.simantics.application.arguments.SimanticsArguments; +import org.simantics.application.template.TemplateDatabaseApplication; +import org.simantics.db.Session; + +/** + * A headless (non-ui) application base class for one-shot connection to and + * usage of a Simantics database. + * + *

+ * The simplest way to use this application class is to override the + * {@link #afterConnect(DatabaseHandle)} method to do your own stuff with the + * database in question. Use {@link #getSession()} to get a hold of the database + * session. + *

+ * + *

+ * The application expects at least the {@link SimanticsArguments#SERVER} and + * supports the {@link SimanticsArguments#CHECKOUT}. + *

+ * + * @see TemplateDatabaseApplication for a usage example + */ +public abstract class HeadlessDatabaseApplication implements IApplication { + + private DatabaseHandle database; + + @Override + public Object start(IApplicationContext context) throws Exception { + try { + beforeConnect(context); + connectToServer(context); + afterConnect(); + + return IApplication.EXIT_OK; + } finally { + if (database != null) { + try { + database.dispose(); + } catch (Throwable t) { + t.printStackTrace(); + } + } + } + } + + @Override + public void stop() { + // TODO: force the application to stop + } + + protected IDatabaseHandle connectToServer(IApplicationContext context) throws Exception { + String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS); + database = new DatabaseHandle(args, deleteLocalCopyAtExit()); + return database; + } + + protected Session getSession() { + if (database == null) + throw new IllegalStateException("null database session"); + return database.getSession(); + } + + /** + * Override this method to perform actions before the application + * attempts to connect to the database specified by the command line + * arguments. + * + * @param context the application startup context + */ + protected void beforeConnect(IApplicationContext context) { + } + + /** + * Override this method to perform actions after the application + * has connected to the database specified by the command line arguments. + */ + protected void afterConnect() { + } + + protected boolean deleteLocalCopyAtExit() { + return false; + } + +}