001: /*
002: * $Id: NonceCache.java,v 1.4 2007/01/08 16:06:02 shyam_rao Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.wss.impl.misc;
028:
029: import java.util.Hashtable;
030: import java.util.TimerTask;
031: import java.util.logging.Level;
032: import java.util.logging.Logger;
033:
034: import com.sun.xml.wss.logging.LogDomainConstants;
035: import com.sun.xml.wss.impl.MessageConstants;
036:
037: /*
038: * This class holds a Nonce Cache and is a TimerTask
039: */
040: public class NonceCache extends TimerTask {
041:
042: /** logger */
043: protected static final Logger log = Logger.getLogger(
044: LogDomainConstants.WSS_API_DOMAIN,
045: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
046:
047: // Nonce Cache
048: private Hashtable nonceCache = new Hashtable();
049: private Hashtable oldNonceCache = new Hashtable();
050:
051: // default
052: private long MAX_NONCE_AGE = 900000;
053:
054: // flag to indicate if this timertask is scheduled into the Timer queue
055: private boolean scheduledFlag = false;
056: private boolean canceledFlag = false;
057:
058: public NonceCache() {
059: }
060:
061: public NonceCache(long maxNonceAge) {
062: MAX_NONCE_AGE = maxNonceAge;
063: }
064:
065: public boolean validateAndCacheNonce(String nonce, String created) {
066: if (nonceCache.containsKey(nonce)
067: || oldNonceCache.containsKey(nonce)) {
068: log.log(Level.INFO,
069: "Nonce Cache already contains the new Nonce Value received :"
070: + nonce);
071: return false;
072: }
073:
074: if (MessageConstants.debug)
075: log.log(Level.FINE, "Storing Nonce Value " + nonce
076: + " into " + this );
077:
078: nonceCache.put(nonce, created);
079: return true;
080: }
081:
082: public boolean isScheduled() {
083: return scheduledFlag;
084: }
085:
086: public void scheduled(boolean flag) {
087: scheduledFlag = flag;
088: }
089:
090: public boolean wasCanceled() {
091: return canceledFlag;
092: }
093:
094: public void run() {
095:
096: if (nonceCache.size() == 0) {
097: //boolean canceled = cancel();
098: if (MessageConstants.debug)
099: log.log(Level.FINE,
100: "Canceled Timer Task due to inactivity ...for "
101: + this );
102: return;
103: }
104:
105: if (MessageConstants.debug)
106: log.log(Level.FINE, "Clearing old Nonce values...for "
107: + this );
108:
109: oldNonceCache.clear();
110: Hashtable temp = nonceCache;
111: nonceCache = oldNonceCache;
112: oldNonceCache = temp;
113: }
114:
115: public boolean cancel() {
116: boolean ret = super .cancel();
117: canceledFlag = true;
118: oldNonceCache.clear();
119: nonceCache.clear();
120:
121: return ret;
122: }
123:
124: public long getMaxNonceAge() {
125: return MAX_NONCE_AGE;
126: }
127:
128: }
|