KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > connectionmanager > ConnectionValidator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.connectionmanager;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.jboss.logging.Logger;
31
32
33 /**
34  * A ConnectionValidator that performs background validation of managed connections for an
35  * InternalManagedConnectionPool.
36  *
37  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
38  * @version $Revision: 45410 $
39  */

40 public class ConnectionValidator
41 {
42    
43    /** The log */
44    private static final Logger log = Logger.getLogger(ConnectionValidator.class);
45    
46    /** The pools */
47    private Collection JavaDoc pools = new ArrayList JavaDoc();
48    
49    /** The interval */
50    private long interval = Long.MAX_VALUE;
51
52    /** The next */
53    private long next = Long.MAX_VALUE;//important initialization!
54

55    /** The validator */
56    private static final ConnectionValidator validator = new ConnectionValidator();
57    
58    
59    private ConnectionValidator(){
60       
61       AccessController.doPrivileged(new PrivilegedAction JavaDoc()
62       {
63          public Object JavaDoc run()
64          {
65             Runnable JavaDoc runnable = new ConnectionValidatorRunnable();
66             Thread JavaDoc removerThread = new Thread JavaDoc(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       // Could be null if loaded from system classloader
128
final ClassLoader JavaDoc cl = IdleRemover.class.getClassLoader();
129       if (cl == null)
130          return;
131       
132       SecurityManager JavaDoc sm = System.getSecurityManager();
133       if (sm == null)
134          Thread.currentThread().setContextClassLoader(cl);
135       
136       AccessController.doPrivileged(new PrivilegedAction JavaDoc()
137       {
138          public Object JavaDoc 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 JavaDoc
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 JavaDoc 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 JavaDoc e)
191                {
192                   log.info("run: ConnectionValidator has been interrupted, returning");
193                   return;
194                }
195                catch (RuntimeException JavaDoc e)
196                {
197                   log.warn("run: ConnectionValidator ignored unexpected runtime exception", e);
198                }
199                catch (Exception JavaDoc 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