View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2000 William Chesters
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     William Chesters <williamc At paneris.org>
42   *     http://paneris.org/~williamc
43   *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
44   */
45  package org.melati.poem.transaction;
46  
47  import java.util.Vector;
48  import java.util.Enumeration;
49  import java.io.OutputStream;
50  import java.io.InputStream;
51  import java.io.Reader;
52  import java.io.Writer;
53  import java.sql.SQLException;
54  import java.sql.Statement;
55  import java.sql.ResultSet;
56  
57  import org.melati.poem.PoemBugPoemException;
58  
59  /**
60   * List of objects which need closing when a <code>Transaction</code> is
61   * terminated.
62   */
63  public class ToTidyList {
64  
65    /** Can be closed. */
66    public interface Closeable {
67      /**
68       * Free any resources and prepare for death or reuse.
69       */
70      void close();
71    }
72  
73    private Vector<Object> objects = new Vector<Object>();
74  
75    private static void tidy(Object o) {
76      try {
77        if (o instanceof ResultSet) 
78          try { 
79            ((ResultSet)o).close();
80          } catch (SQLException e) {
81            // HACK MSAccess not playing nice
82            if (!e.getMessage().equals("ResultSet is closed"))
83              throw e;
84          }
85            
86        else if (o instanceof Statement)
87          ((Statement)o).close();
88        else if (o instanceof Reader)
89          ((Reader)o).close();
90        else if (o instanceof Writer)
91          ((Writer)o).close();
92        else if (o instanceof InputStream)
93          ((InputStream)o).close();
94        else if (o instanceof OutputStream)
95          ((OutputStream)o).close();
96        else if (o instanceof Closeable)
97          ((Closeable)o).close();
98      } catch (Exception e) {
99        throw new PoemBugPoemException("Unexpected object type in ToTidy: " + o.getClass(), e);
100     }
101   }
102 
103   /**
104    * Close all objects on list.
105    */
106   public synchronized void close() {
107     for (int i = objects.size() - 1; i >= 0; --i)
108       tidy(objects.elementAt(i));
109 
110     objects.setSize(0);
111   }
112 
113   private void addObject(Object o) {
114     objects.addElement(o);
115   }
116 
117   /**
118    * Add a ResultSet to the list.
119    * @param o the ResultSet to add
120    */
121   public void add(ResultSet o) {
122     addObject(o);
123   }
124 
125   /**
126    * Add a Statement to the list.
127    * @param o the Statement to add
128    */
129   public void add(Statement o) {
130     addObject(o);
131   }
132 
133   /**
134    * Add a Reader to the list.
135    * @param o the Reader to add
136    */
137   public void add(Reader o) {
138     addObject(o);
139   }
140 
141   /**
142    * Add a Writer to the list.
143    * @param o the Writer to add
144    */
145   public void add(Writer o) {
146     addObject(o);
147   }
148 
149   /**
150    * Add an InputStream to the list.
151    * @param o the InputStream to add
152    */
153   public void add(InputStream o) {
154     addObject(o);
155   }
156 
157   /**
158    * Add an OutputStream to the list.
159    * @param o the OutputStream to add
160    */
161   public void add(OutputStream o) {
162     addObject(o);
163   }
164 
165   /**
166    * Add any Closeable to the list.
167    * @param o the Closeable to add
168    */
169   public void add(Closeable o) {
170     addObject(o);
171   }
172 
173   /**
174    * @return an Enumeration of the items on the list
175    */
176   public Enumeration<Object> elements() {
177     return objects.elements();
178   }
179 }