]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplDb.java
3c0f71eaf645847ad2fb7b50022995354ce2451b
[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 import java.io.IOException;\r
16 \r
17 import org.simantics.db.Database;\r
18 import org.simantics.db.SessionManager;\r
19 import org.simantics.db.SessionReference;\r
20 import org.simantics.db.VirtualGraph;\r
21 import org.simantics.db.authentication.UserAuthenticationAgent;\r
22 import org.simantics.db.common.utils.Logger;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.db.exception.InvalidAuthenticationException;\r
25 import org.simantics.db.exception.InvalidUserException;\r
26 import org.simantics.db.impl.ClusterI;\r
27 import org.simantics.db.impl.ClusterSupport;\r
28 import org.simantics.db.impl.ClusterTranslator;\r
29 import org.simantics.db.impl.ResourceImpl;\r
30 import org.simantics.db.impl.graph.WriteSupport;\r
31 import org.simantics.db.impl.query.QueryProcessor;\r
32 import org.simantics.db.impl.query.QuerySupport;\r
33 import org.simantics.db.service.ServerInformation;\r
34 \r
35 import fi.vtt.simantics.procore.BackdoorAuthenticator;\r
36 \r
37 final public class SessionImplDb extends SessionImplSocket {\r
38 \r
39     /**\r
40      * Cached ServerInformation structure fetched from the server at connection\r
41      * time. It should never change during a single session and therefore it\r
42      * should be perfectly safe to cache it.\r
43      */\r
44     protected ServerInformationImpl serverInfo;\r
45 \r
46     public SessionImplDb(SessionManager sessionManager, UserAuthenticationAgent authAgent) {\r
47         super(sessionManager, authAgent);\r
48     }\r
49 \r
50     @Override\r
51     protected VirtualGraph getProvider(VirtualGraph vg) {\r
52         return vg;\r
53     }\r
54 \r
55     @Override\r
56     public ResourceImpl getNewResource() throws DatabaseException {\r
57         if (null != defaultClusterSet)\r
58             return getNewResource(defaultClusterSet);\r
59         ClusterI cluster = getNewResourceCluster();\r
60         int newId;\r
61         try {\r
62             newId = cluster.createResource(clusterTranslator);\r
63         } catch (DatabaseException e) {\r
64             Logger.defaultLogError(e);\r
65             return null;\r
66         }\r
67         return new ResourceImpl(resourceSupport, newId);\r
68     }\r
69 \r
70     public void connect(SessionReference sessionReference, final Database.Session dbSession) throws Exception {\r
71         if (null == clusterTable) {\r
72             File t = StaticSessionProperties.virtualGraphStoragePath;\r
73             if (null == t)\r
74                 t = new File(".");\r
75             clusterTable = new ClusterTable(this, t);\r
76         }\r
77 \r
78         graphSession = new GraphSessionSocket(this, sessionReference, dbSession);\r
79 \r
80         try {\r
81 \r
82             clusterStream = new ClusterStream(this, graphSession, false);\r
83 \r
84             clusterTranslator = new ClusterTranslatorImpl(this);\r
85             serviceLocator.registerService(ClusterTranslator.class, clusterTranslator);\r
86             serviceLocator.registerService(ClusterSupport.class, clusterTranslator);\r
87 \r
88             resourceSupport = new ResourceSupportImpl(this);\r
89 \r
90             requestManager = new SessionRequestManager(this, state);\r
91 \r
92             querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);\r
93             serviceLocator.registerService(QuerySupport.class, querySupport);\r
94 \r
95             queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);\r
96 \r
97             writeSupport = new WriteSupportImpl(this);\r
98             serviceLocator.registerService(WriteSupport.class, writeSupport);\r
99 \r
100             state.setGraphSession(this, graphSession, queryProvider2, clusterTable);\r
101 \r
102             this.serverInfo = graphSession.getServerInformation();\r
103             \r
104 //            authenticator = authAgent.getAuthenticator(serverInfo);\r
105 //            if (authenticator == null)\r
106 //                throw new InvalidAuthenticationException("Authentication agent did not provide an authenticator");\r
107 //            if (authenticator instanceof BackdoorAuthenticator)\r
108 //                user = authenticator.getUser(this);\r
109 \r
110         } catch (InvalidAuthenticationException e) {\r
111             throw e;\r
112         } catch (InvalidUserException e) {\r
113             throw e;\r
114 //        } catch (IOException e) {\r
115 //            Logger.defaultLogError("I/O error. See exception for details.", e);\r
116 //            graphSession = null;\r
117 //            clusterTable.dispose(); clusterTable = null;\r
118 //            throw e;\r
119         } catch (Throwable e) {\r
120             e.printStackTrace();\r
121             Logger.defaultLogError("Unhandled error. See exception for details.", e);\r
122             graphSession = null;\r
123             clusterTable.dispose(); clusterTable = null;\r
124             throw new Exception(e);\r
125         }\r
126 \r
127         String databaseId = serverInfo.getDatabaseId();\r
128         String serverId = serverInfo.getServerId();\r
129         virtualGraphServerSupport.connect(databaseId + "." + serverId);\r
130         clusterSetsSupport.connect(databaseId + "." + serverId);\r
131 \r
132     }\r
133 \r
134     @Override\r
135     protected ServerInformation getCachedServerInformation() {\r
136         return serverInfo;\r
137     }\r
138 \r
139 }\r