1 22 package org.jboss.resource.connectionmanager; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 30 import org.jboss.logging.Logger; 31 32 33 40 public class ConnectionValidator 41 { 42 43 44 private static final Logger log = Logger.getLogger(ConnectionValidator.class); 45 46 47 private Collection pools = new ArrayList (); 48 49 50 private long interval = Long.MAX_VALUE; 51 52 53 private long next = Long.MAX_VALUE; 55 56 private static final ConnectionValidator validator = new ConnectionValidator(); 57 58 59 private ConnectionValidator(){ 60 61 AccessController.doPrivileged(new PrivilegedAction () 62 { 63 public Object run() 64 { 65 Runnable runnable = new ConnectionValidatorRunnable(); 66 Thread removerThread = new Thread (runnable, "ConnectionValidator"); 67 removerThread.setDaemon(true); 68 removerThread.start(); 69 return null; 70 } 71 }); 72 73 } 74 75 public static void registerPool(InternalManagedConnectionPool mcp, long interval) 76 { 77 validator.internalRegisterPool(mcp, interval); 78 79 80 } 81 82 public static void unRegisterPool(InternalManagedConnectionPool mcp) 83 { 84 validator.internalUnregisterPool(mcp); 85 86 } 87 88 89 private void internalRegisterPool(InternalManagedConnectionPool mcp, long interval){ 90 91 synchronized (pools) 92 { 93 pools.add(mcp); 94 95 if (interval > 1 && interval/2 < this.interval) 96 { 97 this.interval = interval/2; 98 long maybeNext = System.currentTimeMillis() + this.interval; 99 if (next > maybeNext && maybeNext > 0) 100 { 101 next = maybeNext; 102 log.debug("internalRegisterPool: about to notify thread: old next: " + next + ", new next: " + maybeNext); 103 pools.notify(); 104 } 105 } 106 } 107 108 } 109 110 private void internalUnregisterPool(InternalManagedConnectionPool mcp){ 111 112 synchronized (pools) 113 { 114 pools.remove(mcp); 115 if (pools.size() == 0) 116 { 117 log.debug("internalUnregisterPool: setting interval to Long.MAX_VALUE"); 118 interval = Long.MAX_VALUE; 119 } 120 121 } 122 123 } 124 125 private void setupContextClassLoader() 126 { 127 final ClassLoader cl = IdleRemover.class.getClassLoader(); 129 if (cl == null) 130 return; 131 132 SecurityManager sm = System.getSecurityManager(); 133 if (sm == null) 134 Thread.currentThread().setContextClassLoader(cl); 135 136 AccessController.doPrivileged(new PrivilegedAction () 137 { 138 public Object run() 139 { 140 Thread.currentThread().setContextClassLoader(cl); 141 return null; 142 } 143 }); 144 } 145 146 public static void waitForBackgroundThread() 147 { 148 synchronized (validator.pools) 149 { 150 return; 151 } 152 } 153 154 private class ConnectionValidatorRunnable implements Runnable 155 { 156 157 public void run() 158 { 159 setupContextClassLoader(); 160 161 synchronized (pools) 162 { 163 164 while (true) 165 { 166 167 try 168 { 169 170 pools.wait(interval); 171 172 log.debug("run: ConnectionValidator notifying pools, interval: " + interval); 173 174 for(Iterator iter = pools.iterator(); iter.hasNext();) 175 { 176 177 InternalManagedConnectionPool mcp = (InternalManagedConnectionPool)iter.next(); 178 mcp.validateConnections(); 179 } 180 181 next = System.currentTimeMillis() + interval; 182 183 if(next < 0) 184 { 185 next = Long.MAX_VALUE; 186 187 } 188 189 } 190 catch (InterruptedException e) 191 { 192 log.info("run: ConnectionValidator has been interrupted, returning"); 193 return; 194 } 195 catch (RuntimeException e) 196 { 197 log.warn("run: ConnectionValidator ignored unexpected runtime exception", e); 198 } 199 catch (Exception e) 200 { 201 log.warn("run: ConnectionValidator ignored unexpected error", e); 202 203 } 204 205 206 } 207 208 } 209 210 } 211 212 } 213 } 214 | Popular Tags |