KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > semaphores > LimitLogins


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */
2
3 package com.db4odoc.f1.semaphores;
4
5
6 import java.io.*;
7 import com.db4o.*;
8
9 /**
10  * This class demonstrates the use of semaphores to limit the
11  * number of logins to a server.
12  */

13 public class LimitLogins {
14     
15     static final String JavaDoc HOST = "localhost";
16     static final int PORT = 4455;
17     static final String JavaDoc USER = "db4o";
18     static final String JavaDoc PASSWORD = "db4o";
19     
20     static final int MAXIMUM_USERS = 10;
21     
22     public static ObjectContainer login(){
23         
24         ObjectContainer objectContainer;
25         try {
26             objectContainer = Db4o.openClient(HOST, PORT, USER, PASSWORD);
27         } catch (IOException e) {
28             return null;
29         }
30         
31         boolean allowedToLogin = false;
32         
33         for (int i = 0; i < MAXIMUM_USERS; i++) {
34             if(objectContainer.ext().setSemaphore("max_user_check_" + i, 0)){
35                 allowedToLogin = true;
36                 break;
37             }
38         }
39         
40         if(! allowedToLogin){
41             objectContainer.close();
42             return null;
43         }
44         
45         return objectContainer;
46     }
47 }
48
Popular Tags