
automately.core.file.VirtualFileService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of automately-core Show documentation
Show all versions of automately-core Show documentation
A Scalable Web Application Platform
package automately.core.file;
import automately.core.file.stores.AmazonS3Store;
import automately.core.file.stores.FileSystemStore;
import automately.core.services.core.AutomatelyService;
import com.hazelcast.core.IMap;
import io.jsync.app.core.Cluster;
import io.jsync.app.core.persistence.DataType;
import io.jsync.buffer.Buffer;
import io.jsync.json.JsonObject;
import java.lang.reflect.Method;
/**
* FileConductor is a service that is used to conduct some extra things
* for our VirtualFile API.
*/
public class VirtualFileService extends AutomatelyService {
public static String DEFAULT_STORE = FileSystemStore.class.getCanonicalName();
/**
* By default this is null. If it is null we will store the data directly.
*/
private static VirtualFileStore fileStore = null;
public static VirtualFileStore getFileStore() {
return fileStore;
}
public static void setFileStore(VirtualFileStore fileStore) {
if (fileStore == null) {
throw new NullPointerException("The fileStore cannot be null!");
}
VirtualFileService.fileStore = fileStore;
}
@Override
public void start(Cluster owner) {
owner.logger().info("Initializing the VirtualFileSystem.");
String store = coreConfig().getObject("file", new JsonObject()).getString("store", DEFAULT_STORE);
// This line allows us to keep support for the legacy configuration
if(store.equals("aws")){
store = AmazonS3Store.class.getCanonicalName();
}
try {
ClassLoader classLoader = getClass().getClassLoader();
Class clazz = classLoader.loadClass(store);
if(clazz != null){
if(VirtualFileStore.class.isAssignableFrom(clazz)){
cluster().logger().info("Using \"" + store + "\" as the VirtualFileStore.");
VirtualFileStore newInstance = (VirtualFileStore) clazz.newInstance();
VirtualFileService.setFileStore(newInstance);
} else {
cluster().logger().fatal("\"" + store + "\" is not a valid VirtualFileStore.");
}
}
} catch (Exception e){
cluster().logger().fatal(e.getMessage());
return;
}
if (fileStore == null) {
owner.logger().fatal("No FileStore was found!");
return;
}
fileStore.initialize(cluster());
VirtualFileSystem.initialize(owner);
IMap persistentGlobalTmpData = owner.data().persistentMap("global.temp.data");
persistentGlobalTmpData.loadAll(true);
if (!persistentGlobalTmpData.containsKey("checked_empty_files")) {
owner.logger().info("Checking for files with a size of 0 and ensuring they are not empty.");
try {
IMap files = owner.data().persistentMap("files");
for (VirtualFile file : files.values()) {
if (file.size == 0) {
Buffer data = VirtualFileSystem.readFileData(file);
if (data != null) {
file.size = data.length();
}
VirtualFileSystem.putUserFile(file.userToken, file);
}
}
} catch (Exception e) {
e.printStackTrace();
}
persistentGlobalTmpData.put("checked_empty_files", true);
}
}
@Override
public void stop() {
fileStore.stop();
}
@Override
public String name() {
return getClass().getCanonicalName();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy