source code formatting.
[utils] / security / src / main / java / org / wamblee / security / authorization / hibernate / PersistentAuthorizationService.java
index dfe2439caa1a930a491e8178525c9db658268c4e..caf69903d72802d48dd142b4c61411ceb9aba9eb 100644 (file)
@@ -1,42 +1,44 @@
 /*
  * Copyright 2005 the original author or authors.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.wamblee.security.authorization.hibernate;
 
-import java.util.List;
-
 import org.springframework.orm.hibernate3.HibernateTemplate;
+
 import org.wamblee.persistence.AbstractPersistent;
 import org.wamblee.persistence.hibernate.HibernateSupport;
+
 import org.wamblee.security.authorization.AuthorizationRule;
 import org.wamblee.security.authorization.AuthorizationService;
 import org.wamblee.security.authorization.DefaultAuthorizationService;
 import org.wamblee.security.authorization.Operation;
+
 import org.wamblee.usermgt.UserAccessor;
 
+import java.util.List;
+
+
 /**
- * Authorization service with persistent storage.
- * This is a wrapper for {@link org.wamblee.security.authorization.DefaultAuthorizationService}
- * which refreshes the state of the service at certain time intervals. 
+ * Authorization service with persistent storage. This is a wrapper for
+ * {@link org.wamblee.security.authorization.DefaultAuthorizationService}
+ * which refreshes the state of the service at certain time intervals.
  *
  * @author Erik Brakkee
  */
 public class PersistentAuthorizationService extends AbstractPersistent
