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.
16 package org.wamblee.security.authorization.hibernate;
18 import org.springframework.orm.hibernate3.HibernateTemplate;
20 import org.wamblee.persistence.AbstractPersistent;
21 import org.wamblee.persistence.hibernate.HibernateSupport;
23 import org.wamblee.security.authorization.AuthorizationRule;
24 import org.wamblee.security.authorization.AuthorizationService;
25 import org.wamblee.security.authorization.DefaultAuthorizationService;
26 import org.wamblee.security.authorization.Operation;
28 import org.wamblee.usermgt.UserAccessor;
30 import java.util.List;
33 * Authorization service with persistent storage. This is a wrapper for
34 * {@link org.wamblee.security.authorization.DefaultAuthorizationService} which
35 * refreshes the state of the service at certain time intervals.
37 * @author Erik Brakkee
39 public class PersistentAuthorizationService extends AbstractPersistent
40 implements AuthorizationService {
42 * Name of query to find the service by name.
44 private static final String FIND_QUERY = "findAuthorizationServiceByName";
47 * Name of the query parameter for the service name.
49 private static final String NAME_PARAM = "name";
52 * Authorization service to use.
54 private DefaultAuthorizationService service;
57 * Hibernate template to use.
59 private HibernateTemplate template;
64 private UserAccessor userAccessor;
67 * Name of the service.
72 * Refresh interval in milliseconds.
74 private final long refreshInterval;
79 private long lastRefreshTime;
82 * Constructs the persistent service.
85 * Name of the service.
87 * Hibernate template for hibernate usage.
91 * Whether or not to refresh the state of the service at the
92 * start of every operation.
94 public PersistentAuthorizationService(String aName,
95 HibernateTemplate aTemplate, UserAccessor aAccessor,
96 long aRefreshInterval) {
98 refreshInterval = aRefreshInterval;
99 lastRefreshTime = System.currentTimeMillis();
100 userAccessor = aAccessor;
105 * Initialize service if needed.
108 private void initialize() {
109 if (service == null) {
110 List<DefaultAuthorizationService> result = template
111 .findByNamedQueryAndNamedParam(FIND_QUERY, NAME_PARAM, name);
113 if (result.size() > 1) {
114 throw new IllegalArgumentException(
115 "Returned more than one service for name '" + name + "' (" +
116 result.size() + ")");
119 if (result.size() == 0) {
120 service = new DefaultAuthorizationService(userAccessor, name);
121 template.persist(service);
123 service = result.get(0);
124 service.setUserAccessor(userAccessor);
133 * org.wamblee.security.authorization.AuthorizationService#isAllowed(java
134 * .lang.Object, org.wamblee.security.authorization.Operation)
136 public boolean isAllowed(Object aResource, Operation aOperation) {
140 return service.isAllowed(aResource, aOperation);
146 * @see org.wamblee.security.authorization.AuthorizationService#check(T,
147 * org.wamblee.security.authorization.Operation)
149 public <T> T check(T aResource, Operation aOperation) {
153 return service.check(aResource, aOperation);
159 * @see org.wamblee.security.authorization.AuthorizationService#getRules()
161 public AuthorizationRule[] getRules() {
165 return service.getRules();
172 * org.wamblee.security.authorization.AuthorizationService#appendRule(org
173 * .wamblee.security.authorization.AuthorizationRule)
175 public void appendRule(AuthorizationRule aRule) {
178 service.appendRule(aRule);
186 * org.wamblee.security.authorization.AuthorizationService#removeRule(int)
188 public void removeRule(int aIndex) {
191 service.removeRule(aIndex);
199 * org.wamblee.security.authorization.AuthorizationService#insertRuleAfter
200 * (int, org.wamblee.security.authorization.AuthorizationRule)
202 public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
205 service.insertRuleAfter(aIndex, aRule);
210 * Refreshes the state of the service through hibernate.
212 private synchronized void refresh() {
213 long time = System.currentTimeMillis();
215 if ((time - lastRefreshTime) > refreshInterval) {
216 template.refresh(service);
217 lastRefreshTime = time;
222 * Saves any changes to the service state if necessary.
224 private void save() {
225 HibernateSupport.merge(template, service);