]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplVirtual.java
Declare asyncCount volatile since it is accessed from multiple threads
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SessionImplVirtual.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package fi.vtt.simantics.procore.internal;
13
14 import java.io.IOException;
15
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;
27
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;
32
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);
39     }
40     public SessionImplVirtual(UserAuthenticationAgent authAgent, boolean init)
41     throws DatabaseException {
42         super(null, authAgent);
43         init(authAgent, init);
44     }
45     private void init(UserAuthenticationAgent authAgent, boolean init)
46     throws DatabaseException {
47         ProCoreServerReference serverReference = new ProCoreServerReference();
48         ProCoreSessionReference sessionReference = new ProCoreSessionReference(serverReference, SessionManagerSource.NullSessionId);
49         if (init)
50             graphSession = new GraphSessionVirtualInit(this, sessionReference, virtualGraphServerSupport);
51         else
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;
56         try {
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) {
74             throw e;
75         } catch (InvalidUserException e) {
76             throw e;
77         } catch (IOException e) {
78                 Logger.defaultLogError("I/O error. See exception for details.", e);
79             graphSession = null;
80             throw new DatabaseException(e);
81         } catch (Throwable e) {
82             e.printStackTrace();
83             Logger.defaultLogError("Unhandled error. See exception for details.", e);
84             graphSession = null;
85             throw new DatabaseException(e);
86         }
87         this.clusterStream.setOff(true);
88     }
89     public static ServerI newVirtualProCoreServer() {
90         if (DEBUG)
91             System.out.println("SessionImplVirtual.newVirtualProCoreServer");
92         return new VirtualServer();
93     }
94     @Override
95     protected VirtualGraph getProvider(VirtualGraph vg) {
96         return null != vg ? vg : virtualGraphImpl;
97     }
98     @Override
99     protected ResourceImpl getNewResource() throws DatabaseException {
100         int newId = virtualGraphImpl.newResource(false);
101         return new ResourceImpl(resourceSupport, newId);
102     }
103     @Override
104     protected ServerInformation getCachedServerInformation() {
105         GraphSession gs = graphSession;
106         if (null == gs)
107             return null;
108         try {
109             return gs.getServerInformation();
110         } catch (DatabaseException e) {
111             Logger.defaultLogError("Failed to get server info.", e);
112             return null;
113         }
114     }
115 }