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