This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
FileHandler.java
221 lines (188 loc) · 6.63 KB
/
FileHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*******************************************************************************
* Copyright (c) 2019- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Initial API and implementation and/or initial documentation -
* Jay Jay Billings, Joe Osborn
*******************************************************************************/
package org.eclipse.ice.commands;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The FileHandler class is a utility class for using commands that move files.
* The class uses source and destination designations to identify how files
* should be handled, and it can handle both local and remote files as sources
* and destinations. Files can be moved, copied or checked for existence.
*
* @author Jay Jay Billings, Joe Osborn
*
*/
public abstract class FileHandler implements IFileHandler {
/**
* Logger for handling event messages and other information.
*/
protected static final Logger logger = LoggerFactory.getLogger(FileHandler.class);
/**
* The command member variable that will actually execute the transfer that was
* requested by the user
*/
Command command;
/**
* A status member variable that indicates the status of the file transfer. See
* also {@link org.eclipse.ice.commands.CommandStatus}
*/
CommandStatus transferStatus;
/**
* Default constructor
*/
public FileHandler() {
}
/**
* This method is responsible for moving a file from a source to a destination
* path If the operation fails, an exception is thrown
*
* @return - CommandStatus - a CommandStatus indicating whether or not the move
* was successful
* @throws IOException
*/
public CommandStatus move(final String source, final String destination) throws IOException {
// Check the file existence. If they don't exist, an exception is thrown
checkExistence(source, destination);
// Set the commands to have the appropriate properties
configureMoveCommand(source, destination);
// Execute and process the file transfer
transferStatus = executeTransfer(destination);
// Return whether or not it succeeded
return transferStatus;
}
/**
* This method is responsible for copying a file from a source to a destination
* path If the operation fails, an exception is thrown
*
* @return - CommandStatus - a CommandStatus indicating whether or not the copy
* was successful
* @throws IOException
*/
public CommandStatus copy(final String source, final String destination) throws IOException {
// Check the file existence. If one or both don't exist, an exception is thrown
checkExistence(source, destination);
// Set the commands to have the appropriate properties
configureCopyCommand(source, destination);
// Execute and process the file transfer
transferStatus = executeTransfer(destination);
// Return whether or not it succeeded
return transferStatus;
}
/**
* This function sets the command member variables to have the source and
* destination strings. It is delegated to the subclasses so that the commands
* can be cast appropriately
*
* @param source
* @param destination
*/
protected abstract void configureMoveCommand(final String source, final String destination);
/**
* This function sets the command member variables to have the source and
* destination strings. It is delegated to the subclasses so that the commands
* can be cast appropriately
*
* @param source
* @param destination
*/
protected abstract void configureCopyCommand(final String source, final String destination);
/**
* This method is responsible for determining whether or not a file or directory
* already exists for a given path.
*
* @param - String - a string with the path for the method to check its
* existence
* @return - boolean indicating whether or not the file exists (returns true) or
* does not exist (returns false)
* @throws IOException
*/
public abstract boolean exists(final String file) throws IOException;
/**
* This method checks the existence of the source and destination files. If the
* destination doesn't exist, it tries to make it. If the destination can't be
* made, or the source doesn't exist, the method throws an exception.
*
* @param source
* @param destination
* @return
* @throws IOException
*/
public abstract void checkExistence(final String source, final String destination) throws IOException;
/**
* This function gets and returns the private member variable command of
* type Command
*
* @return Command - the command associated with this FileHandler
*/
public Command getCommand() {
return command;
}
/**
* This operation creates all the directories that are parents of the
* destination.
*
* @param dest the destination for which parent directories should be created
* @return true if the directories were created
* @throws IOException thrown if the dest cannot be created
*/
protected boolean createDirectories(String dest) throws IOException {
boolean exists = false;
if (!exists(dest)) {
try {
Path destination = Paths.get(dest);
Files.createDirectories(destination);
// If an exception wasn't thrown, then destination now exists
exists = exists(dest);
} catch (IOException e) {
logger.error("Couldn't create directory for local move! Failed.");
e.printStackTrace();
}
}
return exists;
}
/**
* This function actually executes the file transfer and then checks that it was
* completed correctly
*
* @param destination - destination for the file to go to
* @return - CommandStatus indicating whether or not the transfer completed
* successfully
*/
protected CommandStatus executeTransfer(final String destination) {
// Execute the file transfer
transferStatus = command.execute();
// Check that the move succeeded
boolean check = false;
try {
check = exists(destination);
} catch (IOException e) {
e.printStackTrace();
}
if (check)
return CommandStatus.SUCCESS;
else
return CommandStatus.FAILED;
}
/**
* This function returns the current status of the transfer, as it is given by the
* member variable {@link org.eclipse.ice.commands.FileHandler#copyCommand}
*
* @return - CommandStatus indicating the status of the file transfer
*/
public CommandStatus getStatus() {
return command.getStatus();
}
}