001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.wss.impl.misc;
024:
025: import java.util.Hashtable;
026: import java.util.Enumeration;
027:
028: public class NonceContainer {
029:
030: //stores applicationId Vs NonceCache pairs
031: static Hashtable nonceTable = new Hashtable();
032:
033: public static boolean validateAndCacheNonce(String applicationId,
034: String nonce, String created, long maxNonceAge) {
035:
036: NonceCache cache = (NonceCache) nonceTable.get(applicationId);
037:
038: if ((cache == null) || (cache.wasCanceled()))
039: cache = initNonceCache(applicationId, maxNonceAge);
040:
041: setNonceCacheCleanup(cache);
042: return cache.validateAndCacheNonce(nonce, created);
043: }
044:
045: private static synchronized NonceCache initNonceCache(
046: String applicationId, long maxNonceAge) {
047:
048: NonceCache cache = (NonceCache) nonceTable.get(applicationId);
049:
050: if (cache == null) {
051: if (maxNonceAge == 0)
052: cache = new NonceCache();
053: else
054: cache = new NonceCache(maxNonceAge);
055: //log.log(Level.FINE, "Creating NonceCache for first time....." + cache);
056: nonceTable.put(applicationId, cache);
057: } else if (cache.wasCanceled()) {
058: if (maxNonceAge == 0)
059: cache = new NonceCache();
060: else
061: cache = new NonceCache(maxNonceAge);
062: //log.log(Level.FINE, "Re-creating NonceCache because it was canceled....." + nonceCache);
063: nonceTable.put(applicationId, cache);
064: }
065:
066: return cache;
067: }
068:
069: private static synchronized void setNonceCacheCleanup(
070: NonceCache nonceCache) {
071:
072: if (!nonceCache.isScheduled()) {
073: //log.log(Level.FINE, "Scheduling Nonce Reclaimer task...... for " + this + ":" + nonceCache);
074: DefaultSecurityEnvironmentImpl.nonceCleanupTimer.schedule(
075: nonceCache, nonceCache.getMaxNonceAge(), // run it the first time after
076: nonceCache.getMaxNonceAge()); //repeat every
077: nonceCache.scheduled(true);
078: }
079: }
080:
081: /**
082: * Management method to cleanup an entry in NonceContainer
083: */
084: public static synchronized void cleanup(String applicationId) {
085: NonceCache cache = (NonceCache) nonceTable.get(applicationId);
086: if (cache != null) {
087: cache.cancel();
088: nonceTable.remove(applicationId);
089: }
090: }
091:
092: /**
093: * Management method to cleanup the NonceContainer
094: */
095: public static synchronized void cleanup() {
096: // cancel all timer tasks
097: Enumeration keys = nonceTable.keys();
098:
099: while (keys.hasMoreElements()) {
100: String appId = (String) keys.nextElement();
101: NonceCache cache = (NonceCache) nonceTable.get(appId);
102:
103: if (cache != null) {
104: cache.cancel();
105: }
106: }
107: nonceTable.clear();
108: }
109: }
|