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.security;
19  
20  import java.util.Collection;
21  import at.ac.tuwien.infosys.sm4all.copal.api.util.BaseRegistry;
22  import at.ac.tuwien.infosys.sm4all.copal.api.util.NotRegisteredException;
23  import at.ac.tuwien.infosys.sm4all.copal.api.util.Registry;
24  
25  /**
26   * Implementation of a {@link Registry} for {@link Authorization}s.
27   * 
28   * @author sanjin
29   */
30  public class Authorizations extends BaseRegistry<String, Authorization> {
31  
32      /**
33       * Create instance of empty {@link Authorization} {@link Registry}.
34       */
35      public Authorizations() {
36          super();
37      }
38  
39      /**
40       * Create instance of {@link Authorization} {@link Registry} that has
41       * specified {@link Authorization}s registered.
42       * 
43       * @param authorizations the {@link Authorization}s.
44       * @throws NullPointerException if specified array of {@link Authorization}s
45       *         or any {@link Authorization} in the array is <code>null</code>.
46       */
47      public Authorizations(final Authorization... authorizations) {
48          super();
49  
50          setAll(authorizations);
51      }
52  
53      /**
54       * Register specified {@link Authorization}.
55       * 
56       * @param authorization the {@link Authorization}.
57       * @throws NullPointerException if specified {@link Authorization} is
58       *         <code>null</code>.
59       */
60      @Override
61      public void register(final Authorization authorization) {
62          if (null == authorization) {
63              throw new NullPointerException("Authorization cannot be null.");
64          }
65  
66          set(authorization);
67      }
68  
69      /**
70       * Unregister {@link Authorization} that has specified name of
71       * {@link AuthorizationMethod}.
72       * 
73       * @param method the name of {@link AuthorizationMethod}.
74       */
75      @Override
76      public void unregister(final String method) {
77          if (null == method) {
78              throw new NullPointerException(
79                      "Authorization method cannot be null.");
80          }
81  
82          try {
83              super.unregister(method);
84          } catch (final NotRegisteredException ignored) {
85              /* authorization is not registered; do nothing */
86          }
87      }
88  
89      /**
90       * Returns if an {@link Authorization} that has specified name of
91       * {@link AuthorizationMethod} is currently registered.
92       * 
93       * @param method the name of {@link AuthorizationMethod}.
94       * @return if an {@link Authorization} that has specified name of
95       *         {@link AuthorizationMethod} is currently registered.
96       */
97      @Override
98      public boolean isRegistered(final String method) {
99          if (null == method) {
100             throw new NullPointerException(
101                     "Authorization method cannot be null.");
102         }
103 
104         return super.isRegistered(method);
105     }
106 
107     /**
108      * Returns {@link Authorization} that has specified name of
109      * {@link AuthorizationMethod}.
110      * 
111      * @param method the name of {@link AuthorizationMethod}.
112      * @return the {@link Authorization} or <code>null</code> if there is no
113      *         such {@link Authorization} registered.
114      */
115     @Override
116     public Authorization get(final String method) {
117         if (null == method) {
118             throw new NullPointerException(
119                     "Authorization method cannot be null.");
120         }
121 
122         return super.get(method);
123     }
124 
125     /**
126      * Returns all currently registered {@link Authorization}s.
127      * 
128      * @return all currently registered {@link Authorization}s.
129      */
130     @Override
131     public Authorization[] getAll() {
132         final Collection<Authorization> result = getValues();
133         return result.toArray(new Authorization[result.size()]);
134     }
135 
136     /**
137      * Register specified {@link Authorization}s and remove all previously
138      * registered {@link Authorization}s.
139      * 
140      * @param authorizations the {@link Authorization}s.
141      * @throws NullPointerException if specified array of {@link Authorization}s
142      *         or any {@link Authorization} in the array is <code>null</code>.
143      */
144     public void setAll(final Authorization... authorizations) {
145         if (null == authorizations) {
146             throw new NullPointerException("Authorizations cannot be null.");
147         }
148         for (final Authorization authorization : authorizations) {
149             if (null == authorization) {
150                 throw new NullPointerException("Authorization cannot be null.");
151             }
152         }
153 
154         setValues(authorizations);
155     }
156 
157     @Override
158     protected String getKey(final Authorization authorization) {
159         if (null == authorization) {
160             throw new NullPointerException("Authorization cannot be null.");
161         }
162 
163         return authorization.getMethod();
164     }
165 }