@Override public void connect() throws StorageException { if (repoPath == null) { throw new StorageException("Repository folder '" + repoPath + "' does not exist or is not writable."); } }
public AbstractTransferOperation(Config config, String operationName) { super(config); this.eventBus = LocalEventBus.getInstance(); try { // Do NOT reuse TransferManager for action file renewal; see #140 TransferManager actionFileTransferManager = TransferManagerFactory .build(config) .withFeature(ReadAfterWriteConsistent.class) .withFeature(Retriable.class) .asDefault(); TransactionAwareFeatureTransferManager regularFileTransferManager = TransferManagerFactory .build(config) .withFeature(ReadAfterWriteConsistent.class) .withFeature(Retriable.class) .withFeature(PathAware.class) .withFeature(TransactionAware.class) .as(TransactionAware.class); this.actionHandler = new ActionFileHandler(actionFileTransferManager, operationName, config.getMachineName()); this.transferManager = regularFileTransferManager; } catch (StorageException e) { logger.log(Level.SEVERE, "Unable to create AbstractTransferOperation: Unable to create TransferManager", e); throw new RuntimeException("Unable to create AbstractTransferOperation: Unable to create TransferManager: " + e.getMessage()); } }
@Override protected String validateName(String name) throws StorageException { if (!NAME_FORMAT.equals(name)) { throw new StorageException(name + ": remote filename pattern does not match: " + NAME_FORMAT + " expected."); } return name; } }
@Override protected String validateName(String name) throws StorageException { if (!NAME_FORMAT.equals(name)) { throw new StorageException(name + ": remote filename pattern does not match: " + NAME_FORMAT + " expected."); } return name; } }
@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."); } return name; } }
@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."); } cleanupNumber = Long.parseLong(matcher.group(1)); return name; }
@Override protected String validateName(String name) throws StorageException { Matcher matcher = NAME_PATTERN.matcher(name); if (!matcher.matches()) { throw new StorageException(name + ": remote database filename pattern does not match: " + NAME_PATTERN.pattern() + " expected."); } clientName = matcher.group(1); clientVersion = Long.parseLong(matcher.group(2)); return name; }
@Override protected String validateName(String name) throws StorageException { Matcher matcher = NAME_PATTERN.matcher(name); if (!matcher.matches()) { throw new StorageException(name + ": remote database filename pattern does not match: " + NAME_PATTERN.pattern() + " expected."); } operationName = matcher.group(1); clientName = matcher.group(2); timestamp = Long.parseLong(matcher.group(3)); return name; } }
/** * Creates a remote file based on a name and a class name. * * <p>The name must match the corresponding name pattern, and the class name * can either be <tt>RemoteFile</tt>, or a sub-class thereof. * * @param name The name of the remote file * @param remoteFileClass Class name of the object to instantiate, <tt>RemoteFile</tt> or a sub-class thereof * @return Returns a new object of the given class */ public static <T extends RemoteFile> T createRemoteFile(String name, Class<T> remoteFileClass) throws StorageException { try { return remoteFileClass.getConstructor(String.class).newInstance(name); } catch (Exception e) { throw new StorageException(e); } }
@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; } }
@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 { multiChunkId = StringUtil.fromHex(matcher.group(1)); } catch (Exception e) { throw new StorageException(name + ": remote filename pattern does not match (invalid hex): " + NAME_PATTERN.pattern() + " expected."); } return name; } }
private void cleanLocalRepository(Exception e) throws Exception { try { deleteAppDirs(options.getLocalDir()); } catch (Exception e1) { throw new StorageException("Couldn't upload to remote repo. Cleanup failed. There may be local directories left"); } throw new StorageException("Couldn't upload to remote repo. Cleaned local repository.", e); }
protected TransferSettings updateTransferSettings(TransferSettings transferSettings) throws StorageException { try { return askPluginSettings(transferSettings, new HashMap<String, String>()); } catch (Exception e) { logger.log(Level.SEVERE, "Unable to reload old plugin settings", e); throw new StorageException("Unable to reload old plugin settings: " + e.getMessage()); } }
protected File downloadFile(TransferManager transferManager, RemoteFile remoteFile) throws StorageException { try { File tmpRepoFile = File.createTempFile("syncanyfile", "tmp"); transferManager.download(remoteFile, tmpRepoFile); return tmpRepoFile; } catch (Exception e) { throw new StorageException("Unable to connect to repository.", e); } }
/** * This method serializes the current state of the {@link RemoteTransaction} to a file. * * @param transactionFile The file where the transaction will be written to. */ public void writeToFile(Transformer transformer, File transactionFile) throws StorageException { try { transactionTO.save(transformer, transactionFile); logger.log(Level.INFO, "Wrote transaction manifest to temporary file: " + transactionFile); } catch (Exception e) { throw new StorageException("Could not write transaction to file: " + transactionFile, e); } }
/** * Get a setting's value. * * @param key The field name as it is used in the {@link TransferSettings} * @return The value converted to a string using {@link Class#toString()} * @throws StorageException Thrown if the field either does not exist or isn't accessible */ public final String getField(String key) throws StorageException { try { Field field = this.getClass().getDeclaredField(key); field.setAccessible(true); Object fieldValueAsObject = field.get(this); if (fieldValueAsObject == null) { return null; } return fieldValueAsObject.toString(); } catch (NoSuchFieldException | IllegalAccessException e) { throw new StorageException("Unable to getField named " + key + ": " + e.getMessage()); } }
@Override public void init(boolean createIfRequired) throws StorageException { connect(); try { if (!testTargetExists() && createIfRequired) { if (!Files.exists(Files.createDirectory(repoPath))) { throw new StorageException("Cannot create repository directory: " + repoPath); } } if (!Files.exists(Files.createDirectory(multichunksPath))) { throw new StorageException("Cannot create multichunk directory: " + multichunksPath); } if (!Files.exists(Files.createDirectory(databasesPath))) { throw new StorageException("Cannot create databases directory: " + databasesPath); } if (!Files.exists(Files.createDirectory(actionsPath))) { throw new StorageException("Cannot create actions directory: " + actionsPath); } if (!Files.exists(Files.createDirectory(transactionsPath))) { throw new StorageException("Cannot create transactions directory: " + transactionsPath); } if (!Files.exists(Files.createDirectory(temporaryPath))) { throw new StorageException("Cannot create temporary directory: " + temporaryPath); } } catch (IOException e) { throw new StorageException("Unable to create directories", e); } }
/** * Creates an empty plugin-specific {@link org.syncany.plugins.transfer.TransferSettings} instance. * * @return Empty plugin-specific {@link org.syncany.plugins.transfer.TransferSettings} instance. * @throws StorageException Thrown if no {@link org.syncany.plugins.transfer.TransferSettings} are attached to a * plugin using {@link org.syncany.plugins.transfer.PluginSettings} */ @SuppressWarnings("unchecked") public final <T extends TransferSettings> T createEmptySettings() throws StorageException { final Class<? extends TransferSettings> transferSettings = TransferPluginUtil.getTransferSettingsClass(this.getClass()); if (transferSettings == null) { throw new StorageException("TransferPlugin does not have any settings attached!"); } try { return (T) transferSettings.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException("Unable to create TransferSettings: " + e.getMessage()); } }
@Override public void upload(File localFile, RemoteFile remoteFile) throws StorageException { connect(); File repoFile = getRemoteFile(remoteFile); File tempRepoFile = new File(getAbsoluteParentDirectory(repoFile) + File.separator + ".temp-" + repoFile.getName()); // Do not overwrite files with same size! if (repoFile.exists() && repoFile.length() == localFile.length()) { return; } // No such local file if (!localFile.exists()) { throw new StorageException("No such file on local disk: " + localFile); } try { FileUtils.copyFile(localFile, tempRepoFile); FileUtils.moveFile(tempRepoFile, repoFile); } catch (IOException ex) { throw new StorageException("Unable to copy file " + localFile + " to local repository " + repoFile, ex); } }
/** * Validate if all required fields are present. * * @throws StorageException Thrown if the validation failed due to missing field values. */ @Validate public final void validateRequiredFields() throws StorageException { logger.log(Level.FINE, "Validating required fields"); try { Field[] elementFields = ReflectionUtil.getAllFieldsWithAnnotation(this.getClass(), Element.class); for (Field field : elementFields) { field.setAccessible(true); if (field.getAnnotation(Element.class).required() && field.get(this) == null) { logger.log(Level.WARNING, "Missing mandatory field {0}#{1}", new Object[] { this.getClass().getSimpleName(), field.getName() }); throw new StorageException("Missing mandatory field " + this.getClass().getSimpleName() + "#" + field.getName()); } } } catch (IllegalAccessException e) { throw new RuntimeException("IllegalAccessException when validating required fields: ", e); } }