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;
15 import java.nio.file.Path;
16 import java.util.concurrent.ConcurrentHashMap;
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;
31 import fi.vtt.simantics.procore.ProCoreSessionReference;
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;
39 SessionManagerImpl() throws IOException {
44 sessionListeners = null;
48 public void addSessionListener(SessionListener listener) {
49 sessionListeners.add(listener);
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);
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);
68 } catch (IOException e) {
71 } catch (Throwable e) {
72 Logger.defaultLogError("Connection failed. See exception for details.", e);
74 fireSessionClosed(sessionImpl, e);
75 sessionMap.remove(sessionImpl);
77 } catch (Throwable t) {
79 throw new DatabaseException(e);
81 if (!ok && null != sessionImpl)
82 sessionImpl.getService(LifecycleSupport.class).close();
88 public void removeSessionListener(SessionListener listener) {
89 sessionListeners.remove(listener);
92 private void fireSessionOpened(SessionImplSocket session) {
93 SessionEvent se = new SessionEvent(session, null);
94 for (SessionListener listener : sessionListeners.getListeners()) {
95 listener.sessionOpened(se);
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);
107 public void shutdown(Session s, Throwable cause) {
108 SessionImplSocket sis = sessionMap.get(s);
112 fireSessionClosed(sis, cause);
114 sessionMap.remove(s);
119 public SessionErrorHandler getErrorHandler() {
124 public void setErrorHandler(SessionErrorHandler errorHandler) {
125 this.errorHandler = errorHandler;
129 public Database getDatabase() {