diff --git a/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/DataSourceCloser.java b/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/DataSourceCloser.java index d4b53d1e329..4c3db979732 100644 --- a/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/DataSourceCloser.java +++ b/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/DataSourceCloser.java @@ -1,6 +1,7 @@ package org.eclipse.jetty.jndi; import java.lang.reflect.Method; +import java.sql.Statement; import javax.sql.DataSource; @@ -18,26 +19,50 @@ import org.eclipse.jetty.util.log.Log; public class DataSourceCloser implements Destroyable { final DataSource _datasource; + final String _shutdown; public DataSourceCloser(DataSource datasource) { + if (datasource==null) + throw new IllegalArgumentException(); _datasource=datasource; + _shutdown=null; + } + + public DataSourceCloser(DataSource datasource,String shutdownSQL) + { + if (datasource==null) + throw new IllegalArgumentException(); + _datasource=datasource; + _shutdown=shutdownSQL; } public void destroy() { - if (_datasource != null) + try { - try + if (_shutdown!=null) { - Method close = _datasource.getClass().getMethod("close", new Class[]{}); - close.invoke(_datasource, new Object[]{}); - } - catch (Exception e) - { - Log.warn(e); + Log.info("Shutdown datasource {}",_datasource); + Statement stmt = _datasource.getConnection().createStatement(); + stmt.executeUpdate(_shutdown); + stmt.close(); } } + catch (Exception e) + { + Log.warn(e); + } + + try + { + Method close = _datasource.getClass().getMethod("close", new Class[]{}); + Log.info("Close datasource {}",_datasource); + close.invoke(_datasource, new Object[]{}); + } + catch (Exception e) + { + Log.warn(e); + } } - } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/component/FileDestroyable.java b/jetty-util/src/main/java/org/eclipse/jetty/util/component/FileDestroyable.java new file mode 100644 index 00000000000..740007ab456 --- /dev/null +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/component/FileDestroyable.java @@ -0,0 +1,68 @@ +package org.eclipse.jetty.util.component; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.eclipse.jetty.util.IO; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.resource.Resource; + +public class FileDestroyable implements Destroyable +{ + final List _files = new ArrayList(); + + public FileDestroyable() + { + } + + public FileDestroyable(String file) throws IOException + { + _files.add(Resource.newResource(file).getFile()); + } + + public FileDestroyable(File file) + { + _files.add(file); + } + + public void addFile(String file) throws IOException + { + _files.add(Resource.newResource(file).getFile()); + } + + public void addFile(File file) + { + _files.add(file); + } + + public void addFiles(Collection files) + { + _files.addAll(files); + } + + public void removeFile(String file) throws IOException + { + _files.remove(Resource.newResource(file).getFile()); + } + + public void removeFile(File file) + { + _files.remove(file); + } + + public void destroy() + { + for (File file : _files) + { + if (file.exists()) + { + Log.debug("Destroy {}",file); + IO.delete(file); + } + } + } + +}