X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=security%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsecurity%2Fauthorization%2Fhibernate%2FPersistentAuthorizationService.java;h=cd8995f4586b2fda22c8d7122c7dc5822f386650;hb=8de36ff0206c996baf3ee4adc3e2293b12ff5f39;hp=895e77e642d9129bcb24478ddebd6695179c15ca;hpb=0d8d8f24656e585ee75558cfd6a4c661f8f14985;p=utils diff --git a/security/src/main/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationService.java b/security/src/main/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationService.java index 895e77e6..cd8995f4 100644 --- a/security/src/main/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationService.java +++ b/security/src/main/java/org/wamblee/security/authorization/hibernate/PersistentAuthorizationService.java @@ -1,42 +1,43 @@ /* * Copyright 2005 the original author or authors. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ - package org.wamblee.security.authorization.hibernate; -import java.util.List; - import org.springframework.orm.hibernate3.HibernateTemplate; + import org.wamblee.persistence.AbstractPersistent; import org.wamblee.persistence.hibernate.HibernateSupport; + import org.wamblee.security.authorization.AuthorizationRule; import org.wamblee.security.authorization.AuthorizationService; import org.wamblee.security.authorization.DefaultAuthorizationService; import org.wamblee.security.authorization.Operation; + import org.wamblee.usermgt.UserAccessor; +import java.util.List; + /** - * Authorization service with persistent storage. - * This is a wrapper for {@link org.wamblee.security.authorization.DefaultAuthorizationService} - * which refreshes the state of the service at certain time intervals. - * + * Authorization service with persistent storage. This is a wrapper for + * {@link org.wamblee.security.authorization.DefaultAuthorizationService} which + * refreshes the state of the service at certain time intervals. + * * @author Erik Brakkee */ public class PersistentAuthorizationService extends AbstractPersistent - implements AuthorizationService { - + implements AuthorizationService { /** * Name of query to find the service by name. */ @@ -61,21 +62,21 @@ public class PersistentAuthorizationService extends AbstractPersistent * User accessor. */ private UserAccessor userAccessor; - + /** - * Name of the service. + * Name of the service. */ - private String name; + private String name; /** - * Refresh interval in milliseconds. + * Refresh interval in milliseconds. */ private final long refreshInterval; - + /** - * Last refresh time. + * Last refresh time. */ - private long lastRefreshTime; + private long lastRefreshTime; /** * Constructs the persistent service. @@ -91,28 +92,28 @@ public class PersistentAuthorizationService extends AbstractPersistent * start of every operation. */ public PersistentAuthorizationService(String aName, - HibernateTemplate aTemplate, UserAccessor aAccessor, - long aRefreshInterval) { + HibernateTemplate aTemplate, UserAccessor aAccessor, + long aRefreshInterval) { template = aTemplate; refreshInterval = aRefreshInterval; - lastRefreshTime = System.currentTimeMillis(); + lastRefreshTime = System.currentTimeMillis(); userAccessor = aAccessor; - name = aName; + name = aName; } /** - * Initialize service if needed. + * Initialize service if needed. + * */ private void initialize() { if (service == null) { List result = template - .findByNamedQueryAndNamedParam(FIND_QUERY, NAME_PARAM, - name); + .findByNamedQueryAndNamedParam(FIND_QUERY, NAME_PARAM, name); if (result.size() > 1) { throw new IllegalArgumentException( - "Returned more than one service for name '" + name - + "' (" + result.size() + ")"); + "Returned more than one service for name '" + name + "' (" + + result.size() + ")"); } if (result.size() == 0) { @@ -128,21 +129,27 @@ public class PersistentAuthorizationService extends AbstractPersistent /* * (non-Javadoc) * - * @see org.wamblee.security.authorization.AuthorizationService#isAllowed(java.lang.Object, - * org.wamblee.security.authorization.Operation) + * @see + * org.wamblee.security.authorization.AuthorizationService#isAllowed(java + * .lang.Object, org.wamblee.security.authorization.Operation) */ public boolean isAllowed(Object aResource, Operation aOperation) { initialize(); refresh(); + return service.isAllowed(aResource, aOperation); } - - /* (non-Javadoc) - * @see org.wamblee.security.authorization.AuthorizationService#check(T, org.wamblee.security.authorization.Operation) + + /* + * (non-Javadoc) + * + * @see org.wamblee.security.authorization.AuthorizationService#check(T, + * org.wamblee.security.authorization.Operation) */ public T check(T aResource, Operation aOperation) { initialize(); refresh(); + return service.check(aResource, aOperation); } @@ -154,13 +161,16 @@ public class PersistentAuthorizationService extends AbstractPersistent public AuthorizationRule[] getRules() { initialize(); refresh(); + return service.getRules(); } /* * (non-Javadoc) * - * @see org.wamblee.security.authorization.AuthorizationService#appendRule(org.wamblee.security.authorization.AuthorizationRule) + * @see + * org.wamblee.security.authorization.AuthorizationService#appendRule(org + * .wamblee.security.authorization.AuthorizationRule) */ public void appendRule(AuthorizationRule aRule) { initialize(); @@ -172,7 +182,8 @@ public class PersistentAuthorizationService extends AbstractPersistent /* * (non-Javadoc) * - * @see org.wamblee.security.authorization.AuthorizationService#removeRule(int) + * @see + * org.wamblee.security.authorization.AuthorizationService#removeRule(int) */ public void removeRule(int aIndex) { initialize(); @@ -184,8 +195,9 @@ public class PersistentAuthorizationService extends AbstractPersistent /* * (non-Javadoc) * - * @see org.wamblee.security.authorization.AuthorizationService#insertRuleAfter(int, - * org.wamblee.security.authorization.AuthorizationRule) + * @see + * org.wamblee.security.authorization.AuthorizationService#insertRuleAfter + * (int, org.wamblee.security.authorization.AuthorizationRule) */ public void insertRuleAfter(int aIndex, AuthorizationRule aRule) { initialize(); @@ -196,13 +208,13 @@ public class PersistentAuthorizationService extends AbstractPersistent /** * Refreshes the state of the service through hibernate. - * */ private synchronized void refresh() { - long time = System.currentTimeMillis(); - if ( time - lastRefreshTime > refreshInterval ) { + long time = System.currentTimeMillis(); + + if ((time - lastRefreshTime) > refreshInterval) { template.refresh(service); - lastRefreshTime = time; + lastRefreshTime = time; } }