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.AbstractAuthorizationRule;
22 import org.wamblee.security.authorization.AbstractAuthorizationService;
23 import org.wamblee.security.authorization.AuthorizationRule;
24 import org.wamblee.security.authorization.DefaultAuthorizationService;
25 import org.wamblee.security.authorization.Operation;
26 import org.wamblee.usermgt.UserAccessor;
29 * Authorization service with persistent storage. This is a wrapper for
30 * {@link org.wamblee.security.authorization.DefaultAuthorizationService} which
31 * refreshes the state of the service at certain time intervals.
33 * @author Erik Brakkee
35 public class PersistentAuthorizationService extends AbstractAuthorizationService {
37 * Name of query to find the service by name.
39 private static final String FIND_QUERY = "findAuthorizationServiceByName";
42 * Name of the query parameter for the service name.
44 private static final String NAME_PARAM = "name";
47 * Authorization service to use.
49 private DefaultAuthorizationService service;
52 * Hibernate template to use.
54 private HibernateTemplate template;
59 private UserAccessor userAccessor;
62 * Name of the service.
67 * Refresh interval in milliseconds.
69 private final long refreshInterval;
74 private long lastRefreshTime;
77 * Constructs the persistent service.
80 * Name of the service.
82 * Hibernate template for hibernate usage.
86 * Whether or not to refresh the state of the service at the
87 * start of every operation.
89 public PersistentAuthorizationService(String aName,
90 HibernateTemplate aTemplate, UserAccessor aAccessor,
91 long aRefreshInterval) {
93 refreshInterval = aRefreshInterval;
94 lastRefreshTime = System.currentTimeMillis();
95 userAccessor = aAccessor;
100 public void setUserAccessor(UserAccessor aUserAccessor) {
101 userAccessor = aUserAccessor;
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);