@Override public void validateOnCreate() { if (!new FileHelper().isWritableDirectory(getStorePath())) { throw new IllegalConfigurationException("The store path is not writable directory"); } }
public Path createNewFile(File newFile, String posixFileAttributes) throws IOException { return createNewFile(newFile.toPath(), posixFileAttributes); }
public Path createNewFile(Path newFile, Set<PosixFilePermission> permissions) throws IOException { if (!Files.exists(newFile)) { newFile = Files.createFile(newFile); } if (permissions != null && isPosixFileSystem(newFile)) { Files.setPosixFilePermissions(newFile, permissions); } return newFile; }
@Override protected void onCreate() { super.onCreate(); File file = new File(_path); if (!file.exists()) { File parent = file.getAbsoluteFile().getParentFile(); if (!parent.exists() && !parent.mkdirs()) { throw new IllegalConfigurationException(String.format("Cannot create groups file at '%s'",_path)); } try { String posixFileAttributes = getContextValue(String.class, SystemConfig.POSIX_FILE_PERMISSIONS); new FileHelper().createNewFile(file, posixFileAttributes); } catch (IOException e) { throw new IllegalConfigurationException(String.format("Cannot create groups file at '%s'", _path), e); } } }
public void writeFileSafely(Path targetFile, Path backupFile, Path tmpFile, BaseAction<File, IOException> write) throws IOException { Files.deleteIfExists(tmpFile); Files.deleteIfExists(backupFile); Set<PosixFilePermission> permissions = null; if (Files.exists(targetFile) && isPosixFileSystem(targetFile)) { permissions = Files.getPosixFilePermissions(targetFile); } tmpFile = createNewFile(tmpFile, permissions); write.performAction(tmpFile.toFile()); atomicFileMoveOrReplace(targetFile, backupFile); if (permissions != null) { Files.setPosixFilePermissions(backupFile, permissions); } atomicFileMoveOrReplace(tmpFile, targetFile); Files.deleteIfExists(tmpFile); Files.deleteIfExists(backupFile); }
private synchronized void writeGroupFile(final String groupFile) throws IOException { final Properties propertiesFile = new Properties(); for (String group : _groupToUserMap.keySet()) { Set<String> users = _groupToUserMap.get(group); final String userList = Joiner.on(",").useForNull("").join(users); propertiesFile.setProperty(group + ".users", userList); } new FileHelper().writeFileSafely(new File(groupFile).toPath(), new BaseAction<File, IOException>() { @Override public void performAction(File file) throws IOException { String comment = "Written " + new Date(); try(FileOutputStream fileOutputStream = new FileOutputStream(file)) { propertiesFile.store(fileOutputStream, comment); } } }); }
protected AbstractJsonFileStore() { _fileHelper = new FileHelper(); }
storeFile = _fileHelper.createNewFile(storeFile, posixFileAttributes); getSerialisationObjectMapper().writeValue(storeFile.toFile(), initialData); _fileHelper.atomicFileMoveOrReplace(backupFile, storeFile);
protected void savePasswordFile() throws IOException { try { _userUpdate.lock(); _fileHelper.writeFileSafely(_passwordFile.toPath(), new BaseAction<File,IOException>() { @Override public void performAction(File file) throws IOException { writeToFile(file); } }); } finally { _userUpdate.unlock(); } }
@Override protected void onCreate() { super.onCreate(); File passwordFile = new File(_path); if (!passwordFile.exists()) { try { Path path = new FileHelper().createNewFile(passwordFile, getContextValue(String.class, SystemConfig.POSIX_FILE_PERMISSIONS)); if (!Files.exists(path)) { throw new IllegalConfigurationException(String.format("Cannot create password file at '%s'", _path)); } } catch (IOException e) { throw new IllegalConfigurationException(String.format("Cannot create password file at '%s'", _path), e); } } }
public void writeFileSafely(Path targetFile, BaseAction<File, IOException> operation) throws IOException { String name = targetFile.toFile().getName(); writeFileSafely(targetFile, targetFile.resolveSibling(name + ".bak"), targetFile.resolveSibling(name + ".tmp"), operation); }
@Override protected void validateMessageStoreCreation() { if (!new FileHelper().isWritableDirectory(getStorePath())) { throw new IllegalConfigurationException("The store path is not writable directory"); } }
protected void save(final Object data) { try { Path tmpFile = new File(_directoryName, _tempFileName).toPath(); _fileHelper.writeFileSafely( new File(_directoryName, _configFileName).toPath(), new File(_directoryName, _backupFileName).toPath(), tmpFile, new BaseAction<File, IOException>() { @Override public void performAction(File file) throws IOException { getSerialisationObjectMapper().writeValue(file, data); } }); } catch (IOException e) { throw new StoreException("Cannot save to store", e); } }
public Path createNewFile(Path newFile, String posixFileAttributes) throws IOException { Set<PosixFilePermission> permissions = posixFileAttributes == null ? null : PosixFilePermissions.fromString(posixFileAttributes); return createNewFile(newFile, permissions ); }