]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplVirtual.java
Merge commit '55ca14c'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SessionImplVirtual.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package fi.vtt.simantics.procore.internal;\r
13 \r
14 import java.io.IOException;\r
15 \r
16 import org.simantics.db.ServerI;\r
17 import org.simantics.db.VirtualGraph;\r
18 import org.simantics.db.authentication.UserAuthenticationAgent;\r
19 import org.simantics.db.common.utils.Logger;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.db.exception.InvalidAuthenticationException;\r
22 import org.simantics.db.exception.InvalidUserException;\r
23 import org.simantics.db.impl.ResourceImpl;\r
24 import org.simantics.db.impl.VirtualGraphImpl;\r
25 import org.simantics.db.impl.query.QueryProcessor;\r
26 import org.simantics.db.service.ServerInformation;\r
27 \r
28 import fi.vtt.simantics.procore.BackdoorAuthenticator;\r
29 import fi.vtt.simantics.procore.ProCoreServerReference;\r
30 import fi.vtt.simantics.procore.ProCoreSessionReference;\r
31 import fi.vtt.simantics.procore.SessionManagerSource;\r
32 \r
33 public class SessionImplVirtual extends SessionImplSocket {\r
34     protected VirtualGraphImpl virtualGraphImpl;\r
35     public SessionImplVirtual(UserAuthenticationAgent authAgent)\r
36     throws DatabaseException {\r
37         super(null, authAgent);\r
38         init(authAgent, false);\r
39     }\r
40     public SessionImplVirtual(UserAuthenticationAgent authAgent, boolean init)\r
41     throws DatabaseException {\r
42         super(null, authAgent);\r
43         init(authAgent, init);\r
44     }\r
45     private void init(UserAuthenticationAgent authAgent, boolean init)\r
46     throws DatabaseException {\r
47         ProCoreServerReference serverReference = new ProCoreServerReference();\r
48         ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, SessionManagerSource.NullSessionId);\r
49         if (init)\r
50             graphSession = new GraphSessionVirtualInit(this, sessionReference, virtualGraphServerSupport);\r
51         else\r
52             graphSession = new GraphSessionVirtual(this, sessionReference, virtualGraphServerSupport);\r
53         // This is used by QueryProcessor among others.\r
54         String databaseId = GraphSessionVirtual.serverInfo.databaseId;\r
55         String serverId = GraphSessionVirtual.serverInfo.serverId;\r
56         try {\r
57             virtualGraphServerSupport.connect(databaseId + "." + serverId);\r
58             virtualGraphImpl = (VirtualGraphImpl)virtualGraphServerSupport.getWorkspacePersistent("virtualGraph" + "." + serverId);\r
59             requestManager = new SessionRequestManager(this, state);\r
60             clusterStream = new ClusterStream(this, graphSession, true);\r
61             clusterTranslator = new ClusterTranslatorImpl(this);\r
62             writeSupport = new WriteSupportImpl(this);\r
63             resourceSupport = new ResourceSupportImpl(this);\r
64             querySupport = new QuerySupportImpl(this, clusterTranslator, new SerialisationSupportImpl(this), requestManager);\r
65             queryProvider2 = new QueryProcessor(getAmountOfQueryThreads(), querySupport, sessionThreads);\r
66             state.setGraphSession(this, graphSession, queryProvider2, clusterTable);\r
67             ServerInformationImpl serverInfo = graphSession.getServerInformation();\r
68             authenticator = authAgent.getAuthenticator(serverInfo);\r
69             if (authenticator == null)\r
70                 throw new InvalidAuthenticationException("Authentication agent did not provide an authenticator");\r
71             if (authenticator instanceof BackdoorAuthenticator)\r
72                 user = authenticator.getUser(this);\r
73         } catch (InvalidAuthenticationException e) {\r
74             throw e;\r
75         } catch (InvalidUserException e) {\r
76             throw e;\r
77         } catch (IOException e) {\r
78                 Logger.defaultLogError("I/O error. See exception for details.", e);\r
79             graphSession = null;\r
80             throw new DatabaseException(e);\r
81         } catch (Throwable e) {\r
82             e.printStackTrace();\r
83             Logger.defaultLogError("Unhandled error. See exception for details.", e);\r
84             graphSession = null;\r
85             throw new DatabaseException(e);\r
86         }\r
87         this.clusterStream.setOff(true);\r
88     }\r
89     public static ServerI newVirtualProCoreServer() {\r
90         if (DEBUG)\r
91             System.out.println("SessionImplVirtual.newVirtualProCoreServer");\r
92         return new VirtualServer();\r
93     }\r
94     @Override\r
95     protected VirtualGraph getProvider(VirtualGraph vg) {\r
96         return null != vg ? vg : virtualGraphImpl;\r
97     }\r
98     @Override\r
99     protected ResourceImpl getNewResource() throws DatabaseException {\r
100         int newId = virtualGraphImpl.newResource(false);\r
101         return new ResourceImpl(resourceSupport, newId);\r
102     }\r
103     @Override\r
104     protected ServerInformation getCachedServerInformation() {\r
105         GraphSession gs = graphSession;\r
106         if (null == gs)\r
107             return null;\r
108         try {\r
109             return gs.getServerInformation();\r
110         } catch (DatabaseException e) {\r
111             Logger.defaultLogError("Failed to get server info.", e);\r
112             return null;\r
113         }\r
114     }\r
115 }\r