1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
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
53
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
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
119 FileHandle srcFileCompressed = createCompressedCopyHandle(true);
120 FileHandle otherFileCompressed = otherFile.createCompressedCopyHandle(false);
121
122
123 srcFileCompressed.copyTo(otherFileCompressed);
124 srcFileCompressed.delete();
125 srcFileCompressed.close();
126
127
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
143
144
145
146
147 public abstract FileHandle[] getChildren() throws HandleOperationException;
148
149
150
151
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
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 }