]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionManagerImpl.java
Merge "Declare effects for constants and variables"
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / SessionManagerImpl.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 import java.nio.file.Path;
16 import java.util.concurrent.ConcurrentHashMap;
17
18 import org.simantics.db.Database;
19 import org.simantics.db.Session;
20 import org.simantics.db.SessionErrorHandler;
21 import org.simantics.db.SessionManager;
22 import org.simantics.db.SessionReference;
23 import org.simantics.db.authentication.UserAuthenticationAgent;
24 import org.simantics.db.common.utils.Logger;
25 import org.simantics.db.event.SessionEvent;
26 import org.simantics.db.event.SessionListener;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.server.DatabaseManager;
29 import org.simantics.db.service.LifecycleSupport;
30
31 import fi.vtt.simantics.procore.ProCoreSessionReference;
32
33 class SessionManagerImpl implements SessionManager {
34     private ConcurrentHashMap<SessionImplSocket, SessionImplSocket> sessionMap = new ConcurrentHashMap<SessionImplSocket, SessionImplSocket>();
35     private ListenerList<SessionListener> sessionListeners = new ListenerList<SessionListener>(SessionListener.class);
36     private SessionErrorHandler errorHandler;
37         private Database database;
38
39     SessionManagerImpl() throws IOException {
40     }
41
42     void finish() {
43         sessionMap = null;
44         sessionListeners = null;
45     }
46
47     @Override
48     public void addSessionListener(SessionListener listener) {
49         sessionListeners.add(listener);
50     }
51
52     @Override
53     public Session createSession(SessionReference sessionReference, UserAuthenticationAgent authAgent)
54     throws DatabaseException, IOException {
55         if (!(sessionReference instanceof ProCoreSessionReference))
56             throw new DatabaseException("Illegal argument. ProCoreSessionReference needed for creation of corresponding session. ref=" + sessionReference);
57         SessionImplDb sessionImpl = new SessionImplDb(this, authAgent);
58         boolean ok = false;
59         try {
60             ProCoreSessionReference pcsr = (ProCoreSessionReference)sessionReference;
61             Path dbFolder = pcsr.serverReference.dbFolder;
62             database = DatabaseManager.getDatabase(dbFolder);
63             Database.Session dbSession = database.newSession(sessionImpl);
64             sessionImpl.connect(sessionReference, dbSession);
65             sessionMap.put(sessionImpl, sessionImpl);
66             fireSessionOpened(sessionImpl);
67             ok = true;
68         } catch (IOException e) {
69             sessionImpl = null;
70             throw e;
71         } catch (Throwable e) {
72             Logger.defaultLogError("Connection failed. See exception for details.", e);
73             try {
74                 fireSessionClosed(sessionImpl, e);
75                 sessionMap.remove(sessionImpl);
76                 sessionImpl = null;
77             } catch (Throwable t) {
78             }
79             throw new DatabaseException(e);
80         } finally {
81             if (!ok && null != sessionImpl)
82                 sessionImpl.getService(LifecycleSupport.class).close();
83         }
84         return sessionImpl;
85     }
86
87     @Override
88     public void removeSessionListener(SessionListener listener) {
89         sessionListeners.remove(listener);
90     }
91
92     private void fireSessionOpened(SessionImplSocket session) {
93         SessionEvent se = new SessionEvent(session, null);
94         for (SessionListener listener : sessionListeners.getListeners()) {
95                listener.sessionOpened(se);
96         }
97     }
98
99     private void fireSessionClosed(SessionImplSocket session, Throwable cause) {
100         SessionEvent se = new SessionEvent(session, cause);
101         for (SessionListener listener : sessionListeners.getListeners()) {
102                listener.sessionClosed(se);
103         }
104     }
105
106     @Override
107     public void shutdown(Session s, Throwable cause) {
108         SessionImplSocket sis = sessionMap.get(s);
109         if (null == sis)
110             return;
111         try {
112             fireSessionClosed(sis, cause);
113         } finally {
114             sessionMap.remove(s);
115         }
116     }
117
118     @Override
119     public SessionErrorHandler getErrorHandler() {
120         return errorHandler;
121     }
122
123     @Override
124     public void setErrorHandler(SessionErrorHandler errorHandler) {
125         this.errorHandler = errorHandler;
126     }
127
128     @Override
129     public Database getDatabase() {
130         return database;
131     }
132
133 }