--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package fi.vtt.simantics.procore.internal;\r
+\r
+import java.io.IOException;\r
+\r
+import org.simantics.db.ServerI;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.authentication.UserAuthenticationAgent;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.InvalidAuthenticationException;\r
+import org.simantics.db.exception.InvalidUserException;\r
+import org.simantics.db.impl.ResourceImpl;\r
+import org.simantics.db.impl.VirtualGraphImpl;\r
+import org.simantics.db.impl.query.QueryProcessor;\r
+import org.simantics.db.service.ServerInformation;\r
+\r
+import fi.vtt.simantics.procore.BackdoorAuthenticator;\r
+import fi.vtt.simantics.procore.ProCoreServerReference;\r
+import fi.vtt.simantics.procore.ProCoreSessionReference;\r
+import fi.vtt.simantics.procore.SessionManagerSource;\r
+\r
+public class SessionImplVirtual extends SessionImplSocket {\r
+ protected VirtualGraphImpl virtualGraphImpl;\r
+ public SessionImplVirtual(UserAuthenticationAgent authAgent)\r
+ throws DatabaseException {\r
+ super(null, authAgent);\r
+ init(authAgent, false);\r
+ }\r
+ public SessionImplVirtual(UserAuthenticationAgent authAgent, boolean init)\r
+ throws DatabaseException {\r
+ super(null, authAgent);\r
+ init(authAgent, init);\r
+ }\r
+ private void init(UserAuthenticationAgent authAgent, boolean init)\r
+ throws DatabaseException {\r
+ ProCoreServerReference serverReference = new ProCoreServerReference();\r
+ ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, SessionManagerSource.NullSessionId);\r
+ if (init)\r
+ graphSession = new GraphSessionVirtualInit(this, sessionReference, virtualGraphServerSupport);\r
+ else\r
+ graphSession = new GraphSessionVirtual(this, sessionReference, virtualGraphServerSupport);\r
+ // This is used by QueryProcessor among others.\r
+ String databaseId = GraphSessionVirtual.serverInfo.databaseId;\r
+ String serverId = GraphSessionVirtual.serverInfo.serverId;\r
+ virtualGraphServerSupport.connect(databaseId + "." + serverId);\r
+ virtualGraphImpl = (VirtualGraphImpl)virtualGraphServerSupport.getWorkspacePersistent("virtualGraph" + "." + serverId);\r
+ try {\r
+ requestManager = new SessionRequestManager(this, state);\r
+ clusterStream = new ClusterStream(this, graphSession, true);\r
+ clusterTranslator = new ClusterTranslatorImpl(this);\r
+ writeSupport = new WriteSupportImpl(this);\r
+ resourceSupport = new ResourceSupportImpl(this);\r
+ querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);\r
+ queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);\r
+ state.setGraphSession(this, graphSession, queryProvider2, clusterTable);\r
+ ServerInformationImpl serverInfo = graphSession.getServerInformation();\r
+ authenticator = authAgent.getAuthenticator(serverInfo);\r
+ if (authenticator == null)\r
+ throw new InvalidAuthenticationException("Authentication agent did not provide an authenticator");\r
+ if (authenticator instanceof BackdoorAuthenticator)\r
+ user = authenticator.getUser(this);\r
+ } catch (InvalidAuthenticationException e) {\r
+ throw e;\r
+ } catch (InvalidUserException e) {\r
+ throw e;\r
+ } catch (IOException e) {\r
+ Logger.defaultLogError("I/O error. See exception for details.", e);\r
+ graphSession = null;\r
+ throw new DatabaseException(e);\r
+ } catch (Throwable e) {\r
+ e.printStackTrace();\r
+ Logger.defaultLogError("Unhandled error. See exception for details.", e);\r
+ graphSession = null;\r
+ throw new DatabaseException(e);\r
+ }\r
+ this.clusterStream.setOff(true);\r
+ }\r
+ public static ServerI newVirtualProCoreServer() {\r
+ if (DEBUG)\r
+ System.out.println("SessionImplVirtual.newVirtualProCoreServer");\r
+ return new VirtualServer();\r
+ }\r
+ @Override\r
+ protected VirtualGraph getProvider(VirtualGraph vg) {\r
+ return null != vg ? vg : virtualGraphImpl;\r
+ }\r
+ @Override\r
+ protected ResourceImpl getNewResource() throws DatabaseException {\r
+ int newId = virtualGraphImpl.newResource(false);\r
+ return new ResourceImpl(resourceSupport, newId);\r
+ }\r
+ @Override\r
+ protected ServerInformation getCachedServerInformation() {\r
+ GraphSession gs = graphSession;\r
+ if (null == gs)\r
+ return null;\r
+ try {\r
+ return gs.getServerInformation();\r
+ } catch (DatabaseException e) {\r
+ Logger.defaultLogError("Failed to get server info.", e);\r
+ return null;\r
+ }\r
+ }\r
+}\r