Limiting the Number of Users

LimitLogins.java
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.f1.semaphores; 04 05 06import java.io.*; 07import com.db4o.*; 08 09/** 10 * This class demonstrates the use of semaphores to limit the 11 * number of logins to a server. 12 */ 13public class LimitLogins { 14 15 static final String HOST = "localhost"; 16 static final int PORT = 4455; 17 static final String USER = "db4o"; 18 static final String 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}