You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SynchronizedStaticAspect.aj 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package foo;
  2. /**
  3. * For synchronization we synchronize each static method mark as @Synchronized
  4. * by a ReentrantLock.
  5. *
  6. * @author <a href="boehm@javatux.de">oliver</a>
  7. * @since 0.8
  8. */
  9. import org.aspectj.lang.annotation.SuppressAjWarnings;
  10. public aspect SynchronizedStaticAspect pertypewithin(@Synchronized *) {
  11. /*
  12. private static final Logger log = LogManager.getLogger(SynchronizedStaticAspect.class);
  13. private Lock classLock = new ReentrantLock();
  14. protected long timeout = 1800;
  15. protected TimeUnit unit = TimeUnit.SECONDS;
  16. */
  17. /**
  18. * This advice is used to get the timeout value for the wrapped static
  19. * methods.
  20. *
  21. * @param t the annotation with the timeout value
  22. */
  23. before(Synchronized t) :
  24. staticinitialization(@Synchronized *) && @annotation(t) {
  25. /*
  26. this.timeout = t.timeout();
  27. this.unit = t.unit();
  28. if (log.isTraceEnabled()) {
  29. log.trace("lock timeout for "
  30. + thisJoinPointStaticPart.getSignature().getDeclaringType().getSimpleName()
  31. + " set to " + this.timeout + " " + this.unit);
  32. }
  33. */
  34. }
  35. private Log log = new Log();
  36. static class Log {
  37. public boolean isTraceEnabled() { return true;}
  38. public void trace(String s) {}
  39. public void error(String s) {}
  40. public void warn(String s,InterruptedException ie) {}
  41. }
  42. ClassLock classLock = new ClassLock();
  43. static class ClassLock {
  44. public boolean tryLock(Object o, Object b) {
  45. return true;
  46. }
  47. public void unlock(){}
  48. }
  49. String timeout="";
  50. String unit="";
  51. /**
  52. * This is the synchronization wrapper for the static methods which are
  53. * synchronized by a ReentrantLock class.
  54. *
  55. * @return the return value of the static method
  56. */
  57. @SuppressAjWarnings
  58. Object around() : SynchronizedAspect.synchronizedStaticMethods() {
  59. if (log.isTraceEnabled()) {
  60. log.trace("synchronizing " + thisJoinPointStaticPart.getSignature().toShortString() + "...");
  61. }
  62. try {
  63. if (classLock.tryLock(timeout, unit)) {
  64. if (log.isTraceEnabled()) {
  65. log.trace("lock granted for "
  66. + thisJoinPointStaticPart.getSignature().toShortString());
  67. }
  68. try {
  69. return proceed();
  70. } finally {
  71. classLock.unlock();
  72. if (log.isTraceEnabled()) {
  73. log.trace("lock released for "
  74. + thisJoinPointStaticPart.getSignature().toShortString());
  75. }
  76. }
  77. } else {
  78. String msg = "can't get " + classLock + " for "
  79. + thisJoinPointStaticPart.getSignature().toShortString();
  80. log.error(msg);
  81. throw new RuntimeException(msg);
  82. }
  83. } catch (InterruptedException ie) {
  84. String msg = "interrupted: "
  85. + thisJoinPoint.getSignature().toShortString();
  86. log.warn(msg, ie);
  87. throw new RuntimeException(msg, ie);
  88. }
  89. }
  90. }