-        implements AuthorizationService {
-
+    implements AuthorizationService {
     /**
      * Name of query to find the service by name.
      */
@@ -50,34 +52,34 @@ public class PersistentAuthorizationService extends AbstractPersistent
     /**
      * Authorization service to use.
      */
-    private DefaultAuthorizationService _service;
+    private DefaultAuthorizationService service;
 
     /**
      * Hibernate template to use.
      */
-    private HibernateTemplate _template;
+    private HibernateTemplate template;
 
     /**
      * User accessor.
      */
-    private UserAccessor _userAccessor;
-    
+    private UserAccessor userAccessor;
+
     /**
-     * Name of the service. 
+     * Name of the service.
      */
-    private String _name; 
+    private String name;
 
     /**
-     * Refresh interval in milliseconds. 
+     * Refresh interval in milliseconds.
      */
-    private final long _refreshInterval;
-    
+    private final long refreshInterval;
+
     /**
-     * Last refresh time. 
+     * Last refresh time.
      */
-    private long _lastRefreshTime; 
+    private long lastRefreshTime;
 
-    /**
+/**
      * Constructs the persistent service.
      * 
      * @param aName
@@ -91,118 +93,160 @@ public class PersistentAuthorizationService extends AbstractPersistent
      *            start of every operation.
      */
     public PersistentAuthorizationService(String aName,
-            HibernateTemplate aTemplate, UserAccessor aAccessor,
-            long aRefreshInterval) {
-        _template = aTemplate;
-        _refreshInterval = aRefreshInterval;
-        _lastRefreshTime = System.currentTimeMillis(); 
-        _userAccessor = aAccessor;
-        _name = aName; 
+        HibernateTemplate aTemplate, UserAccessor aAccessor,
+        long aRefreshInterval) {
+        template            = aTemplate;
+        refreshInterval     = aRefreshInterval;
+        lastRefreshTime     = System.currentTimeMillis();
+        userAccessor        = aAccessor;
+        name                = aName;
     }
 
     /**
-     * Initialize service if needed. 
+     * Initialize service if needed.
+     *
+     * @throws IllegalArgumentException DOCUMENT ME!
      */
     private void initialize() {
-        if (_service == null) {
-            List<DefaultAuthorizationService> result = _template
-                    .findByNamedQueryAndNamedParam(FIND_QUERY, NAME_PARAM,
-                            _name);
+        if (service == null) {
+            List<DefaultAuthorizationService> result = template
+                .findByNamedQueryAndNamedParam(FIND_QUERY, NAME_PARAM, name);
 
             if (result.size() > 1) {
                 throw new IllegalArgumentException(
-                        "Returned more than one service for name '" + _name
-                                + "' (" + result.size() + ")");
+                    "Returned more than one service for name '" + name + "' ("
+                    + result.size() + ")");
             }
 
             if (result.size() == 0) {
-                _service = new DefaultAuthorizationService(_userAccessor, _name);
-                _template.persist(_service);
+                service = new DefaultAuthorizationService(userAccessor, name);
+                template.persist(service);
             } else {
-                _service = result.get(0);
-                _service.setUserAccessor(_userAccessor);
+                service = result.get(0);
+                service.setUserAccessor(userAccessor);
             }
         }
     }
 
     /*
      * (non-Javadoc)
-     * 
+     *
      * @see org.wamblee.security.authorization.AuthorizationService#isAllowed(java.lang.Object,
      *      org.wamblee.security.authorization.Operation)
      */
+    /**
+     * DOCUMENT ME!
+     *
+     * @param aResource DOCUMENT ME!
+     * @param aOperation DOCUMENT ME!
+     *
+     * @return DOCUMENT ME!
+     */
     public boolean isAllowed(Object aResource, Operation aOperation) {
         initialize();
         refresh();
-        return _service.isAllowed(aResource, aOperation);
+
+        return service.isAllowed(aResource, aOperation);
     }
-    
+
     /* (non-Javadoc)
      * @see org.wamblee.security.authorization.AuthorizationService#check(T, org.wamblee.security.authorization.Operation)
      */
+    /**
+     * DOCUMENT ME!
+     *
+     * @param <T> DOCUMENT ME!
+     * @param aResource DOCUMENT ME!
+     * @param aOperation DOCUMENT ME!
+     *
+     * @return DOCUMENT ME!
+     */
     public <T> T check(T aResource, Operation aOperation) {
         initialize();
         refresh();
-        return _service.check(aResource, aOperation);
+
+        return service.check(aResource, aOperation);
     }
 
     /*
      * (non-Javadoc)
-     * 
+     *
      * @see org.wamblee.security.authorization.AuthorizationService#getRules()
      */
+    /**
+     * DOCUMENT ME!
+     *
+     * @return DOCUMENT ME!
+     */
     public AuthorizationRule[] getRules() {
         initialize();
         refresh();
-        return _service.getRules();
+
+        return service.getRules();
     }
 
     /*
      * (non-Javadoc)
-     * 
+     *
      * @see org.wamblee.security.authorization.AuthorizationService#appendRule(org.wamblee.security.authorization.AuthorizationRule)
      */
+    /**
+     * DOCUMENT ME!
+     *
+     * @param aRule DOCUMENT ME!
+     */
     public void appendRule(AuthorizationRule aRule) {
         initialize();
         refresh();
-        _service.appendRule(aRule);
+        service.appendRule(aRule);
         save();
     }
 
     /*
      * (non-Javadoc)
-     * 
+     *
      * @see org.wamblee.security.authorization.AuthorizationService#removeRule(int)
      */
+    /**
+     * DOCUMENT ME!
+     *
+     * @param aIndex DOCUMENT ME!
+     */
     public void removeRule(int aIndex) {
         initialize();
         refresh();
-        _service.removeRule(aIndex);
+        service.removeRule(aIndex);
         save();
     }
 
     /*
      * (non-Javadoc)
-     * 
+     *
      * @see org.wamblee.security.authorization.AuthorizationService#insertRuleAfter(int,
      *      org.wamblee.security.authorization.AuthorizationRule)
      */
+    /**
+     * DOCUMENT ME!
+     *
+     * @param aIndex DOCUMENT ME!
+     * @param aRule DOCUMENT ME!
+     */
     public void insertRuleAfter(int aIndex, AuthorizationRule aRule) {
         initialize();
         refresh();
-        _service.insertRuleAfter(aIndex, aRule);
+        service.insertRuleAfter(aIndex, aRule);
         save();
     }
 
     /**
      * Refreshes the state of the service through hibernate.
-     * 
      */
     private synchronized void refresh() {
-        long time = System.currentTimeMillis(); 
-        if ( time - _lastRefreshTime > _refreshInterval ) { 
-            _template.refresh(_service);
-            _lastRefreshTime = time; 
+        long time = System.currentTimeMillis();
+
+        if ((time - lastRefreshTime) > refreshInterval) {
+            template.refresh(service);
+            lastRefreshTime = time;
         }
     }
 
@@ -210,6 +254,6 @@ public class PersistentAuthorizationService extends AbstractPersistent
      * Saves any changes to the service state if necessary.
      */
     private void save() {
-        HibernateSupport.merge(_template, _service);
+        HibernateSupport.merge(template, service);
     }
 }