1 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 package org.springframework.security;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.springframework.util.Assert;
26
27
28 /**
29 * Holds a group of {@link ConfigAttribute}s that are associated with a given secure object target - effectively a
30 * Collection<ConfigAttribute>.
31 * <p>
32 * Once created, the object is immutable.
33 * <p>
34 * All the <code>ConfigAttributeDefinition</code>s associated with a given {@link
35 * org.springframework.security.intercept.AbstractSecurityInterceptor} are stored in an {@link
36 * org.springframework.security.intercept.ObjectDefinitionSource}.
37 *
38 * @author Ben Alex
39 * @version $Id: ConfigAttributeDefinition.java 2998 2008-04-24 23:12:55Z luke_t $
40 */
41 public class ConfigAttributeDefinition implements Serializable {
42 public static final ConfigAttributeDefinition NO_ATTRIBUTES = new ConfigAttributeDefinition();
43
44 //~ Instance fields ================================================================================================
45
46 private List configAttributes;
47
48 //~ Constructors ===================================================================================================
49
50 private ConfigAttributeDefinition() {
51 configAttributes = Collections.EMPTY_LIST;
52 }
53
54 /**
55 * Creates a ConfigAttributeDefinition containing a single attribute
56 * @param attribute the String name of the attribute (converted internally to a <tt>SecurityConfig</tt> instance).
57 */
58 public ConfigAttributeDefinition(String attribute) {
59 configAttributes = new ArrayList(1);
60 configAttributes.add(new SecurityConfig(attribute));
61 configAttributes = Collections.unmodifiableList(configAttributes);
62 }
63
64 /**
65 * Creates a ConfigAttributeDefinition containing a single attribute.
66 */
67 public ConfigAttributeDefinition(ConfigAttribute attribute) {
68 configAttributes = new ArrayList(1);
69 configAttributes.add(attribute);
70 configAttributes = Collections.unmodifiableList(configAttributes);
71 }
72
73 /**
74 * Builds a collection of ConfigAttributes from an array of String tokens, each of which will be wrapped in a
75 * <tt>SecurityConfig</tt> instance.
76 *
77 * @param attributeTokens the tokens which will be turned into attributes.
78 */
79 public ConfigAttributeDefinition(String[] attributeTokens) {
80 configAttributes = new ArrayList(attributeTokens.length);
81
82 for (int i = 0; i < attributeTokens.length; i++) {
83 configAttributes.add(new SecurityConfig(attributeTokens[i].trim()));
84 }
85
86 configAttributes = Collections.unmodifiableList(configAttributes);
87 }
88
89 /**
90 * Creates an immutable ConfigAttributeDefinition from the supplied list of <tt>ConfigAttribute</tt> objects.
91 */
92 public ConfigAttributeDefinition(List configAttributes) {
93 Iterator attributes = configAttributes.iterator();
94 while (attributes.hasNext()) {
95 Assert.isInstanceOf(ConfigAttribute.class, attributes.next(),
96 "List entries must be of type ConfigAttribute");
97 }
98
99 this.configAttributes = Collections.unmodifiableList(new ArrayList(configAttributes));
100 }
101
102 /**
103 * Creates a <tt>ConfigAttributeDefinition</tt> by including only those attributes which implement <tt>ConfigAttribute</tt>.
104 *
105 * @param unfilteredInput a collection of various elements, zero or more which implement <tt>ConfigAttribute</tt> (can also be <tt>null</tt>)
106 * @return a ConfigAttributeDefinition if at least one <tt>ConfigAttribute</tt> was present, or <tt>null</tt> if none implemented it
107 */
108 public static ConfigAttributeDefinition createFiltered(Collection unfilteredInput) {
109 if (unfilteredInput == null) {
110 return null;
111 }
112
113 List configAttributes = new ArrayList();
114 Iterator i = unfilteredInput.iterator();
115 while (i.hasNext()) {
116 Object element = i.next();
117 if (element instanceof ConfigAttribute) {
118 configAttributes.add(element);
119 }
120 }
121
122 if (configAttributes.size() == 0) {
123 return null;
124 }
125
126 return new ConfigAttributeDefinition(configAttributes);
127 }
128
129 //~ Methods ========================================================================================================
130
131 /**
132 * Indicates whether the specified <code>ConfigAttribute</code> is contained within this
133 * <code>ConfigAttributeDefinition</code>.
134 *
135 * @param configAttribute the attribute to locate
136 *
137 * @return <code>true</code> if the specified <code>ConfigAttribute</code> is contained, <code>false</code>
138 * otherwise
139 */
140 public boolean contains(ConfigAttribute configAttribute) {
141 return configAttributes.contains(configAttribute);
142 }
143
144 public boolean equals(Object obj) {
145 if (!(obj instanceof ConfigAttributeDefinition)) {
146 return false;
147 }
148
149 ConfigAttributeDefinition test = (ConfigAttributeDefinition) obj;
150
151 return configAttributes.equals(test.configAttributes);
152 }
153
154 /**
155 * Returns the internal collection of <code>ConfigAttribute</code>s defined by this
156 * <code>ConfigAttributeDefinition</code>.
157 * <p>
158 * Allows <code>AccessDecisionManager</code>s and other classes to loop through every configuration attribute
159 * associated with a target secure object.
160 *
161 * @return the configuration attributes stored in this instance.
162 */
163 public Collection getConfigAttributes() {
164 return this.configAttributes;
165 }
166
167 public String toString() {
168 return this.configAttributes.toString();
169 }
170 }