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 public class PersistentAuthorizationService extends AbstractPersistent
36 implements AuthorizationService {
39 * Name of query to find the service by name.
41 private static final String FIND_QUERY = "findAuthorizationServiceByName";
44 * Name of the query parameter for the service name.
46 private static final String NAME_PARAM = "name";
49 * Authorization service to use.
51 private DefaultAuthorizationService _service;
54 * Hibernate template to use.
56 private HibernateTemplate _template;
61 private UserAccessor _userAccessor;
64 * Name of the service.
69 * Refresh interval in milliseconds.
71 private final long _refreshInterval;
76 private long _lastRefreshTime;
79 * Constructs the persistent service.
82 * Name of the service.
84 * Hibernate template for hibernate usage.
88 * Whether or not to refresh the state of the service at the
89 * start of every operation.
91 public PersistentAuthorizationService(String aName,
92 HibernateTemplate aTemplate, UserAccessor aAccessor,
93 long aRefreshInterval) {
94 _template = aTemplate;
95 _refreshInterval = aRefreshInterval;
96 _lastRefreshTime = System.currentTimeMillis();
97 _userAccessor = aAccessor;
102 * Initialize service if needed.
104 private void initialize() {
105 if (_service == null) {
106 List<DefaultAuthorizationService> result = _template
107 .findByNamedQueryAndNamedParam(FIND_QUERY, NAME_PARAM,
110 if (result.size() > 1) {
111 throw new IllegalArgumentException(
112 "Returned more than one service for name '" + _name
113 + "' (" + result.size() + ")");
116 if (result.size() == 0) {
117 _service = new DefaultAuthorizationService(_userAccessor, _name);
118 _template.persist(_service);
120 _service = result.get(0);
121 _service.setUserAccessor(_userAccessor);
129 * @see org.wamblee.security.authorization.AuthorizationService#isAllowed(java.lang.Object,
130 * org.wamblee.security.authorization.Operation)
132 public boolean isAllowed(Object aResource, Operation aOperation) {
135 return _service.isAllowed(aResource, aOperation);
139 * @see org.wamblee.security.authorization.AuthorizationService#check(T, org.wamblee.security.authorization.Operation)
141 public <T> T check(T aResource, Operation aOperation) {
144 return _service.check(aResource, aOperation);
150 * @see org.wamblee.security.authorization.AuthorizationService#getRules()
152 public AuthorizationRule[] getRules() {
155 return _service.getRules();
161 * @see org.wamblee.security.authorization.AuthorizationService#appendRule(org.wamblee.security.authorization.AuthorizationRule)
163 public void appendRule(AuthorizationRule aRule) {
166 _service.appendRule(aRule);
173 * @see org.wamblee.security.authorization.AuthorizationService#removeRule(int)
175 public void removeRule(int aIndex) {
178 _service.removeRule(aIndex);
185 * @see org.wamblee.security.authorization.AuthorizationService#insertRuleAfter(int,
186 * org.wamblee.security.authorization.AuthorizationRule)
188 public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
191 _service.insertRuleAfter(aIndex, aRule);
196 * Refreshes the state of the service through hibernate.
199 private synchronized void refresh() {
200 long time = System.currentTimeMillis();
201 if ( time - _lastRefreshTime > _refreshInterval ) {
202 _template.refresh(_service);
203 _lastRefreshTime = time;
208 * Saves any changes to the service state if necessary.
210 private void save() {
211 HibernateSupport.merge(_template, _service);