extensions/net.sf.basedb.ftp/trunk/src/net/sf/basedb/clients/ftp/TransactionalOutputStream.java

Code
Comments
Other
Rev Date Author Line
716 02 Jun 08 nicklas 1 /**
716 02 Jun 08 nicklas 2   $Id $
716 02 Jun 08 nicklas 3
716 02 Jun 08 nicklas 4   Copyright (C) 2008 Nicklas Nordborg
716 02 Jun 08 nicklas 5
1381 15 Aug 11 martin 6   This file is part of the FTP Server extension for BASE.
716 02 Jun 08 nicklas 7   Available at http://baseplugins.thep.lu.se/
1381 15 Aug 11 martin 8   BASE main site: http://base.thep.lu.se/
1381 15 Aug 11 martin 9   -----------------------------------------------------------
1381 15 Aug 11 martin 10   
1381 15 Aug 11 martin 11   This is free software; you can redistribute it and/or
716 02 Jun 08 nicklas 12   modify it under the terms of the GNU General Public License
1381 15 Aug 11 martin 13   as published by the Free Software Foundation; either version 3
716 02 Jun 08 nicklas 14   of the License, or (at your option) any later version.
1381 15 Aug 11 martin 15   
1381 15 Aug 11 martin 16   The software is distributed in the hope that it will be useful,
716 02 Jun 08 nicklas 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
716 02 Jun 08 nicklas 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
716 02 Jun 08 nicklas 19   GNU General Public License for more details.
1381 15 Aug 11 martin 20   
1381 15 Aug 11 martin 21   You should have received a copy of the GNU General Public License
1381 15 Aug 11 martin 22   along with BASE. If not, see <http://www.gnu.org/licenses/>.
716 02 Jun 08 nicklas 23
716 02 Jun 08 nicklas 24 */
714 30 May 08 nicklas 25 package net.sf.basedb.clients.ftp;
714 30 May 08 nicklas 26
714 30 May 08 nicklas 27 import java.io.FilterOutputStream;
714 30 May 08 nicklas 28 import java.io.IOException;
714 30 May 08 nicklas 29 import java.io.OutputStream;
714 30 May 08 nicklas 30
716 02 Jun 08 nicklas 31 import org.slf4j.Logger;
716 02 Jun 08 nicklas 32 import org.slf4j.LoggerFactory;
716 02 Jun 08 nicklas 33
714 30 May 08 nicklas 34 import net.sf.basedb.core.DbControl;
714 30 May 08 nicklas 35
716 02 Jun 08 nicklas 36 /**
716 02 Jun 08 nicklas 37   An output stream implementation that has a {@link DbControl} attached
716 02 Jun 08 nicklas 38   to it. When the stream is {@link #close()}:ed the DbControl will be
716 02 Jun 08 nicklas 39   {@link DbControl#commit()}:ed. If the stream is never closed, the finalize
716 02 Jun 08 nicklas 40   method will rollback the transaction.
716 02 Jun 08 nicklas 41   
716 02 Jun 08 nicklas 42   @author Nicklas
716 02 Jun 08 nicklas 43   @version 1.0
716 02 Jun 08 nicklas 44 */
714 30 May 08 nicklas 45 public class TransactionalOutputStream 
714 30 May 08 nicklas 46   extends FilterOutputStream 
714 30 May 08 nicklas 47 {
714 30 May 08 nicklas 48
716 02 Jun 08 nicklas 49   private static final Logger log = LoggerFactory.getLogger(TransactionalOutputStream.class);
716 02 Jun 08 nicklas 50   private DbControl dc;
716 02 Jun 08 nicklas 51   private final String filename;
714 30 May 08 nicklas 52   
716 02 Jun 08 nicklas 53   /**
716 02 Jun 08 nicklas 54     Create a new stream.
716 02 Jun 08 nicklas 55     @param dc The DbControl which should be committed when
716 02 Jun 08 nicklas 56       the stream is closed
716 02 Jun 08 nicklas 57     @param out The stream to write to
716 02 Jun 08 nicklas 58   */
716 02 Jun 08 nicklas 59   public TransactionalOutputStream(DbControl dc, OutputStream out, String filename)
714 30 May 08 nicklas 60   {
714 30 May 08 nicklas 61     super(out);
714 30 May 08 nicklas 62     this.dc = dc;
716 02 Jun 08 nicklas 63     this.filename = filename;
714 30 May 08 nicklas 64   }
714 30 May 08 nicklas 65   
716 02 Jun 08 nicklas 66   /*
716 02 Jun 08 nicklas 67     From the OutputStream class
716 02 Jun 08 nicklas 68     ---------------------------
716 02 Jun 08 nicklas 69   */
714 30 May 08 nicklas 70   @Override
714 30 May 08 nicklas 71   public void close() 
714 30 May 08 nicklas 72     throws IOException 
714 30 May 08 nicklas 73   {
716 02 Jun 08 nicklas 74     log.debug("Closing upload to: " + filename);
714 30 May 08 nicklas 75     super.close();
716 02 Jun 08 nicklas 76     if (!dc.isConnected()) dc.reconnect();
714 30 May 08 nicklas 77     dc.commit();
716 02 Jun 08 nicklas 78     dc = null;
714 30 May 08 nicklas 79   }
716 02 Jun 08 nicklas 80   // ------------------------------
716 02 Jun 08 nicklas 81
716 02 Jun 08 nicklas 82   /*
716 02 Jun 08 nicklas 83     From the Object class
716 02 Jun 08 nicklas 84     ---------------------------
716 02 Jun 08 nicklas 85   */
716 02 Jun 08 nicklas 86   /**
716 02 Jun 08 nicklas 87     Make sure the DbControl is closed.
716 02 Jun 08 nicklas 88   */
716 02 Jun 08 nicklas 89   @Override
716 02 Jun 08 nicklas 90   protected void finalize() 
716 02 Jun 08 nicklas 91     throws Throwable 
716 02 Jun 08 nicklas 92   {
716 02 Jun 08 nicklas 93     if (dc != null) dc.close();
716 02 Jun 08 nicklas 94     super.finalize();
716 02 Jun 08 nicklas 95   }
716 02 Jun 08 nicklas 96   // ------------------------------
714 30 May 08 nicklas 97   
714 30 May 08 nicklas 98 }