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
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
34 final public class SessionImplDb extends SessionImplSocket {
\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
41 protected ServerInformationImpl serverInfo;
\r
43 public SessionImplDb(SessionManager sessionManager, UserAuthenticationAgent authAgent) {
\r
44 super(sessionManager, authAgent);
\r
48 protected VirtualGraph getProvider(VirtualGraph vg) {
\r
53 public ResourceImpl getNewResource() throws DatabaseException {
\r
54 if (null != defaultClusterSet)
\r
55 return getNewResource(defaultClusterSet);
\r
56 ClusterI cluster = getNewResourceCluster();
\r
59 newId = cluster.createResource(clusterTranslator);
\r
60 } catch (DatabaseException e) {
\r
61 Logger.defaultLogError(e);
\r
64 return new ResourceImpl(resourceSupport, newId);
\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
72 clusterTable = new ClusterTable(this, t);
\r
75 graphSession = new GraphSessionSocket(this, sessionReference, dbSession);
\r
79 clusterStream = new ClusterStream(this, graphSession, false);
\r
81 clusterTranslator = new ClusterTranslatorImpl(this);
\r
82 serviceLocator.registerService(ClusterTranslator.class, clusterTranslator);
\r
83 serviceLocator.registerService(ClusterSupport.class, clusterTranslator);
\r
85 resourceSupport = new ResourceSupportImpl(this);
\r
87 requestManager = new SessionRequestManager(this, state);
\r
89 querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);
\r
90 serviceLocator.registerService(QuerySupport.class, querySupport);
\r
92 queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);
\r
94 writeSupport = new WriteSupportImpl(this);
\r
95 serviceLocator.registerService(WriteSupport.class, writeSupport);
\r
97 state.setGraphSession(this, graphSession, queryProvider2, clusterTable);
\r
99 this.serverInfo = graphSession.getServerInformation();
\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
107 } catch (InvalidAuthenticationException e) {
\r
109 } catch (InvalidUserException 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
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
124 String databaseId = serverInfo.getDatabaseId();
\r
125 String serverId = serverInfo.getServerId();
\r
126 virtualGraphServerSupport.connect(databaseId + "." + serverId);
\r
127 clusterSetsSupport.connect(databaseId + "." + serverId);
\r
132 protected ServerInformation getCachedServerInformation() {
\r