001: /*
002: * JOSSO: Java Open Single Sign-On
003: *
004: * Copyright 2004-2008, Atricore, Inc.
005: *
006: * This is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as
008: * published by the Free Software Foundation; either version 2.1 of
009: * the License, or (at your option) any later version.
010: *
011: * This software is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this software; if not, write to the Free
018: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
020: */
021:
022: package org.josso.gateway.assertion;
023:
024: import org.josso.gateway.session.SSOSession;
025: import org.josso.gateway.assertion.service.AssertionIdGenerator;
026: import org.josso.gateway.assertion.service.AssertionIdGeneratorImpl;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import java.util.*;
031:
032: /**
033: * TODO: Comment Me!
034: *
035: * @author <a href="mailto:gbrigand@josso.org">Gianluca Brigandi</a>
036: * @version $Id$
037: */
038: public class AssertionManagerImpl implements AssertionManager {
039:
040: private static final Log logger = LogFactory
041: .getLog(AssertionManagerImpl.class);
042:
043: private List assertions = new java.util.ArrayList();
044: private AssertionMonitor _monitor;
045:
046: public AssertionManagerImpl() {
047: // Start session monitor.
048: _monitor = new AssertionMonitor(this );
049: Thread t = new Thread(_monitor);
050: t.setName("JOSSOAssertionMonitor");
051: t.start();
052:
053: }
054:
055: public synchronized AuthenticationAssertion requestAssertion(
056: SSOSession ssoSession) {
057: AssertionIdGenerator assertionIdGen = new AssertionIdGeneratorImpl();
058:
059: AuthenticationAssertionImpl assertion = new AuthenticationAssertionImpl(
060: assertionIdGen.generateId(), ssoSession);
061:
062: assertions.add(assertion);
063:
064: return assertion;
065: }
066:
067: public synchronized AuthenticationAssertion consumeAssertion(
068: String assertionId) {
069:
070: AuthenticationAssertion targetAuthenticationAssertion = null;
071:
072: for (Iterator i = assertions.iterator(); i.hasNext();) {
073: AuthenticationAssertion aa = (AuthenticationAssertion) i
074: .next();
075:
076: if (aa.getId().equals(assertionId)) {
077: targetAuthenticationAssertion = aa;
078: assertions.remove(aa);
079: logger.debug("Consumed assertion: "
080: + aa.getSSOSession().getId());
081: break;
082: }
083: }
084:
085: return targetAuthenticationAssertion;
086: }
087:
088: public void checkPendingAssertions() {
089: Iterator i = assertions.iterator();
090: while (i.hasNext()) {
091: try {
092:
093: // Ignore valid sessions, they have not expired yet.
094: AuthenticationAssertion assertion = (AuthenticationAssertion) i
095: .next();
096:
097: if (!assertion.isValid()) {
098: assertions.remove(assertion);
099: if (logger.isDebugEnabled())
100: logger
101: .debug("[checkPendingAssertions()] Assertion expired : "
102: + assertion.getId());
103: }
104:
105: } catch (Exception e) {
106: logger
107: .warn("Can't remove assertion "
108: + e.getMessage() != null ? e
109: .getMessage() : e.toString(), e);
110: }
111: }
112:
113: }
114:
115: /**
116: * Checks for assertions which have not been consumed yet.
117: */
118: private class AssertionMonitor implements Runnable {
119:
120: private long _interval;
121:
122: private AssertionManager _m;
123:
124: AssertionMonitor(AssertionManager m) {
125: _m = m;
126: }
127:
128: AssertionMonitor(AssertionManager m, long interval) {
129: _interval = interval;
130: _m = m;
131: }
132:
133: public long getInterval() {
134: return _interval;
135: }
136:
137: public void setInterval(long interval) {
138: _interval = interval;
139: }
140:
141: /**
142: * Check for pending assertiong ...
143: */
144: public void run() {
145:
146: do {
147: try {
148:
149: if (logger.isDebugEnabled())
150: logger
151: .debug("[run()] calling checkPendingAssertions ... ");
152:
153: _m.checkPendingAssertions();
154:
155: synchronized (this ) {
156: try {
157:
158: if (logger.isDebugEnabled())
159: logger.debug("[run()] waiting "
160: + _interval + " ms");
161:
162: wait(_interval);
163:
164: } catch (InterruptedException e) {
165: logger.warn(e, e);
166: }
167: }
168: } catch (Exception e) {
169: logger.warn("Exception received : "
170: + e.getMessage() != null ? e.getMessage()
171: : e.toString(), e);
172: }
173:
174: } while (true);
175: }
176: }
177:
178: }
|