]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/org/simantics/db/procore/ProCoreDriver.java
Working towards multiple readers.
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / ProCoreDriver.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.db.procore;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19 import java.util.Properties;
20
21 import org.simantics.db.Database;
22 import org.simantics.db.DatabaseUserAgent;
23 import org.simantics.db.Driver;
24 import org.simantics.db.Driver.Management;
25 import org.simantics.db.ServerI;
26 import org.simantics.db.Session;
27 import org.simantics.db.authentication.UserAuthenticationAgent;
28 import org.simantics.db.authentication.UserAuthenticator;
29 import org.simantics.db.common.auth.UserAuthenticationAgents;
30 import org.simantics.db.common.auth.UserAuthenticators;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.exception.InternalException;
33 import org.simantics.db.exception.SDBException;
34 import org.simantics.db.server.DatabaseManager;
35 import org.simantics.db.server.ProCoreException;
36
37 import fi.vtt.simantics.procore.BackdoorAuthenticator;
38 import fi.vtt.simantics.procore.ProCoreServerReference;
39 import fi.vtt.simantics.procore.ProCoreSessionReference;
40 import fi.vtt.simantics.procore.SessionManagerSource;
41
42 public class ProCoreDriver implements Driver {
43     private static long sessionId = SessionManagerSource.NullSessionId;
44     public static final String ProCoreDriverName = "procore";
45     public static final String ProCoreDriverNameVirtual = "virtual";
46
47     @Override
48     public final String getName() {
49         return ProCoreDriverName;
50     }
51     @Override
52     public DatabaseUserAgent getDatabaseUserAgent(String address) throws DatabaseException {
53         Path dbFolder = Paths.get(address);
54         return DatabaseManager.getDatabase(dbFolder).getUserAgent();
55     }
56     @Override
57     public void setDatabaseUserAgent(String address, DatabaseUserAgent dbUserAgent) throws DatabaseException {
58         Path dbFolder = Paths.get(address);
59         DatabaseManager.getDatabase(dbFolder).setUserAgent(dbUserAgent);
60     }
61     @Override
62     public Session getSession(String address, Properties properties) throws SDBException {
63         Path dbFolder = Paths.get(address);
64         if (!Files.isDirectory(dbFolder))
65             throw new ProCoreException("Database folder does not exist. folder=" + dbFolder);
66         Database db = DatabaseManager.getDatabase(dbFolder);
67         if (!db.isFolderOk())
68             throw new ProCoreException("Database folder is not ok. folder=" + dbFolder);
69         if (!db.isRunning())
70             db.start();
71         if (!db.isConnected())
72             db.connect();
73         Session session;
74         ProCoreServerReference serverReference = new ProCoreServerReference(dbFolder);
75         ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, ++sessionId);
76         try {
77             String user = properties.getProperty("user");
78             String password= properties.getProperty("password");
79             if (user == null)
80                 throw new ProCoreException("'user' property not provided");
81             if (password == null)
82                 throw new ProCoreException("'password' property not provided");
83
84             UserAuthenticator authenticator = UserAuthenticators.byNameAndPassword(user, password);
85             // FIXME: remove this hack once the server gets proper authentication
86             if (properties.getProperty("hyshys") != null)
87                 authenticator = new BackdoorAuthenticator();
88             UserAuthenticationAgent agent = UserAuthenticationAgents.staticAgent(authenticator);
89
90             session = SessionManagerSource.getSessionManager().createSession(sessionReference, agent);
91             if (!properties.containsKey("clientId"))
92                 properties.put("clientId", dbFolder.toFile().getAbsolutePath());
93             session.registerService(Properties.class, properties);
94             Session s = session.peekService(Session.class);
95             if (null == s)
96                 session.registerService(Session.class, session);
97             return session;
98
99         } catch (IOException e) {
100             throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);
101         } catch (org.simantics.db.exception.DatabaseException e) {
102             throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);
103         }
104     }
105
106     @Override
107     public ServerI getServer(String address, Properties notUsed) throws ProCoreException {
108         Path dbFolder = Paths.get(address);
109         return new ProCoreServer(dbFolder.toFile());
110     }
111     @Override
112     public Management getManagement(String address, Properties properties) throws DatabaseException {
113         Path dbFolder = Paths.get(address);
114         return new ProCoreManagement(dbFolder, properties);
115     }
116 }
117 class ProCoreServer implements ServerI {
118     private final Database db;
119     ProCoreServer(File dbFolder) throws ProCoreException {
120         db = DatabaseManager.getDatabase(dbFolder.toPath());
121     }
122     @Override
123     public String execute(String aCommand) throws InternalException {
124         return db.execute(aCommand);
125     }
126     @Override
127     public String executeAndDisconnect(String command) throws InternalException {
128         String t = "";
129         try {
130             t = execute(command);
131         } finally {
132             db.disconnect();
133         }
134         return t;
135     }
136     @Override
137     public boolean isActive() throws InternalException {
138         try {
139             db.execute("");
140             return true;
141         } catch (InternalException e) {
142             return false;
143         }
144     }
145     @Override
146     public synchronized void start() throws InternalException {
147         db.start();
148     }
149     @Override
150     public synchronized void stop() throws InternalException {
151         if (!db.tryToStop())
152             throw new InternalException("Could not stop database.");
153     }
154     @Override
155     public String getAddress() throws DatabaseException {
156         return db.getFolder().getAbsolutePath();
157     }
158 //    @Override
159 //    public synchronized IServerAddress getServerAddress() throws InternalException {
160 //        return new ServerAddress("127.0.0.1:0", db.getFolder().getAbsolutePath());
161 //    }
162 }
163 class ProCoreManagement implements Management {
164     private final Database db;
165     private final Properties properties;
166     ProCoreManagement(Path dbFolder, Properties properties) throws ProCoreException {
167         db = DatabaseManager.getDatabase(dbFolder);
168         this.properties = properties;
169     }
170     @Override
171     public boolean exist() throws DatabaseException {
172         return db.isFolderOk();
173     }
174     @Override
175     public void delete() throws DatabaseException {
176         db.deleteFiles();
177         if (exist())
178             throw new DatabaseException("Failed to delete database. folder=" + db.getFolder());
179     }
180     @Override
181     public void create() throws DatabaseException {
182         db.initFolder(properties);
183         if (!exist())
184             throw new DatabaseException("Failed to create ProCore database. folder=" + db.getFolder());
185     }
186     @Override
187     public void purge() throws DatabaseException {
188         db.purgeDatabase();
189     }
190     @Override
191     public void shutdown() throws DatabaseException {
192         db.tryToStop();
193         db.disconnect();
194     }
195 }