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