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