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;
34 * Authorization service with persistent storage. This is a wrapper for
35 * {@link org.wamblee.security.authorization.DefaultAuthorizationService}
36 * which refreshes the state of the service at certain time intervals.
38 * @author Erik Brakkee
40 public class PersistentAuthorizationService extends AbstractPersistent
41 implements AuthorizationService {
43 * Name of query to find the service by name.
45 private static final String FIND_QUERY = "findAuthorizationServiceByName";
48 * Name of the query parameter for the service name.
50 private static final String NAME_PARAM = "name";
53 * Authorization service to use.
55 private DefaultAuthorizationService service;
58 * Hibernate template to use.
60 private HibernateTemplate template;
65 private UserAccessor userAccessor;
68 * Name of the service.
73 * Refresh interval in milliseconds.
75 private final long refreshInterval;
80 private long lastRefreshTime;
83 * Constructs the persistent service.
86 * Name of the service.
88 * Hibernate template for hibernate usage.
92 * Whether or not to refresh the state of the service at the
93 * start of every operation.
95 public PersistentAuthorizationService(String aName,
96 HibernateTemplate aTemplate, UserAccessor aAccessor,
97 long aRefreshInterval) {
99 refreshInterval = aRefreshInterval;
100 lastRefreshTime = System.currentTimeMillis();
101 userAccessor = aAccessor;
106 * Initialize service if needed.
108 * @throws IllegalArgumentException DOCUMENT ME!
110 private void initialize() {
111 if (service == null) {
112 List<DefaultAuthorizationService> result = template
113 .findByNamedQueryAndNamedParam(FIND_QUERY, NAME_PARAM, name);
115 if (result.size() > 1) {
116 throw new IllegalArgumentException(
117 "Returned more than one service for name '" + name + "' ("
118 + result.size() + ")");
121 if (result.size() == 0) {
122 service = new DefaultAuthorizationService(userAccessor, name);
123 template.persist(service);
125 service = result.get(0);
126 service.setUserAccessor(userAccessor);
134 * @see org.wamblee.security.authorization.AuthorizationService#isAllowed(java.lang.Object,
135 * org.wamblee.security.authorization.Operation)
140 * @param aResource DOCUMENT ME!
141 * @param aOperation DOCUMENT ME!
143 * @return DOCUMENT ME!
145 public boolean isAllowed(Object aResource, Operation aOperation) {
149 return service.isAllowed(aResource, aOperation);
153 * @see org.wamblee.security.authorization.AuthorizationService#check(T, org.wamblee.security.authorization.Operation)
158 * @param <T> DOCUMENT ME!
159 * @param aResource DOCUMENT ME!
160 * @param aOperation DOCUMENT ME!
162 * @return DOCUMENT ME!
164 public <T> T check(T aResource, Operation aOperation) {
168 return service.check(aResource, aOperation);
174 * @see org.wamblee.security.authorization.AuthorizationService#getRules()
179 * @return DOCUMENT ME!
181 public AuthorizationRule[] getRules() {
185 return service.getRules();
191 * @see org.wamblee.security.authorization.AuthorizationService#appendRule(org.wamblee.security.authorization.AuthorizationRule)
196 * @param aRule DOCUMENT ME!
198 public void appendRule(AuthorizationRule aRule) {
201 service.appendRule(aRule);
208 * @see org.wamblee.security.authorization.AuthorizationService#removeRule(int)
213 * @param aIndex DOCUMENT ME!
215 public void removeRule(int aIndex) {
218 service.removeRule(aIndex);
225 * @see org.wamblee.security.authorization.AuthorizationService#insertRuleAfter(int,
226 * org.wamblee.security.authorization.AuthorizationRule)
231 * @param aIndex DOCUMENT ME!
232 * @param aRule DOCUMENT ME!
234 public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
237 service.insertRuleAfter(aIndex, aRule);
242 * Refreshes the state of the service through hibernate.
244 private synchronized void refresh() {
245 long time = System.currentTimeMillis();
247 if ((time - lastRefreshTime) > refreshInterval) {
248 template.refresh(service);
249 lastRefreshTime = time;
254 * Saves any changes to the service state if necessary.
256 private void save() {
257 HibernateSupport.merge(template, service);