472b2dacfe80d5b05c2fbc5f5f9bbc229af6091d
[utils] / security / src / main / java / org / wamblee / security / authorization / DefaultOperationRegistry.java
1 /*
2  * Copyright 2005 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
17 package org.wamblee.security.authorization;
18
19 import java.util.ArrayList;
20 import java.util.Map;
21 import java.util.TreeMap;
22
23 /**
24  * Operation registry implementation. 
25  * This implementation ignores the distinction between different types of resources
26  * and simply assumes that every operation is applicable to every type of resource. 
27  *
28  * @author Erik Brakkee
29  */
30 public class DefaultOperationRegistry implements OperationRegistry {
31
32     private Map<String,Operation> operations; 
33     
34     public DefaultOperationRegistry(Operation[] aOperations) { 
35         operations = new TreeMap<String, Operation>();
36         for (Operation operation: aOperations) { 
37             operations.put(operation.getName(), operation);
38         }
39     }
40     
41     /* (non-Javadoc)
42      * @see org.wamblee.security.authorization.OperationRegistry#getOperations(java.lang.Class)
43      */
44     public Operation[] getOperations(Class aResourceClass) {
45         return operations.values().toArray(new Operation[0]); 
46     }
47
48     /* (non-Javadoc)
49      * @see org.wamblee.security.authorization.OperationRegistry#encode(org.wamblee.security.authorization.Operation[])
50      */
51     public String encode(Operation[] aOperations) {
52         StringBuffer buffer = new StringBuffer(); 
53         for (Operation operation: aOperations) {
54             if ( buffer.length() > 0 ) { 
55                 buffer.append(',');
56             }
57             buffer.append(operation.getName());
58         }
59         return buffer.toString();
60     }
61
62     /* (non-Javadoc)
63      * @see org.wamblee.security.authorization.OperationRegistry#decode(java.lang.Class, java.lang.String)
64      */
65     public Operation[] decode(Class aResourceClass, String aOperationsString) {
66         return decode(aOperationsString); 
67     }
68     
69     /* (non-Javadoc)
70      * @see org.wamblee.security.authorization.OperationRegistry#decode(java.lang.String)
71      */
72     public Operation[] decode(String aOperationsString) {
73         if ( aOperationsString.length() == 0 ) { 
74             return new Operation[0]; 
75         }
76         String[] names = aOperationsString.split(",");
77         ArrayList<Operation> result = new ArrayList<Operation>(); 
78         for (String name: names) { 
79             Operation operation = operations.get(name);
80             if ( operation == null ) { 
81                 throw new IllegalArgumentException("Unknown operation '" + name + "'");
82             }
83             result.add(operation);
84         }
85         return result.toArray(new Operation[0]); 
86     }
87
88 }