1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package fi.vtt.simantics.procore.internal;
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;
35 final public class SessionImplDb extends SessionImplSocket {
37 private static final Logger LOGGER = LoggerFactory.getLogger(SessionImplDb.class);
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.
44 protected ServerInformationImpl serverInfo;
46 public SessionImplDb(SessionManager sessionManager, UserAuthenticationAgent authAgent) {
47 super(sessionManager, authAgent);
51 protected VirtualGraph getProvider(VirtualGraph vg) {
56 public ResourceImpl getNewResource() throws DatabaseException {
57 if (null != defaultClusterSet)
58 return getNewResource(defaultClusterSet);
59 ClusterI cluster = getNewResourceCluster();
62 newId = cluster.createResource(clusterTranslator);
63 } catch (DatabaseException e) {
64 LOGGER.error("createResource failed", e);
67 return new ResourceImpl(resourceSupport, newId);
70 public void connect(SessionReference sessionReference, final Database.Session dbSession) throws Exception {
71 if (null == clusterTable) {
72 File t = StaticSessionProperties.virtualGraphStoragePath;
75 clusterTable = new ClusterTable(this, t);
78 graphSession = new GraphSessionSocket(this, sessionReference, dbSession);
82 clusterStream = new ClusterStream(this, graphSession, false);
84 clusterTranslator = new ClusterTranslatorImpl(this);
85 serviceLocator.registerService(ClusterTranslator.class, clusterTranslator);
86 serviceLocator.registerService(ClusterSupport.class, clusterTranslator);
88 resourceSupport = new ResourceSupportImpl(this);
90 requestManager = new SessionRequestManager(this, state);
92 querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);
93 serviceLocator.registerService(QuerySupport.class, querySupport);
95 queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);
97 writeSupport = new WriteSupportImpl(this);
98 serviceLocator.registerService(WriteSupport.class, writeSupport);
100 state.setGraphSession(this, graphSession, queryProvider2, clusterTable);
102 this.serverInfo = graphSession.getServerInformation();
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);
110 } catch (InvalidAuthenticationException e) {
112 } catch (InvalidUserException e) {
114 // } catch (IOException e) {
115 // Logger.defaultLogError("I/O error. See exception for details.", e);
116 // graphSession = null;
117 // clusterTable.dispose(); clusterTable = null;
119 } catch (Throwable e) {
120 LOGGER.error("Unhandled error. See exception for details.", e);
122 clusterTable.dispose(); clusterTable = null;
123 throw new Exception(e);
126 String databaseId = serverInfo.getDatabaseId();
127 String serverId = serverInfo.getServerId();
128 String id = databaseId + "." + serverId;
129 virtualGraphServerSupport.connect(id);
130 clusterSetsSupport.connect(id);
135 protected ServerInformation getCachedServerInformation() {