|
2 | 2 | * The MIT License
|
3 | 3 | *
|
4 | 4 | * Copyright (c) 2014 Karol Bucek LTD.
|
| 5 | + * Copyright (c) 2017 Ketan Padegaonkar |
5 | 6 | *
|
6 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 8 | * of this software and associated documentation files (the "Software"), to deal
|
|
23 | 24 | */
|
24 | 25 | package org.jruby.ext.openssl;
|
25 | 26 |
|
26 |
| -import java.security.NoSuchProviderException; |
27 |
| -import java.security.SecureRandom; |
28 |
| -import java.util.Map; |
29 |
| - |
30 |
| -import org.jruby.CompatVersion; |
31 |
| -import org.jruby.Ruby; |
32 |
| -import org.jruby.RubyArray; |
33 |
| -import org.jruby.RubyClass; |
34 |
| -import org.jruby.RubyModule; |
35 |
| -import org.jruby.RubyString; |
| 27 | +import org.jruby.*; |
36 | 28 | import org.jruby.anno.JRubyMethod;
|
37 | 29 | import org.jruby.anno.JRubyModule;
|
38 | 30 | import org.jruby.runtime.ThreadContext;
|
|
41 | 33 | import org.jruby.util.ByteList;
|
42 | 34 | import org.jruby.util.SafePropertyAccessor;
|
43 | 35 |
|
| 36 | +import java.security.NoSuchProviderException; |
| 37 | +import java.security.SecureRandom; |
| 38 | +import java.util.Map; |
| 39 | + |
44 | 40 | /**
|
45 | 41 | * OpenSSL (methods as well as an entry point)
|
46 | 42 | *
|
@@ -106,8 +102,7 @@ public static void createOpenSSL(final Ruby runtime) {
|
106 | 102 | _OpenSSL.setConstant("VERSION", StringHelper.newString(runtime, version));
|
107 | 103 |
|
108 | 104 | final RubyModule _Jopenssl = runtime.getModule("Jopenssl");
|
109 |
| - final RubyModule _Version = (RubyModule) _Jopenssl.getConstantAt("Version"); |
110 |
| - final RubyString jVERSION = _Version.getConstantAt("VERSION").asString(); |
| 105 | + final RubyString jVERSION = _Jopenssl.getConstantAt("VERSION").asString(); |
111 | 106 |
|
112 | 107 | final byte[] JRuby_OpenSSL_ = { 'J','R','u','b','y','-','O','p','e','n','S','S','L',' ' };
|
113 | 108 | final int OPENSSL_VERSION_NUMBER = 999999999; // NOTE: smt more useful?
|
@@ -255,27 +250,27 @@ static void warn(final ThreadContext context, final IRubyObject msg) {
|
255 | 250 | public static String javaVersion(final String def) {
|
256 | 251 | final String javaVersionProperty =
|
257 | 252 | SafePropertyAccessor.getProperty("java.version", def);
|
258 |
| - if ( javaVersionProperty == "0" ) return "1.7.0"; // Android |
| 253 | + if ("0".equals(javaVersionProperty)) return "1.7.0"; // Android |
259 | 254 | return javaVersionProperty;
|
260 | 255 | }
|
261 | 256 |
|
262 | 257 | static boolean javaVersion6(final boolean atLeast) {
|
263 |
| - final int gt = "1.6".compareTo( javaVersion("0.0").substring(0, 3) ); |
| 258 | + final int gt = new Version("1.6").compareTo(new Version(javaVersion("0.0"))); |
264 | 259 | return atLeast ? gt <= 0 : gt == 0;
|
265 | 260 | }
|
266 | 261 |
|
267 | 262 | static boolean javaVersion7(final boolean atLeast) {
|
268 |
| - final int gt = "1.7".compareTo( javaVersion("0.0").substring(0, 3) ); |
| 263 | + final int gt = new Version("1.7").compareTo(new Version(javaVersion("0.0"))); |
269 | 264 | return atLeast ? gt <= 0 : gt == 0;
|
270 | 265 | }
|
271 | 266 |
|
272 | 267 | static boolean javaVersion8(final boolean atLeast) {
|
273 |
| - final int gt = "1.8".compareTo( javaVersion("0.0").substring(0, 3) ); |
| 268 | + final int gt = new Version("1.8").compareTo(new Version(javaVersion("0.0"))); |
274 | 269 | return atLeast ? gt <= 0 : gt == 0;
|
275 | 270 | }
|
276 | 271 |
|
277 | 272 | static boolean javaVersion9(final boolean atLeast) {
|
278 |
| - final int gt = "9".compareTo( javaVersion("0").substring(0, 1) ); |
| 273 | + final int gt = new Version("9").compareTo(new Version(javaVersion("0.0"))); |
279 | 274 | return atLeast ? gt <= 0 : gt == 0;
|
280 | 275 | }
|
281 | 276 |
|
@@ -346,4 +341,29 @@ static String bcExceptionMessage(NoClassDefFoundError ex) {
|
346 | 341 | return "You need to configure JVM/classpath to enable BouncyCastle Security Provider: " + ex;
|
347 | 342 | }
|
348 | 343 |
|
| 344 | + static class Version implements Comparable<Version> { |
| 345 | + public final int[] numbers; |
| 346 | + |
| 347 | + public Version(String version) { |
| 348 | + final String split[] = version.split("[-_]")[0].split("\\."); |
| 349 | + numbers = new int[split.length]; |
| 350 | + for (int i = 0; i < split.length; i++) { |
| 351 | + numbers[i] = Integer.valueOf(split[i]); |
| 352 | + } |
| 353 | + } |
| 354 | + |
| 355 | + @Override |
| 356 | + public int compareTo(Version another) { |
| 357 | + final int maxLength = Math.max(numbers.length, another.numbers.length); |
| 358 | + for (int i = 0; i < maxLength; i++) { |
| 359 | + final int left = i < numbers.length ? numbers[i] : 0; |
| 360 | + final int right = i < another.numbers.length ? another.numbers[i] : 0; |
| 361 | + if (left != right) { |
| 362 | + return left < right ? -1 : 1; |
| 363 | + } |
| 364 | + } |
| 365 | + return 0; |
| 366 | + } |
| 367 | + } |
| 368 | + |
349 | 369 | }
|
0 commit comments