import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
+import org.wamblee.cache.ComputedValue;
+import org.wamblee.cache.ComputedValue.Computation;
import org.wamblee.persistence.JpaMergeSupport;
import org.wamblee.security.authentication.UserAccessor;
import org.wamblee.security.authentication.UserAdministration;
/**
* Authorization service to use.
*/
- private AuthorizationService service;
+ private ComputedValue<AuthorizationService> service;
/**
* Hibernate template to use.
* User accessor.
*/
private UserAccessor userAccessor;
-
+
/**
* User administration.
*/
* Entity manager.
* @param aAccessor
* User accessor.
- * @param aUserAdmin User administration.
+ * @param aUserAdmin
+ * User administration.
* @param aRefresh
* Whether or not to refresh the state of the service at the
* start of every operation.
*/
public JpaAuthorizationService(String aName, EntityManager aEntityManager,
- UserAccessor aAccessor, UserAdministration aUserAdmin, long aRefreshInterval) {
+ UserAccessor aAccessor, UserAdministration aUserAdmin,
+ long aRefreshInterval) {
entityManager = aEntityManager;
refreshInterval = aRefreshInterval;
lastRefreshTime = System.currentTimeMillis();
userAccessor = aAccessor;
userAdmin = aUserAdmin;
name = aName;
+ Computation<AuthorizationService> computation = new Computation<AuthorizationService>() {
+ @Override
+ public boolean isOutOfDate() {
+ long time = System.currentTimeMillis();
+ return service.getCached() == null ||
+ (time - lastRefreshTime) > refreshInterval;
+ }
+
+ @Override
+ public AuthorizationService compute() {
+ AuthorizationService svc = refreshByReload();
+ lastRefreshTime = System.currentTimeMillis();
+ return svc;
+ }
+ };
+ service = new ComputedValue<AuthorizationService>(this, computation);
}
-
+
@Override
public void setUserAccessor(UserAccessor aUserAccessor) {
- userAccessor = aUserAccessor;
+ userAccessor = aUserAccessor;
}
-
+
@Override
public void setUserAdministration(UserAdministration aUserAdmin) {
- userAdmin = aUserAdmin;
+ userAdmin = aUserAdmin;
}
private AuthorizationService refreshByReload() {
- if ( userAdmin == null ) {
- throw new IllegalArgumentException("useradmin is null");
- }
- AuthorizationService svc;
+ AuthorizationService svc;
try {
svc = entityManager.createNamedQuery(
AbstractAuthorizationService.QUERY_FIND_BY_NAME,
AbstractAuthorizationService.class).setParameter(
- DefaultAuthorizationService.NAME_PARAM, name).getSingleResult();
+ DefaultAuthorizationService.NAME_PARAM, name).getSingleResult();
svc.setUserAccessor(userAccessor);
svc.setUserAdministration(userAdmin);
} catch (NonUniqueResultException e) {
* .lang.Object, org.wamblee.security.authorization.Operation)
*/
public boolean isAllowed(Object aResource, Operation aOperation) {
- refresh();
-
- return service.isAllowed(aResource, aOperation);
+ return service.get().isAllowed(aResource, aOperation);
}
/*
* org.wamblee.security.authorization.Operation)
*/
public <T> T check(T aResource, Operation aOperation) {
- refresh();
-
- return service.check(aResource, aOperation);
+ return service.get().check(aResource, aOperation);
}
/*
* @see org.wamblee.security.authorization.AuthorizationService#getRules()
*/
public AuthorizationRule[] getRules() {
- refresh();
-
- return service.getRules();
+ return service.get().getRules();
}
/*
public void appendRule(AuthorizationRule aRule) {
AuthorizationService svc = refreshByReload();
svc.appendRule(aRule);
- // Setting service to null will force reload the next time the
- // service is used. This deals effectively with the case where the
- // current transaction would roll back and the change would not have been made.
- setService(null);
- }
-
- private synchronized AuthorizationService getService() {
- return service;
- }
-
- private synchronized void setService(AuthorizationService aSvc) {
- service = aSvc;
+ // Setting service to null will force reload the next time the
+ // service is used. This deals effectively with the case where the
+ // current transaction would roll back and the change would not have
+ // been made.
+ service.set(null);
}
/*
* org.wamblee.security.authorization.AuthorizationService#removeRule(int)
*/
public void removeRule(int aIndex) {
- AuthorizationService svc = refreshByReload();
+ AuthorizationService svc = refreshByReload();
svc.removeRule(aIndex);
- setService(null);
+ service.set(null);
}
/*
* (int, org.wamblee.security.authorization.AuthorizationRule)
*/
public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
- AuthorizationService svc = refreshByReload();
+ AuthorizationService svc = refreshByReload();
svc.insertRuleAfter(aIndex, aRule);
- setService(null);
- }
-
- /**
- * Refreshes the state of the service through hibernate.
- */
- private synchronized void refresh() {
- long time = System.currentTimeMillis();
-
- if (service == null || (time - lastRefreshTime) > refreshInterval) {
- service = refreshByReload();
- lastRefreshTime = time;
- }
+ service.set(null);
}
}