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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package fi.vtt.simantics.procore.internal;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\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
35 import fi.vtt.simantics.procore.BackdoorAuthenticator;
\r
37 final public class SessionImplDb extends SessionImplSocket {
\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
44 protected ServerInformationImpl serverInfo;
\r
46 public SessionImplDb(SessionManager sessionManager, UserAuthenticationAgent authAgent) {
\r
47 super(sessionManager, authAgent);
\r
51 protected VirtualGraph getProvider(VirtualGraph vg) {
\r
56 public ResourceImpl getNewResource() throws DatabaseException {
\r
57 if (null != defaultClusterSet)
\r
58 return getNewResource(defaultClusterSet);
\r
59 ClusterI cluster = getNewResourceCluster();
\r
62 newId = cluster.createResource(clusterTranslator);
\r
63 } catch (DatabaseException e) {
\r
64 Logger.defaultLogError(e);
\r
67 return new ResourceImpl(resourceSupport, newId);
\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
75 clusterTable = new ClusterTable(this, t);
\r
78 graphSession = new GraphSessionSocket(this, sessionReference, dbSession);
\r
82 clusterStream = new ClusterStream(this, graphSession, false);
\r
84 clusterTranslator = new ClusterTranslatorImpl(this);
\r
85 serviceLocator.registerService(ClusterTranslator.class, clusterTranslator);
\r
86 serviceLocator.registerService(ClusterSupport.class, clusterTranslator);
\r
88 resourceSupport = new ResourceSupportImpl(this);
\r
90 requestManager = new SessionRequestManager(this, state);
\r
92 querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);
\r
93 serviceLocator.registerService(QuerySupport.class, querySupport);
\r
95 queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);
\r
97 writeSupport = new WriteSupportImpl(this);
\r
98 serviceLocator.registerService(WriteSupport.class, writeSupport);
\r
100 state.setGraphSession(this, graphSession, queryProvider2, clusterTable);
\r
102 this.serverInfo = graphSession.getServerInformation();
\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
110 } catch (InvalidAuthenticationException e) {
\r
112 } catch (InvalidUserException 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
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
127 String databaseId = serverInfo.getDatabaseId();
\r
128 String serverId = serverInfo.getServerId();
\r
129 virtualGraphServerSupport.connect(databaseId + "." + serverId);
\r
130 clusterSetsSupport.connect(databaseId + "." + serverId);
\r
135 protected ServerInformation getCachedServerInformation() {
\r