package cf.spring;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.FactoryBean;

import cf.client.CloudController;
import cf.client.Token;
import cf.client.TokenProvider;
import cf.client.Uaa;

/**
 * Does a client authentication with UAA to get a valid token. The token should be used with the
 * {@link javax.inject.Provider} interface so that a fresh token gets fetched after the token expires.
 *
 * @author Mike Heath <elcapo@gmail.com>
 */
public class ClientTokenProviderFactoryBean implements FactoryBean<TokenProvider> {

	private final CloudController cloudController;
	private final String client;
	private final String clientSecret;

	private final TokenProvider tokenProvider = new TokenProvider() {
		@Override
		public Token get() {
			return fetchToken();
		}
	};

	private Token token;

	private Token fetchToken() {
		synchronized (this) {
			if (token == null || token.getExpiration().after(new Date(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(10)))) {
				final Uaa uaa = cloudController.getUaa();
				token = uaa.getClientToken(client, clientSecret);
			}
			return token;
		}
	}

	public ClientTokenProviderFactoryBean(CloudController cloudController, String client, String clientSecret) {
		this.cloudController = cloudController;
		this.client = client;
		this.clientSecret = clientSecret;
	}

	@Override
	public TokenProvider getObject() throws Exception {
		return tokenProvider;
	}

	@Override
	public Class<?> getObjectType() {
		return TokenProvider.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}

@@ -17,31 +17,8 @@
 g;%0A%0A
-import java.util.Date;%0A
 impo
@@ -934,47 +934,15 @@
 n().
-after(new Date(System.currentTimeMillis
+getTime
 () -
@@ -971,17 +971,44 @@
 llis(10)
-)
+ %3C System.currentTimeMillis(
 )) %7B%0A%09%09%09

Fix expiration buffere calculations.