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.exception.DatabaseException;
20 import org.simantics.db.exception.InvalidAuthenticationException;
21 import org.simantics.db.exception.InvalidUserException;
22 import org.simantics.db.impl.ResourceImpl;
23 import org.simantics.db.impl.VirtualGraphImpl;
24 import org.simantics.db.impl.query.QueryProcessor;
25 import org.simantics.db.service.ServerInformation;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import fi.vtt.simantics.procore.BackdoorAuthenticator;
30 import fi.vtt.simantics.procore.ProCoreServerReference;
31 import fi.vtt.simantics.procore.ProCoreSessionReference;
32 import fi.vtt.simantics.procore.SessionManagerSource;
34 public class SessionImplVirtual extends SessionImplSocket {
36 private static final Logger LOGGER = LoggerFactory.getLogger(SessionImplVirtual.class);
38 protected VirtualGraphImpl virtualGraphImpl;
39 public SessionImplVirtual(UserAuthenticationAgent authAgent)
40 throws DatabaseException {
41 super(null, authAgent);
42 init(authAgent, false);
44 public SessionImplVirtual(UserAuthenticationAgent authAgent, boolean init)
45 throws DatabaseException {
46 super(null, authAgent);
47 init(authAgent, init);
49 private void init(UserAuthenticationAgent authAgent, boolean init)
50 throws DatabaseException {
51 ProCoreServerReference serverReference = new ProCoreServerReference();
52 ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, SessionManagerSource.NullSessionId);
54 graphSession = new GraphSessionVirtualInit(this, sessionReference, virtualGraphServerSupport);
56 graphSession = new GraphSessionVirtual(this, sessionReference, virtualGraphServerSupport);
57 // This is used by QueryProcessor among others.
58 String databaseId = GraphSessionVirtual.serverInfo.databaseId;
59 String serverId = GraphSessionVirtual.serverInfo.serverId;
61 virtualGraphServerSupport.connect(databaseId + "." + serverId);
62 virtualGraphImpl = (VirtualGraphImpl)virtualGraphServerSupport.getWorkspacePersistent("virtualGraph" + "." + serverId);
63 requestManager = new SessionRequestManager(this, state);
64 clusterStream = new ClusterStream(this, graphSession, true);
65 clusterTranslator = new ClusterTranslatorImpl(this);
66 writeSupport = new WriteSupportImpl(this);
67 resourceSupport = new ResourceSupportImpl(this);
68 querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);
69 queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);
70 state.setGraphSession(this, graphSession, queryProvider2, clusterTable);
71 ServerInformationImpl serverInfo = graphSession.getServerInformation();
72 authenticator = authAgent.getAuthenticator(serverInfo);
73 if (authenticator == null)
74 throw new InvalidAuthenticationException("Authentication agent did not provide an authenticator");
75 if (authenticator instanceof BackdoorAuthenticator)
76 user = authenticator.getUser(this);
77 } catch (InvalidAuthenticationException e) {
79 } catch (InvalidUserException e) {
81 } catch (IOException e) {
82 LOGGER.error("I/O error. See exception for details.", e);
84 throw new DatabaseException(e);
85 } catch (Throwable e) {
86 LOGGER.error("Unhandled error. See exception for details.", e);
88 throw new DatabaseException(e);
90 this.clusterStream.setOff(true);
92 public static ServerI newVirtualProCoreServer() {
94 System.out.println("SessionImplVirtual.newVirtualProCoreServer");
95 return new VirtualServer();
98 protected VirtualGraph getProvider(VirtualGraph vg) {
99 return null != vg ? vg : virtualGraphImpl;
102 protected ResourceImpl getNewResource() throws DatabaseException {
103 int newId = virtualGraphImpl.newResource(false);
104 return new ResourceImpl(resourceSupport, newId);
107 protected ServerInformation getCachedServerInformation() {
108 GraphSession gs = graphSession;
112 return gs.getServerInformation();
113 } catch (DatabaseException e) {
114 LOGGER.error("Failed to get server info.", e);