Limiting the Number of Users

LimitLogins.java
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.semaphores; 04 05import com.db4o.*; 06 07/** 08 * This class demonstrates the use of semaphores to limit the number 09 * of logins to a server. 10 */ 11public class LimitLogins { 12 13 static final String HOST = "localhost"; 14 15 static final int PORT = 4455; 16 17 static final String USER = "db4o"; 18 19 static final String PASSWORD = "db4o"; 20 21 static final int MAXIMUM_USERS = 10; 22 23 public static ObjectContainer login() { 24 25 ObjectContainer objectContainer; 26 try { 27 objectContainer = Db4o.openClient(HOST, PORT, USER, 28 PASSWORD); 29 } catch (Exception e) { 30 return null; 31 } 32 33 boolean allowedToLogin = false; 34 35 for (int i = 0; i < MAXIMUM_USERS; i++) { 36 if (objectContainer.ext().setSemaphore( 37 "max_user_check_" + i, 0)) { 38 allowedToLogin = true; 39 break; 40 } 41 } 42 43 if (!allowedToLogin) { 44 objectContainer.close(); 45 return null; 46 } 47 48 return objectContainer; 49 } 50}