Skip to content

Commit

Permalink
HACK: VirtualFile + getResourceAsStream + leak :D
Browse files Browse the repository at this point in the history
  • Loading branch information
xabolcs committed Jul 4, 2023
1 parent 874db35 commit 948c809
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion framework/src/play/Play.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void init(String id) {
routes = appRoot.child("conf/routes");
if (!routes.exists()) {
try {
routes = VirtualFile.open(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("conf/routes")).getFile());
routes = VirtualFile.openWithEntryName(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("conf/routes")).getFile(), "conf/routes");
} catch (NullPointerException e) {
// keep going! VirtualFile's exception is more descriptive
}
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/PropertiesConfLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected final VirtualFile resolveFileNameWithApplicationPath(String filename)
}

protected final VirtualFile resolveFileNameWithClasspath(String filename) {
return VirtualFile.open(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("conf/" + filename)).getPath());
return VirtualFile.openWithEntryName(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("conf/" + filename)).getPath(), "conf/" + filename);
}

private Properties readOneConfigurationFile(VirtualFile conf, String playId, String inheritedId, Set<VirtualFile> confs) {
Expand Down
25 changes: 21 additions & 4 deletions framework/src/play/vfs/VirtualFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package play.vfs;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.Play;
import play.exceptions.UnexpectedException;

Expand All @@ -13,10 +15,7 @@
import java.io.InputStream;
import java.io.Writer;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -28,11 +27,19 @@
public class VirtualFile {

private final File realFile;
private final String entry;

private VirtualFile(File file) {
this.realFile = file;
this.entry = null;
}

private VirtualFile(File file, String entryName) {
this.realFile = file;
this.entry = entryName;
}


public String getName() {
return realFile.getName();
}
Expand Down Expand Up @@ -101,6 +108,9 @@ public boolean exists() {

public InputStream inputstream() {
try {
if (entry != null ) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(entry);
}
return new FileInputStream(realFile);
} catch (IOException e) {
throw new UnexpectedException("Failed to read " + realFile.getAbsolutePath(), e);
Expand Down Expand Up @@ -151,6 +161,10 @@ public VirtualFile child(String name) {
return new VirtualFile(new File(realFile, name));
}

public static VirtualFile openWithEntryName(String file, String entryName) {
return new VirtualFile(new File(file), entryName);
}

public static VirtualFile open(String file) {
return open(new File(file));
}
Expand All @@ -161,6 +175,9 @@ public static VirtualFile open(File file) {

public String contentAsString() {
try {
if (entry != null) {
return IOUtils.toString(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResourceAsStream(entry)), UTF_8);
}
return readFileToString(realFile, UTF_8);
} catch (IOException e) {
throw new UnexpectedException("Failed to read " + realFile.getAbsolutePath(), e);
Expand Down

0 comments on commit 948c809

Please sign in to comment.