Skip to main content
summaryrefslogtreecommitdiffstats
blob: 46b5e34a5479d01a0f19e43c112c8ada9e845ac0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.core.server.internal.session;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import org.eclipse.osee.cache.admin.Cache;
import org.eclipse.osee.framework.core.data.IUserToken;
import org.eclipse.osee.framework.core.data.OseeCredential;
import org.eclipse.osee.framework.core.data.OseeSessionGrant;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.server.IAuthenticationManager;
import org.eclipse.osee.framework.core.server.ISession;
import org.eclipse.osee.framework.core.server.ISessionManager;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.time.GlobalTime;

/**
 * @author Roberto E. Escobar
 */
public final class SessionManagerImpl implements ISessionManager {

   private final SessionFactory sessionFactory;
   private final Cache<String, Session> sessionCache;
   private final IAuthenticationManager authenticationManager;
   private final WriteDataAccessor<Session> storeDataAccessor;

   public SessionManagerImpl(SessionFactory sessionFactory, Cache<String, Session> sessionCache, IAuthenticationManager authenticationManager, WriteDataAccessor<Session> storeDataAccessor) {
      this.sessionFactory = sessionFactory;
      this.sessionCache = sessionCache;
      this.authenticationManager = authenticationManager;
      this.storeDataAccessor = storeDataAccessor;
   }

   @Override
   public OseeSessionGrant createSession(final OseeCredential credential) throws OseeCoreException {
      Conditions.checkNotNull(credential, "credential");
      OseeSessionGrant sessionGrant = null;
      final String newSessionId = GUID.create();
      boolean isAuthenticated = authenticationManager.authenticate(credential);
      if (isAuthenticated) {
         final IUserToken userToken = authenticationManager.asUserToken(credential);

         Callable<Session> callable = new Callable<Session>() {

            @Override
            public Session call() throws Exception {

               Date creationDate = GlobalTime.GreenwichMeanTimestamp();
               Session session =
                  sessionFactory.createNewSession(newSessionId, userToken.getUserId(), creationDate,
                     credential.getVersion(), credential.getClientMachineName(), credential.getClientAddress(),
                     credential.getPort());

               // if the user is BootStrap we do not want to insert into database since tables may not exist
               if (!SystemUser.BootStrap.equals(userToken)) {
                  storeDataAccessor.create(Collections.singleton(session));
               }

               return session;
            }
         };

         try {
            Session session = sessionCache.get(newSessionId, callable);
            sessionGrant = sessionFactory.createSessionGrant(session, userToken, authenticationManager.getProtocol());
         } catch (Exception e) {
            OseeExceptions.wrapAndThrow(e);
         }

      }
      return sessionGrant;
   }

   @Override
   public void releaseSession(String sessionId) throws OseeCoreException {
      releaseSessionImmediate(sessionId);
   }

   @Override
   public Session getSessionById(String sessionId) throws OseeCoreException {
      Conditions.checkNotNull(sessionId, "sessionId");
      Session session = null;
      try {
         session = sessionCache.get(sessionId);
      } catch (Exception e) {
         session = null;
      }
      return session;
   }

   @Override
   public Collection<ISession> getSessionByClientAddress(String clientAddress) throws OseeCoreException {
      Conditions.checkNotNull(clientAddress, "clientAddress");
      Set<ISession> sessions = new HashSet<>();
      Iterable<Session> all = null;
      try {
         all = sessionCache.getAll();
      } catch (Exception e) {
         OseeExceptions.wrapAndThrow(e);
      }
      if (all != null) {
         for (Session session : all) {
            if (session.getClientAddress().equals(clientAddress)) {
               sessions.add(session);
            }
         }
      }
      return sessions;
   }

   @Override
   public Collection<ISession> getSessionsByUserId(String userId) throws OseeCoreException {
      Conditions.checkNotNull(userId, "userId");
      Collection<ISession> toReturn = new HashSet<>();
      for (ISession session : getAllSessions()) {
         if (session.getUserId().equals(userId)) {
            toReturn.add(session);
         }
      }
      return toReturn;
   }

   @Override
   public Collection<ISession> getAllSessions() throws OseeCoreException {
      Collection<ISession> toReturn = new HashSet<>();
      try {
         Iterable<Session> all = sessionCache.getAll();
         for (Session session : all) {
            toReturn.add(session);
         }
      } catch (Exception e) {
         OseeExceptions.wrapAndThrow(e);
      }
      return toReturn;
   }

   @Override
   public void releaseSessionImmediate(String... sessionIds) throws OseeCoreException {
      Conditions.checkNotNull(sessionIds, "sessionIds");

      Set<Session> sessions = new HashSet<>();
      for (String sessionId : sessionIds) {
         Session session = getSessionById(sessionId);
         if (session != null) {
            sessions.add(session);
         }
      }

      if (!sessions.isEmpty()) {
         storeDataAccessor.delete(sessions);
      }
      sessionCache.invalidate(Arrays.asList(sessionIds));
   }

}

Back to the top