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;
14 import java.io.IOException;
16 import org.simantics.db.ServerI;
17 import org.simantics.db.VirtualGraph;
18 import org.simantics.db.authentication.UserAuthenticationAgent;
19 import org.simantics.db.common.utils.Logger;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.exception.InvalidAuthenticationException;
22 import org.simantics.db.exception.InvalidUserException;
23 import org.simantics.db.impl.ResourceImpl;
24 import org.simantics.db.impl.VirtualGraphImpl;
25 import org.simantics.db.impl.query.QueryProcessor;
26 import org.simantics.db.service.ServerInformation;
28 import fi.vtt.simantics.procore.BackdoorAuthenticator;
29 import fi.vtt.simantics.procore.ProCoreServerReference;
30 import fi.vtt.simantics.procore.ProCoreSessionReference;
31 import fi.vtt.simantics.procore.SessionManagerSource;
33 public class SessionImplVirtual extends SessionImplSocket {
34 protected VirtualGraphImpl virtualGraphImpl;
35 public SessionImplVirtual(UserAuthenticationAgent authAgent)
36 throws DatabaseException {
37 super(null, authAgent);
38 init(authAgent, false);
40 public SessionImplVirtual(UserAuthenticationAgent authAgent, boolean init)
41 throws DatabaseException {
42 super(null, authAgent);
43 init(authAgent, init);
45 private void init(UserAuthenticationAgent authAgent, boolean init)
46 throws DatabaseException {
47 ProCoreServerReference serverReference = new ProCoreServerReference();
48 ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, SessionManagerSource.NullSessionId);
50 graphSession = new GraphSessionVirtualInit(this, sessionReference, virtualGraphServerSupport);
52 graphSession = new GraphSessionVirtual(this, sessionReference, virtualGraphServerSupport);
53 // This is used by QueryProcessor among others.
54 String databaseId = GraphSessionVirtual.serverInfo.databaseId;
55 String serverId = GraphSessionVirtual.serverInfo.serverId;
57 virtualGraphServerSupport.connect(databaseId + "." + serverId);
58 virtualGraphImpl = (VirtualGraphImpl)virtualGraphServerSupport.getWorkspacePersistent("virtualGraph" + "." + serverId);
59 requestManager = new SessionRequestManager(this, state);
60 clusterStream = new ClusterStream(this, graphSession, true);
61 clusterTranslator = new ClusterTranslatorImpl(this);
62 writeSupport = new WriteSupportImpl(this);
63 resourceSupport = new ResourceSupportImpl(this);
64 querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);
65 queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);
66 state.setGraphSession(this, graphSession, queryProvider2, clusterTable);
67 ServerInformationImpl serverInfo = graphSession.getServerInformation();
68 authenticator = authAgent.getAuthenticator(serverInfo);
69 if (authenticator == null)
70 throw new InvalidAuthenticationException("Authentication agent did not provide an authenticator");
71 if (authenticator instanceof BackdoorAuthenticator)
72 user = authenticator.getUser(this);
73 } catch (InvalidAuthenticationException e) {
75 } catch (InvalidUserException e) {
77 } catch (IOException e) {
78 Logger.defaultLogError("I/O error. See exception for details.", e);
80 throw new DatabaseException(e);
81 } catch (Throwable e) {
83 Logger.defaultLogError("Unhandled error. See exception for details.", e);
85 throw new DatabaseException(e);
87 this.clusterStream.setOff(true);
89 public static ServerI newVirtualProCoreServer() {
91 System.out.println("SessionImplVirtual.newVirtualProCoreServer");
92 return new VirtualServer();
95 protected VirtualGraph getProvider(VirtualGraph vg) {
96 return null != vg ? vg : virtualGraphImpl;
99 protected ResourceImpl getNewResource() throws DatabaseException {
100 int newId = virtualGraphImpl.newResource(false);
101 return new ResourceImpl(resourceSupport, newId);
104 protected ServerInformation getCachedServerInformation() {
105 GraphSession gs = graphSession;
109 return gs.getServerInformation();
110 } catch (DatabaseException e) {
111 Logger.defaultLogError("Failed to get server info.", e);