2 * Copyright 2005-2010 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 java.util.List;
20 import org.springframework.orm.hibernate3.HibernateTemplate;
21 import org.wamblee.security.authorization.AuthorizationRule;
22 import org.wamblee.security.authorization.AuthorizationService;
23 import org.wamblee.security.authorization.DefaultAuthorizationService;
24 import org.wamblee.security.authorization.Operation;
25 import org.wamblee.usermgt.UserAccessor;
28 * Authorization service with persistent storage. This is a wrapper for
29 * {@link org.wamblee.security.authorization.DefaultAuthorizationService} which
30 * refreshes the state of the service at certain time intervals.
32 * @author Erik Brakkee
34 public class PersistentAuthorizationService extends AuthorizationService {
36 * Name of query to find the service by name.
38 private static final String FIND_QUERY = "findAuthorizationServiceByName";
41 * Name of the query parameter for the service name.
43 private static final String NAME_PARAM = "name";
46 * Authorization service to use.
48 private DefaultAuthorizationService service;
51 * Hibernate template to use.
53 private HibernateTemplate template;
58 private UserAccessor userAccessor;
61 * Name of the service.
66 * Refresh interval in milliseconds.
68 private final long refreshInterval;
73 private long lastRefreshTime;
76 * Constructs the persistent service.
79 * Name of the service.
81 * Hibernate template for hibernate usage.
85 * Whether or not to refresh the state of the service at the
86 * start of every operation.
88 public PersistentAuthorizationService(String aName,
89 HibernateTemplate aTemplate, UserAccessor aAccessor,
90 long aRefreshInterval) {
92 refreshInterval = aRefreshInterval;
93 lastRefreshTime = System.currentTimeMillis();
94 userAccessor = aAccessor;
99 public void setUserAccessor(UserAccessor aUserAccessor) {
100 userAccessor = aUserAccessor;
104 * Initialize service if needed.
107 private void initialize() {
108 if (service == null) {
109 List<DefaultAuthorizationService> result = template
110 .findByNamedQueryAndNamedParam(FIND_QUERY, NAME_PARAM, name);
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);
132 * org.wamblee.security.authorization.AuthorizationService#isAllowed(java
133 * .lang.Object, org.wamblee.security.authorization.Operation)
135 public boolean isAllowed(Object aResource, Operation aOperation) {
139 return service.isAllowed(aResource, aOperation);
145 * @see org.wamblee.security.authorization.AuthorizationService#check(T,
146 * org.wamblee.security.authorization.Operation)
148 public <T> T check(T aResource, Operation aOperation) {
152 return service.check(aResource, aOperation);
158 * @see org.wamblee.security.authorization.AuthorizationService#getRules()
160 public AuthorizationRule[] getRules() {
164 return service.getRules();
171 * org.wamblee.security.authorization.AuthorizationService#appendRule(org
172 * .wamblee.security.authorization.AuthorizationRule)
174 public void appendRule(AuthorizationRule aRule) {
177 service.appendRule(aRule);
185 * org.wamblee.security.authorization.AuthorizationService#removeRule(int)
187 public void removeRule(int aIndex) {
190 service.removeRule(aIndex);
198 * org.wamblee.security.authorization.AuthorizationService#insertRuleAfter
199 * (int, org.wamblee.security.authorization.AuthorizationRule)
201 public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
204 service.insertRuleAfter(aIndex, aRule);
209 * Refreshes the state of the service through hibernate.
211 private synchronized void refresh() {
212 long time = System.currentTimeMillis();
214 if ((time - lastRefreshTime) > refreshInterval) {
215 template.refresh(service);
216 lastRefreshTime = time;
221 * Saves any changes to the service state if necessary.
223 private void save() {
224 // HibernateSupport.merge(template, service);