Skip to content

Commit

Permalink
Merge branch 'thirdparty' of https://github.com/itesla/ipst into thir…
Browse files Browse the repository at this point in the history
…dparty
  • Loading branch information
Mathieu Bague committed Nov 8, 2016
2 parents fb8a9f0 + 70998ba commit a106696
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,50 @@
*/
public class ComponentDefaultConfig {

public static ComponentDefaultConfig load() {
return new ComponentDefaultConfig();
}

private final ModuleConfig config;

public ComponentDefaultConfig() {
this(PlatformConfig.defaultConfig().getModuleConfig("componentDefaultConfig"));
this(PlatformConfig.defaultConfig().getModuleConfigIfExists("componentDefaultConfig"));
}

public ComponentDefaultConfig(ModuleConfig config) {
this.config = Objects.requireNonNull(config);
this.config = config;
}

public <T, U extends T> Class<? extends T> findFactoryImplClass(Class<T> factoryBaseClass) {
public <T> Class<? extends T> findFactoryImplClass(Class<T> factoryBaseClass) {
Objects.requireNonNull(factoryBaseClass);
String propertyName = factoryBaseClass.getSimpleName();
if (config == null) {
throw new RuntimeException("Property " + propertyName + " is not set");
}
return config.getClassProperty(propertyName, factoryBaseClass);
}

public <T, U extends T> Class<? extends T> findFactoryImplClass(Class<T> factoryBaseClass, Class<U> defaultFactoryImplClass) {
Objects.requireNonNull(factoryBaseClass);
Objects.requireNonNull(defaultFactoryImplClass);
String propertyName = factoryBaseClass.getSimpleName();
return config != null ? config.getClassProperty(propertyName, factoryBaseClass, defaultFactoryImplClass)
: defaultFactoryImplClass;
}

public <T> T newFactoryImpl(Class<T> factoryBaseClass) {
try {
return findFactoryImplClass(factoryBaseClass).newInstance();
} catch (IllegalAccessException|InstantiationException e) {
throw new RuntimeException(e);
}
}

public <T, U extends T> T newFactoryImpl(Class<T> factoryBaseClass, Class<U> defaultFactoryImplClass) {
try {
return findFactoryImplClass(factoryBaseClass, defaultFactoryImplClass).newInstance();
} catch (IllegalAccessException|InstantiationException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,37 @@ public class XmlPlatformConfig extends InMemoryPlatformConfig {
public XmlPlatformConfig(Path configDir, String configName, FileSystem fs) throws IOException, SAXException, ParserConfigurationException {
super(fs);
Path file = configDir.resolve(configName + ".xml");
LOGGER.info("Platform configuration defined by XML file {}", file);
try (InputStream is = Files.newInputStream(file)) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
Element root = doc.getDocumentElement();
root.normalize();
NodeList moduleNodes = root.getChildNodes();
for (int i = 0; i < moduleNodes.getLength(); i++) {
Node moduleNode = moduleNodes.item(i);
if (moduleNode.getNodeType() == Node.ELEMENT_NODE) {
String moduleName = moduleNode.getLocalName();
Map<Object, Object> properties = new HashMap<>();
NodeList propertyNodes = moduleNode.getChildNodes();
for (int j = 0; j < propertyNodes.getLength(); j++) {
Node propertyNode = propertyNodes.item(j);
if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
String propertyName = propertyNode.getLocalName();
Node child = propertyNode.getFirstChild();
String propertyValue = child != null ? child.getTextContent() : "";
properties.put(propertyName, propertyValue);
}
if (Files.exists(file)) {
LOGGER.info("Platform configuration defined by XML file {}", file);
try (InputStream is = Files.newInputStream(file)) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
Element root = doc.getDocumentElement();
root.normalize();
NodeList moduleNodes = root.getChildNodes();
for (int i = 0; i < moduleNodes.getLength(); i++) {
Node moduleNode = moduleNodes.item(i);
if (moduleNode.getNodeType() == Node.ELEMENT_NODE) {
String moduleName = moduleNode.getLocalName();
Map<Object, Object> properties = new HashMap<>();
NodeList propertyNodes = moduleNode.getChildNodes();
for (int j = 0; j < propertyNodes.getLength(); j++) {
Node propertyNode = propertyNodes.item(j);
if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
String propertyName = propertyNode.getLocalName();
Node child = propertyNode.getFirstChild();
String propertyValue = child != null ? child.getTextContent() : "";
properties.put(propertyName, propertyValue);
}
}
configs.put(moduleName, new MapModuleConfig(properties, fs));
}
configs.put(moduleName, new MapModuleConfig(properties, fs));
}
}
} else {
LOGGER.info("Platform configuration XML file {} not found", file);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,69 @@
*/
package eu.itesla_project.commons.config;

import eu.itesla_project.commons.config.ComponentDefaultConfig;
import eu.itesla_project.commons.config.InMemoryPlatformConfig;
import eu.itesla_project.commons.config.MapModuleConfig;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.nio.file.ShrinkWrapFileSystems;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.FileSystem;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

interface A {
}

class B implements A {
}

/**
* @author Mathieu Bague <mathieu.bague at rte-france.com>
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class ComponentDefaultConfigTest {

private FileSystem fileSystem;
private MapModuleConfig moduleConfig;
private ComponentDefaultConfig config;

@Before
public void setUp() throws IOException {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
fileSystem = ShrinkWrapFileSystems.newFileSystem(archive);
InMemoryPlatformConfig platformConfig = new InMemoryPlatformConfig(fileSystem);
moduleConfig = platformConfig.createModuleConfig("componentDefaultConfig");
config = new ComponentDefaultConfig(moduleConfig);
}

@After
public void tearDown() throws IOException {
fileSystem.close();
}

@Test
public void findFactoryImplClassTest() throws IOException {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class);
try (FileSystem fileSystem = ShrinkWrapFileSystems.newFileSystem(archive)) {
InMemoryPlatformConfig platformConfig = new InMemoryPlatformConfig(fileSystem);
MapModuleConfig moduleConfig = platformConfig.createModuleConfig("componentDefaultConfig");
moduleConfig.setStringProperty("Test", "org.junit.Test");

ComponentDefaultConfig config = new ComponentDefaultConfig(moduleConfig);
Assert.assertEquals(Test.class, config.findFactoryImplClass(Test.class));
}
moduleConfig.setStringProperty(A.class.getSimpleName(), B.class.getName());
assertEquals(B.class, config.findFactoryImplClass(A.class));
}

@Test
public void findFactoryImplClassDefaultTest() throws IOException {
assertEquals(B.class, config.findFactoryImplClass(A.class, B.class));
}

@Test
public void newFactoryImplTest() throws IOException {
moduleConfig.setStringProperty(A.class.getSimpleName(), B.class.getName());
assertTrue(config.newFactoryImpl(A.class) instanceof B);
}

@Test
public void newFactoryImplDefaultTest() throws IOException {
assertTrue(config.newFactoryImpl(A.class, B.class) instanceof B);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Command getCommand() {

@Override
public void run(CommandLine line) throws Exception {
ComponentDefaultConfig config = new ComponentDefaultConfig();
ComponentDefaultConfig config = ComponentDefaultConfig.load();
EurostagConfig eurostagConfig = EurostagConfig.load();
String caseFormat = line.getOptionValue("case-format");
String caseDirName = line.getOptionValue("case-dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public String getUsageFooter() {

@Override
public void run(CommandLine line) throws Exception {
ComponentDefaultConfig config = new ComponentDefaultConfig();
ComponentDefaultConfig config = ComponentDefaultConfig.load();
CaseRepository caseRepository = config.findFactoryImplClass(CaseRepositoryFactory.class).newInstance().create(LocalComputationManager.getDefault());
LoadFlowFactory loadFlowFactory = config.findFactoryImplClass(LoadFlowFactory.class).newInstance();
MergeOptimizerFactory mergeOptimizerFactory = config.findFactoryImplClass(MergeOptimizerFactory.class).newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class SecurityAnalysisFactoryImpl implements SecurityAnalysisFactory {
@Override
public SecurityAnalysis create(Network network, ComputationManager computationManager, int priority) {
ComponentDefaultConfig defaultConfig = new ComponentDefaultConfig();
ComponentDefaultConfig defaultConfig = ComponentDefaultConfig.load();
try {
LoadFlowFactory loadFlowFactory = defaultConfig.findFactoryImplClass(LoadFlowFactory.class).newInstance();
return new SecurityAnalysisImpl(network, computationManager, loadFlowFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void run(CommandLine line) throws Exception {
}
network.getStateManager().allowStateMultiThreadAccess(true);

ComponentDefaultConfig defaultConfig = new ComponentDefaultConfig();
ComponentDefaultConfig defaultConfig = ComponentDefaultConfig.load();
SecurityAnalysisFactory securityAnalysisFactory = defaultConfig.findFactoryImplClass(SecurityAnalysisFactory.class).newInstance();
SecurityAnalysis securityAnalysis = securityAnalysisFactory.create(network, LocalComputationManager.getDefault(), 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public String apply(SecurityIndex securityIndex) {

@Override
public void run(CommandLine line) throws Exception {
ComponentDefaultConfig config = new ComponentDefaultConfig();
ComponentDefaultConfig config = ComponentDefaultConfig.load();
String caseFormat = line.getOptionValue("case-format");
Path caseDir = Paths.get(line.getOptionValue("case-dir"));
String caseBaseName = null;
Expand Down

0 comments on commit a106696

Please sign in to comment.