1 package org.eclipse.jetty.jndi;
2
3 import java.lang.reflect.Method;
4 import java.sql.Statement;
5
6 import javax.sql.DataSource;
7
8 import org.eclipse.jetty.util.component.AggregateLifeCycle;
9 import org.eclipse.jetty.util.component.Destroyable;
10 import org.eclipse.jetty.util.log.Log;
11
12
13
14
15
16
17
18
19 public class DataSourceCloser implements Destroyable
20 {
21 final DataSource _datasource;
22 final String _shutdown;
23
24 public DataSourceCloser(DataSource datasource)
25 {
26 if (datasource==null)
27 throw new IllegalArgumentException();
28 _datasource=datasource;
29 _shutdown=null;
30 }
31
32 public DataSourceCloser(DataSource datasource,String shutdownSQL)
33 {
34 if (datasource==null)
35 throw new IllegalArgumentException();
36 _datasource=datasource;
37 _shutdown=shutdownSQL;
38 }
39
40 public void destroy()
41 {
42 try
43 {
44 if (_shutdown!=null)
45 {
46 Log.info("Shutdown datasource {}",_datasource);
47 Statement stmt = _datasource.getConnection().createStatement();
48 stmt.executeUpdate(_shutdown);
49 stmt.close();
50 }
51 }
52 catch (Exception e)
53 {
54 Log.warn(e);
55 }
56
57 try
58 {
59 Method close = _datasource.getClass().getMethod("close", new Class[]{});
60 Log.info("Close datasource {}",_datasource);
61 close.invoke(_datasource, new Object[]{});
62 }
63 catch (Exception e)
64 {
65 Log.warn(e);
66 }
67 }
68 }