View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.jndi;
20  
21  import java.lang.reflect.Method;
22  import java.sql.Statement;
23  
24  import javax.sql.DataSource;
25  
26  import org.eclipse.jetty.util.component.ContainerLifeCycle;
27  import org.eclipse.jetty.util.component.Destroyable;
28  import org.eclipse.jetty.util.log.Log;
29  import org.eclipse.jetty.util.log.Logger;
30  
31  /**
32   * Close a DataSource.
33   * Some {@link DataSource}'s need to be close (eg. Atomikos).  This bean is a {@link Destroyable} and
34   * may be added to any {@link ContainerLifeCycle} so that {@link #destroy()}
35   * will be called.   The {@link #destroy()} method calls any no-arg method called "close" on the passed DataSource.
36   *
37   */
38  public class DataSourceCloser implements Destroyable
39  {
40      private static final Logger LOG = Log.getLogger(DataSourceCloser.class);
41  
42      final DataSource _datasource;
43      final String _shutdown;
44  
45      public DataSourceCloser(DataSource datasource)
46      {
47          if (datasource==null)
48              throw new IllegalArgumentException();
49          _datasource=datasource;
50          _shutdown=null;
51      }
52  
53      public DataSourceCloser(DataSource datasource,String shutdownSQL)
54      {
55          if (datasource==null)
56              throw new IllegalArgumentException();
57          _datasource=datasource;
58          _shutdown=shutdownSQL;
59      }
60  
61      public void destroy()
62      {
63          try
64          {
65              if (_shutdown!=null)
66              {
67                  LOG.info("Shutdown datasource {}",_datasource);
68                  Statement stmt = _datasource.getConnection().createStatement();
69                  stmt.executeUpdate(_shutdown);
70                  stmt.close();
71              }
72          }
73          catch (Exception e)
74          {
75              LOG.warn(e);
76          }
77  
78          try
79          {
80              Method close = _datasource.getClass().getMethod("close", new Class[]{});
81              LOG.info("Close datasource {}",_datasource);
82              close.invoke(_datasource, new Object[]{});
83          }
84          catch (Exception e)
85          {
86              LOG.warn(e);
87          }
88      }
89  }