
biz.gabrys.lesscss.extended.compiler.storage.DataStorageImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of extended-compiler Show documentation
Show all versions of extended-compiler Show documentation
Extended version of the LessCSS Compiler (Java library which compiles Less source files to the CSS code).
The newest version!
/*
* Extended LessCSS Compiler
* http://lesscss-extended-compiler.projects.gabrys.biz/
*
* Copyright (c) 2015 Adam Gabryś
*
* This file is licensed under the BSD 3-Clause (the "License").
* You may not use this file except in compliance with the License.
* You may obtain:
* - a copy of the License at project page
* - a template of the License at https://opensource.org/licenses/BSD-3-Clause
*/
package biz.gabrys.lesscss.extended.compiler.storage;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.FileUtils;
/**
* Default implementation of the {@link DataStorage}.
* @since 1.0
*/
public class DataStorageImpl implements DataStorage {
private final Object mutex = new Object();
/**
* The storage working directory.
* @since 2.1.0
*/
protected final File workingDirectory;
/**
* Constructs a new instance.
* @param workingDirectory the working directory.
* @throws IllegalArgumentException if working directory is equal to {@code null}.
* @since 1.0
*/
public DataStorageImpl(final File workingDirectory) {
if (workingDirectory == null) {
throw new IllegalArgumentException("Working directory cannot be null");
}
this.workingDirectory = workingDirectory;
}
/**
* Returns the working directory.
* @return the working directory.
* @since 2.1.0
*/
public File getWorkingDirectory() {
return workingDirectory;
}
public boolean hasData(final String fileName) {
return getFile(fileName) != null;
}
/**
* {@inheritDoc}
* @throws DataStorageException if an I/O error occurred.
* @since 1.0
*/
public void put(final String fileName, final File file) {
synchronized (mutex) {
final File cache = getFilePreparedForPut(fileName);
try {
FileUtils.copyFile(file, cache);
} catch (final IOException e) {
throw new DataStorageException(e);
}
}
}
public File getFile(final String fileName) {
synchronized (mutex) {
final File file = new File(workingDirectory, fileName);
if (file.exists()) {
return file;
}
return null;
}
}
/**
* {@inheritDoc}
* @throws DataStorageException if an I/O error occurred.
* @since 1.0
*/
public void put(final String fileName, final String text) {
synchronized (mutex) {
final File cache = getFilePreparedForPut(fileName);
try {
FileUtils.write(cache, text);
} catch (final IOException e) {
throw new DataStorageException(e);
}
}
}
/**
* {@inheritDoc}
* @throws DataStorageException if an I/O error occurred.
* @since 1.0
*/
public String getText(final String fileName) {
synchronized (mutex) {
final File cache = getFile(fileName);
if (cache == null) {
return null;
}
try {
return FileUtils.readFileToString(cache);
} catch (final IOException e) {
throw new DataStorageException(e);
}
}
}
/**
* {@inheritDoc}
* @throws DataStorageException if an I/O error occurred.
* @since 2.0
*/
public void put(final String fileName, final Collection lines) {
synchronized (mutex) {
final File cache = getFilePreparedForPut(fileName);
try {
FileUtils.writeLines(cache, lines);
} catch (final IOException e) {
throw new DataStorageException(e);
}
}
}
/**
* {@inheritDoc}
* @throws DataStorageException if an I/O error occurred.
* @since 1.0
*/
public List getLines(final String fileName) {
synchronized (mutex) {
final File cache = getFile(fileName);
if (cache == null) {
return null;
}
try {
return FileUtils.readLines(cache);
} catch (final IOException e) {
throw new DataStorageException(e);
}
}
}
/**
* {@inheritDoc}
* @throws DataStorageException if an I/O error occurred.
* @since 1.0
*/
public void delete(final String fileName) {
synchronized (mutex) {
getFilePreparedForPut(fileName);
}
}
private File getFilePreparedForPut(final String fileName) {
final File file = new File(workingDirectory, fileName);
if (file.exists() && !file.delete()) {
throw new DataStorageException(String.format("Cannot remove old source file \"%s\"", file));
}
return file;
}
/**
* {@inheritDoc}
* @throws DataStorageException if an I/O error occurred.
* @since 1.0
*/
public void deleteAll() {
synchronized (mutex) {
try {
FileUtils.cleanDirectory(workingDirectory);
} catch (final IOException e) {
throw new DataStorageException(e);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy