View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.drivers.fileSystems;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.gwe.utils.security.KeyStore;
22  import org.gwe.utils.services.BrokeredService;
23  import org.gwe.utils.services.PlainService;
24  
25  /**
26   * @author Marco Ruiz
27   * @since Sep 17, 2008
28   */
29  public class FileTransferer extends BrokeredService<Long> {
30  
31  	private static Log log = LogFactory.getLog(FileTransferer.class);
32  
33  	public FileTransferer(int maxParallelRequest, String name) {
34  	    super(maxParallelRequest, name + " File Transferer Service");
35      }
36  
37  	public Long transferFile(final String srcFile, final String destFile, final KeyStore keys) throws Exception {
38  		return processRequestBlocking(new PlainService<Long>() {
39  			public Long runService() throws Exception {
40  				return transferFileSync(srcFile, destFile, keys);
41  			}
42  		});
43  	}
44  
45  	public long transferFileSync(String srcFile, String destFile, KeyStore keys) throws Exception {
46  		log.info("Transfering file from '" + srcFile + "' to '" + destFile + "'"); 
47  		
48  		FileHandle srcFileHandle = null;
49  		FileHandle destFileHandle = null;
50  		try {
51  			srcFileHandle  = keys.createFileLink(srcFile).createHandle();
52  			try {
53  				if (!srcFileHandle.exists()) return 0;
54  			} catch(Exception e) { 
55  				// Ignore this precautionary verification
56  			}
57  			destFileHandle = keys.createFileLink(destFile).createHandle();
58  			srcFileHandle.copyTo(destFileHandle);
59  			
60  			log.info("File transfer operation completed! ('" + srcFile + "' --> '" + destFile + "')");
61  			return GridFileSystemUtils.computeSize(srcFileHandle, destFileHandle);
62  		} catch(Exception e) {
63  			throw e;
64  		} finally {
65  			GridFileSystemUtils.cleanUpHandle(srcFileHandle);
66  			GridFileSystemUtils.cleanUpHandle(destFileHandle);
67  		}
68  	}
69  }
70