]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplDb.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SessionImplDb.java
diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplDb.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplDb.java
new file mode 100644 (file)
index 0000000..3c0f71e
--- /dev/null
@@ -0,0 +1,139 @@
+/*******************************************************************************\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 fi.vtt.simantics.procore.internal;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+\r
+import org.simantics.db.Database;\r
+import org.simantics.db.SessionManager;\r
+import org.simantics.db.SessionReference;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.authentication.UserAuthenticationAgent;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.InvalidAuthenticationException;\r
+import org.simantics.db.exception.InvalidUserException;\r
+import org.simantics.db.impl.ClusterI;\r
+import org.simantics.db.impl.ClusterSupport;\r
+import org.simantics.db.impl.ClusterTranslator;\r
+import org.simantics.db.impl.ResourceImpl;\r
+import org.simantics.db.impl.graph.WriteSupport;\r
+import org.simantics.db.impl.query.QueryProcessor;\r
+import org.simantics.db.impl.query.QuerySupport;\r
+import org.simantics.db.service.ServerInformation;\r
+\r
+import fi.vtt.simantics.procore.BackdoorAuthenticator;\r
+\r
+final public class SessionImplDb extends SessionImplSocket {\r
+\r
+    /**\r
+     * Cached ServerInformation structure fetched from the server at connection\r
+     * time. It should never change during a single session and therefore it\r
+     * should be perfectly safe to cache it.\r
+     */\r
+    protected ServerInformationImpl serverInfo;\r
+\r
+    public SessionImplDb(SessionManager sessionManager, UserAuthenticationAgent authAgent) {\r
+        super(sessionManager, authAgent);\r
+    }\r
+\r
+    @Override\r
+    protected VirtualGraph getProvider(VirtualGraph vg) {\r
+        return vg;\r
+    }\r
+\r
+    @Override\r
+    public ResourceImpl getNewResource() throws DatabaseException {\r
+        if (null != defaultClusterSet)\r
+            return getNewResource(defaultClusterSet);\r
+        ClusterI cluster = getNewResourceCluster();\r
+        int newId;\r
+        try {\r
+            newId = cluster.createResource(clusterTranslator);\r
+        } catch (DatabaseException e) {\r
+            Logger.defaultLogError(e);\r
+            return null;\r
+        }\r
+        return new ResourceImpl(resourceSupport, newId);\r
+    }\r
+\r
+    public void connect(SessionReference sessionReference, final Database.Session dbSession) throws Exception {\r
+        if (null == clusterTable) {\r
+            File t = StaticSessionProperties.virtualGraphStoragePath;\r
+            if (null == t)\r
+                t = new File(".");\r
+            clusterTable = new ClusterTable(this, t);\r
+        }\r
+\r
+        graphSession = new GraphSessionSocket(this, sessionReference, dbSession);\r
+\r
+        try {\r
+\r
+            clusterStream = new ClusterStream(this, graphSession, false);\r
+\r
+            clusterTranslator = new ClusterTranslatorImpl(this);\r
+            serviceLocator.registerService(ClusterTranslator.class, clusterTranslator);\r
+            serviceLocator.registerService(ClusterSupport.class, clusterTranslator);\r
+\r
+            resourceSupport = new ResourceSupportImpl(this);\r
+\r
+            requestManager = new SessionRequestManager(this, state);\r
+\r
+            querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);\r
+            serviceLocator.registerService(QuerySupport.class, querySupport);\r
+\r
+            queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);\r
+\r
+            writeSupport = new WriteSupportImpl(this);\r
+            serviceLocator.registerService(WriteSupport.class, writeSupport);\r
+\r
+            state.setGraphSession(this, graphSession, queryProvider2, clusterTable);\r
+\r
+            this.serverInfo = graphSession.getServerInformation();\r
+            \r
+//            authenticator = authAgent.getAuthenticator(serverInfo);\r
+//            if (authenticator == null)\r
+//                throw new InvalidAuthenticationException("Authentication agent did not provide an authenticator");\r
+//            if (authenticator instanceof BackdoorAuthenticator)\r
+//                user = authenticator.getUser(this);\r
+\r
+        } catch (InvalidAuthenticationException e) {\r
+            throw e;\r
+        } catch (InvalidUserException e) {\r
+            throw e;\r
+//        } catch (IOException e) {\r
+//            Logger.defaultLogError("I/O error. See exception for details.", e);\r
+//            graphSession = null;\r
+//            clusterTable.dispose(); clusterTable = null;\r
+//            throw e;\r
+        } catch (Throwable e) {\r
+            e.printStackTrace();\r
+            Logger.defaultLogError("Unhandled error. See exception for details.", e);\r
+            graphSession = null;\r
+            clusterTable.dispose(); clusterTable = null;\r
+            throw new Exception(e);\r
+        }\r
+\r
+        String databaseId = serverInfo.getDatabaseId();\r
+        String serverId = serverInfo.getServerId();\r
+        virtualGraphServerSupport.connect(databaseId + "." + serverId);\r
+        clusterSetsSupport.connect(databaseId + "." + serverId);\r
+\r
+    }\r
+\r
+    @Override\r
+    protected ServerInformation getCachedServerInformation() {\r
+        return serverInfo;\r
+    }\r
+\r
+}\r