heting

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            40 隨筆 :: 9 文章 :: 45 評論 :: 0 Trackbacks
          void f();{    
          Cleaner cleaner
          =new Cleaner();;    
          try    
          {    
            Connection conn 
          = ;    
            cleaner.add(conn);;    
            Statement stmt 
          = conn.createStatement();;    
            cleaner.add(stmt );;    
            ResultSet rset 
          = ;    
            cleaner.add(rset );;    
             
          //using ResultSet   
            cleaner.clean();//close rset   
          }
              
          catch(Exception e);    
          {    
          throw new RuntimeException("",e);;    
          }
              
          finally    
          {    
          cleaner.cleanAll();;    
          }
              
          }
            

          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  
           *   
           
          */
            
          public final class Cleaner   
          {   
              
          public static interface CleanListener   
              
          {   
                  
          void catchClean(Object obj, Throwable t);;   
              }
             
              
          private final static CleanListener NONE_LISTENER = new CleanListener();   
              
          {   
                  
          public void catchClean(Object obj, Throwable t);   
                  
          {   
                  }
             
              }
          ;   
              
          private final static Map map = new HashMap();   
              
          {   
                  
          {   
                      put(Connection.
          class"close");;   
                      put(Statement.
          class"close");;   
                      put(PreparedStatement.
          class"close");;   
                      put(ResultSet.
          class"close");;   
                      put(InputStream.
          class"close");;   
                      put(Socket.
          class"close");;   
                  }
             
              }
          ;   
              
          private final Stack garbageContainer = new Stack();;   
              
          public void add(Object garbage);   
              
          {   
                 add(garbage, NONE_LISTENER);;   
              }
             
              
          public void add(Object garbage, CleanListener listener);   
              
          {   
                  garbageContainer.add(
          new Garbage(garbage, listener););;   
              }
             
              
          public void clean();   
              
          {   
                  
          if (garbageContainer.size(); > 0);   
                      
          try  
                      
          {   
                          doClean();;   
                      }
             
                      
          catch (Throwable e);   
                      
          {   
                          throwThrowable(e);;   
                      }
             
              }
             
              
          private void doClean(); throws Throwable   
              
          {   
                  Garbage garbage 
          = (Garbage); garbageContainer.pop();;   
                  garbage.clean();;   
              }
             
              
          public void cleanAll();   
              
          {   
                  Throwable throwable 
          = null;   
                  
          while (garbageContainer.size(); > 0);   
                      
          try  
                      
          {   
                          doClean();;   
                      }
             
                      
          catch (Throwable t);   
                      
          {   
                          
          if (throwable == null);   
                              throwable 
          = t;   
                      }
             
                  throwThrowable(throwable);;   
              }
             
              
          private void throwThrowable(Throwable throwable);   
              
          {   
                  
          if (throwable == null);   
                      
          return;   
                  
          if (throwable instanceof Error);   
                      
          throw (Error); throwable;   
                  
          else  
                      
          throw new RuntimeException("error in closing garbage!", throwable);;   
              }
             
              
          private static Method getMethod(Object obj);   
              
          {   
                  Class clazz 
          = obj.getClass();;   
                  String methodName 
          = getMethodName(clazz);;   
                  
          try  
                  
          {   
                      
          return clazz.getMethod(methodName, new Class[0]);;   
                  }
             
                  
          catch (Exception e);   
                  
          {   
                      
          throw new RuntimeException("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;   
                      }
             
                  }
             
              }
             
          }
            
          posted on 2009-01-10 10:01 賀挺 閱讀(180) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 萨迦县| 朝阳区| 拜泉县| 陆河县| 无锡市| 逊克县| 苍溪县| 东辽县| 揭东县| 思南县| 泽库县| 偏关县| 扎鲁特旗| 河池市| 重庆市| 弥渡县| 蒙自县| 永兴县| 高邮市| 贵南县| 昭平县| 金乡县| 定兴县| 左权县| 彭山县| 高唐县| 太保市| 上饶县| 六枝特区| 肥城市| 泾川县| 盖州市| 资源县| 武山县| 上饶市| 永康市| 霍山县| 晋江市| 姜堰市| 象山县| 翼城县|