public RemoteFile getRemoteFile() throws StorageException { return RemoteFile.createRemoteFile(remoteLocation); }
public TempRemoteFile getTempRemoteFile() { try { return RemoteFile.createRemoteFile(remoteTempLocation, TempRemoteFile.class); } catch (StorageException e) { logger.log(Level.INFO, "Invalid remote temporary filename: " + remoteTempLocation, e); return null; } }
@Override protected String validateName(String name) throws StorageException { Matcher matcher = NAME_PATTERN.matcher(name); if (!matcher.matches()) { throw new StorageException(name + ": remote filename pattern does not match: " + NAME_PATTERN.pattern() + " expected."); } try { targetRemoteFile = RemoteFile.createRemoteFile(matcher.group(2)); } catch (Exception e) { throw new StorageException(name + ": remote filename pattern does not match: " + NAME_PATTERN.pattern() + " expected."); } return name; } }
private <T extends RemoteFile> void list(String remoteFilePath, Map<String, T> remoteFiles, Class<T> remoteFileClass) throws StorageException { logger.log(Level.INFO, "Listing folder for files matching " + remoteFileClass.getSimpleName() + ": " + remoteFilePath); Map<String, FileType> folderList = pathAwareFeatureExtension.listFolder(remoteFilePath); for (Map.Entry<String, FileType> folderListEntry : folderList.entrySet()) { String fileName = folderListEntry.getKey(); FileType fileType = folderListEntry.getValue(); if (fileType == FileType.FILE) { try { remoteFiles.put(fileName, RemoteFile.createRemoteFile(fileName, remoteFileClass)); logger.log(Level.INFO, "- File: " + fileName); } catch (StorageException e) { // We don't care and ignore non-matching files! } } else if (fileType == FileType.FOLDER) { logger.log(Level.INFO, "- Folder: " + fileName); String newRemoteFilePath = remoteFilePath + folderSeparator + fileName; list(newRemoteFilePath, remoteFiles, remoteFileClass); } } }
@Override public <T extends RemoteFile> Map<String, T> list(Class<T> remoteFileClass) throws StorageException { connect(); Path folder = Paths.get(getRemoteFilePath(remoteFileClass)); Map<String, T> files = Maps.newHashMap(); try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(folder)) { for (Path path : directoryStream) { try { T remoteFile = RemoteFile.createRemoteFile(path.getFileName().toString(), remoteFileClass); files.put(path.getFileName().toString(), remoteFile); } catch (StorageException e) { logger.log(Level.INFO, "Cannot create instance of " + remoteFileClass.getSimpleName() + " for file " + path + "; maybe invalid file name pattern. Ignoring file."); } } } catch (IOException e) { logger.log(Level.SEVERE, "Unable to list directory", e); } return files; }
/** * Creates a remote file based on a name and derives the class name using the * file name. * * <p>The name must match the corresponding name pattern (nameprefix-...), and * the derived class can either be <tt>RemoteFile</tt>, or a sub-class thereof. * * @param name The name of the remote file * @return Returns a new object of the given class */ @SuppressWarnings("unchecked") public static <T extends RemoteFile> T createRemoteFile(String name) throws StorageException { String prefix = name.contains("-") ? name.substring(0, name.indexOf('-')) : name; String camelCasePrefix = StringUtil.toCamelCase(prefix); try { Class<T> remoteFileClass = (Class<T>) Class.forName(REMOTE_FILE_PACKAGE + "." + camelCasePrefix + REMOTE_FILE_SUFFIX); return createRemoteFile(name, remoteFileClass); } catch (ClassNotFoundException | StorageException e) { logger.log(Level.INFO, "Invalid filename for remote file " + name); throw new StorageException("Invalid filename for remote file " + name); } }