- * {@link CleanUpListener}, the other implementation, does require - * changes to web.xml, but does not require any Spring configuration. - *
- * The decision as to which timer implementation you use is entirely - * subjective, as both perform the same operation. - *
- * Example Spring config:
- *
- *
- *
- * Note the destroy-method attribute of SchedulerFactoryBean. Without this attribute,
- * a classloader leak may occur.
- *
- * @author Brad Cupit (brad [at] lsu {dot} edu)
- */
-public class CleanUpJob extends QuartzJobBean {
- private final CleanUpRegistry cleanUpRegistry;
-
- public CleanUpJob() {
- this.cleanUpRegistry = CleanUpRegistryImpl.getInstance();
- }
-
- public CleanUpJob(CleanUpRegistry cleanUpRegistry) {
- this.cleanUpRegistry = cleanUpRegistry;
- }
-
- protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
- cleanUpRegistry.cleanAll();
- }
-}
diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/CleanUpListener.java b/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/CleanUpListener.java
deleted file mode 100644
index bc2cdf3..0000000
--- a/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/CleanUpListener.java
+++ /dev/null
@@ -1,97 +0,0 @@
-package org.jasig.cas.client.cleanup;
-
-import java.util.Timer;
-import java.util.TimerTask;
-
-import javax.servlet.ServletContext;
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
-
-/**
- * One of two timer implementations that regularly calls
- * {@link CleanUpRegistry#cleanAll()}. This implementation
- * is configured in web.xml and does not require any Spring
- * configuration.
- *
- * {@link CleanUpJob}, the other implementation, does require - * Spring, but does not require changes to web.xml - *
- * The decision as to which timer implementation you use is entirely - * subjective, as both perform the same operation. - *
- * You can customize the time between cleanup runs by setting a - * context-param in web.xml - *
- * Example web.xml config:
- *
- *
- *
- * With this listener configured, a timer will be set to clean up any {@link Cleanable}s.
- * This timer will automatically shut down on webapp undeploy, preventing any classloader
- * leaks from occurring.
- *
- * @author Brad Cupit (brad [at] lsu {dot} edu)
- */
-public class CleanUpListener implements ServletContextListener {
- protected static final int DEFAULT_MILLIS_BETWEEN_CLEANUPS = 60 * 1000;
- protected static final String MILLIS_BETWEEN_CLEANUPS_INIT_PARAM = "millisBetweenCleanUps";
- private final Timer timer;
- private final CleanUpRegistry cleanUpRegistry;
-
- public CleanUpListener() {
- this.timer = new Timer(true);
- this.cleanUpRegistry = CleanUpRegistryImpl.getInstance();
- }
-
- protected CleanUpListener(Timer timer, CleanUpRegistry cleanUpRegistry) {
- this.timer = timer;
- this.cleanUpRegistry = cleanUpRegistry;
- }
-
- public void contextInitialized(ServletContextEvent servletContextEvent) {
- final Cleaner cleaner = new Cleaner(this.cleanUpRegistry);
- final long millisBetweenCleanUps = getMillisBetweenCleanups(servletContextEvent.getServletContext());
- final long millisBeforeStart = millisBetweenCleanUps;
-
- this.timer.schedule(cleaner, millisBeforeStart, millisBetweenCleanUps);
- }
-
- public void contextDestroyed(ServletContextEvent servletContextEvent) {
- this.timer.cancel();
- }
-
- protected long getMillisBetweenCleanups(ServletContext servletContext) {
- final String millisBetweenCleanUps = servletContext.getInitParameter(MILLIS_BETWEEN_CLEANUPS_INIT_PARAM);
-
- if (millisBetweenCleanUps == null) {
- return DEFAULT_MILLIS_BETWEEN_CLEANUPS;
- }
-
- try {
- return Long.parseLong(millisBetweenCleanUps);
- } catch (NumberFormatException exception) {
- throw new RuntimeException("The servlet context-param " + MILLIS_BETWEEN_CLEANUPS_INIT_PARAM + " must be a valid number (hint: this is usually set in web.xml)", exception);
- }
- }
-
- /**
- * Does the actual clean up each time the timer goes off.
- *
- * @author Brad Cupit (brad [at] lsu {dot} edu)
- */
- private static final class Cleaner extends TimerTask {
- private final CleanUpRegistry cleanUpRegistry;
-
- private Cleaner(CleanUpRegistry cleanUpRegistry) {
- this.cleanUpRegistry = cleanUpRegistry;
- }
-
- public void run() {
- this.cleanUpRegistry.cleanAll();
- }
- }
-}
diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/CleanUpRegistry.java b/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/CleanUpRegistry.java
deleted file mode 100644
index dbabd39..0000000
--- a/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/CleanUpRegistry.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package org.jasig.cas.client.cleanup;
-
-
-/**
- * A central location for all {@link Cleanable}s to register themselves.
- * Then a thread or timer can occasionally call {@link #cleanAll()} to
- * run the {@link Cleanable#cleanUp()} methods.
- *
- * See {@link CleanUpListener} or {@link CleanUpJob} for two implementations - * of a timer which calls {@link #cleanAll()}. Either implementation will - * work, though you only need to choose one. - * - * @author Brad Cupit (brad [at] lsu {dot} edu) - */ -public interface CleanUpRegistry { - /** - * Adds a {@link Cleanable} to the list of objects whose - * {@link Cleanable#cleanUp()} method will be called. - * - * @param cleanable the {@link Cleanable} to add to the list. If a - * {@link Cleanable} is added twice, it's {@link Cleanable#cleanUp()} - * method will be called twice each time the timer goes off - * (this is probably not the desired outcome). - */ - public void addCleanble(Cleanable cleanable); - - /** - * Runs {@link Cleanable#cleanUp()} on each {@link Cleanable} - * passed in to {@link #addCleanble(Cleanable)} - */ - public void cleanAll(); -} diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/CleanUpRegistryImpl.java b/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/CleanUpRegistryImpl.java deleted file mode 100644 index 4970e8a..0000000 --- a/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/CleanUpRegistryImpl.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.jasig.cas.client.cleanup; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -/** - * An old-school singleton implementation of {@link CleanUpRegistry}. - * This implementation does not require clients to add any extra Spring - * configuration, hence the old-school singleton. - * - * A thread or timer should occasionally call {@link #cleanAll()} to - * run the {@link Cleanable#cleanUp()} method on each {@link Cleanable}. - * - * @author Brad Cupit (brad [at] lsu {dot} edu) - */ -public final class CleanUpRegistryImpl implements CleanUpRegistry { - private static CleanUpRegistryImpl cleanUpRegistry = new CleanUpRegistryImpl(); - private List cleanables = Collections.synchronizedList(new ArrayList()); - - private CleanUpRegistryImpl() { - } - - public static CleanUpRegistryImpl getInstance() { - return cleanUpRegistry; - } - - /** - * {@inheritDoc} - */ - public void addCleanble(Cleanable cleanable) { - cleanables.add(cleanable); - } - - /** - * {@inheritDoc} - */ - public void cleanAll() { - synchronized (cleanables) { - for (Iterator iterator = cleanables.iterator(); iterator.hasNext();) { - Cleanable cleanable = (Cleanable) iterator.next(); - cleanable.cleanUp(); - } - } - } -} diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/Cleanable.java b/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/Cleanable.java deleted file mode 100644 index f4bdaaf..0000000 --- a/cas-client-core/src/main/java/org/jasig/cas/client/cleanup/Cleanable.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.jasig.cas.client.cleanup; - -/** - * A simple interface representing an object which needs regular cleaning. - * - * @see CleanUpRegistry - * - * @author Brad Cupit (brad [at] lsu {dot} edu) - */ -public interface Cleanable { - /** - * This method will be called on a regular basis - * to perform internal clean up (for example: removing - * old items from a cache). - *
- * Objects implementing this interface so they can be - * registered with the {@link CleanUpRegistry}. - */ - void cleanUp(); -} diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/proxy/CleanUpListener.java b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/CleanUpListener.java new file mode 100644 index 0000000..01da2c5 --- /dev/null +++ b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/CleanUpListener.java @@ -0,0 +1,88 @@ +package org.jasig.cas.client.proxy; + +import java.util.Timer; +import java.util.TimerTask; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + + +/** + * One of two choices for cleaning up {@link ProxyGrantingTicketStorage}. + * You must either configure this listener in web.xml or configure + * {@link CleanUpTimerTask} in Spring xml. Both choices perform the same + * operation, the only difference is one is configured via the Spring + * Framework and the other via web.xml. + *
+ * See below for example web.xml configuration. + * See {@link CleanUpTimerTask} for an example Spring xml configuration. + *
+ * With this listener configured, a timer will clean up the + * {@link ProxyGrantingTicketStorage storage}. This timer automatically + * shuts down on webapp undeploy, so there will be no classloader leaks + * due to an orphan thread. + *
+ * Example web.xml configuration:
+ *
+ *
+ *
+ * The default time between cleanups is 60 seconds, but you can optionally
+ * customize it by setting a context-param in web.xml. Example config:
+ *
+ *
+ *
+ * @author Brad Cupit (brad [at] lsu {dot} edu)
+ */
+public final class CleanUpListener implements ServletContextListener {
+ protected static final int DEFAULT_MILLIS_BETWEEN_CLEANUPS = 60 * 1000;
+ protected static final String MILLIS_BETWEEN_CLEANUPS_INIT_PARAM = "millisBetweenCleanUps";
+ private final Timer timer;
+ private final TimerTask timerTask;
+
+ public CleanUpListener() {
+ this.timer = new Timer(true);
+ this.timerTask = new CleanUpTimerTask();
+ }
+
+ /**
+ * for unit test use only
+ */
+ protected CleanUpListener(final Timer timer, TimerTask timerTask ) {
+ this.timer = timer;
+ this.timerTask = timerTask;
+ }
+
+ public void contextInitialized(ServletContextEvent servletContextEvent) {
+ final long millisBetweenCleanUps = getMillisBetweenCleanups(servletContextEvent.getServletContext());
+ final long millisBeforeStart = millisBetweenCleanUps;
+
+ this.timer.schedule(timerTask, millisBeforeStart, millisBetweenCleanUps);
+ }
+
+ public void contextDestroyed(ServletContextEvent servletContextEvent) {
+ this.timer.cancel();
+ }
+
+ protected long getMillisBetweenCleanups(ServletContext servletContext) {
+ final String millisBetweenCleanUps = servletContext.getInitParameter(MILLIS_BETWEEN_CLEANUPS_INIT_PARAM);
+
+ if (millisBetweenCleanUps == null) {
+ return DEFAULT_MILLIS_BETWEEN_CLEANUPS;
+ }
+
+ try {
+ return Long.parseLong(millisBetweenCleanUps);
+ } catch (NumberFormatException exception) {
+ throw new RuntimeException("The servlet context-param " + MILLIS_BETWEEN_CLEANUPS_INIT_PARAM + " must be a valid number (hint: this is usually set in web.xml)", exception);
+ }
+ }
+}
diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/proxy/CleanUpTimerTask.java b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/CleanUpTimerTask.java
new file mode 100644
index 0000000..689a600
--- /dev/null
+++ b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/CleanUpTimerTask.java
@@ -0,0 +1,44 @@
+package org.jasig.cas.client.proxy;
+
+import java.util.TimerTask;
+
+import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
+
+/**
+ * A {@link TimerTask} implementation which performs the
+ * actual 'cleaning' by calling {@link ProxyGrantingTicketStorage#cleanUp()}.
+ *
+ * You must configure either this TimerTask directly in Spring, + * or the {@link CleanUpListener} (which is configured in + * web.xml). Both choices perform the same operation, the only difference is + * one is configured via the Spring Framework and the other via web.xml. + *
+ * For an example web.xml configuration, see {@link CleanUpListener}.
+ * For an example Spring xml configuration, see below:
+ *
+ *
+ *
+ * @author Brad Cupit (brad [at] lsu {dot} edu)
+ */
+public final class CleanUpTimerTask extends TimerTask {
+ public void run() {
+ Cas20ProxyReceivingTicketValidationFilter.getProxyGrantingTicketStorage().cleanUp();
+ }
+}
diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/proxy/ProxyGrantingTicketStorage.java b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/ProxyGrantingTicketStorage.java
index d4fd824..89d6721 100644
--- a/cas-client-core/src/main/java/org/jasig/cas/client/proxy/ProxyGrantingTicketStorage.java
+++ b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/ProxyGrantingTicketStorage.java
@@ -33,4 +33,10 @@ public interface ProxyGrantingTicketStorage {
* @return the ProxyGrantingTicket Id or null if it can't be found
*/
public String retrieve(String proxyGrantingTicketIou);
+
+ /**
+ * Called on a regular basis by an external timer,
+ * giving implementations a chance to remove stale data.
+ */
+ public void cleanUp();
}
diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/proxy/ProxyGrantingTicketStorageImpl.java b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/ProxyGrantingTicketStorageImpl.java
index dc3f62e..2b533dd 100644
--- a/cas-client-core/src/main/java/org/jasig/cas/client/proxy/ProxyGrantingTicketStorageImpl.java
+++ b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/ProxyGrantingTicketStorageImpl.java
@@ -12,8 +12,6 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.jasig.cas.client.cleanup.CleanUpRegistry;
-import org.jasig.cas.client.cleanup.Cleanable;
/**
* Implementation of {@link ProxyGrantingTicketStorage} that is backed by a
@@ -28,7 +26,7 @@ import org.jasig.cas.client.cleanup.Cleanable;
* @since 3.0
*/
public final class ProxyGrantingTicketStorageImpl implements
- ProxyGrantingTicketStorage, Cleanable {
+ ProxyGrantingTicketStorage {
private final Log log = LogFactory.getLog(getClass());
@@ -100,10 +98,8 @@ public final class ProxyGrantingTicketStorageImpl implements
}
/**
- * Cleans up old, expired proxy tickets. This method should be
- * called regularly via a thread or timer.
- *
- * @see CleanUpRegistry#cleanAll()
+ * Cleans up old, expired proxy tickets. This method must be
+ * called regularly via an external thread or timer.
*/
public void cleanUp() {
synchronized (this.cache) {
diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/validation/Cas20ProxyReceivingTicketValidationFilter.java b/cas-client-core/src/main/java/org/jasig/cas/client/validation/Cas20ProxyReceivingTicketValidationFilter.java
index 9a5e0ee..d306338 100644
--- a/cas-client-core/src/main/java/org/jasig/cas/client/validation/Cas20ProxyReceivingTicketValidationFilter.java
+++ b/cas-client-core/src/main/java/org/jasig/cas/client/validation/Cas20ProxyReceivingTicketValidationFilter.java
@@ -21,12 +21,10 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.jasig.cas.client.cleanup.CleanUpRegistry;
-import org.jasig.cas.client.cleanup.CleanUpRegistryImpl;
-import org.jasig.cas.client.cleanup.Cleanable;
import org.jasig.cas.client.proxy.Cas20ProxyRetriever;
import org.jasig.cas.client.proxy.ProxyGrantingTicketStorage;
import org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl;
+import org.jasig.cas.client.proxy.CleanUpListener;
import org.jasig.cas.client.util.CommonUtils;
/**
@@ -49,16 +47,11 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
* The URL to send to the CAS server as the URL that will process proxying requests on the CAS client.
*/
private String proxyReceptorUrl;
-
- /**
- * a place to register implementations of {@link Cleanable}
- */
- private CleanUpRegistry cleanUpRegistry;
/**
* Storage location of ProxyGrantingTickets and Proxy Ticket IOUs.
*/
- private ProxyGrantingTicketStorage proxyGrantingTicketStorage;
+ private static ProxyGrantingTicketStorage proxyGrantingTicketStorage = new ProxyGrantingTicketStorageImpl();
protected void initInternal(final FilterConfig filterConfig) throws ServletException {
super.initInternal(filterConfig);
@@ -69,13 +62,7 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
public void init() {
super.init();
- if (this.cleanUpRegistry == null) {
- this.cleanUpRegistry = CleanUpRegistryImpl.getInstance();
- }
-
- this.proxyGrantingTicketStorage = newProxyGrantingTicketStorage(this.cleanUpRegistry);
-
- CommonUtils.assertNotNull(this.proxyGrantingTicketStorage, "proxyGrantingTicketStorage cannot be null.");
+ CommonUtils.assertNotNull(proxyGrantingTicketStorage, "proxyGrantingTicketStorage cannot be null.");
}
/**
@@ -99,7 +86,7 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
validator = new Cas20ServiceTicketValidator(casServerUrlPrefix);
}
validator.setProxyCallbackUrl(getPropertyFromInitParams(filterConfig, "proxyCallbackUrl", null));
- validator.setProxyGrantingTicketStorage(this.proxyGrantingTicketStorage);
+ validator.setProxyGrantingTicketStorage(proxyGrantingTicketStorage);
validator.setProxyRetriever(new Cas20ProxyRetriever(casServerUrlPrefix));
validator.setRenew(parseBoolean(getPropertyFromInitParams(filterConfig, "renew", "false")));
@@ -143,7 +130,7 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
return true;
}
- CommonUtils.readAndRespondToProxyReceptorRequest(request, response, this.proxyGrantingTicketStorage);
+ CommonUtils.readAndRespondToProxyReceptorRequest(request, response, proxyGrantingTicketStorage);
return false;
}
@@ -151,15 +138,17 @@ public class Cas20ProxyReceivingTicketValidationFilter extends AbstractTicketVal
public final void setProxyReceptorUrl(final String proxyReceptorUrl) {
this.proxyReceptorUrl = proxyReceptorUrl;
}
-
- protected final void setCleanUpRegistry(final CleanUpRegistry cleanUpRegistry) {
- this.cleanUpRegistry = cleanUpRegistry;
- }
- private ProxyGrantingTicketStorage newProxyGrantingTicketStorage(final CleanUpRegistry cleanUpRegistry) {
- ProxyGrantingTicketStorageImpl proxyGrantingTicketStorageImpl = new ProxyGrantingTicketStorageImpl();
- cleanUpRegistry.addCleanble(proxyGrantingTicketStorageImpl);
-
- return proxyGrantingTicketStorageImpl;
+ /**
+ * Static getter so {@link CleanUpListener} has some way of retrieving
+ * the actual storage. This relies on the fact that this class (the Filter)
+ * will only be instantiated once per webapp.
+ */
+ public static ProxyGrantingTicketStorage getProxyGrantingTicketStorage() {
+ return proxyGrantingTicketStorage;
+ }
+
+ public void setProxyGrantingTicketStorage(ProxyGrantingTicketStorage storage) {
+ proxyGrantingTicketStorage = storage;
}
}
diff --git a/cas-client-core/src/test/java/org/jasig/cas/client/cleanup/CleanUpJobTest.java b/cas-client-core/src/test/java/org/jasig/cas/client/cleanup/CleanUpJobTest.java
deleted file mode 100644
index 015bee6..0000000
--- a/cas-client-core/src/test/java/org/jasig/cas/client/cleanup/CleanUpJobTest.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package org.jasig.cas.client.cleanup;
-
-import org.jasig.cas.client.util.MethodFlag;
-
-import junit.framework.TestCase;
-
-/**
- * Unit test for {@link CleanUpJob}
- *
- * @author Brad Cupit (brad [at] lsu {dot} edu)
- */
-public class CleanUpJobTest extends TestCase {
- public void testExecuteInternal() throws Exception {
- final MethodFlag cleanAllMethodFlag = new MethodFlag();
-
- CleanUpRegistry localCleanUpRegistry = new CleanUpRegistry() {
- public void addCleanble(Cleanable cleanable) {
- }
-
- public void cleanAll() {
- cleanAllMethodFlag.setCalled();
- }
- };
-
- final CleanUpJob cleanUpJob = new CleanUpJob(localCleanUpRegistry);
- cleanUpJob.executeInternal(null);
-
- assertTrue(cleanAllMethodFlag.wasCalled());
- }
-}
diff --git a/cas-client-core/src/test/java/org/jasig/cas/client/cleanup/CleanUpRegistryImplTest.java b/cas-client-core/src/test/java/org/jasig/cas/client/cleanup/CleanUpRegistryImplTest.java
deleted file mode 100644
index 6eab4c4..0000000
--- a/cas-client-core/src/test/java/org/jasig/cas/client/cleanup/CleanUpRegistryImplTest.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package org.jasig.cas.client.cleanup;
-
-import org.jasig.cas.client.util.MethodFlag;
-
-import junit.framework.TestCase;
-
-/**
- * Unit test for {@link CleanUpRegistryImpl}
- *
- * @author Brad Cupit (brad [at] lsu {dot} edu)
- */
-public class CleanUpRegistryImplTest extends TestCase {
- private final CleanUpRegistry cleanUpRegistry = CleanUpRegistryImpl.getInstance();
-
- public void testCleanAll() throws Exception {
- final MethodFlag cleanUpMethodFlag = new MethodFlag();
-
- cleanUpRegistry.addCleanble(new Cleanable() {
- public void cleanUp() {
- cleanUpMethodFlag.setCalled();
- }
- });
-
- cleanUpRegistry.cleanAll();
-
- assertTrue(cleanUpMethodFlag.wasCalled());
- }
-}
diff --git a/cas-client-core/src/test/java/org/jasig/cas/client/cleanup/CleanUpListenerTest.java b/cas-client-core/src/test/java/org/jasig/cas/client/proxy/CleanUpListenerTest.java
similarity index 79%
rename from cas-client-core/src/test/java/org/jasig/cas/client/cleanup/CleanUpListenerTest.java
rename to cas-client-core/src/test/java/org/jasig/cas/client/proxy/CleanUpListenerTest.java
index 7e23c50..9ad916f 100644
--- a/cas-client-core/src/test/java/org/jasig/cas/client/cleanup/CleanUpListenerTest.java
+++ b/cas-client-core/src/test/java/org/jasig/cas/client/proxy/CleanUpListenerTest.java
@@ -1,4 +1,4 @@
-package org.jasig.cas.client.cleanup;
+package org.jasig.cas.client.proxy;
import java.util.Timer;
import java.util.TimerTask;
@@ -17,8 +17,8 @@ import org.springframework.mock.web.MockServletContext;
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public class CleanUpListenerTest extends TestCase {
- private final CleanUpRegistry defaultCleanUpRegistry = CleanUpRegistryImpl.getInstance();
private final Timer defaultTimer = new Timer(true);
+ private final CleanUpTimerTask defaultTimerTask = new CleanUpTimerTask();
public void testStartsThreadAtStartup() throws Exception {
final MethodFlag scheduleMethodFlag = new MethodFlag();
@@ -29,7 +29,7 @@ public class CleanUpListenerTest extends TestCase {
}
};
- final CleanUpListener cleanUpListener = new CleanUpListener(timer, defaultCleanUpRegistry);
+ final CleanUpListener cleanUpListener = new CleanUpListener(timer, defaultTimerTask);
cleanUpListener.contextInitialized(new TestServletContextEvent(1));
assertTrue(scheduleMethodFlag.wasCalled());
@@ -45,7 +45,7 @@ public class CleanUpListenerTest extends TestCase {
}
};
- final CleanUpListener cleanUpListener = new CleanUpListener(timer, defaultCleanUpRegistry);
+ final CleanUpListener cleanUpListener = new CleanUpListener(timer, defaultTimerTask);
cleanUpListener.contextInitialized(new TestServletContextEvent(1));
cleanUpListener.contextDestroyed(null);
@@ -53,51 +53,45 @@ public class CleanUpListenerTest extends TestCase {
}
public void testCallsCleanAllOnSchedule() throws Exception {
- final MethodFlag cleanAllMethodFlag = new MethodFlag();
+ final MethodFlag timerTaskFlag = new MethodFlag();
- final CleanUpRegistry cleanUpRegistry = new CleanUpRegistry() {
- public void addCleanble(Cleanable cleanable) {
- }
-
- public void cleanAll() {
- cleanAllMethodFlag.setCalled();
+ final TimerTask timerTask = new TimerTask() {
+ public void run() {
+ timerTaskFlag.setCalled();
}
};
long millisBetweenCleanUps = 250;
- final CleanUpListener cleanUpListener = new CleanUpListener(defaultTimer, cleanUpRegistry);
+ final CleanUpListener cleanUpListener = new CleanUpListener(defaultTimer, timerTask);
cleanUpListener.contextInitialized(new TestServletContextEvent(millisBetweenCleanUps));
// wait long enough for the clean up to occur
Thread.sleep(millisBetweenCleanUps * 2);
- assertTrue(cleanAllMethodFlag.wasCalled());
+ assertTrue(timerTaskFlag.wasCalled());
}
public void testDelaysFirstCleanAll() throws Exception {
- final MethodFlag cleanAllMethodFlag = new MethodFlag();
+ final MethodFlag timerTaskFlag = new MethodFlag();
- final CleanUpRegistry cleanUpRegistry = new CleanUpRegistry() {
- public void addCleanble(Cleanable cleanable) {
- }
-
- public void cleanAll() {
- cleanAllMethodFlag.setCalled();
+ final TimerTask timerTask = new TimerTask() {
+ public void run() {
+ timerTaskFlag.setCalled();
}
};
long millisBetweenCleanUps = 250;
- final CleanUpListener cleanUpListener = new CleanUpListener(defaultTimer, cleanUpRegistry);
+ final CleanUpListener cleanUpListener = new CleanUpListener(defaultTimer, timerTask);
cleanUpListener.contextInitialized(new TestServletContextEvent(millisBetweenCleanUps));
- assertFalse(cleanAllMethodFlag.wasCalled());
+ assertFalse(timerTaskFlag.wasCalled());
// wait long enough for the clean up to occur
Thread.sleep(millisBetweenCleanUps * 2);
- assertTrue(cleanAllMethodFlag.wasCalled());
+ assertTrue(timerTaskFlag.wasCalled());
}
public void testReturnsDefaultWhenNoContextParamConfigured() throws Exception {
diff --git a/cas-client-core/src/test/java/org/jasig/cas/client/proxy/CleanUpTimerTaskTest.java b/cas-client-core/src/test/java/org/jasig/cas/client/proxy/CleanUpTimerTaskTest.java
new file mode 100644
index 0000000..0cbeece
--- /dev/null
+++ b/cas-client-core/src/test/java/org/jasig/cas/client/proxy/CleanUpTimerTaskTest.java
@@ -0,0 +1,49 @@
+package org.jasig.cas.client.proxy;
+
+import java.util.TimerTask;
+
+import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit test for the {@link CleanUpTimerTask}
+ *
+ * @author Brad Cupit (brad [at] lsu {dot} edu)
+ */
+public class CleanUpTimerTaskTest extends TestCase {
+
+ public void testRun() throws Exception {
+ TimerTask timerTask = new CleanUpTimerTask();
+
+ ProxyGrantingTicketStorageTestImpl storage = new ProxyGrantingTicketStorageTestImpl();
+ new Cas20ProxyReceivingTicketValidationFilter().setProxyGrantingTicketStorage(storage);
+
+ timerTask.run();
+ assertTrue(storage.cleanUpWasCalled());
+ }
+
+ /**
+ * implementation of the storage interface used only for testing
+ *
+ * @author Brad Cupit (brad [at] lsu {dot} edu)
+ */
+ private static final class ProxyGrantingTicketStorageTestImpl implements ProxyGrantingTicketStorage {
+ private boolean cleanUpCalled = false;
+
+ public boolean cleanUpWasCalled() {
+ return cleanUpCalled;
+ }
+
+ public void cleanUp() {
+ cleanUpCalled = true;
+ }
+
+ public String retrieve(String proxyGrantingTicketIou) {
+ return null;
+ }
+
+ public void save(String proxyGrantingTicketIou, String proxyGrantingTicket) {
+ }
+ }
+}
diff --git a/cas-client-core/src/test/java/org/jasig/cas/client/validation/Cas20ProxyReceivingTicketValidationFilterTest.java b/cas-client-core/src/test/java/org/jasig/cas/client/validation/Cas20ProxyReceivingTicketValidationFilterTest.java
index e79c3f6..c6754fa 100644
--- a/cas-client-core/src/test/java/org/jasig/cas/client/validation/Cas20ProxyReceivingTicketValidationFilterTest.java
+++ b/cas-client-core/src/test/java/org/jasig/cas/client/validation/Cas20ProxyReceivingTicketValidationFilterTest.java
@@ -1,31 +1,32 @@
package org.jasig.cas.client.validation;
-import java.util.ArrayList;
-import java.util.List;
-
import junit.framework.TestCase;
-import org.jasig.cas.client.cleanup.CleanUpRegistry;
-import org.jasig.cas.client.cleanup.Cleanable;
-import org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl;
-
/**
* Unit test for {@link Cas20ProxyReceivingTicketValidationFilter}
*
* @author Brad Cupit (brad [at] lsu {dot} edu)
*/
public class Cas20ProxyReceivingTicketValidationFilterTest extends TestCase {
- public void testRegistersPGTicketStorageWithCleanUpRegistry() throws Exception {
- final TestCleanUpRegistry cleanUpRegistry = new TestCleanUpRegistry();
-
- final Cas20ProxyReceivingTicketValidationFilter filter = newCas20ProxyReceivingTicketValidationFilter();
- filter.setCleanUpRegistry(cleanUpRegistry);
- filter.init();
-
- assertEquals(1, cleanUpRegistry.getCleanables().size());
- assertSame(ProxyGrantingTicketStorageImpl.class, cleanUpRegistry.getCleanables().get(0).getClass());
+ public void testHasDefaultStorage() throws Exception {
+ assertNotNull(Cas20ProxyReceivingTicketValidationFilter.getProxyGrantingTicketStorage());
}
-
+
+ public void testThrowsForNullStorage() throws Exception {
+ Cas20ProxyReceivingTicketValidationFilter filter = newCas20ProxyReceivingTicketValidationFilter();
+ filter.setProxyGrantingTicketStorage(null);
+
+ try {
+ filter.init();
+ fail("expected an exception due to null ProxyGrantingTicketStorage");
+ } catch (IllegalArgumentException exception) {
+ // test passes
+ }
+ }
+
+ /**
+ * construct a working {@link Cas20ProxyReceivingTicketValidationFilter}
+ */
private Cas20ProxyReceivingTicketValidationFilter newCas20ProxyReceivingTicketValidationFilter() {
final Cas20ProxyReceivingTicketValidationFilter filter = new Cas20ProxyReceivingTicketValidationFilter();
filter.setServerName("localhost");
@@ -33,25 +34,4 @@ public class Cas20ProxyReceivingTicketValidationFilterTest extends TestCase {
return filter;
}
-
- /**
- * A test implementation of {@link CleanUpRegistry} that allows us to see
- * which {@link Cleanable}s were registered.
- *
- * @author Brad Cupit (brad [at] lsu {dot} edu)
- */
- private static final class TestCleanUpRegistry implements CleanUpRegistry {
- private final List cleanables = new ArrayList();
-
- public void addCleanble(Cleanable cleanable) {
- cleanables.add(cleanable);
- }
-
- public void cleanAll() {
- }
-
- public List getCleanables() {
- return cleanables;
- }
- };
}