Skip to main content
summaryrefslogtreecommitdiffstats
blob: 466d5be49f3b71f192ba3b4c97f10973aba690d8 (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
/*******************************************************************************
 * 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.Date;
import java.util.Random;
import org.eclipse.osee.framework.core.server.internal.util.CharJoinQuery;
import org.eclipse.osee.framework.core.server.internal.util.DatabaseJoinAccessor;
import org.eclipse.osee.framework.core.server.internal.util.IJoinAccessor;
import org.eclipse.osee.framework.core.server.internal.util.OseeInfo;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.jdbc.JdbcClient;
import org.eclipse.osee.jdbc.JdbcStatement;

/**
 * @author Roberto E. Escobar
 */
public final class DatabaseSessionQuery implements ISessionQuery {
   private static final Long DEFAULT_JOIN_EXPIRATION_SECONDS = 3L * 60L * 60L; // 3 hours
   private static final String EXPIRATION_SECS__CHAR_JOIN_QUERY = "char.join.expiration.secs";

   private static final String SELECT_ALL_SESSIONS = "select * from osee_session";

   private static final String SELECT_SESSIONS_BY_SESSION_ID =
      "select * from osee_session, osee_join_char_id jid WHERE session_id = jid.id and jid.query_id = ?";

   private final JdbcClient jdbcClient;
   private final IJoinAccessor joinAccessor;
   private final Random random = new Random();

   public DatabaseSessionQuery(JdbcClient jdbcClient) {
      super();
      this.jdbcClient = jdbcClient;
      this.joinAccessor = new DatabaseJoinAccessor(jdbcClient);
   }

   @Override
   public void selectAllServerManagedSessions(ISessionCollector collector) throws OseeCoreException {
      querySessions(collector, SELECT_ALL_SESSIONS);
   }

   @Override
   public void selectSessionsById(ISessionCollector collector, Iterable<? extends String> ids) throws OseeCoreException {
      CharJoinQuery joinQuery = createCharJoinQuery();
      try {
         for (String id : ids) {
            joinQuery.add(id);
         }
         joinQuery.store();
         querySessions(collector, SELECT_SESSIONS_BY_SESSION_ID, joinQuery.getQueryId());
      } finally {
         joinQuery.delete();
      }
   }

   private void querySessions(ISessionCollector collector, String sql, Object... params) throws OseeCoreException {
      JdbcStatement chStmt = jdbcClient.getStatement();
      try {
         chStmt.runPreparedQuery(sql, params);
         while (chStmt.next()) {
            String sessionGuid = chStmt.getString("session_id");
            String userId = chStmt.getString("user_id");
            Date creationDate = chStmt.getTimestamp("created_on");
            String clientVersion = chStmt.getString("client_version");
            String clientMachineName = chStmt.getString("client_machine_name");
            String clientAddress = chStmt.getString("client_address");
            int clientPort = chStmt.getInt("client_port");
            collector.collect(sessionGuid, userId, creationDate, clientVersion, clientMachineName, clientAddress,
               clientPort);
         }
      } finally {
         chStmt.close();
      }
   }

   private int getNewQueryId() {
      return random.nextInt();
   }

   private CharJoinQuery createCharJoinQuery() {
      Long actualExpiration = getExpiresIn(null, EXPIRATION_SECS__CHAR_JOIN_QUERY);
      return new CharJoinQuery(joinAccessor, actualExpiration, getNewQueryId());
   }

   private Long getExpiresIn(Long actual, String defaultKey) {
      Long toReturn = DEFAULT_JOIN_EXPIRATION_SECONDS;
      if (actual != null) {
         toReturn = actual;
      } else {
         String expiration = OseeInfo.getCachedValue(jdbcClient, defaultKey);
         if (Strings.isNumeric(expiration)) {
            toReturn = Long.parseLong(expiration);
         }
      }
      return toReturn;
   }
}

Back to the top