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 import org.eclipse.jetty.util.log.Logger;
12
13
14
15
16
17
18
19
20 public class DataSourceCloser implements Destroyable
21 {
22 private static final Logger LOG = Log.getLogger(DataSourceCloser.class);
23
24 final DataSource _datasource;
25 final String _shutdown;
26
27 public DataSourceCloser(DataSource datasource)
28 {
29 if (datasource==null)
30 throw new IllegalArgumentException();
31 _datasource=datasource;
32 _shutdown=null;
33 }
34
35 public DataSourceCloser(DataSource datasource,String shutdownSQL)
36 {
37 if (datasource==null)
38 throw new IllegalArgumentException();
39 _datasource=datasource;
40 _shutdown=shutdownSQL;
41 }
42
43 public void destroy()
44 {
45 try
46 {
47 if (_shutdown!=null)
48 {
49 LOG.info("Shutdown datasource {}",_datasource);
50 Statement stmt = _datasource.getConnection().createStatement();
51 stmt.executeUpdate(_shutdown);
52 stmt.close();
53 }
54 }
55 catch (Exception e)
56 {
57 LOG.warn(e);
58 }
59
60 try
61 {
62 Method close = _datasource.getClass().getMethod("close", new Class[]{});
63 LOG.info("Close datasource {}",_datasource);
64 close.invoke(_datasource, new Object[]{});
65 }
66 catch (Exception e)
67 {
68 LOG.warn(e);
69 }
70 }
71 }