From db7d2afe822948ca2da378b45ec8dcee7dc097bc Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 6 Jan 2011 11:16:01 +0000 Subject: [PATCH] file destroyer git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2628 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../eclipse/jetty/jndi/DataSourceCloser.java | 43 +++++++++--- .../jetty/util/component/FileDestroyable.java | 68 +++++++++++++++++++ 2 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 jetty-util/src/main/java/org/eclipse/jetty/util/component/FileDestroyable.java 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); + } + } + } + +}