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 org.springframework.transaction.TransactionDefinition;
  19. import org.springframework.util.StringUtils;
  20. /**
  21. * PropertyEditor for TransactionAttribute objects. Takes Strings of form
  22. * <p><code>PROPAGATION_NAME,ISOLATION_NAME,readOnly,+Exception1,-Exception2</code>
  23. * <p>where only propagation code is required. For example:
  24. * <p><code>PROPAGATION_MANDATORY,ISOLATION_DEFAULT</code>
  25. *
  26. * <p>The tokens can be in any order. Propagation and isolation codes
  27. * must use the names of the constants in the TransactionDefinition class.
  28. *
  29. * <p>A "+" before an exception name substring indicates that
  30. * transactions should commit even if this exception is thrown;
  31. * a "-" that they should roll back.
  32. *
  33. * @author Rod Johnson
  34. * @author Juergen Hoeller
  35. * @since 24-Apr-2003
  36. * @version $Id: TransactionAttributeEditor.java,v 1.5 2004/04/09 05:43:27 jhoeller Exp $
  37. * @see org.springframework.transaction.TransactionDefinition
  38. * @see org.springframework.core.Constants
  39. */
  40. public class TransactionAttributeEditor extends PropertyEditorSupport {
  41. /**
  42. * Format is PROPAGATION_NAME,ISOLATION_NAME,readOnly,+Exception1,-Exception2.
  43. * Null or the empty string means that the method is non transactional.
  44. * @see java.beans.PropertyEditor#setAsText(java.lang.String)
  45. */
  46. public void setAsText(String s) throws IllegalArgumentException {
  47. if (s == null || "".equals(s)) {
  48. setValue(null);
  49. }
  50. else {
  51. // tokenize it with ","
  52. String[] tokens = StringUtils.commaDelimitedListToStringArray(s);
  53. RuleBasedTransactionAttribute attr = new RuleBasedTransactionAttribute();
  54. for (int i = 0; i < tokens.length; i++) {
  55. String token = tokens[i].trim();
  56. if (token.startsWith(TransactionDefinition.PROPAGATION_CONSTANT_PREFIX)) {
  57. attr.setPropagationBehaviorName(token);
  58. }
  59. else if (token.startsWith(TransactionDefinition.ISOLATION_CONSTANT_PREFIX)) {
  60. attr.setIsolationLevelName(token);
  61. }
  62. else if (token.startsWith(DefaultTransactionAttribute.TIMEOUT_PREFIX)) {
  63. String value = token.substring(DefaultTransactionAttribute.TIMEOUT_PREFIX.length());
  64. attr.setTimeout(Integer.parseInt(value));
  65. }
  66. else if (token.equals(DefaultTransactionAttribute.READ_ONLY_MARKER)) {
  67. attr.setReadOnly(true);
  68. }
  69. else if (token.startsWith(DefaultTransactionAttribute.COMMIT_RULE_PREFIX)) {
  70. attr.getRollbackRules().add(new NoRollbackRuleAttribute(token.substring(1)));
  71. }
  72. else if (token.startsWith(DefaultTransactionAttribute.ROLLBACK_RULE_PREFIX)) {
  73. attr.getRollbackRules().add(new RollbackRuleAttribute(token.substring(1)));
  74. }
  75. else {
  76. throw new IllegalArgumentException("Illegal transaction attribute token: [" + token + "]");
  77. }
  78. }
  79. setValue(attr);
  80. }
  81. }
  82. }