2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.security.authorization.hibernate;
19 import java.util.List;
21 import org.springframework.orm.hibernate3.HibernateTemplate;
22 import org.wamblee.persistence.AbstractPersistent;
23 import org.wamblee.persistence.hibernate.HibernateSupport;
24 import org.wamblee.security.authorization.AuthorizationRule;
25 import org.wamblee.security.authorization.AuthorizationService;
26 import org.wamblee.security.authorization.DefaultAuthorizationService;
27 import org.wamblee.security.authorization.Operation;
28 import org.wamblee.usermgt.UserAccessor;
31 * Authorization service with persistent storage.
32 * This is a wrapper for {@link org.wamblee.security.authorization.DefaultAuthorizationService}
33 * which refreshes the state of the service at certain time intervals.
35 * @author Erik Brakkee
37 public class PersistentAuthorizationService extends AbstractPersistent
38 implements AuthorizationService {
41 * Name of query to find the service by name.
43 private static final String FIND_QUERY = "findAuthorizationServiceByName";
46 * Name of the query parameter for the service name.
48 private static final String NAME_PARAM = "name";
51 * Authorization service to use.
53 private DefaultAuthorizationService _service;
56 * Hibernate template to use.
58 private HibernateTemplate _template;
63 private UserAccessor _userAccessor;
66 * Name of the service.
71 * Refresh interval in milliseconds.
73 private final long _refreshInterval;
78 private long _lastRefreshTime;
81 * Constructs the persistent service.
84 * Name of the service.
86 * Hibernate template for hibernate usage.
90 * Whether or not to refresh the state of the service at the
91 * start of every operation.
93 public PersistentAuthorizationService(String aName,
94 HibernateTemplate aTemplate, UserAccessor aAccessor,
95 long aRefreshInterval) {
96 _template = aTemplate;
97 _refreshInterval = aRefreshInterval;
98 _lastRefreshTime = System.currentTimeMillis();
99 _userAccessor = aAccessor;
104 * Initialize service if needed.
106 private void initialize() {
107 if (_service == null) {
108 List<DefaultAuthorizationService> result = _template
109 .findByNamedQueryAndNamedParam(FIND_QUERY, NAME_PARAM,
112 if (result.size() > 1) {
113 throw new IllegalArgumentException(
114 "Returned more than one service for name '" + _name
115 + "' (" + result.size() + ")");
118 if (result.size() == 0) {
119 _service = new DefaultAuthorizationService(_userAccessor, _name);
120 _template.persist(_service);
122 _service = result.get(0);
123 _service.setUserAccessor(_userAccessor);
131 * @see org.wamblee.security.authorization.AuthorizationService#isAllowed(java.lang.Object,
132 * org.wamblee.security.authorization.Operation)
134 public boolean isAllowed(Object aResource, Operation aOperation) {
137 return _service.isAllowed(aResource, aOperation);
141 * @see org.wamblee.security.authorization.AuthorizationService#check(T, org.wamblee.security.authorization.Operation)
143 public <T> T check(T aResource, Operation aOperation) {
146 return _service.check(aResource, aOperation);
152 * @see org.wamblee.security.authorization.AuthorizationService#getRules()
154 public AuthorizationRule[] getRules() {
157 return _service.getRules();
163 * @see org.wamblee.security.authorization.AuthorizationService#appendRule(org.wamblee.security.authorization.AuthorizationRule)
165 public void appendRule(AuthorizationRule aRule) {
168 _service.appendRule(aRule);
175 * @see org.wamblee.security.authorization.AuthorizationService#removeRule(int)
177 public void removeRule(int aIndex) {
180 _service.removeRule(aIndex);
187 * @see org.wamblee.security.authorization.AuthorizationService#insertRuleAfter(int,
188 * org.wamblee.security.authorization.AuthorizationRule)
190 public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
193 _service.insertRuleAfter(aIndex, aRule);
198 * Refreshes the state of the service through hibernate.
201 private synchronized void refresh() {
202 long time = System.currentTimeMillis();
203 if ( time - _lastRefreshTime > _refreshInterval ) {
204 _template.refresh(_service);
205 _lastRefreshTime = time;
210 * Saves any changes to the service state if necessary.
212 private void save() {
213 HibernateSupport.merge(_template, _service);