/Users/detro/workspace-nb/JAEBI/src/java/org/jaebi/server/JAEBIWSImpl.java

Vai alla documentazione di questo file.
00001 package org.jaebi.server;
00002 
00003 import java.util.Collection;
00004 import java.util.HashMap;
00005 import java.util.Iterator;
00006 import org.jaebi.server.auth.Authenticator;
00007 import org.jaebi.server.auth.AuthenticatorImpl;
00008 import org.jaebi.server.auth.Session;
00009 import org.jaebi.server.core.User;
00010 import org.jaebi.server.exception.JAEBIServerExecException;
00011 import org.jaebi.server.exception.JAEBIServerInitException;
00012 import org.jaebi.server.exception.JAEBIServerUserRegistrationException;
00013 import org.jaebi.server.exception.JAEBIServerWrongSessionException;
00014 
00015 
00066 public class JAEBIWSImpl implements JAEBIWSSEI {
00068     private JAEBIServer coreServer;
00070     private final Authenticator authenticator;
00080     private final HashMap activeSessions;
00081 
00083     private static final String configBaseDir = 
00084             "/opt/tomcat/webapps/axis/JAEBIWS/config/";
00086     private static final String backEndPropFile = 
00087             "BackEndHandlerImpl.properties";
00088 
00090     private static final String messageWrongSessionErrorID = 
00091             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
00092             "<xresponse xmlns='http://xml.netbeans.org/examples/targetNS' " +
00093             "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
00094             "xsi:schemaLocation='http://xml.netbeans.org/examples/targetNS " +
00095             "xresponse.xsd'>" +
00096             "<message result=\"error\">" +
00097             "Received a Wrong Session ID: please, check it." +
00098             "</message>" +
00099             "</xresponse>";
00100 
00102     private static final String messageServiceError = 
00103             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
00104             "<xresponse xmlns='http://xml.netbeans.org/examples/targetNS' " +
00105             "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
00106             "xsi:schemaLocation='http://xml.netbeans.org/examples/targetNS " +
00107             "xresponse.xsd'>" +
00108             "<message result=\"error\">" +
00109             "Error during Service Execution." +
00110             "</message>" +
00111             "</xresponse>";
00112 
00114     private static final String messageSessionError = 
00115             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
00116             "<xresponse xmlns='http://xml.netbeans.org/examples/targetNS' " +
00117             "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
00118             "xsi:schemaLocation='http://xml.netbeans.org/examples/targetNS " +
00119             "xresponse.xsd'>" +
00120             "<message result=\"error\">" +
00121             "User Session Error." +
00122             "</message>" +
00123             "</xresponse>";
00124 
00126     private static final String messageLogout = 
00127             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
00128             "<xresponse xmlns='http://xml.netbeans.org/examples/targetNS' " +
00129             "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
00130             "xsi:schemaLocation='http://xml.netbeans.org/examples/targetNS " +
00131             "xresponse.xsd'>" +
00132             "<message result=\"error\">" +
00133             "Logged Out. Bye from JAEBI Server." +
00134             "</message>" +
00135             "</xresponse>";
00136 
00138     private String lastErrorMessage = null;
00139         
00167     public JAEBIWSImpl() {
00168         // Inizializzazione Authenticator
00169         authenticator = new AuthenticatorImpl();
00170 
00171         // Inizializzazione "archivio Sessioni"
00172         activeSessions = new HashMap();
00173 
00174         // Inizializzazione JAEBIServer
00175         try {
00176             coreServer = new JAEBIServer( backEndPropFile );
00177         } catch (JAEBIServerInitException e) {
00178             /* Nel caso si verifichi qui un errore,
00179              * sarebbe irrimediabile per il corretto
00180              * funzionamento del Server: conserviamo in ogni 
00181              * caso il messaggio circa l'errore prodotto.
00182              */
00183             lastErrorMessage = e.getMessage();
00184         }
00185     }
00186     
00200     public int login(java.lang.String usernick, java.lang.String usertype) {
00201         Session userSession;
00202                 
00203         // Allochiamo un Session per il nuovo User
00204         userSession = authenticator.getAuthentication(usernick);
00205 
00206         /* Registriamo il Session, indicizzato in base al SessionID (in pratica, 
00207          * l'hashcode di Session) */ 
00208         activeSessions.put(
00209             new Integer( userSession.hashCode() ), 
00210             userSession);
00211 
00212         if ( coreServer == null )
00213             return -1;
00214 
00215         // Registrazione nuovo User
00216         try {
00217             coreServer.registerNewUser(usernick, userSession, usertype);
00218         } catch (JAEBIServerUserRegistrationException e) {
00219             lastErrorMessage = e.getMessage();
00220             return -1;
00221         }
00222 
00223         /* TODO
00224          * Cercare un algoritmo per la generazione del SessionID
00225          * piu' sicuro: l'HASHCODE potrebbe generare un valore
00226          * uguale al valore di errore (-1), creando confusione
00227          */
00228         return userSession.hashCode();
00229     }
00230     
00237     public String logout(int sessionid) {
00238         Session userSession;
00239         String xresponse = null;
00240 
00241         // Recupero oggetto Session in base al SessionID 
00242         userSession = (Session)activeSessions.get( new Integer(sessionid) );
00243 
00244         if ( userSession == null ) {
00245             xresponse = messageWrongSessionErrorID;
00246         } else {
00247             // Deregistrazione User
00248             try {
00249                 coreServer.unregisterUser(userSession);
00250                 xresponse = messageLogout;
00251             } catch (JAEBIServerWrongSessionException e) {
00252                 xresponse = messageSessionError;
00253                 lastErrorMessage = e.getMessage();
00254             }
00255         }
00256 
00257         return xresponse;
00258     }
00259     
00260     
00266     public String getLastError() {
00267         return lastErrorMessage;
00268     }
00269 
00278     public String executeRequest(int sessionid, java.lang.String xrequest) {
00279         String xresponse = null;
00280         Session userSession = (Session)activeSessions.get( new Integer(sessionid) );
00281 
00282         if ( userSession == null ) {
00283             // Il "sessionId" fornito e' errato
00284             xresponse = messageWrongSessionErrorID;
00285         } else {
00286             // Esecuzione Richiesta
00287             try {
00288                 xresponse = coreServer.executeRequest( userSession, xrequest);
00289             } catch (JAEBIServerExecException e) {
00290                 lastErrorMessage = e.getMessage();
00291                 xresponse = messageServiceError;
00292             } catch (JAEBIServerWrongSessionException e) {
00293                 lastErrorMessage = e.getMessage();
00294                 xresponse = messageSessionError;
00295             }
00296         }
00297 
00298         return xresponse;
00299     }
00300 
00307     public String getUsersList() {
00308         StringBuffer result = new StringBuffer();
00309         Collection coll = activeSessions.values();
00310         Iterator iter = coll.iterator();
00311         Session currentSession = null;
00312         User currentUser = null;
00313         int i = 0;
00314 
00315         result.append("[JAEBIWS] REGISTERED USERS:\n");
00316         while ( iter.hasNext() ) {
00317             currentSession = (Session)iter.next();
00318 
00319             try {
00320                 currentUser = coreServer.getRegisteredUser(currentSession);
00321                 result.append("[" + ++i + "] - " + currentUser.getNick() + "\n" );
00322             } catch (JAEBIServerWrongSessionException e) {
00323                 lastErrorMessage = e.getMessage() 
00324                     + "Session ID: " + currentSession.toString();
00325                 //iter.remove();
00326             }
00327         }
00328 
00329         return result.toString();
00330     }
00331 
00337     public int getUsersNumber() {
00338         StringBuffer result = new StringBuffer();
00339         Collection coll = activeSessions.values();
00340         return coll.size();
00341     }
00342 }

Generato il Mon Jun 27 22:50:37 2005 per JAEBI - JAva Enterprice BackEnd Interfacement da  doxygen 1.4.3