Skip to content

Commit

Permalink
feat(mpconfig): Make DirConfigSource ignore files with certains exten…
Browse files Browse the repository at this point in the history
…sions

Ignore *.properties, *.yaml, *.yml, *.xml, *.json files, as those are likely
to contain more complex structures used for other config sources.

Remember: the file name compiles to the property name (up to three letter
endings silently cut off) and might contain whatever. If you put a
file with any of these extensions in the directory on purpose, it should not
be used with this config source.

- Relates to payara#5006
- Based on request from @pdudits in payara#5007
  • Loading branch information
poikilotherm committed Jan 27, 2021
1 parent 784995f commit 401555d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

Expand Down Expand Up @@ -210,6 +206,7 @@ public final void run() {
}

private static final Logger logger = Logger.getLogger(DirConfigSource.class.getName());
static final String[] ignoredExtensions = {".xml", ".yaml", ".yml", ".json", ".properties"};
private Path directory;
private final ConcurrentHashMap<String, DirProperty> properties = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -404,6 +401,7 @@ public final static boolean isAptFile(Path path, BasicFileAttributes atts) throw
return path != null && Files.exists(path) &&
Files.isRegularFile(path) && Files.isReadable(path) &&
!path.getFileName().toString().startsWith(".") &&
Arrays.stream(ignoredExtensions).noneMatch(ext -> path.getFileName().toString().endsWith(ext)) &&
atts.size() < 512*1024;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class DirConfigSourceTest {

Expand Down Expand Up @@ -149,25 +150,30 @@ public void testIsAptDir() throws IOException {

@Test
public void testIsAptFile() throws IOException {

Map<Path, Boolean> examples = new HashMap<>();
examples.put(subpath( "aptdir", "aptfile"), TRUE);
examples.put(subpath( "aptdir", ".unaptfile"), FALSE);
assertEquals(FALSE, DirConfigSource.isAptFile(null, null));
assertEquals(FALSE, DirConfigSource.isAptFile(subpath( "aptdir", "aptnotexisting"), null));

assertFalse(DirConfigSource.isAptFile(null, null));
assertFalse(DirConfigSource.isAptFile(subpath( "aptdir", "aptnotexisting"), null));
for (Map.Entry<Path, Boolean> ex : examples.entrySet()) {
BasicFileAttributes atts = writeFile(ex.getKey(), "test");
assertEquals(ex.getValue(), DirConfigSource.isAptFile(ex.getKey(), atts));
}

Path file100k = subpath("aptdir", "100k-file");
BasicFileAttributes atts100k = writeRandFile(file100k, 100*1024);
assertEquals(TRUE, DirConfigSource.isAptFile(file100k, atts100k));
assertTrue(DirConfigSource.isAptFile(file100k, atts100k));

Path file600k = subpath("aptdir", "600k-file");
BasicFileAttributes atts600k = writeRandFile(file600k, 600*1024);
assertEquals(FALSE, DirConfigSource.isAptFile(file600k, atts600k));
assertFalse(DirConfigSource.isAptFile(file600k, atts600k));

for (String ext : DirConfigSource.ignoredExtensions) {
Path file = subpath("aptdir", "ignorefile"+ext);
BasicFileAttributes attsFile = writeFile(file, "test");
assertFalse(DirConfigSource.isAptFile(file, attsFile));
}
}

@Test
Expand Down

0 comments on commit 401555d

Please sign in to comment.