Skip to content

Commit a5744ed

Browse files
authored
Merge pull request #716 from kares/jruby-fix-resource
jruby: safer engine version resolution
2 parents 7da76a7 + d971204 commit a5744ed

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

ext/java/org/jruby/ext/psych/PsychLibrary.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,39 @@
4141
import org.jruby.runtime.builtin.IRubyObject;
4242
import org.jruby.runtime.load.Library;
4343

44+
import org.snakeyaml.engine.v2.common.SpecVersion;
45+
4446
import java.io.IOException;
4547
import java.io.InputStream;
4648
import java.util.Properties;
4749

4850
public class PsychLibrary implements Library {
51+
52+
private static final String POM_PROPERTIES = "META-INF/maven/org.snakeyaml/snakeyaml-engine/pom.properties";
4953
private static final String DUMMY_VERSION = "0.0";
5054

5155
public void load(final Ruby runtime, boolean wrap) {
5256
RubyModule psych = runtime.defineModule("Psych");
5357

5458
// load version from properties packed with the jar
5559
Properties props = new Properties();
56-
try( InputStream is = runtime.getJRubyClassLoader().getResourceAsStream("META-INF/maven/org.snakeyaml/snakeyaml-engine/pom.properties") ) {
57-
props.load(is);
60+
try( InputStream is = SpecVersion.class.getResourceAsStream(POM_PROPERTIES) ) {
61+
if (is != null) props.load(is);
5862
}
5963
catch( IOException e ) {
6064
// ignored
6165
}
6266
String snakeyamlVersion = props.getProperty("version", DUMMY_VERSION);
6367

68+
RubyString version = runtime.newString(snakeyamlVersion);
69+
version.setFrozen(true);
70+
psych.setConstant("SNAKEYAML_VERSION", version); // e.g. 2.10-SNAPSHOT
71+
6472
if (snakeyamlVersion.endsWith("-SNAPSHOT")) {
6573
snakeyamlVersion = snakeyamlVersion.substring(0, snakeyamlVersion.length() - "-SNAPSHOT".length());
6674
}
6775

68-
RubyString version = runtime.newString(snakeyamlVersion + ".0");
69-
version.setFrozen(true);
70-
psych.setConstant("SNAKEYAML_VERSION", version);
71-
72-
String[] versionParts = version.toString().split("\\.");
76+
String[] versionParts = (snakeyamlVersion + ".0").split("\\."); // 2.10-SNAPSHOT -> 2.10.0
7377
final RubyArray versionElements = runtime.newArray(runtime.newFixnum(Integer.parseInt(versionParts[0])), runtime.newFixnum(Integer.parseInt(versionParts[1])), runtime.newFixnum(Integer.parseInt(versionParts[2])));
7478
versionElements.setFrozen(true);
7579

0 commit comments

Comments
 (0)