import java.io.InputStream; import java.lang.reflect.Method; import java.net.Socket; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Stack;
/** * @author pufan * */ publicfinalclass Cleaner { ? ? publicstaticinterface CleanListener ? ? { ? ? ? ? void catchClean(Object obj, Throwable t); ? ? } ? ? privatefinalstatic CleanListener NONE_LISTENER = new CleanListener() ? ? { ? ? ? ? publicvoid catchClean(Object obj, Throwable t) ? ? ? ? { ? ? ? ? } ? ? }; ? ? privatefinalstaticMap map = newHashMap() ? ? { ? ? ? ? { ? ? ? ? ? ? put(Connection.class, "close"); ? ? ? ? ? ? put(Statement.class, "close"); ? ? ? ? ? ? put(PreparedStatement.class, "close"); ? ? ? ? ? ? put(ResultSet.class, "close"); ? ? ? ? ? ? put(InputStream.class, "close"); ? ? ? ? ? ? put(Socket.class, "close"); ? ? ? ? } ? ? }; ? ? privatefinalStack garbageContainer = newStack(); ? ? publicvoid add(Object garbage) ? ? { ? ? ? ? garbageContainer.add(new Garbage(garbage, NONE_LISTENER)); ? ? } ? ? publicvoid add(Object garbage, CleanListener listener) ? ? { ? ? ? ? garbageContainer.add(new Garbage(garbage, listener)); ? ? } ? ? publicvoid cleanAll() ? ? { ? ? ? ? Throwable throwable = null; ? ? ? ? while(garbageContainer.size() > 0) ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Garbage garbage = (Garbage) garbageContainer.pop(); ? ? ? ? ? ? ? ? garbage.clean(); ? ? ? ? ? ? } ? ? ? ? ? ? catch(Throwable t) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if(throwable == null) ? ? ? ? ? ? ? ? ? ? throwable = t; ? ? ? ? ? ? } ? ? ? ? if(throwable != null) ? ? ? ? ? ? if(throwable instanceof Error) ? ? ? ? ? ? ? ? throw (Error) throwable; ? ? ? ? ? ? else ? ? ? ? ? ? ? ? throw newRuntimeException("error in closing garbage!", throwable); ? ? } ? ? privatestaticMethod getMethod(Object obj) ? ? { ? ? ? ? Class clazz = obj.getClass(); ? ? ? ? String methodName = getMethodName(clazz); ? ? ? ? try ? ? ? ? { ? ? ? ? ? ? return clazz.getMethod(methodName, newClass[0]); ? ? ? ? } ? ? ? ? catch(Exception e) ? ? ? ? { ? ? ? ? ? ? throw newRuntimeException("can't find close method by " + obj, e); ? ? ? ? } ? ? } ? ? private static String getMethodName(Class clazz) ? ? { ? ? ? ? for (Iterator it = map.keySet().iterator(); it.hasNext() ; ) ? ? ? ? { ? ? ? ? ? ? Class c = (Class) it.next(); ? ? ? ? ? ? if (c.isAssignableFrom(clazz)) ? ? ? ? ? ? ? ? return (String) map.get(c); ? ? ? ? } ? ? ? ? return null; ? ? } ? ? private static class Garbage ? ? { ? ? ? ? Object obj; ? ? ? ? Method method; ? ? ? ? CleanListener listener; ? ? ? ? Garbage(Object obj, CleanListener listener) ? ? ? ? { ? ? ? ? ? ? method = getMethod(obj); ? ? ? ? ? ? this.obj = obj; ? ? ? ? ? ? this.listener = listener; ? ? ? ? } ? ? ? ? void clean() throws Throwable ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? method.invoke(obj, new Object[0]); ? ? ? ? ? ? } ? ? ? ? ? ? catch (Throwable t) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? listener.catchClean(obj, t); ? ? ? ? ? ? ? ? throw t; ? ? ? ? ? ? } ? ? ? ? } ? ? } }
由計劃的擦pipi
java代碼:?
|
Job job = new JdbcCloseJobImpl(); ? ? ? ? try ? ? ? ? { ? ? ? ? ? ? Connection conn = ... ? ? ? ? ? ? job.addJob(conn); ? ? ? ? ? ? Statement sta = ... ? ? ? ? ? ? job.addJob(sta); ? ? ? ? ? ? ResultSet rs = .. ? ? ? ? ? ? job.addJob(rs); ? ? ? ? }finally ? ? ? ? { ? ? ? ? ? ? job.startJob(); ? ? ? ? }
|
java代碼:?
|
public? interface Job {
? ? public? void startJob()throwsThrowable;
? ? publicvoid addJob(Object o);
}
|
java代碼:?
|
public
class JdbcCloseJobImpl implements Job { ? ? private java.util.Stack customerJob = newStack(); ? ? private java.util.List innerJob = newArrayList(); ? ? publicvoid startJob()throwsThrowable{ ? ? ? ? Object o = null; ? ? ? ? while((o = customerJob.pop()) != null) ? ? ? ? { ? ? ? ? ? ? if(o instanceof ResultSet) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? try{ ? ? ? ? ? ? ? ? ? ? ((ResultSet)o).close(); ? ? ? ? ? ? ? ? }catch(Throwable e){ ? ? ? ? ? ? ? ? ? ? innerJob.add(e); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? elseif(o instanceof Statement) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? try{ ? ? ? ? ? ? ? ? ? ? ((Statement)o).close(); ? ? ? ? ? ? ? ? }catch(Throwable e){ ? ? ? ? ? ? ? ? ? ? innerJob.add(e); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? elseif(o instanceof Connection) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? try{ ? ? ? ? ? ? ? ? ? ? ((Connection)o).close(); ? ? ? ? ? ? ? ? }catch(Throwable e){ ? ? ? ? ? ? ? ? ? ? innerJob.add(e); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }
? ? ? ? if(innerJob.size() > 0) ? ? ? ? { ? ? ? ? ? ? throw (Throwable) innerJob.get(0); ? ? ? ? } ? ? }
? ? publicvoid addJob(Object o){ ? ? ? ? customerJob.push(o); ? ? } }
|
|