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 java.io.IOException;
16 import org.simantics.application.arguments.Arguments;
17 import org.simantics.application.arguments.IArguments;
18 import org.simantics.application.arguments.SimanticsArguments;
19 import org.simantics.db.ServerI;
20 import org.simantics.db.Session;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.service.LifecycleSupport;
23 import org.simantics.utils.datastructures.disposable.AbstractDisposable;
24 import org.simantics.utils.datastructures.disposable.IDisposable;
26 public class DatabaseHandle extends AbstractDisposable implements IDatabaseHandle, IDisposable {
28 private ServerI server;
30 private Session session;
32 @SuppressWarnings("unused")
33 private final boolean deleteAtDispose;
40 public static DatabaseHandle open(String[] arguments) throws Exception {
41 IArguments args = Arguments.parse(arguments, SimanticsArguments.SERVER);
42 if (!args.contains(SimanticsArguments.SERVER))
43 throw new IllegalArgumentException("Missing " + SimanticsArguments.SERVER.getArgument() + " argument, a server is required.");
45 DatabaseHandle handle = new DatabaseHandle();
46 handle.init(args.get(SimanticsArguments.SERVER), false);
55 public static DatabaseHandle open(String serverAddress) throws Exception {
56 return new DatabaseHandle().init(serverAddress, false);
62 // * @throws Exception
64 // public static DatabaseHandle open(ServerAddress address) throws Exception {
65 // return DatabaseHandle.open(address.getDbid());
73 public static DatabaseHandle checkout(String serverAddress) throws Exception {
74 return new DatabaseHandle().init(serverAddress, true);
80 // * @throws Exception
82 // public static DatabaseHandle checkout(ServerAddress address) throws Exception {
83 // return new DatabaseHandle().init(address.getDbid(), true);
86 private DatabaseHandle() {
87 deleteAtDispose = false;
92 * @param deleteAtDispose
95 public DatabaseHandle(String[] arguments, boolean deleteAtDispose) throws Exception {
96 this.deleteAtDispose = deleteAtDispose;
98 IArguments args = Arguments.parse(arguments,
99 SimanticsArguments.SERVER,
100 SimanticsArguments.CHECKOUT);
102 if (!args.contains(SimanticsArguments.SERVER))
103 throw new IllegalArgumentException("Missing " + SimanticsArguments.SERVER.getArgument() + " argument, a server is required.");
104 boolean checkout = args.contains(SimanticsArguments.CHECKOUT);
106 init(args.get(SimanticsArguments.SERVER), checkout);
113 * @throws IOException
115 private DatabaseHandle init(String serverAddress, boolean checkout) throws IOException, Exception {
117 // Let UndoCoreManager pick a port on localhost for the working
118 // copy by setting the address of the local ServerInfo to null.
119 throw new UnsupportedOperationException("re-implement checkout");
120 // activation = UndoCoreManager.getManager().checkout(
121 // new ServerInfo("Remote Server", server),
122 // new ServerInfo(UUID.randomUUID().toString()));
124 // ServerAddress localServer = activation.getDescriptor().getLocal().getAddress();
125 // session = DatabaseUtils.connect(localServer);
127 session = DatabaseUtils.connect(serverAddress);
128 // session = DatabaseUtils.connect(new ServerAddress("", 0, serverAddress));
135 protected void doDispose() {
136 // TODO: make this robust and safe
138 if (session != null) {
140 LifecycleSupport support = session.getService(LifecycleSupport.class);
141 support.close(-1, false);
143 } catch (DatabaseException e) {
147 // if (activation != null) {
149 // UndoCoreManager manager = UndoCoreManager.getManager();
150 // manager.deactivate(activation, 3000, TimeUnit.MILLISECONDS);
152 // if (deleteAtDispose) {
153 // IServerDescriptor desc = activation.getDescriptor();
154 // manager.deleteLocal(new ServerInfo[] { desc.getParent(), desc.getLocal() });
156 // } catch (IOException e) {
157 // } catch (InterruptedException e) {
159 // activation = null;
164 * @see org.simantics.application.db.IDatabaseHandle#getServer()
167 public ServerI getServer() {
172 * @see org.simantics.application.db.IDatabaseHandle#getSession()
175 public Session getSession() {