Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Commit

Permalink
Add parameter to specify if a file system has to be accessible remote…
Browse files Browse the repository at this point in the history
…ly (#161)
  • Loading branch information
geofjamg authored and mathbagu committed Jul 25, 2017
1 parent 1efa68e commit a819f70
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 62 deletions.
23 changes: 19 additions & 4 deletions afs/afs-core/src/main/java/eu/itesla_project/afs/core/AppData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
*/
package eu.itesla_project.afs.core;

import eu.itesla_project.afs.storage.AppFileSystemStorage;
import eu.itesla_project.commons.util.ServiceLoaderCache;
import eu.itesla_project.computation.ComputationManager;
import eu.itesla_project.computation.local.LocalComputationManager;
import eu.itesla_project.iidm.import_.ImportersLoader;
import eu.itesla_project.iidm.import_.ImportersServiceLoader;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand Down Expand Up @@ -101,7 +103,7 @@ public Set<Class<? extends ProjectFile>> getProjectFileClasses() {
return projectFileClasses;
}

public FileExtension getFileExtension(Class<? extends File> fileClass) {
FileExtension getFileExtension(Class<? extends File> fileClass) {
Objects.requireNonNull(fileClass);
FileExtension extension = fileExtensions.get(fileClass);
if (extension == null) {
Expand All @@ -110,7 +112,7 @@ public FileExtension getFileExtension(Class<? extends File> fileClass) {
return extension;
}

public FileExtension getFileExtensionByPseudoClass(String filePseudoClass) {
FileExtension getFileExtensionByPseudoClass(String filePseudoClass) {
Objects.requireNonNull(filePseudoClass);
FileExtension extension = fileExtensionsByPseudoClass.get(filePseudoClass);
if (extension == null) {
Expand All @@ -119,7 +121,7 @@ public FileExtension getFileExtensionByPseudoClass(String filePseudoClass) {
return extension;
}

public ProjectFileExtension getProjectFileExtension(Class<?> projectFileOrProjectFileBuilderClass) {
ProjectFileExtension getProjectFileExtension(Class<?> projectFileOrProjectFileBuilderClass) {
Objects.requireNonNull(projectFileOrProjectFileBuilderClass);
ProjectFileExtension extension = projectFileExtensions.get(projectFileOrProjectFileBuilderClass);
if (extension == null) {
Expand All @@ -129,7 +131,7 @@ public ProjectFileExtension getProjectFileExtension(Class<?> projectFileOrProjec
return extension;
}

public ProjectFileExtension getProjectFileExtensionByPseudoClass(String projectFilePseudoClass) {
ProjectFileExtension getProjectFileExtensionByPseudoClass(String projectFilePseudoClass) {
Objects.requireNonNull(projectFilePseudoClass);
ProjectFileExtension extension = projectFileExtensionsByPseudoClass.get(projectFilePseudoClass);
if (extension == null) {
Expand All @@ -147,6 +149,19 @@ public ComputationManager getComputationManager() {
return computationManager;
}

public List<String> getRemotelyAccessibleFileSystemNames() {
return fileSystems.entrySet().stream()
.map(Map.Entry::getValue)
.filter(AppFileSystem::isRemotelyAccessible)
.map(AppFileSystem::getName)
.collect(Collectors.toList());
}

public AppFileSystemStorage getRemotelyAccessibleStorage(String fileSystemName) {
AppFileSystem afs = fileSystems.get(fileSystemName);
return afs != null ? afs.getStorage() : null;
}

@Override
public void close() throws Exception {
getFileSystems().forEach(AppFileSystem::close);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,30 @@ public class AppFileSystem implements AutoCloseable {

private final String name;

private final boolean remotelyAccessible;

private final AppFileSystemStorage storage;

private AppData data;

public AppFileSystem(String name, AppFileSystemStorage storage) {
public AppFileSystem(String name, boolean remotelyAccessible, AppFileSystemStorage storage) {
this.name = Objects.requireNonNull(name);
this.remotelyAccessible = remotelyAccessible;
this.storage = Objects.requireNonNull(storage);
}

public String getName() {
return name;
}

public boolean isRemotelyAccessible() {
return remotelyAccessible;
}

AppFileSystemStorage getStorage() {
return storage;
}

public Folder getRootFolder() {
return new Folder(storage.getRootNode(), storage, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void setup() throws IOException {
ImportersLoader importersLoader = new ImportersLoaderList(getImporters(), Collections.emptyList());
ComputationManager computationManager = Mockito.mock(ComputationManager.class);
storage = createStorage();
afs = new AppFileSystem("mem", storage);
afs = new AppFileSystem("mem", false, storage);
ad = new AppData(computationManager,
importersLoader,
Collections.singletonList(computationManager1 -> Collections.singletonList(afs)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package eu.itesla_project.afs.core;

import com.google.common.collect.ImmutableList;
import eu.itesla_project.afs.mapdb.storage.MapDbAppFileSystemStorage;
import eu.itesla_project.afs.storage.AppFileSystemStorage;
import eu.itesla_project.afs.storage.NodeId;
import eu.itesla_project.afs.mapdb.storage.MapDbAppFileSystemStorage;
import eu.itesla_project.computation.ComputationManager;
import eu.itesla_project.iidm.import_.ImportersLoader;
import eu.itesla_project.iidm.import_.ImportersLoaderList;
Expand Down Expand Up @@ -109,7 +109,7 @@ public void setup() throws IOException {

ImportersLoader importersLoader = new ImportersLoaderList(Collections.emptyList(), Collections.emptyList());
ComputationManager computationManager = Mockito.mock(ComputationManager.class);
afs = new AppFileSystem("mem", storage);
afs = new AppFileSystem("mem", true, storage);
ad = new AppData(computationManager, importersLoader, Collections.singletonList(computationManager1 -> Collections.singletonList(afs)),
Collections.emptyList(), Collections.singletonList(new FooFileExtension()));
}
Expand All @@ -123,6 +123,8 @@ public void tearDown() throws Exception {
public void baseTest() throws IOException {
assertSame(afs, ad.getFileSystem("mem"));
assertNull(ad.getFileSystem("???"));
assertEquals(Collections.singletonList("mem"), ad.getRemotelyAccessibleFileSystemNames());
assertNotNull(ad.getRemotelyAccessibleStorage("mem"));
assertEquals("mem", afs.getName());
assertEquals(1, ad.getProjectFileClasses().size());
Folder root = afs.getRootFolder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AppFileSystemToolTest() {
@Override
protected AppData createAppData() {
AppFileSystemStorage storage = MapDbAppFileSystemStorage.createHeap("mem");
AppFileSystem afs = new AppFileSystem("mem", storage);
AppFileSystem afs = new AppFileSystem("mem", false, storage);
AppData appData = new AppData(computationManager, importersLoader, Collections.singletonList(computationManager1 -> Collections.singletonList(afs)),
Collections.emptyList(), Collections.emptyList());
afs.getRootFolder().createProject("test_project1", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class LocalAppFileSystem extends AppFileSystem {
public LocalAppFileSystem(LocalAppFileSystemConfig config, ComputationManager computationManager, ImportConfig importConfig,
ImportersLoader importersLoader) {
super(config.getDriveName(),
new LocalAppFileSystemStorage(config.getRootDir(), config.getDriveName(), computationManager, importConfig, importersLoader));
config.isRemotelyAccessible(),
new LocalAppFileSystemStorage(config.getRootDir(), config.getDriveName(), computationManager, importConfig, importersLoader));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package eu.itesla_project.afs.local;

import eu.itesla_project.afs.core.AfsException;
import eu.itesla_project.afs.storage.AbstractAppFileSystemConfig;
import eu.itesla_project.commons.config.ModuleConfig;
import eu.itesla_project.commons.config.PlatformConfig;

Expand All @@ -19,9 +20,7 @@
/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class LocalAppFileSystemConfig {

private String driveName;
public class LocalAppFileSystemConfig extends AbstractAppFileSystemConfig<LocalAppFileSystemConfig> {

private Path rootDir;

Expand All @@ -33,17 +32,21 @@ public static List<LocalAppFileSystemConfig> load(PlatformConfig platformConfig)
List<LocalAppFileSystemConfig> configs = new ArrayList<>();
ModuleConfig moduleConfig = platformConfig.getModuleConfigIfExists("local-app-file-system");
if (moduleConfig != null) {
if (moduleConfig.hasProperty("drive-name") && moduleConfig.hasProperty("root-dir")) {
if (moduleConfig.hasProperty("drive-name")
&& moduleConfig.hasProperty("root-dir")) {
String driveName = moduleConfig.getStringProperty("drive-name");
boolean remotelyAccessible = moduleConfig.getBooleanProperty("remotely-accessible", DEFAULT_REMOTELY_ACCESSIBLE);
Path rootDir = moduleConfig.getPathProperty("root-dir");
configs.add(new LocalAppFileSystemConfig(driveName, rootDir));
configs.add(new LocalAppFileSystemConfig(driveName, remotelyAccessible, rootDir));
}
int maxAdditionalDriveCount = moduleConfig.getIntProperty("max-additional-drive-count", 0);
for (int i = 0; i < maxAdditionalDriveCount; i++) {
if (moduleConfig.hasProperty("drive-name-" + i) && moduleConfig.hasProperty("root-dir-" + i)) {
if (moduleConfig.hasProperty("drive-name-" + i)
&& moduleConfig.hasProperty("root-dir-" + i)) {
String driveName = moduleConfig.getStringProperty("drive-name-" + i);
boolean remotelyAccessible = moduleConfig.getBooleanProperty("remotely-accessible-" + i, DEFAULT_REMOTELY_ACCESSIBLE);
Path rootDir = moduleConfig.getPathProperty("root-dir-" + i);
configs.add(new LocalAppFileSystemConfig(driveName, rootDir));
configs.add(new LocalAppFileSystemConfig(driveName, remotelyAccessible, rootDir));
}
}
}
Expand All @@ -58,20 +61,11 @@ private static Path checkRootDir(Path rootDir) {
return rootDir;
}

public LocalAppFileSystemConfig(String driveName, Path rootDir) {
this.driveName = Objects.requireNonNull(driveName);
public LocalAppFileSystemConfig(String driveName, boolean remotelyAccessible, Path rootDir) {
super(driveName, remotelyAccessible);
this.rootDir = checkRootDir(rootDir).toAbsolutePath();
}

public String getDriveName() {
return driveName;
}

public LocalAppFileSystemConfig setDriveName(String driveName) {
this.driveName = Objects.requireNonNull(driveName);
return this;
}

public Path getRootDir() {
return rootDir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import java.nio.file.Files;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand All @@ -41,6 +40,7 @@ public void setUp() throws Exception {
moduleConfig.setPathProperty("root-dir", fileSystem.getPath("/work"));
moduleConfig.setStringProperty("max-additional-drive-count", "2");
moduleConfig.setStringProperty("drive-name-1", "local1");
moduleConfig.setStringProperty("remotely-accessible-1", "true");
moduleConfig.setPathProperty("root-dir-1", fileSystem.getPath("/work"));
}

Expand All @@ -56,8 +56,10 @@ public void loadTest() {
LocalAppFileSystemConfig config = configs.get(0);
LocalAppFileSystemConfig config1 = configs.get(1);
assertEquals("local", config.getDriveName());
assertFalse(config.isRemotelyAccessible());
assertEquals(fileSystem.getPath("/work"), config.getRootDir());
assertEquals("local1", config1.getDriveName());
assertTrue(config1.isRemotelyAccessible());
assertEquals(fileSystem.getPath("/work"), config1.getRootDir());
config.setDriveName("local2");
config.setRootDir(fileSystem.getPath("/tmp"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ public void tearDown() throws Exception {
@Test
public void test() {
ComputationManager computationManager = Mockito.mock(ComputationManager.class);
LocalAppFileSystemConfig config = new LocalAppFileSystemConfig("drive", fileSystem.getPath("/work"));
LocalAppFileSystemConfig config = new LocalAppFileSystemConfig("drive", true, fileSystem.getPath("/work"));
List<AppFileSystem> fileSystems = new LocalAppFileSystemProvider(Collections.singletonList(config),
new ImportConfig(),
new ImportersLoaderList(Collections.emptyList(), Collections.emptyList()))
.getFileSystems(computationManager);
assertEquals(1, fileSystems.size());
assertTrue(fileSystems.get(0) instanceof LocalAppFileSystem);
assertEquals("drive", fileSystems.get(0).getName());
assertTrue(fileSystems.get(0).isRemotelyAccessible());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@
import eu.itesla_project.afs.core.AppFileSystem;
import eu.itesla_project.afs.mapdb.storage.MapDbAppFileSystemStorage;

import java.nio.file.Path;
import java.util.Objects;
import java.util.function.BiFunction;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class MapDbAppFileSystem extends AppFileSystem {

public MapDbAppFileSystem(MapDbAppFileSystemConfig config,
BiFunction<String, Path, MapDbAppFileSystemStorage> storageProvider) {
super(Objects.requireNonNull(config).getDriveName(),
Objects.requireNonNull(storageProvider).apply(config.getDriveName(),
config.getDbFile()));
public MapDbAppFileSystem(String driveName, boolean remotelyAccessible, MapDbAppFileSystemStorage storage) {
super(driveName, remotelyAccessible, storage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package eu.itesla_project.afs.mapdb;

import eu.itesla_project.afs.core.AfsException;
import eu.itesla_project.afs.storage.AbstractAppFileSystemConfig;
import eu.itesla_project.commons.config.ModuleConfig;
import eu.itesla_project.commons.config.PlatformConfig;

Expand All @@ -19,9 +20,7 @@
/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class MapDbAppFileSystemConfig {

private String driveName;
public class MapDbAppFileSystemConfig extends AbstractAppFileSystemConfig<MapDbAppFileSystemConfig> {

private Path dbFile;

Expand All @@ -33,17 +32,21 @@ public static List<MapDbAppFileSystemConfig> load(PlatformConfig platformConfig)
List<MapDbAppFileSystemConfig> configs = new ArrayList<>();
ModuleConfig moduleConfig = platformConfig.getModuleConfigIfExists("mapdb-app-file-system");
if (moduleConfig != null) {
if (moduleConfig.hasProperty("drive-name") && moduleConfig.hasProperty("db-file")) {
if (moduleConfig.hasProperty("drive-name")
&& moduleConfig.hasProperty("db-file")) {
String driveName = moduleConfig.getStringProperty("drive-name");
boolean remotelyAccessible = moduleConfig.getBooleanProperty("remotely-accessible", DEFAULT_REMOTELY_ACCESSIBLE);
Path rootDir = moduleConfig.getPathProperty("db-file");
configs.add(new MapDbAppFileSystemConfig(driveName, rootDir));
configs.add(new MapDbAppFileSystemConfig(driveName, remotelyAccessible, rootDir));
}
int maxAdditionalDriveCount = moduleConfig.getIntProperty("max-additional-drive-count", 0);
for (int i = 0; i < maxAdditionalDriveCount; i++) {
if (moduleConfig.hasProperty("drive-name-" + i) && moduleConfig.hasProperty("db-file-" + i)) {
if (moduleConfig.hasProperty("drive-name-" + i)
&& moduleConfig.hasProperty("db-file-" + i)) {
String driveName = moduleConfig.getStringProperty("drive-name-" + i);
boolean remotelyAccessible = moduleConfig.getBooleanProperty("remotely-accessible-" + i, DEFAULT_REMOTELY_ACCESSIBLE);
Path rootDir = moduleConfig.getPathProperty("db-file-" + i);
configs.add(new MapDbAppFileSystemConfig(driveName, rootDir));
configs.add(new MapDbAppFileSystemConfig(driveName, remotelyAccessible, rootDir));
}
}
}
Expand All @@ -58,20 +61,11 @@ private static Path checkDbFile(Path dbFile) {
return dbFile;
}

public MapDbAppFileSystemConfig(String driveName, Path dbFile) {
this.driveName = Objects.requireNonNull(driveName);
public MapDbAppFileSystemConfig(String driveName, boolean remotelyAccessible, Path dbFile) {
super(driveName, remotelyAccessible);
this.dbFile = checkDbFile(dbFile);
}

public String getDriveName() {
return driveName;
}

public MapDbAppFileSystemConfig setDriveName(String driveName) {
this.driveName = Objects.requireNonNull(driveName);
return this;
}

public Path getDbFile() {
return dbFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public MapDbAppFileSystemProvider(List<MapDbAppFileSystemConfig> configs,
@Override
public List<AppFileSystem> getFileSystems(ComputationManager computationManager) {
return configs.stream()
.map(config -> new MapDbAppFileSystem(config, storageProvider))
.map(config -> {
MapDbAppFileSystemStorage storage = storageProvider.apply(config.getDriveName(), config.getDbFile());
return new MapDbAppFileSystem(config.getDriveName(), config.isRemotelyAccessible(), storage);
})
.collect(Collectors.toList());
}
}
Loading

0 comments on commit a819f70

Please sign in to comment.