2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.security.authorization;
19 import java.util.ArrayList;
21 import java.util.TreeMap;
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.
28 * @author Erik Brakkee
30 public class DefaultOperationRegistry implements OperationRegistry {
32 private Map<String,Operation> _operations;
34 public DefaultOperationRegistry(Operation[] aOperations) {
35 _operations = new TreeMap<String, Operation>();
36 for (Operation operation: aOperations) {
37 _operations.put(operation.getName(), operation);
42 * @see org.wamblee.security.authorization.OperationRegistry#getOperations(java.lang.Class)
44 public Operation[] getOperations(Class aResourceClass) {
45 return _operations.values().toArray(new Operation[0]);
49 * @see org.wamblee.security.authorization.OperationRegistry#encode(org.wamblee.security.authorization.Operation[])
51 public String encode(Operation[] aOperations) {
52 StringBuffer buffer = new StringBuffer();
53 for (Operation operation: aOperations) {
54 if ( buffer.length() > 0 ) {
57 buffer.append(operation.getName());
59 return buffer.toString();
63 * @see org.wamblee.security.authorization.OperationRegistry#decode(java.lang.Class, java.lang.String)
65 public Operation[] decode(Class aResourceClass, String aOperationsString) {
66 return decode(aOperationsString);
70 * @see org.wamblee.security.authorization.OperationRegistry#decode(java.lang.String)
72 public Operation[] decode(String aOperationsString) {
73 if ( aOperationsString.length() == 0 ) {
74 return new Operation[0];
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 + "'");
83 result.add(operation);
85 return result.toArray(new Operation[0]);