Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / sasl / srp / ServerStore.java
blob99bf96a94441f4365c8a216e08b129a2ea2b6eb2
1 /* ServerStore.java --
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.crypto.sasl.srp;
41 import java.util.HashMap;
43 /**
44 * <p>The server-side implementation of the SRP security context store.</p>
46 public class ServerStore
49 // Constants and variables
50 // -------------------------------------------------------------------------
52 /** The underlying singleton. */
53 private static ServerStore singleton = null;
55 /** The map of sid --> Security Context record. */
56 private static final HashMap sid2ssc = new HashMap();
58 /** The map of sid --> Session timing record. */
59 private static final HashMap sid2ttl = new HashMap();
61 /** A synchronisation lock. */
62 private static final Object lock = new Object();
64 /** A counter to generate legible SIDs. */
65 private static int counter = 0;
67 // Constructor(s)
68 // -------------------------------------------------------------------------
70 /** Private constructor to enforce Singleton pattern. */
71 private ServerStore()
73 super();
75 // TODO: add a cleaning timer thread
78 // Class methods
79 // -------------------------------------------------------------------------
81 /**
82 * <p>Returns the classloader Singleton.</p>
84 * @return the classloader Singleton instance.
86 static synchronized final ServerStore instance()
88 if (singleton == null)
90 singleton = new ServerStore();
92 return singleton;
95 /**
96 * <p>Returns a legible new session identifier.</p>
98 * @return a new session identifier.
100 static synchronized final byte[] getNewSessionID()
102 final String sid = String.valueOf(++counter);
103 return new StringBuffer("SID-").append(
104 "0000000000".substring(
106 10 - sid.length())).append(
107 sid).toString().getBytes();
110 // Instance methods
111 // -------------------------------------------------------------------------
114 * <p>Returns a boolean flag indicating if the designated session is still
115 * alive or not.</p>
117 * @param sid the identifier of the session to check.
118 * @return <code>true</code> if the designated session is still alive.
119 * <code>false</code> otherwise.
121 boolean isAlive(final byte[] sid)
123 boolean result = false;
124 if (sid != null && sid.length != 0)
126 synchronized (lock)
128 final String key = new String(sid);
129 final StoreEntry ctx = (StoreEntry) sid2ttl.get(key);
130 if (ctx != null)
132 result = ctx.isAlive();
133 if (!result)
134 { // invalidate it en-passant
135 sid2ssc.remove(key);
136 sid2ttl.remove(key);
141 return result;
145 * <p>Records a mapping between a session identifier and the Security Context
146 * of the designated SRP server mechanism instance.</p>
148 * @param ttl the session's Time-To-Live indicator (in seconds).
149 * @param ctx the server's security context.
151 void cacheSession(final int ttl, final SecurityContext ctx)
153 synchronized (lock)
155 final String key = new String(ctx.getSID());
156 sid2ssc.put(key, ctx);
157 sid2ttl.put(key, new StoreEntry(ttl));
162 * <p>Updates the mapping between the designated session identifier and the
163 * designated server's SASL Security Context. In the process, computes
164 * and return the underlying mechanism server's evidence that shall be
165 * returned to the client in a session re-use exchange.</p>
167 * @param sid the identifier of the session to restore.
168 * @return an SRP server's security context.
170 SecurityContext restoreSession(final byte[] sid)
172 final String key = new String(sid);
173 final SecurityContext result;
174 synchronized (lock)
176 result = (SecurityContext) sid2ssc.remove(key);
177 sid2ttl.remove(key);
179 return result;
183 * <p>Removes all information related to the designated session ID.</p>
185 * @param sid the identifier of the seesion to invalidate.
187 void invalidateSession(final byte[] sid)
189 final String key = new String(sid);
190 synchronized (lock)
192 sid2ssc.remove(key);
193 sid2ttl.remove(key);