All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hudson.plugins.sfee.Memoizer Maven / Gradle / Ivy

Go to download

This plugin integrates Hudson with a Source Forge Enterprise Edition (SFEE) Server This integration allows you to use SFEE database to perform user authentication, and to publish build artifacts to a SFEE project's release folder

The newest version!
package hudson.plugins.sfee;

import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Memoizer implements Computable {
	private final ConcurrentMap> cache = new ConcurrentHashMap>();
	private final Computable c;

	public Memoizer(Computable c) {
		this.c = c;
	}

	public V compute(final A arg) throws InterruptedException {
		while (true) {
			ExpiringFutureTask f = cache.get(arg);
			if (f != null && f.hasExpired()) {
				cache.remove(arg);
				f = null;
			}
			if (f == null) {
				Callable eval = new Callable() {
					public V call() throws InterruptedException {
						return c.compute(arg);
					}
				};
				ExpiringFutureTask ft = new ExpiringFutureTask(eval);
				f = cache.putIfAbsent(arg, ft);
				if (f == null) {
					f = ft;
					ft.run();
				}
			}
			try {
				return f.get();
			} catch (CancellationException e) {
				cache.remove(arg, f);
			} catch (ExecutionException e) {
				// Kabutz: this is my addition to the code...
				try {
					cache.remove(arg, f);
					throw e.getCause();
				} catch (RuntimeException ex) {
					throw ex;
				} catch (Error ex) {
					throw ex;
				} catch (Throwable t) {
					throw new IllegalStateException("Not unchecked", t);
				}
			}
		}
	}

	private static class ExpiringFutureTask extends FutureTask {
		private long creationTime = System.currentTimeMillis();

		public ExpiringFutureTask(Callable callable) {
			super(callable);
		}

		public boolean hasExpired() {
			return System.currentTimeMillis() - creationTime > 60000;
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy