ClientStore.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* ClientStore.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.sasl.srp;
  32. import java.util.HashMap;
  33. /**
  34. * The client-side implementation of the SRP security context store.
  35. */
  36. public class ClientStore
  37. {
  38. /** The underlying singleton. */
  39. private static ClientStore singleton = null;
  40. /** The map of uid --> SASL Security Context record. */
  41. private static final HashMap uid2ssc = new HashMap();
  42. /** The map of sid --> Session timing record. */
  43. private static final HashMap uid2ttl = new HashMap();
  44. /** A synchronisation lock. */
  45. private static final Object lock = new Object();
  46. /** Private constructor to enforce Singleton pattern. */
  47. private ClientStore()
  48. {
  49. super();
  50. // TODO: add a cleaning timer thread
  51. }
  52. /**
  53. * Returns the classloader Singleton.
  54. *
  55. * @return the classloader Singleton instance.
  56. */
  57. static synchronized final ClientStore instance()
  58. {
  59. if (singleton == null)
  60. singleton = new ClientStore();
  61. return singleton;
  62. }
  63. /**
  64. * Returns a boolean flag indicating if the designated client's session is
  65. * still alive or not.
  66. *
  67. * @param uid the identifier of the client whose session to check.
  68. * @return <code>true</code> if the designated client's session is still
  69. * alive. <code>false</code> otherwise.
  70. */
  71. boolean isAlive(final String uid)
  72. {
  73. final boolean result;
  74. synchronized (lock)
  75. {
  76. final Object obj = uid2ssc.get(uid);
  77. result = (obj != null);
  78. if (result) // is it still alive?
  79. {
  80. final StoreEntry sto = (StoreEntry) uid2ttl.get(uid);
  81. if (! sto.isAlive()) // invalidate it
  82. {
  83. uid2ssc.remove(uid);
  84. uid2ttl.remove(uid);
  85. }
  86. }
  87. }
  88. return result;
  89. }
  90. /**
  91. * Records a mapping between a client's unique identifier and its security
  92. * context.
  93. *
  94. * @param uid the unique identifier of the SRP client for which the session is
  95. * to be cached.
  96. * @param ttl the session's Time-To-Live indicator (in seconds).
  97. * @param ctx the client's security context.
  98. */
  99. void cacheSession(final String uid, final int ttl, final SecurityContext ctx)
  100. {
  101. synchronized (lock)
  102. {
  103. uid2ssc.put(uid, ctx);
  104. uid2ttl.put(uid, new StoreEntry(ttl));
  105. }
  106. }
  107. /**
  108. * Removes the mapping between the designated SRP client unique identifier and
  109. * the its session security context (and other timing information).
  110. *
  111. * @param uid the identifier of the client whose session is to invalidate.
  112. */
  113. void invalidateSession(final String uid)
  114. {
  115. synchronized (lock)
  116. {
  117. uid2ssc.remove(uid);
  118. uid2ttl.remove(uid);
  119. }
  120. }
  121. /**
  122. * Returns an SRP client's security context record mapped by that client's
  123. * unique identifier.
  124. *
  125. * @param uid the identifier of the client whose session is to restore.
  126. * @return the SRP client's security context.
  127. */
  128. SecurityContext restoreSession(final String uid)
  129. {
  130. final SecurityContext result;
  131. synchronized (lock)
  132. {
  133. result = (SecurityContext) uid2ssc.remove(uid);
  134. uid2ttl.remove(uid);
  135. }
  136. return result;
  137. }
  138. }