]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/org/simantics/db/procore/ProCoreDriver.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.procore / src / org / simantics / db / procore / ProCoreDriver.java
index 832acb059140d8d25040c2d79144e5508093c902..3e626ac6422fe3f95620ae49434908e1bae6fd33 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.procore;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.nio.file.Files;\r
-import java.nio.file.Path;\r
-import java.nio.file.Paths;\r
-import java.util.Properties;\r
-\r
-import org.simantics.db.Database;\r
-import org.simantics.db.DatabaseUserAgent;\r
-import org.simantics.db.Driver;\r
-import org.simantics.db.Driver.Management;\r
-import org.simantics.db.ServerI;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.authentication.UserAuthenticationAgent;\r
-import org.simantics.db.authentication.UserAuthenticator;\r
-import org.simantics.db.common.auth.UserAuthenticationAgents;\r
-import org.simantics.db.common.auth.UserAuthenticators;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.InternalException;\r
-import org.simantics.db.exception.SDBException;\r
-import org.simantics.db.server.DatabaseManager;\r
-import org.simantics.db.server.ProCoreException;\r
-\r
-import fi.vtt.simantics.procore.BackdoorAuthenticator;\r
-import fi.vtt.simantics.procore.ProCoreServerReference;\r
-import fi.vtt.simantics.procore.ProCoreSessionReference;\r
-import fi.vtt.simantics.procore.SessionManagerSource;\r
-\r
-public class ProCoreDriver implements Driver {\r
-    private static long sessionId = SessionManagerSource.NullSessionId;\r
-    public static final String ProCoreDriverName = "procore";\r
-    public static final String ProCoreDriverNameVirtual = "virtual";\r
-\r
-    @Override\r
-    public final String getName() {\r
-        return ProCoreDriverName;\r
-    }\r
-    @Override\r
-    public DatabaseUserAgent getDatabaseUserAgent(String address) throws DatabaseException {\r
-        Path dbFolder = Paths.get(address);\r
-        return DatabaseManager.getDatabase(dbFolder).getUserAgent();\r
-    }\r
-    @Override\r
-    public void setDatabaseUserAgent(String address, DatabaseUserAgent dbUserAgent) throws DatabaseException {\r
-        Path dbFolder = Paths.get(address);\r
-        DatabaseManager.getDatabase(dbFolder).setUserAgent(dbUserAgent);\r
-    }\r
-    @Override\r
-    public Session getSession(String address, Properties properties) throws SDBException {\r
-        Path dbFolder = Paths.get(address);\r
-        if (!Files.isDirectory(dbFolder))\r
-            throw new ProCoreException("Database folder does not exist. folder=" + dbFolder);\r
-        Database db = DatabaseManager.getDatabase(dbFolder);\r
-        if (!db.isFolderOk())\r
-            throw new ProCoreException("Database folder is not ok. folder=" + dbFolder);\r
-        if (!db.isRunning())\r
-            db.start();\r
-        if (!db.isConnected())\r
-            db.connect();\r
-        Session session;\r
-        ProCoreServerReference serverReference = new ProCoreServerReference(dbFolder);\r
-        ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, ++sessionId);\r
-        try {\r
-            String user = properties.getProperty("user");\r
-            String password= properties.getProperty("password");\r
-            if (user == null)\r
-                throw new ProCoreException("'user' property not provided");\r
-            if (password == null)\r
-                throw new ProCoreException("'password' property not provided");\r
-\r
-            UserAuthenticator authenticator = UserAuthenticators.byNameAndPassword(user, password);\r
-            // FIXME: remove this hack once the server gets proper authentication\r
-            if (properties.getProperty("hyshys") != null)\r
-                authenticator = new BackdoorAuthenticator();\r
-            UserAuthenticationAgent agent = UserAuthenticationAgents.staticAgent(authenticator);\r
-\r
-            session = SessionManagerSource.getSessionManager().createSession(sessionReference, agent);\r
-            if (!properties.containsKey("clientId"))\r
-                properties.put("clientId", dbFolder.toFile().getAbsolutePath());\r
-            session.registerService(Properties.class, properties);\r
-            Session s = session.peekService(Session.class);\r
-            if (null == s)\r
-                session.registerService(Session.class, session);\r
-            return session;\r
-\r
-        } catch (IOException e) {\r
-            throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);\r
-        } catch (org.simantics.db.exception.DatabaseException e) {\r
-            throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public ServerI getServer(String address, Properties notUsed) throws ProCoreException {\r
-        Path dbFolder = Paths.get(address);\r
-        return new ProCoreServer(dbFolder.toFile());\r
-    }\r
-    @Override\r
-    public Management getManagement(String address, Properties properties) throws DatabaseException {\r
-        Path dbFolder = Paths.get(address);\r
-        return new ProCoreManagement(dbFolder, properties);\r
-    }\r
-}\r
-class ProCoreServer implements ServerI {\r
-    private final Database db;\r
-    ProCoreServer(File dbFolder) throws ProCoreException {\r
-        db = DatabaseManager.getDatabase(dbFolder.toPath());\r
-    }\r
-    @Override\r
-    public String execute(String aCommand) throws InternalException {\r
-        return db.execute(aCommand);\r
-    }\r
-    @Override\r
-    public String executeAndDisconnect(String command) throws InternalException {\r
-        String t = "";\r
-        try {\r
-            t = execute(command);\r
-        } finally {\r
-            db.disconnect();\r
-        }\r
-        return t;\r
-    }\r
-    @Override\r
-    public boolean isActive() throws InternalException {\r
-        try {\r
-            db.execute("");\r
-            return true;\r
-        } catch (InternalException e) {\r
-            return false;\r
-        }\r
-    }\r
-    @Override\r
-    public synchronized void start() throws InternalException {\r
-        db.start();\r
-    }\r
-    @Override\r
-    public synchronized void stop() throws InternalException {\r
-        if (!db.tryToStop())\r
-            throw new InternalException("Could not stop database.");\r
-    }\r
-    @Override\r
-    public String getAddress() throws DatabaseException {\r
-        return db.getFolder().getAbsolutePath();\r
-    }\r
-//    @Override\r
-//    public synchronized IServerAddress getServerAddress() throws InternalException {\r
-//        return new ServerAddress("127.0.0.1:0", db.getFolder().getAbsolutePath());\r
-//    }\r
-}\r
-class ProCoreManagement implements Management {\r
-    private final Database db;\r
-    private final Properties properties;\r
-    ProCoreManagement(Path dbFolder, Properties properties) throws ProCoreException {\r
-        db = DatabaseManager.getDatabase(dbFolder);\r
-        this.properties = properties;\r
-    }\r
-    @Override\r
-    public boolean exist() throws DatabaseException {\r
-        return db.isFolderOk();\r
-    }\r
-    @Override\r
-    public void delete() throws DatabaseException {\r
-        db.deleteFiles();\r
-        if (exist())\r
-            throw new DatabaseException("Failed to delete database. folder=" + db.getFolder());\r
-    }\r
-    @Override\r
-    public void create() throws DatabaseException {\r
-        db.initFolder(properties);\r
-        if (!exist())\r
-            throw new DatabaseException("Failed to create ProCore database. folder=" + db.getFolder());\r
-    }\r
-    @Override\r
-    public void purge() throws DatabaseException {\r
-        db.purgeDatabase();\r
-    }\r
-    @Override\r
-    public void shutdown() throws DatabaseException {\r
-        db.tryToStop();\r
-        db.disconnect();\r
-    }\r
-}\r
+/*******************************************************************************
+ * 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.db.procore;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Properties;
+
+import org.simantics.db.Database;
+import org.simantics.db.DatabaseUserAgent;
+import org.simantics.db.Driver;
+import org.simantics.db.Driver.Management;
+import org.simantics.db.ServerI;
+import org.simantics.db.Session;
+import org.simantics.db.authentication.UserAuthenticationAgent;
+import org.simantics.db.authentication.UserAuthenticator;
+import org.simantics.db.common.auth.UserAuthenticationAgents;
+import org.simantics.db.common.auth.UserAuthenticators;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.InternalException;
+import org.simantics.db.exception.SDBException;
+import org.simantics.db.server.DatabaseManager;
+import org.simantics.db.server.ProCoreException;
+
+import fi.vtt.simantics.procore.BackdoorAuthenticator;
+import fi.vtt.simantics.procore.ProCoreServerReference;
+import fi.vtt.simantics.procore.ProCoreSessionReference;
+import fi.vtt.simantics.procore.SessionManagerSource;
+
+public class ProCoreDriver implements Driver {
+    private static long sessionId = SessionManagerSource.NullSessionId;
+    public static final String ProCoreDriverName = "procore";
+    public static final String ProCoreDriverNameVirtual = "virtual";
+
+    @Override
+    public final String getName() {
+        return ProCoreDriverName;
+    }
+    @Override
+    public DatabaseUserAgent getDatabaseUserAgent(String address) throws DatabaseException {
+        Path dbFolder = Paths.get(address);
+        return DatabaseManager.getDatabase(dbFolder).getUserAgent();
+    }
+    @Override
+    public void setDatabaseUserAgent(String address, DatabaseUserAgent dbUserAgent) throws DatabaseException {
+        Path dbFolder = Paths.get(address);
+        DatabaseManager.getDatabase(dbFolder).setUserAgent(dbUserAgent);
+    }
+    @Override
+    public Session getSession(String address, Properties properties) throws SDBException {
+        Path dbFolder = Paths.get(address);
+        if (!Files.isDirectory(dbFolder))
+            throw new ProCoreException("Database folder does not exist. folder=" + dbFolder);
+        Database db = DatabaseManager.getDatabase(dbFolder);
+        if (!db.isFolderOk())
+            throw new ProCoreException("Database folder is not ok. folder=" + dbFolder);
+        if (!db.isRunning())
+            db.start();
+        if (!db.isConnected())
+            db.connect();
+        Session session;
+        ProCoreServerReference serverReference = new ProCoreServerReference(dbFolder);
+        ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, ++sessionId);
+        try {
+            String user = properties.getProperty("user");
+            String password= properties.getProperty("password");
+            if (user == null)
+                throw new ProCoreException("'user' property not provided");
+            if (password == null)
+                throw new ProCoreException("'password' property not provided");
+
+            UserAuthenticator authenticator = UserAuthenticators.byNameAndPassword(user, password);
+            // FIXME: remove this hack once the server gets proper authentication
+            if (properties.getProperty("hyshys") != null)
+                authenticator = new BackdoorAuthenticator();
+            UserAuthenticationAgent agent = UserAuthenticationAgents.staticAgent(authenticator);
+
+            session = SessionManagerSource.getSessionManager().createSession(sessionReference, agent);
+            if (!properties.containsKey("clientId"))
+                properties.put("clientId", dbFolder.toFile().getAbsolutePath());
+            session.registerService(Properties.class, properties);
+            Session s = session.peekService(Session.class);
+            if (null == s)
+                session.registerService(Session.class, session);
+            return session;
+
+        } catch (IOException e) {
+            throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);
+        } catch (org.simantics.db.exception.DatabaseException e) {
+            throw new ProCoreException("Connect failed. address=" + serverReference.toString(), e);
+        }
+    }
+
+    @Override
+    public ServerI getServer(String address, Properties notUsed) throws ProCoreException {
+        Path dbFolder = Paths.get(address);
+        return new ProCoreServer(dbFolder.toFile());
+    }
+    @Override
+    public Management getManagement(String address, Properties properties) throws DatabaseException {
+        Path dbFolder = Paths.get(address);
+        return new ProCoreManagement(dbFolder, properties);
+    }
+}
+class ProCoreServer implements ServerI {
+    private final Database db;
+    ProCoreServer(File dbFolder) throws ProCoreException {
+        db = DatabaseManager.getDatabase(dbFolder.toPath());
+    }
+    @Override
+    public String execute(String aCommand) throws InternalException {
+        return db.execute(aCommand);
+    }
+    @Override
+    public String executeAndDisconnect(String command) throws InternalException {
+        String t = "";
+        try {
+            t = execute(command);
+        } finally {
+            db.disconnect();
+        }
+        return t;
+    }
+    @Override
+    public boolean isActive() throws InternalException {
+        try {
+            db.execute("");
+            return true;
+        } catch (InternalException e) {
+            return false;
+        }
+    }
+    @Override
+    public synchronized void start() throws InternalException {
+        db.start();
+    }
+    @Override
+    public synchronized void stop() throws InternalException {
+        if (!db.tryToStop())
+            throw new InternalException("Could not stop database.");
+    }
+    @Override
+    public String getAddress() throws DatabaseException {
+        return db.getFolder().getAbsolutePath();
+    }
+//    @Override
+//    public synchronized IServerAddress getServerAddress() throws InternalException {
+//        return new ServerAddress("127.0.0.1:0", db.getFolder().getAbsolutePath());
+//    }
+}
+class ProCoreManagement implements Management {
+    private final Database db;
+    private final Properties properties;
+    ProCoreManagement(Path dbFolder, Properties properties) throws ProCoreException {
+        db = DatabaseManager.getDatabase(dbFolder);
+        this.properties = properties;
+    }
+    @Override
+    public boolean exist() throws DatabaseException {
+        return db.isFolderOk();
+    }
+    @Override
+    public void delete() throws DatabaseException {
+        db.deleteFiles();
+        if (exist())
+            throw new DatabaseException("Failed to delete database. folder=" + db.getFolder());
+    }
+    @Override
+    public void create() throws DatabaseException {
+        db.initFolder(properties);
+        if (!exist())
+            throw new DatabaseException("Failed to create ProCore database. folder=" + db.getFolder());
+    }
+    @Override
+    public void purge() throws DatabaseException {
+        db.purgeDatabase();
+    }
+    @Override
+    public void shutdown() throws DatabaseException {
+        db.tryToStop();
+        db.disconnect();
+    }
+}