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.aop.target;
  17. import org.springframework.aop.TargetSource;
  18. /**
  19. * Implementation of TargetSource interface that caches a local target object,
  20. * but allows the target to be swapped while the application is running.
  21. *
  22. * <p>If configuring an object of this class in a Spring IoC container,
  23. * use constructor injection.
  24. *
  25. * @author Rod Johnson
  26. * @version $Id: HotSwappableTargetSource.java,v 1.7 2004/04/01 15:36:02 jhoeller Exp $
  27. */
  28. public class HotSwappableTargetSource implements TargetSource {
  29. /** Target cached and invoked using reflection */
  30. private Object target;
  31. /**
  32. * Create a new HotSwappableTargetSource with the initial target.
  33. * @param initialTarget initial target
  34. */
  35. public HotSwappableTargetSource(Object initialTarget) {
  36. this.target = initialTarget;
  37. }
  38. public Class getTargetClass() {
  39. return target.getClass();
  40. }
  41. /**
  42. * @see org.springframework.aop.TargetSource#isStatic()
  43. */
  44. public final boolean isStatic() {
  45. return false;
  46. }
  47. /**
  48. * Synchronization around something that takes so little time is fine
  49. * @see org.springframework.aop.TargetSource#getTarget()
  50. */
  51. public final synchronized Object getTarget() {
  52. return this.target;
  53. }
  54. /**
  55. * @see org.springframework.aop.TargetSource#releaseTarget(java.lang.Object)
  56. */
  57. public void releaseTarget(Object o) {
  58. }
  59. /**
  60. * Swap the target, returning the old target.
  61. * @param newTarget new target
  62. * @return the old target
  63. * @throws IllegalArgumentException if the new target is invalid
  64. */
  65. public synchronized Object swap(Object newTarget) throws IllegalArgumentException {
  66. if (newTarget == null) {
  67. throw new IllegalArgumentException("Cannot swap to null");
  68. }
  69. // TODO type checks
  70. Object old = this.target;
  71. this.target = newTarget;
  72. return old;
  73. }
  74. /**
  75. * Two invoker interceptors are equal if they have the same target or
  76. * if the targets are equal.
  77. */
  78. public boolean equals(Object other) {
  79. if (!(other instanceof HotSwappableTargetSource)) {
  80. return false;
  81. }
  82. HotSwappableTargetSource otherTargetSource = (HotSwappableTargetSource) other;
  83. return otherTargetSource.target == this.target || otherTargetSource.target.equals(this.target);
  84. }
  85. }