file destroyer

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2628 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2011-01-06 11:16:01 +00:00
parent a3efe35e4a
commit db7d2afe82
2 changed files with 102 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package org.eclipse.jetty.jndi; package org.eclipse.jetty.jndi;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.sql.Statement;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -18,26 +19,50 @@ import org.eclipse.jetty.util.log.Log;
public class DataSourceCloser implements Destroyable public class DataSourceCloser implements Destroyable
{ {
final DataSource _datasource; final DataSource _datasource;
final String _shutdown;
public DataSourceCloser(DataSource datasource) public DataSourceCloser(DataSource datasource)
{ {
if (datasource==null)
throw new IllegalArgumentException();
_datasource=datasource; _datasource=datasource;
_shutdown=null;
}
public DataSourceCloser(DataSource datasource,String shutdownSQL)
{
if (datasource==null)
throw new IllegalArgumentException();
_datasource=datasource;
_shutdown=shutdownSQL;
} }
public void destroy() public void destroy()
{ {
if (_datasource != null) try
{ {
try if (_shutdown!=null)
{ {
Method close = _datasource.getClass().getMethod("close", new Class[]{}); Log.info("Shutdown datasource {}",_datasource);
close.invoke(_datasource, new Object[]{}); Statement stmt = _datasource.getConnection().createStatement();
} stmt.executeUpdate(_shutdown);
catch (Exception e) stmt.close();
{
Log.warn(e);
} }
} }
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);
}
} }
} }

View File

@ -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<File> _files = new ArrayList<File>();
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<File> 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);
}
}
}
}