1 34 35 36 package com.micronova.jsp.tag; 37 38 import java.util.*; 39 40 41 42 public class SynchronizedTag extends YuzuTag 43 { 44 protected static final Map __lock = new HashMap(); 45 46 protected boolean _isHoldingLock; 47 protected long _waitTime; 48 protected Object _lockId; 49 50 protected void init() 51 { 52 super.init(); 53 54 _isHoldingLock = false; 55 _waitTime = 0L; 56 _lockId = null; 57 } 58 59 public void setWaitTime(Object expression) throws Exception 60 { 61 _waitTime = ((Long )evaluateAttribute("waitTime", expression, Long .class)).longValue(); 62 } 63 64 public void setLockId(Object expression) throws Exception 65 { 66 _lockId = evaluateAttribute("lockId", expression, Object .class); 67 } 68 69 public void doPrepare() throws Exception 70 { 71 Object lockId = _lockId; 72 73 Object lockObject = null; 74 75 Thread currentThread = Thread.currentThread(); 76 77 synchronized(__lock) 78 { 79 lockObject = __lock.get(lockId); 80 81 if (lockObject == null) 82 { 83 __lock.put(lockId, (lockId instanceof String ) ? currentThread : lockId); 84 } 85 } 86 87 if (currentThread != lockObject) 88 { 89 if (lockObject != null) 90 { 91 synchronized(lockObject) 92 { 93 lockObject.wait(_waitTime); 94 95 synchronized(__lock) 96 { 97 lockObject = __lock.get(lockId); 98 99 if (lockObject != null) 100 { 101 throw new Exception ("thread lock timeout"); 102 } 103 else 104 { 105 __lock.put(lockId, (lockId instanceof String ) ? currentThread: lockId); 106 } 107 } 108 } 109 } 110 111 _isHoldingLock = true; 112 } 113 114 super.doPrepare(); 115 } 116 117 public void cleanup() 118 { 119 super.cleanup(); 120 121 if (_isHoldingLock) 122 { 123 Object lockId = _lockId; 124 125 try 126 { 127 synchronized(__lock) 128 { 129 Object lockObject = __lock.remove(lockId); 130 131 if (lockObject != null) 132 { 133 synchronized(lockObject) 134 { 135 lockObject.notify(); 136 } 137 } 138 } 139 } 140 catch (Exception e) 141 { 142 e.printStackTrace(); 143 } 144 } 145 } 146 } 147 | Popular Tags |