DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ActiveSessionList.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Linq;
21 
22 namespace Deveel.Data {
27  public sealed class ActiveSessionList : IEnumerable<ISession> {
28  private readonly List<ISession> sessions;
29 
37  public ActiveSessionList(IDatabase database) {
38  if (database == null)
39  throw new ArgumentNullException("database");
40 
41  Database = database;
42  sessions = new List<ISession>();
43  }
44 
51  public IDatabase Database { get; private set; }
52 
58  public bool IsUserActive(string userName) {
59  lock (this) {
60  return sessions.Any(x => x.User.Name == userName);
61  }
62  }
63 
70  public int Count {
71  get {
72  lock (this) {
73  return sessions.Count;
74  }
75  }
76  }
77 
86  public ISession this[int index] {
87  get {
88  lock (this) {
89  return sessions[index];
90  }
91  }
92  }
93 
100  public IEnumerator<ISession> GetEnumerator() {
101  lock (this) {
102  return sessions.GetEnumerator();
103  }
104  }
105 
106  IEnumerator IEnumerable.GetEnumerator() {
107  return GetEnumerator();
108  }
109 
110  internal bool Add(ISession session) {
111  lock (this) {
112  if (sessions.Contains(session))
113  return false;
114 
115  sessions.Add(session);
116  return true;
117  }
118  }
119 
120  internal void Remove(ISession session) {
121  lock (this) {
122  sessions.Remove(session);
123  }
124  }
125  }
126 }
readonly List< ISession > sessions
The default implementation of a database in a system.
Definition: Database.cs:38
The representation of a single database in the system.
Definition: IDatabase.cs:40
Manages all the open sessions towards a single database within as system.
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
bool Add(ISession session)
void Remove(ISession session)
bool IsUserActive(string userName)
Determines whether the specific user is active.
IEnumerator< ISession > GetEnumerator()
Returns an enumerator that iterates through the collection.
ActiveSessionList(IDatabase database)
Initializes a new instance of the ActiveSessionList class that is wrapped around a given database con...