View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.event;
19  
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.concurrent.locks.ReadWriteLock;
27  import java.util.concurrent.locks.ReentrantReadWriteLock;
28  import at.ac.tuwien.infosys.sm4all.copal.api.security.Authorization;
29  import at.ac.tuwien.infosys.sm4all.copal.api.security.AuthorizationMethod;
30  import at.ac.tuwien.infosys.sm4all.copal.api.util.Attribute;
31  
32  /**
33   * Representation of a list of {@link Authorization}s.
34   * 
35   * @author sanjin
36   */
37  public class Authorizations {
38  
39      private final ReadWriteLock lock = new ReentrantReadWriteLock();
40      private final Map<String, Authorization> authorizations = new HashMap<String, Authorization>();
41  
42      /**
43       * Adds specified {@link Authorization}. If there is already an
44       * {@link Authorization} with same name of the {@link AuthorizationMethod}
45       * as specified {@link Authorization}, their attributes will be merged with
46       * attributes from specified {@link Authorization} potentially overwriting
47       * ones in already added {@link Authorization}.
48       * 
49       * @param authorization the {@link Authorization}.
50       */
51      public void add(final Authorization authorization) {
52          final String method = authorization.getMethod();
53  
54          this.lock.writeLock().lock();
55          try {
56              if (this.authorizations.containsKey(method)) {
57                  final Set<Attribute> newAttributes = new HashSet<Attribute>(
58                          Arrays.asList(this.authorizations.get(method).getAttributes()));
59                  newAttributes.addAll(Arrays.asList(authorization.getAttributes()));
60                  this.authorizations.put(
61                          method,
62                          new Authorization(
63                                  method,
64                                  newAttributes.toArray(new Attribute[newAttributes.size()])));
65              } else
66                  this.authorizations.put(method, authorization);
67          } finally {
68              this.lock.writeLock().unlock();
69          }
70      }
71  
72      /**
73       * Removes an {@link Authorization} that has specified name of
74       * {@link AuthorizationMethod}.
75       * 
76       * @param method the name of {@link AuthorizationMethod}.
77       */
78      public void remove(final String method) {
79          this.lock.writeLock().lock();
80          try {
81              if (this.authorizations.containsKey(method))
82                  this.authorizations.remove(method);
83          } finally {
84              this.lock.writeLock().unlock();
85          }
86      }
87  
88      /**
89       * Returns the {@link Authorization} that has specified name of
90       * {@link AuthorizationMethod}. If this list does not have
91       * {@link Authorization} with specified name {@link AuthorizationMethod},
92       * <code>null</code> will be returned.
93       * 
94       * @param method the name of {@link AuthorizationMethod}.
95       * @return the {@link Authorization} or <code>null</code> if there is no
96       *         such {@link Authorization}.
97       */
98      public Authorization get(final String method) {
99          Authorization result = null;
100 
101         this.lock.readLock().lock();
102         try {
103             if (this.authorizations.containsKey(method))
104                 result = this.authorizations.get(method);
105         } finally {
106             this.lock.readLock().unlock();
107         }
108 
109         return result;
110     }
111 
112     /**
113      * Returns all currently added {@link Authorization}s.
114      * 
115      * @return all currently added {@link Authorization}s.
116      */
117     public Authorization[] getAll() {
118         final Collection<Authorization> result;
119 
120         this.lock.readLock().lock();
121         try {
122             result = this.authorizations.values();
123         } finally {
124             this.lock.readLock().unlock();
125         }
126 
127         return result.toArray(new Authorization[result.size()]);
128     }
129 
130     /**
131      * Returns if an {@link Authorization} that has specified name of
132      * {@link AuthorizationMethod} is currently present in this list. If the
133      * {@link Authorization} was never added (or was added and consequently
134      * removed), <code>false</code> will be returned; otherwise
135      * <code>true</code> will be returned.
136      * 
137      * @param method the name of {@link AuthorizationMethod}.
138      * @return if an {@link Authorization} that has specified name of
139      *         {@link AuthorizationMethod} is currently present in this list.
140      */
141     public boolean contains(final String method) {
142         final boolean result;
143 
144         this.lock.readLock().lock();
145         try {
146             result = this.authorizations.containsKey(method);
147         } finally {
148             this.lock.readLock().unlock();
149         }
150 
151         return result;
152     }
153 
154     /**
155      * Returns if there is any added {@link Authorization}.
156      * 
157      * @return if there is any added {@link Authorization}.
158      */
159     public boolean isNotEmpty() {
160         final boolean result;
161 
162         this.lock.readLock().lock();
163         try {
164             result = !this.authorizations.isEmpty();
165         } finally {
166             this.lock.readLock().unlock();
167         }
168 
169         return result;
170     }
171 }