00001 /* 00002 * JAEBIServer.java 00003 * 00004 * Created on June 14, 2005, 5:30 PM 00005 * Created by Detro - 566/2145 00006 */ 00007 00008 package org.jaebi.server; 00009 00010 import java.util.ArrayList; 00011 import java.util.Collection; 00012 import java.util.List; 00013 00014 import org.jaebi.server.auth.Session; 00015 import org.jaebi.server.core.TimeoutThreadedUserContainer; 00016 import org.jaebi.server.core.User; 00017 import org.jaebi.server.core.UserContainer; 00018 import org.jaebi.server.exception.JAEBIServerExecException; 00019 import org.jaebi.server.exception.JAEBIServerInitException; 00020 import org.jaebi.server.exception.JAEBIServerUserRegistrationException; 00021 import org.jaebi.server.exception.JAEBIServerWrongSessionException; 00022 import org.jaebi.server.service.ServiceMediator; 00023 import org.jaebi.server.service.ServiceMediatorImpl; 00024 import org.jaebi.server.service.exception.ServiceMediatorExecException; 00025 import org.jaebi.server.service.exception.ServiceMediatorInitException; 00026 00041 public class JAEBIServer { 00043 private final UserContainer registeredUsers; 00046 private ServiceMediator serviceMediator; 00047 private static final String exceptionPrefix = "[JAEBIServer Exception]: "; 00048 00058 public JAEBIServer(String backEndPropertiesFilePath) 00059 throws JAEBIServerInitException { 00060 00061 /* Alloca il container di User: 00062 * controlla ogni 30 secondi che uno User non sia inattivo 00063 * da piu' di 5 minuti */ 00064 // param 01: Elimina User dopo 5 min (300 sec) di inattivita' 00065 // param 02: Controlla Timestamp degli User ogni 30 sec 00066 registeredUsers = new TimeoutThreadedUserContainer( 300000, 30000 ); 00067 serviceMediator = null; 00068 00069 try { 00070 /* Alloca il Service Mediator */ 00071 if ( backEndPropertiesFilePath != null ) { 00072 serviceMediator = 00073 new ServiceMediatorImpl(backEndPropertiesFilePath); 00074 } else { 00075 serviceMediator = new ServiceMediatorImpl(); 00076 } 00077 } catch ( ServiceMediatorInitException sminite ) { 00078 throw new JAEBIServerInitException( 00079 exceptionPrefix + "Server Initialization Error\n" + sminite ); 00080 } 00081 } 00082 00089 public JAEBIServer() 00090 throws JAEBIServerInitException { 00091 00092 this(null); 00093 } 00094 00106 public void registerNewUser(String userNick, 00107 Session userSession, String userType) 00108 throws JAEBIServerUserRegistrationException { 00109 00110 if ( !registeredUsers.addUser(userNick, userSession, userType) ) { 00111 throw new JAEBIServerUserRegistrationException( 00112 exceptionPrefix + "Unable to Register User\n" ); 00113 } 00114 } 00115 00124 public void unregisterUser(Session userSession) 00125 throws JAEBIServerWrongSessionException { 00126 00127 if ( !registeredUsers.removeUser(userSession) ) { 00128 throw new JAEBIServerWrongSessionException( 00129 exceptionPrefix + "User or Session Unknown\n" ); 00130 } 00131 } 00132 00142 public User getRegisteredUser(Session userSession) 00143 throws JAEBIServerWrongSessionException { 00144 00145 if ( !registeredUsers.contains(userSession) ) { 00146 throw new JAEBIServerWrongSessionException( 00147 exceptionPrefix + "User or Session Unknown\n" ); 00148 } else { 00149 return registeredUsers.getUser(userSession); 00150 } 00151 } 00152 00165 public String executeRequest(Session userSession, String userRequest) 00166 throws JAEBIServerExecException, JAEBIServerWrongSessionException { 00167 00168 User requestingUser = null; 00169 String result = null; 00170 00171 requestingUser = getRegisteredUser(userSession); 00172 00173 try { 00174 result = serviceMediator.executeServiceToString(requestingUser, userRequest); 00175 } catch (ServiceMediatorExecException sermedee) { 00176 throw new JAEBIServerExecException( 00177 exceptionPrefix + "Service Error\n" + sermedee ); 00178 } 00179 00180 return result; 00181 } 00182 00188 public List getRegisteredUsers() { 00189 Collection coll = registeredUsers.toCollection(); 00190 ArrayList result = new ArrayList(); 00191 00192 result.addAll(coll); 00193 00194 return result; 00195 } 00196 }