now using the simplified user management interface.
[utils] / security / impl / src / main / java / org / wamblee / security / authorization / DefaultOperationRegistry.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */ 
16 package org.wamblee.security.authorization;
17
18 import java.util.ArrayList;
19 import java.util.Map;
20 import java.util.TreeMap;
21
22 /**
23  * Operation registry implementation. This implementation ignores the
24  * distinction between different types of resources and simply assumes that
25  * every operation is applicable to every type of resource.
26  * 
27  * @author Erik Brakkee
28  */
29 public class DefaultOperationRegistry implements OperationRegistry {
30     private Map<String, Operation> operations;
31
32     /**
33      * Creates a new DefaultOperationRegistry object.
34      * 
35      */
36     public DefaultOperationRegistry(Operation[] aOperations) {
37         operations = new TreeMap<String, Operation>();
38
39         for (Operation operation : aOperations) {
40             operations.put(operation.getName(), operation);
41         }
42     }
43
44     /*
45      * (non-Javadoc)
46      * 
47      * @see
48      * org.wamblee.security.authorization.OperationRegistry#getOperations(java
49      * .lang.Class)
50      */
51     public Operation[] getOperations(Class aResourceClass) {
52         return operations.values().toArray(new Operation[0]);
53     }
54
55     /*
56      * (non-Javadoc)
57      * 
58      * @see
59      * org.wamblee.security.authorization.OperationRegistry#encode(org.wamblee
60      * .security.authorization.Operation[])
61      */
62     public String encode(Operation[] aOperations) {
63         StringBuffer buffer = new StringBuffer();
64
65         for (Operation operation : aOperations) {
66             if (buffer.length() > 0) {
67                 buffer.append(',');
68             }
69
70             buffer.append(operation.getName());
71         }
72
73         return buffer.toString();
74     }
75
76     /*
77      * (non-Javadoc)
78      * 
79      * @see
80      * org.wamblee.security.authorization.OperationRegistry#decode(java.lang
81      * .Class, java.lang.String)
82      */
83     public Operation[] decode(Class aResourceClass, String aOperationsString) {
84         return decode(aOperationsString);
85     }
86
87     /*
88      * (non-Javadoc)
89      * 
90      * @see
91      * org.wamblee.security.authorization.OperationRegistry#decode(java.lang
92      * .String)
93      */
94     public Operation[] decode(String aOperationsString) {
95         if (aOperationsString.length() == 0) {
96             return new Operation[0];
97         }
98
99         String[] names = aOperationsString.split(",");
100         ArrayList<Operation> result = new ArrayList<Operation>();
101
102         for (String name : names) {
103             Operation operation = operations.get(name);
104
105             if (operation == null) {
106                 throw new IllegalArgumentException("Unknown operation '" +
107                     name + "'");
108             }
109
110             result.add(operation);
111         }
112
113         return result.toArray(new Operation[0]);
114     }
115 }