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 java.io.IOException;
20  import java.io.InputStream;
21  import java.io.ObjectInputStream;
22  import java.io.ObjectOutputStream;
23  import java.io.OutputStream;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.gwe.app.client.config.ClientConfig;
28  import org.gwe.app.client.config.XMLClientConfigReader;
29  import org.gwe.drivers.HandleOperationException;
30  import org.gwe.drivers.ResourceHandle;
31  import org.gwe.utils.IOUtils;
32  import org.gwe.utils.security.CredentialNotFoundException;
33  import org.gwe.utils.security.ResourceLink;
34  import org.gwe.utils.security.ThinURI;
35  
36  /**
37   * @author Marco Ruiz
38   * @since Jan 24, 2007
39   */
40  public abstract class FileHandle extends ResourceHandle {
41      private static Log log = LogFactory.getLog(FileHandle.class);
42  
43      public FileHandle(ResourceLink<FileHandle> link) { 
44      	super(link);
45      }
46  
47      public ThinURI getURI() { 
48      	return link.getURI(); 
49      }
50  
51      /**
52       * @return is this a directory?
53       * @throws HandleOperationException - system-level error.
54       */
55      public abstract boolean isDirectory() throws HandleOperationException;
56  
57      public abstract InputStream getInputStream() throws HandleOperationException;
58      
59      public abstract OutputStream getOutputStream() throws HandleOperationException;
60      	
61      public abstract void createFolder() throws HandleOperationException;
62  
63      public abstract boolean exists() throws HandleOperationException;
64      
65      public boolean delete() throws HandleOperationException {
66      	throw new HandleOperationException("Operation Not Supported!");
67      }
68      
69      public boolean isGlob() {
70          return link.getURI().toString().matches(".*[\\*\\?\\[].*");
71      }
72      
73      public <OBJECT_TYPE> OBJECT_TYPE readObject() throws HandleOperationException {
74  		try {
75  			ObjectInputStream ois = new ObjectInputStream(getInputStream());
76  			OBJECT_TYPE result = (OBJECT_TYPE) ois.readObject();
77  			ois.close();
78  			return result;
79  		} catch (IOException e) {
80  			throw new HandleOperationException("Could not read object from file", e);
81  		} catch (ClassNotFoundException e) {
82  			throw new HandleOperationException("Could not read object from file", e);
83  		}
84      }
85      
86      public void storeObject(Object toSerialize) throws HandleOperationException {
87  		try {
88  			ObjectOutputStream oos = new ObjectOutputStream(getOutputStream());
89  			oos.writeObject(toSerialize);
90  			oos.close();
91  		} catch (IOException e) {
92  			throw new HandleOperationException(e);
93  		}
94      }
95      
96      public void copyTo(FileHandle otherFile) throws HandleOperationException {
97      	if (!exists()) return; 
98      	if (isDirectory()) {
99      		copyToDir(otherFile);
100     	} else {
101 	        log.trace("Copying to " + otherFile.getURI().getPath());
102 	        try {
103 	        	IOUtils.pipeStreams(getInputStream(), otherFile.getOutputStream(), true);
104 	        } catch (IOException ioex) {
105 	            throw new HandleOperationException("I/O error reading contents from file", ioex);
106 	        }
107 			log.trace("Finished file copy");
108     	}
109     }
110 
111 	protected void copyToDir(FileHandle otherFile) throws HandleOperationException {
112 		// Create path to destination
113 		if (!otherFile.exists()) otherFile.createFolder(); 
114 		if (!otherFile.isDirectory()) 
115 			throw new HandleOperationException("Cannot copy a directory (" + this.getURI() + ") to a file destination (" + otherFile.getURI() + ")");
116 	    otherFile.delete();
117 	    
118 	    // Compress directory
119 	    FileHandle srcFileCompressed = createCompressedCopyHandle(true);
120 	    FileHandle otherFileCompressed = otherFile.createCompressedCopyHandle(false);
121 	    
122 	    // Copy compressed directory to temporary compressed file on destination path
123 	    srcFileCompressed.copyTo(otherFileCompressed);
124 	    srcFileCompressed.delete();
125 	    srcFileCompressed.close();
126 
127 	    // Decompress bundled directory just copied
128 	    otherFileCompressed.decompressInto(otherFile, IOUtils.getFileName(getPath()));
129 	    otherFileCompressed.delete();
130 	    otherFileCompressed.close();
131     }
132 
133 	protected FileHandle createCompressedCopyHandle(boolean createFile) throws HandleOperationException {
134     	throw new HandleOperationException("Cannot compress file " + getURI()); 
135     }
136 
137     protected void decompressInto(FileHandle otherFile, String directoryName) throws HandleOperationException {
138     	throw new HandleOperationException("Cannot decompress file " + getURI()); 
139     }
140 
141 	/**
142      * The equivalent of "ls" in a directory.
143      *
144      * @return a list of files in the directory.
145      * @throws HandleOperationException - system-level error.
146      */
147     public abstract FileHandle[] getChildren() throws HandleOperationException;
148 
149     /**
150      * @return number of bytes in the file
151      * @throws HandleOperationException - system-level error.
152      */
153     public abstract long getSize() throws HandleOperationException;
154     
155 	public String getPath() {
156 		return getURI().getPath();
157 	}
158 	
159 	public static void main(String[] args) throws CredentialNotFoundException, HandleOperationException, IOException {
160 		ClientConfig appConfig = new ClientConfig(new XMLClientConfigReader(null));
161 //		FileHandle srcDir = GWEAppContext.getGridFileSystem().createHandle("http://www.gridwizardenterprise.org/guides/", appConfig.getKeys());
162 		FileHandle srcDir = appConfig.getKeys().createFileLink("sftp://birn-cluster0.nbirn.net/home/mruiz/63Test").createHandle();
163 		FileHandle destDir = appConfig.getKeys().createFileLink("sftp://www.gridwizard.org/home/mruiz/final").createHandle();
164 		srcDir.copyTo(destDir);
165 	}
166 }