]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.application/src/org/simantics/application/db/DatabaseHandle.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.application / src / org / simantics / application / db / DatabaseHandle.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 java.io.IOException;
15
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;
25
26 public class DatabaseHandle extends AbstractDisposable implements IDatabaseHandle, IDisposable {
27
28     private ServerI       server;
29
30     private Session       session;
31
32     @SuppressWarnings("unused")
33     private final boolean deleteAtDispose;
34
35     /**
36      * @param arguments
37      * @return
38      * @throws Exception
39      */
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.");
44
45         DatabaseHandle handle = new DatabaseHandle();
46         handle.init(args.get(SimanticsArguments.SERVER), false);
47         return handle;
48     }
49
50     /**
51      * @param arguments
52      * @return
53      * @throws Exception
54      */
55     public static DatabaseHandle open(String serverAddress) throws Exception {
56         return new DatabaseHandle().init(serverAddress, false);
57     }
58
59 //    /**
60 //     * @param arguments
61 //     * @return
62 //     * @throws Exception
63 //     */
64 //    public static DatabaseHandle open(ServerAddress address) throws Exception {
65 //        return DatabaseHandle.open(address.getDbid());
66 //    }
67
68     /**
69      * @param arguments
70      * @return
71      * @throws Exception
72      */
73     public static DatabaseHandle checkout(String serverAddress) throws Exception {
74         return new DatabaseHandle().init(serverAddress, true);
75     }
76
77 //    /**
78 //     * @param arguments
79 //     * @return
80 //     * @throws Exception
81 //     */
82 //    public static DatabaseHandle checkout(ServerAddress address) throws Exception {
83 //        return new DatabaseHandle().init(address.getDbid(), true);
84 //    }
85
86     private DatabaseHandle() {
87         deleteAtDispose = false;
88     }
89
90     /**
91      * @param arguments
92      * @param deleteAtDispose
93      * @throws Exception
94      */
95     public DatabaseHandle(String[] arguments, boolean deleteAtDispose) throws Exception {
96         this.deleteAtDispose = deleteAtDispose;
97
98         IArguments args = Arguments.parse(arguments,
99                 SimanticsArguments.SERVER,
100                 SimanticsArguments.CHECKOUT);
101
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);
105
106         init(args.get(SimanticsArguments.SERVER), checkout);
107     }
108
109     /**
110      * @param checkout
111      * @return this
112      * @throws Exception
113      * @throws IOException
114      */
115     private DatabaseHandle init(String serverAddress, boolean checkout) throws IOException, Exception {
116         if (checkout) {
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()));
123 //
124 //            ServerAddress localServer = activation.getDescriptor().getLocal().getAddress();
125 //            session = DatabaseUtils.connect(localServer);
126         } else {
127             session = DatabaseUtils.connect(serverAddress);
128 //            session = DatabaseUtils.connect(new ServerAddress("", 0, serverAddress));
129         }
130
131         return this;
132     }
133
134     @Override
135     protected void doDispose() {
136         // TODO: make this robust and safe
137
138         if (session != null) {
139             try {
140                 LifecycleSupport support = session.getService(LifecycleSupport.class);
141                 support.close(-1, false);
142                 session = null;
143             } catch (DatabaseException e) {
144             }
145         }
146
147 //        if (activation != null) {
148 //            try {
149 //                UndoCoreManager manager = UndoCoreManager.getManager();
150 //                manager.deactivate(activation, 3000, TimeUnit.MILLISECONDS);
151 //
152 //                if (deleteAtDispose) {
153 //                    IServerDescriptor desc = activation.getDescriptor();
154 //                    manager.deleteLocal(new ServerInfo[] { desc.getParent(), desc.getLocal() });
155 //                }
156 //            } catch (IOException e) {
157 //            } catch (InterruptedException e) {
158 //            }
159 //            activation = null;
160 //        }
161     }
162
163     /* (non-Javadoc)
164      * @see org.simantics.application.db.IDatabaseHandle#getServer()
165      */
166     @Override
167     public ServerI getServer() {
168         return server;
169     }
170
171     /* (non-Javadoc)
172      * @see org.simantics.application.db.IDatabaseHandle#getSession()
173      */
174     @Override
175     public Session getSession() {
176         return session;
177     }
178
179 }