htiextension/com.nokia.s60tools.hticonnection/src/com/nokia/s60tools/hticonnection/services/ftpservice/AbstractFileTransferRequest.java
author dpodwall
Tue, 12 Jan 2010 13:17:53 -0600
changeset 0 61163b28edca
permissions -rw-r--r--
initial EPL conversion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     1
/*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     3
* All rights reserved.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     4
* This component and the accompanying materials are made available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     6
* which accompanies this distribution, and is available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     8
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     9
* Initial Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    11
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    12
* Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    13
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    14
* Description:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    15
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    16
*/
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    17
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    18
package com.nokia.s60tools.hticonnection.services.ftpservice;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    19
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    20
import java.util.concurrent.FutureTask;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    21
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    22
import com.nokia.HTI.FTPService.FTPService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    23
import com.nokia.s60tools.hticonnection.core.AbstractRequest;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    24
import com.nokia.s60tools.hticonnection.core.RequestResult;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    25
import com.nokia.s60tools.hticonnection.services.IFTPListener;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    26
import com.nokia.s60tools.hticonnection.services.IFTPRequestManager;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    27
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    28
/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    29
 * Abstract file transfer class that hold implementation for canceling
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    30
 * file transfer operation.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    31
 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    32
public abstract class AbstractFileTransferRequest extends AbstractRequest implements IFTPRequestManager {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    33
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    34
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    35
	 * Listener for this operation.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    36
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    37
	private final IFTPListener listener;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    38
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    39
	 * File that is transfered.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    40
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    41
	protected final String remoteFile;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    42
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    43
	 * This service is set when file transfer has started.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    44
	 * This instance can be canceled then if needed. 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    45
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    46
	private FTPService startedService = null;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    47
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    48
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    49
	 * Constructor.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    50
	 * @param remoteFile File in target to be transfered.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    51
	 * @param listener Listener for this request. Or null if information is not needed.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    52
	 * @param requestName Name of this request.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    53
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    54
	public AbstractFileTransferRequest(String remoteFile, IFTPListener listener, String requestName){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    55
		super(requestName);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    56
		this.remoteFile = remoteFile;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    57
		this.listener = listener;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    58
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    59
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    60
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    61
	 * Getter for remote file name.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    62
	 * @return Remote file name.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    63
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    64
	public String getRemoteFileName() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    65
		return remoteFile;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    66
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    67
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    68
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    69
	 * Sets future task so that file transfer can be canceled by using it.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    70
	 * @param resultFutureTask
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    71
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    72
	public void setFutureTask(FutureTask<RequestResult> resultFutureTask) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    73
		this.resultFutureTask = resultFutureTask;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    74
		if(requestCanceled) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    75
			// Cancel has already been ordered.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    76
			resultFutureTask.cancel(false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    77
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    78
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    79
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    80
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    81
	 * Informs listener that request has been put into the queue.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    82
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    83
	public void informInQueue() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    84
		if(listener != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    85
			listener.requestInQueue(this);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    86
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    87
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    88
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    89
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    90
	 * Informs listener that request has been started.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    91
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    92
	public void informStarted() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    93
		if(listener != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    94
			listener.requestStarted(this);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    95
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    96
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    97
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    98
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    99
	 * Informs listener that request has been ended
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   100
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   101
	public void informEnded() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   102
		if(listener != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   103
			listener.requestEnded(this);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   104
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   105
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   106
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   107
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   108
	 * @see com.nokia.s60tools.hticonnection.services.IFTPRequestManager#getFileName()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   109
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   110
	public String getFileName() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   111
		return remoteFile;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   112
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   113
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   114
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   115
	 * @see com.nokia.s60tools.hticonnection.services.IFTPRequestManager#cancel()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   116
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   117
	@Override
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   118
	public void cancel() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   119
		requestCanceled = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   120
		FTPService service = getStartedService();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   121
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   122
		if(service != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   123
			service.cancelCurrentTransfer();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   124
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   125
		// Checking that future task has been created and request is not transferring the data.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   126
		// It future task is canceled when transfer is ongoing, then transfer would continue in background.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   127
		if(resultFutureTask != null && service == null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   128
			resultFutureTask.cancel(false);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   129
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   130
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   131
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   132
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   133
	 * Getter for service that is used in this request.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   134
	 * @param startedService Service that is used in this request.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   135
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   136
	public synchronized void setStartedService(FTPService startedService) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   137
		this.startedService = startedService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   138
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   139
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   140
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   141
	 * Setter for service that is used in this request.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   142
	 * @return Service that is used in this request.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   143
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   144
	public synchronized FTPService getStartedService() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   145
		return startedService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   146
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   147
}