]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplDb.java
Merge commit 'd7afa23'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SessionImplDb.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package fi.vtt.simantics.procore.internal;\r
13 \r
14 import java.io.File;\r
15 \r
16 import org.simantics.db.Database;\r
17 import org.simantics.db.SessionManager;\r
18 import org.simantics.db.SessionReference;\r
19 import org.simantics.db.VirtualGraph;\r
20 import org.simantics.db.authentication.UserAuthenticationAgent;\r
21 import org.simantics.db.common.utils.Logger;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.db.exception.InvalidAuthenticationException;\r
24 import org.simantics.db.exception.InvalidUserException;\r
25 import org.simantics.db.impl.ClusterI;\r
26 import org.simantics.db.impl.ClusterSupport;\r
27 import org.simantics.db.impl.ClusterTranslator;\r
28 import org.simantics.db.impl.ResourceImpl;\r
29 import org.simantics.db.impl.graph.WriteSupport;\r
30 import org.simantics.db.impl.query.QueryProcessor;\r
31 import org.simantics.db.impl.query.QuerySupport;\r
32 import org.simantics.db.service.ServerInformation;\r
33 \r
34 final public class SessionImplDb extends SessionImplSocket {\r
35 \r
36     /**\r
37      * Cached ServerInformation structure fetched from the server at connection\r
38      * time. It should never change during a single session and therefore it\r
39      * should be perfectly safe to cache it.\r
40      */\r
41     protected ServerInformationImpl serverInfo;\r
42 \r
43     public SessionImplDb(SessionManager sessionManager, UserAuthenticationAgent authAgent) {\r
44         super(sessionManager, authAgent);\r
45     }\r
46 \r
47     @Override\r
48     protected VirtualGraph getProvider(VirtualGraph vg) {\r
49         return vg;\r
50     }\r
51 \r
52     @Override\r
53     public ResourceImpl getNewResource() throws DatabaseException {\r
54         if (null != defaultClusterSet)\r
55             return getNewResource(defaultClusterSet);\r
56         ClusterI cluster = getNewResourceCluster();\r
57         int newId;\r
58         try {\r
59             newId = cluster.createResource(clusterTranslator);\r
60         } catch (DatabaseException e) {\r
61             Logger.defaultLogError(e);\r
62             return null;\r
63         }\r
64         return new ResourceImpl(resourceSupport, newId);\r
65     }\r
66 \r
67     public void connect(SessionReference sessionReference, final Database.Session dbSession) throws Exception {\r
68         if (null == clusterTable) {\r
69             File t = StaticSessionProperties.virtualGraphStoragePath;\r
70             if (null == t)\r
71                 t = new File(".");\r
72             clusterTable = new ClusterTable(this, t);\r
73         }\r
74 \r
75         graphSession = new GraphSessionSocket(this, sessionReference, dbSession);\r
76 \r
77         try {\r
78 \r
79             clusterStream = new ClusterStream(this, graphSession, false);\r
80 \r
81             clusterTranslator = new ClusterTranslatorImpl(this);\r
82             serviceLocator.registerService(ClusterTranslator.class, clusterTranslator);\r
83             serviceLocator.registerService(ClusterSupport.class, clusterTranslator);\r
84 \r
85             resourceSupport = new ResourceSupportImpl(this);\r
86 \r
87             requestManager = new SessionRequestManager(this, state);\r
88 \r
89             querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);\r
90             serviceLocator.registerService(QuerySupport.class, querySupport);\r
91 \r
92             queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);\r
93 \r
94             writeSupport = new WriteSupportImpl(this);\r
95             serviceLocator.registerService(WriteSupport.class, writeSupport);\r
96 \r
97             state.setGraphSession(this, graphSession, queryProvider2, clusterTable);\r
98 \r
99             this.serverInfo = graphSession.getServerInformation();\r
100             \r
101 //            authenticator = authAgent.getAuthenticator(serverInfo);\r
102 //            if (authenticator == null)\r
103 //                throw new InvalidAuthenticationException("Authentication agent did not provide an authenticator");\r
104 //            if (authenticator instanceof BackdoorAuthenticator)\r
105 //                user = authenticator.getUser(this);\r
106 \r
107         } catch (InvalidAuthenticationException e) {\r
108             throw e;\r
109         } catch (InvalidUserException e) {\r
110             throw e;\r
111 //        } catch (IOException e) {\r
112 //            Logger.defaultLogError("I/O error. See exception for details.", e);\r
113 //            graphSession = null;\r
114 //            clusterTable.dispose(); clusterTable = null;\r
115 //            throw e;\r
116         } catch (Throwable e) {\r
117             e.printStackTrace();\r
118             Logger.defaultLogError("Unhandled error. See exception for details.", e);\r
119             graphSession = null;\r
120             clusterTable.dispose(); clusterTable = null;\r
121             throw new Exception(e);\r
122         }\r
123 \r
124         String databaseId = serverInfo.getDatabaseId();\r
125         String serverId = serverInfo.getServerId();\r
126         virtualGraphServerSupport.connect(databaseId + "." + serverId);\r
127         clusterSetsSupport.connect(databaseId + "." + serverId);\r
128 \r
129     }\r
130 \r
131     @Override\r
132     protected ServerInformation getCachedServerInformation() {\r
133         return serverInfo;\r
134     }\r
135 \r
136 }\r