View Javadoc

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.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  /**
33   * Property editor to assist with the setup of a {@link MethodDefinitionSource}.
34   * <p>The class creates and populates a {@link MethodDefinitionMap}.</p>
35   *
36   * @author Ben Alex
37   * @version $Id: MethodDefinitionSourceEditor.java 1784 2007-02-24 21:00:24Z luke_t $
38   */
39  public class MethodDefinitionSourceEditor extends PropertyEditorSupport {
40      //~ Static fields/initializers =====================================================================================
41  
42      private static final Log logger = LogFactory.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.hasNext();) {
62                  String name = (String) iter.next();
63                  String value = props.getProperty(name);
64  
65                  MethodDefinitionSourceMapping mapping = new MethodDefinitionSourceMapping();
66                  mapping.setMethodName(name);
67  
68                  String[] tokens = StringUtils.commaDelimitedListToStringArray(value);
69  
70                  for (int i = 0; i < tokens.length; i++) {
71                      mapping.addConfigAttribute(tokens[i].trim());
72                  }
73  
74                  mappings.add(mapping);
75              }
76              source.setMappings(mappings);
77          }
78  
79          setValue(source);
80      }
81  }