001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ade.upload; 029 030import org.opencms.main.CmsLog; 031 032import java.io.Serializable; 033import java.util.Date; 034 035import org.apache.commons.logging.Log; 036 037/** 038 * A class which is executed in a new thread, so its able to detect 039 * when an upload process is frozen and sets an exception in order to 040 * be canceled. This doesn't work in Google application engine.<p> 041 */ 042public class CmsUploadTimeoutWatcher extends Thread implements Serializable { 043 044 /** The log object for this class. */ 045 private static final Log LOG = CmsLog.getLog(CmsUploadTimeoutWatcher.class); 046 047 /** The serial version UID. */ 048 private static final long serialVersionUID = -649803529271569237L; 049 050 /** The watchers interval. */ 051 private static final int WATCHER_INTERVAL = 5000; 052 053 /** Last bytes read. */ 054 private long m_lastBytesRead; 055 056 /** Timestamp for the last received data. */ 057 private long m_lastData = (new Date()).getTime(); 058 059 /** The listener to watch. */ 060 private CmsUploadListener m_listener; 061 062 /** 063 * A public constructor.<p> 064 * 065 * @param listener the listener to watch 066 */ 067 public CmsUploadTimeoutWatcher(CmsUploadListener listener) { 068 069 m_listener = listener; 070 } 071 072 /** 073 * Cancels the watch process.<p> 074 */ 075 public void cancel() { 076 077 m_listener = null; 078 } 079 080 /** 081 * The watching process.<p> 082 * 083 * @see java.lang.Thread#run() 084 */ 085 @Override 086 public void run() { 087 088 try { 089 Thread.sleep(WATCHER_INTERVAL); 090 } catch (InterruptedException e) { 091 LOG.error(Messages.get().container(Messages.ERR_UPLOAD_INTERRUPT_WATCH_DOG_1, m_listener.toString()), e); 092 } 093 if (m_listener != null) { 094 if (((m_listener.getBytesRead() > 0) && (m_listener.getPercent() >= 100)) || m_listener.isCanceled()) { 095 LOG.debug(Messages.get().container(Messages.LOG_UPLOAD_FINISHED_WATCHER_1, m_listener.toString())); 096 m_listener = null; 097 } else { 098 if (isFrozen()) { 099 m_listener.cancelUpload(new CmsUploadException( 100 Messages.get().getBundle().key( 101 Messages.ERR_UPLOAD_FROZEN_1, 102 Integer.valueOf(CmsUploadBean.DEFAULT_UPLOAD_TIMEOUT / 1000)))); 103 } else { 104 run(); 105 } 106 } 107 } 108 } 109 110 /** 111 * Returns <code>true</code> if the upload process is frozen.<p> 112 * 113 * @return <code>true</code> if the upload process is frozen 114 */ 115 private boolean isFrozen() { 116 117 long now = (new Date()).getTime(); 118 119 if (m_listener.getBytesRead() > m_lastBytesRead) { 120 m_lastData = now; 121 m_lastBytesRead = m_listener.getBytesRead(); 122 } else if ((now - m_lastData) > CmsUploadBean.DEFAULT_UPLOAD_TIMEOUT) { 123 return true; 124 } 125 return false; 126 } 127}