1 16 17 package org.springframework.aop.target.dynamic; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import org.springframework.aop.TargetSource; 23 24 36 public abstract class AbstractRefreshableTargetSource implements TargetSource, Refreshable { 37 38 39 protected Log logger = LogFactory.getLog(getClass()); 40 41 protected Object targetObject; 42 43 private long refreshCheckDelay = -1; 44 45 private long lastRefreshCheck = -1; 46 47 private long lastRefreshTime = -1; 48 49 private long refreshCount = 0; 50 51 52 59 public void setRefreshCheckDelay(long refreshCheckDelay) { 60 this.refreshCheckDelay = refreshCheckDelay; 61 } 62 63 64 public Class getTargetClass() { 65 if (this.targetObject == null) { 66 refresh(); 67 } 68 return this.targetObject.getClass(); 69 } 70 71 74 public boolean isStatic() { 75 return false; 76 } 77 78 public final synchronized Object getTarget() { 79 if ((refreshCheckDelayElapsed() && requiresRefresh()) || this.targetObject == null) { 80 refresh(); 81 } 82 return this.targetObject; 83 } 84 85 88 public void releaseTarget(Object object) { 89 } 90 91 92 public final synchronized void refresh() { 93 logger.debug("Attempting to refresh target"); 94 95 this.targetObject = freshTarget(); 96 this.refreshCount++; 97 this.lastRefreshTime = System.currentTimeMillis(); 98 99 logger.debug("Target refreshed successfully"); 100 } 101 102 public long getRefreshCount() { 103 return this.refreshCount; 104 } 105 106 public long getLastRefreshTime() { 107 return this.lastRefreshTime; 108 } 109 110 111 private boolean refreshCheckDelayElapsed() { 112 if (this.refreshCheckDelay < 0) { 113 return false; 114 } 115 116 long currentTimeMillis = System.currentTimeMillis(); 117 118 if (this.lastRefreshCheck < 0 || currentTimeMillis - this.lastRefreshCheck > this.refreshCheckDelay) { 119 this.lastRefreshCheck = currentTimeMillis; 121 logger.debug("Refresh check delay elapsed - checking whether refresh is required"); 122 return true; 123 } 124 125 return false; 126 } 127 128 129 136 protected boolean requiresRefresh() { 137 return true; 138 } 139 140 146 protected abstract Object freshTarget(); 147 148 } 149 | Popular Tags |