ad34465d139a4be3e59f61cece10b76d445d32e2
[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 package org.wamblee.security.authorization;
17
18 import java.util.ArrayList;
19 import java.util.Map;
20 import java.util.TreeMap;
21
22
23 /**
24  * Operation registry implementation.  This implementation ignores the
25  * distinction between different types of resources and simply assumes that
26  * every operation is applicable to every type of resource.
27  *
28  * @author Erik Brakkee
29  */
30 public class DefaultOperationRegistry implements OperationRegistry {
31     /**
32      * DOCUMENT ME!
33      */
34     private Map<String, Operation> operations;
35
36 /**
37      * Creates a new DefaultOperationRegistry object.
38      *
39      * @param aOperations DOCUMENT ME!
40      */
41     public DefaultOperationRegistry(Operation[] aOperations) {
42         operations = new TreeMap<String, Operation>();
43
44         for (Operation operation : aOperations) {
45             operations.put(operation.getName(), operation);
46         }
47     }
48
49     /* (non-Javadoc)
50      * @see org.wamblee.security.authorization.OperationRegistry#getOperations(java.lang.Class)
51      */
52     /**
53      * DOCUMENT ME!
54      *
55      * @param aResourceClass DOCUMENT ME!
56      *
57      * @return DOCUMENT ME!
58      */
59     public Operation[] getOperations(Class aResourceClass) {
60         return operations.values().toArray(new Operation[0]);
61     }
62
63     /* (non-Javadoc)
64      * @see org.wamblee.security.authorization.OperationRegistry#encode(org.wamblee.security.authorization.Operation[])
65      */
66     /**
67      * DOCUMENT ME!
68      *
69      * @param aOperations DOCUMENT ME!
70      *
71      * @return DOCUMENT ME!
72      */
73     public String encode(Operation[] aOperations) {
74         StringBuffer buffer = new StringBuffer();
75
76         for (Operation operation : aOperations) {
77             if (buffer.length() > 0) {
78                 buffer.append(',');
79             }
80
81             buffer.append(operation.getName());
82         }
83
84         return buffer.toString();
85     }
86
87     /* (non-Javadoc)
88      * @see org.wamblee.security.authorization.OperationRegistry#decode(java.lang.Class, java.lang.String)
89      */
90     /**
91      * DOCUMENT ME!
92      *
93      * @param aResourceClass DOCUMENT ME!
94      * @param aOperationsString DOCUMENT ME!
95      *
96      * @return DOCUMENT ME!
97      */
98     public Operation[] decode(Class aResourceClass, String aOperationsString) {
99         return decode(aOperationsString);
100     }
101
102     /* (non-Javadoc)
103      * @see org.wamblee.security.authorization.OperationRegistry#decode(java.lang.String)
104      */
105     /**
106      * DOCUMENT ME!
107      *
108      * @param aOperationsString DOCUMENT ME!
109      *
110      * @return DOCUMENT ME!
111      *
112      * @throws IllegalArgumentException DOCUMENT ME!
113      */
114     public Operation[] decode(String aOperationsString) {
115         if (aOperationsString.length() == 0) {
116             return new Operation[0];
117         }
118
119         String[]             names  = aOperationsString.split(",");
120         ArrayList<Operation> result = new ArrayList<Operation>();
121
122         for (String name : names) {
123             Operation operation = operations.get(name);
124
125             if (operation == null) {
126                 throw new IllegalArgumentException("Unknown operation '" + name
127                     + "'");
128             }
129
130             result.add(operation);
131         }
132
133         return result.toArray(new Operation[0]);
134     }
135 }