diff --git a/oxalis-commons/src/main/java/network/oxalis/commons/filesystem/detector/RelativePropertyHomeDetector.java b/oxalis-commons/src/main/java/network/oxalis/commons/filesystem/detector/RelativePropertyHomeDetector.java new file mode 100644 index 000000000..7f80cf1d4 --- /dev/null +++ b/oxalis-commons/src/main/java/network/oxalis/commons/filesystem/detector/RelativePropertyHomeDetector.java @@ -0,0 +1,59 @@ +/* + * Copyright 2010-2018 Norwegian Agency for Public Management and eGovernment (Difi) + * + * Licensed under the EUPL, Version 1.1 or – as soon they + * will be approved by the European Commission - subsequent + * versions of the EUPL (the "Licence"); + * + * You may not use this work except in compliance with the Licence. + * + * You may obtain a copy of the Licence at: + * + * https://joinup.ec.europa.eu/community/eupl/og_page/eupl + * + * Unless required by applicable law or agreed to in + * writing, software distributed under the Licence is + * distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the Licence for the specific language governing + * permissions and limitations under the Licence. + */ + +package network.oxalis.commons.filesystem.detector; + +import lombok.extern.slf4j.Slf4j; +import network.oxalis.api.filesystem.HomeDetector; +import network.oxalis.api.util.Sort; +import org.kohsuke.MetaInfServices; + +import java.io.File; + +/** + * @author ERST + */ +@Slf4j +@Sort(2500) +@MetaInfServices +public class RelativePropertyHomeDetector implements HomeDetector { + + protected static final String VARIABLE = "RELATIVE_OXALIS_HOME"; + + @Override + public File detect() { + String value = System.getProperty(VARIABLE); + if (value == null || value.isEmpty()) + return null; + + if(value.startsWith("/")){ + value = value.substring(1); + } + + String catalinaBase = System.getProperty("catalina.base"); + + log.info("Using Oxalis folder specified as Java System Property '-D {}' with value '{}/{}'.", + VARIABLE, catalinaBase, value); + return new File(catalinaBase, value); + } +} + diff --git a/oxalis-commons/src/test/java/network/oxalis/commons/filesystem/detector/RelativePropertyHomeDetectorTest.java b/oxalis-commons/src/test/java/network/oxalis/commons/filesystem/detector/RelativePropertyHomeDetectorTest.java new file mode 100644 index 000000000..f12022b75 --- /dev/null +++ b/oxalis-commons/src/test/java/network/oxalis/commons/filesystem/detector/RelativePropertyHomeDetectorTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2010-2018 Norwegian Agency for Public Management and eGovernment (Difi) + * + * Licensed under the EUPL, Version 1.1 or – as soon they + * will be approved by the European Commission - subsequent + * versions of the EUPL (the "Licence"); + * + * You may not use this work except in compliance with the Licence. + * + * You may obtain a copy of the Licence at: + * + * https://joinup.ec.europa.eu/community/eupl/og_page/eupl + * + * Unless required by applicable law or agreed to in + * writing, software distributed under the Licence is + * distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the Licence for the specific language governing + * permissions and limitations under the Licence. + */ + +package network.oxalis.commons.filesystem.detector; + +import network.oxalis.api.filesystem.HomeDetector; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import java.io.File; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +/** + * @author ERST + */ +public class RelativePropertyHomeDetectorTest { + + private HomeDetector homeDetector = new RelativePropertyHomeDetector(); + + @BeforeClass + public void setup(){ + System.setProperty("catalina.base", "/catalina"); + } + + @AfterClass + public void cleanup(){ + System.clearProperty("catalina.base"); + } + + @Test + public void testFromJavaSystemProperty() { + String path = new File("/catalina/some/system/path2").getAbsolutePath(); + String backup = System.getProperty(RelativePropertyHomeDetector.VARIABLE); + + try { + System.clearProperty(RelativePropertyHomeDetector.VARIABLE); + File oxalis_home = homeDetector.detect(); + assertNull(oxalis_home); + + System.setProperty(RelativePropertyHomeDetector.VARIABLE, ""); + oxalis_home = homeDetector.detect(); + assertNull(oxalis_home); + + System.setProperty(RelativePropertyHomeDetector.VARIABLE, "some/system/path2"); + oxalis_home = homeDetector.detect(); + assertEquals(oxalis_home.getAbsolutePath(), path); + + System.setProperty(RelativePropertyHomeDetector.VARIABLE, "/some/system/path2"); + oxalis_home = homeDetector.detect(); + assertEquals(oxalis_home.getAbsolutePath(), path); + } finally { + if (backup == null) backup = ""; // prevent null pointer exception + System.setProperty(RelativePropertyHomeDetector.VARIABLE, backup); + } + } +}