1. /*
  2. * Copyright 2002-2004 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.springframework.transaction.interceptor;
  17. import java.beans.PropertyEditorSupport;
  18. import java.util.Iterator;
  19. import java.util.Properties;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.springframework.beans.propertyeditors.PropertiesEditor;
  23. /**
  24. * Property editor that can convert String into TransactionAttributeSource.
  25. * The transaction attribute string must be parseable by the
  26. * TransactionAttributeEditor in this package.
  27. *
  28. * <p>Strings are in property syntax, with the form:<br>
  29. * <code>FQCN.methodName=<transaction attribute string></code>
  30. *
  31. * <p>For example:<br>
  32. * <code>com.mycompany.mycode.MyClass.myMethod=PROPAGATION_MANDATORY,ISOLATION_DEFAULT</code>
  33. *
  34. * <p><b>NOTE:</b> The specified class must be the one where the methods are
  35. * defined; in case of implementing an interface, the interface class name.
  36. *
  37. * <p>Note: Will register all overloaded methods for a given name.
  38. * Does not support explicit registration of certain overloaded methods.
  39. * Supports "xxx*" mappings, e.g. "notify*" for "notify" and "notifyAll".
  40. *
  41. * @author Rod Johnson
  42. * @author Juergen Hoeller
  43. * @since 26-Apr-2003
  44. * @version $Id: TransactionAttributeSourceEditor.java,v 1.4 2004/03/18 02:46:05 trisberg Exp $
  45. * @see org.springframework.transaction.interceptor.TransactionAttributeEditor
  46. */
  47. public class TransactionAttributeSourceEditor extends PropertyEditorSupport {
  48. protected final Log logger = LogFactory.getLog(getClass());
  49. public void setAsText(String s) throws IllegalArgumentException {
  50. MethodMapTransactionAttributeSource source = new MethodMapTransactionAttributeSource();
  51. if (s == null || "".equals(s)) {
  52. // Leave value in property editor null
  53. }
  54. else {
  55. // Use properties editor to tokenize the hold string
  56. PropertiesEditor propertiesEditor = new PropertiesEditor();
  57. propertiesEditor.setAsText(s);
  58. Properties props = (Properties) propertiesEditor.getValue();
  59. // Now we have properties, process each one individually
  60. TransactionAttributeEditor tae = new TransactionAttributeEditor();
  61. for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
  62. String name = (String) iter.next();
  63. String value = props.getProperty(name);
  64. // Convert value to a transaction attribute
  65. tae.setAsText(value);
  66. TransactionAttribute attr = (TransactionAttribute) tae.getValue();
  67. // Register name and attribute
  68. source.addTransactionalMethod(name, attr);
  69. }
  70. }
  71. setValue(source);
  72. }
  73. }