01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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.acegisecurity.intercept.method;
17:
18: import org.apache.commons.logging.Log;
19: import org.apache.commons.logging.LogFactory;
20:
21: import org.springframework.beans.propertyeditors.PropertiesEditor;
22: import org.springframework.util.StringUtils;
23:
24: import java.beans.PropertyEditorSupport;
25:
26: import java.util.ArrayList;
27: import java.util.Iterator;
28: import java.util.List;
29: import java.util.Properties;
30:
31: /**
32: * Property editor to assist with the setup of a {@link MethodDefinitionSource}.
33: * <p>The class creates and populates a {@link MethodDefinitionMap}.</p>
34: *
35: * @author Ben Alex
36: * @version $Id: MethodDefinitionSourceEditor.java 1784 2007-02-24 21:00:24Z luke_t $
37: */
38: public class MethodDefinitionSourceEditor extends PropertyEditorSupport {
39: //~ Static fields/initializers =====================================================================================
40:
41: private static final Log logger = LogFactory
42: .getLog(MethodDefinitionSourceEditor.class);
43:
44: //~ Methods ========================================================================================================
45:
46: public void setAsText(String s) throws IllegalArgumentException {
47: MethodDefinitionMap source = new MethodDefinitionMap();
48:
49: if ((s == null) || "".equals(s)) {
50: // Leave value in property editor null
51: } else {
52: // Use properties editor to tokenize the string
53: PropertiesEditor propertiesEditor = new PropertiesEditor();
54: propertiesEditor.setAsText(s);
55:
56: Properties props = (Properties) propertiesEditor.getValue();
57:
58: // Now we have properties, process each one individually
59: List mappings = new ArrayList();
60:
61: for (Iterator iter = props.keySet().iterator(); iter
62: .hasNext();) {
63: String name = (String) iter.next();
64: String value = props.getProperty(name);
65:
66: MethodDefinitionSourceMapping mapping = new MethodDefinitionSourceMapping();
67: mapping.setMethodName(name);
68:
69: String[] tokens = StringUtils
70: .commaDelimitedListToStringArray(value);
71:
72: for (int i = 0; i < tokens.length; i++) {
73: mapping.addConfigAttribute(tokens[i].trim());
74: }
75:
76: mappings.add(mapping);
77: }
78: source.setMappings(mappings);
79: }
80:
81: setValue(source);
82: }
83: }
|