Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: maddany/SequenceAuthenticatorManager.java


1   package maddany;
2   
3   import java.util.Vector;
4   import java.util.Enumeration;
5   
6   public class SequenceAuthenticatorManager {
7       Vector v;
8   
9       public SequenceAuthenticatorManager() {
10          v = new Vector();
11      }
12  
13      public synchronized void add(SequenceAuthenticator auth) {
14          v.add(auth);
15      }
16  
17      public synchronized void removeIdent(String ident) {
18          Enumeration en = v.elements();
19          while(en.hasMoreElements()) {
20              SequenceAuthenticator auth = (SequenceAuthenticator)en.nextElement();
21              if(ident.equals(auth.getIdentifier()))
22                  v.remove(auth);
23          }
24      }
25  
26      public synchronized SequenceAuthenticator elementAt(int index) {
27          return (SequenceAuthenticator)v.elementAt(index);
28      }
29  
30      public synchronized Enumeration elements() {
31          return v.elements();
32      }
33  
34      public synchronized void setElementAt(SequenceAuthenticator auth, int index) {
35          v.setElementAt(auth, index);
36      }
37  
38      public synchronized boolean contains(SequenceAuthenticator auth) {
39          return v.contains(auth);
40      }
41  
42      public synchronized boolean containsIdentifier(String ident) {
43          Enumeration en = v.elements();
44          while(en.hasMoreElements()) {
45              SequenceAuthenticator auth = (SequenceAuthenticator)en.nextElement();
46              if(ident.equals(auth.getIdentifier()))
47                  return true;
48          }
49          return false;
50      }
51  
52      public synchronized String removeTimedOut(int timeout) {    // en secondes
53          SequenceAuthenticator auth;
54          String ident = null;
55          for(int i=0; i<v.size(); i++) {
56              auth = (SequenceAuthenticator)v.elementAt(i);
57              int elapsed = (int)(System.currentTimeMillis()-auth.getTimeMillis())/1000;
58              if(elapsed>timeout) {
59                  ident = auth.getIdentifier();
60                  if(v.remove(auth)) 
61                      i--;
62                  else
63                      System.out.println("Can't remove "+auth.toString());
64                  break;
65              }
66          }
67          return ident;
68      }
69  
70      public synchronized boolean isValid(SequenceAuthenticator auth) {
71          if(containsIdentifier(auth.getIdentifier())) {
72              Enumeration en = v.elements();
73              int index=0;
74              while(en.hasMoreElements()) {
75                  SequenceAuthenticator local = (SequenceAuthenticator)en.nextElement();
76                  SequenceAuthenticator incLocal = local.cloneCurrentState();
77                  incLocal.incrementSequence();
78                  if(incLocal.equals(auth)) {
79                      setElementAt(incLocal, index);
80                      return true;
81                  }
82                  index++;
83              }
84          }
85          return false;
86      }
87  }