Skip to content

Commit 51db656

Browse files
committed
feat: use a temp file to didentify the java version
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent ee70dea commit 51db656

2 files changed

Lines changed: 44 additions & 29 deletions

File tree

src/Runtime/JavaRuntimeService.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ public function getPath(JSignParam $params): string
3737
throw new InvalidArgumentException('The java base dir is not a real directory. Create this directory first: '. $baseDir);
3838
}
3939
}
40-
try {
41-
self::validateVersion($params);
42-
} catch (RuntimeException) {
40+
if (!self::validateVersion($params)) {
4341
self::downloadAndExtract($downloadUrl, $javaPath);
4442
}
4543
$params->setJavaDownloadUrl('');
@@ -52,20 +50,10 @@ public function getPath(JSignParam $params): string
5250

5351
private function validateVersion(JSignParam $params): bool
5452
{
55-
$version = $params->getJavaVersion();
56-
if (!$version) {
57-
throw new InvalidArgumentException('Java version required');
58-
}
5953
$javaPath = $params->getJavaPath();
60-
\exec($javaPath . ' -version 2>&1', $javaVersion, $resultCode);
61-
if (count($javaVersion) <= 1) {
62-
throw new RuntimeException('Failed to execute Java. Sounds that your operational system is blocking the JVM.');
63-
}
64-
if ($resultCode !== 0) {
65-
throw new RuntimeException('Failure to check Java version.');
66-
}
67-
$javaVersion = current($javaVersion);
68-
return $javaVersion === $version;
54+
$baseDir = preg_replace('/\/bin\/java$/', '', $javaPath);
55+
$lastVersion = $baseDir . '/.java_version_' . basename($params->getJavaDownloadUrl());
56+
return file_exists($lastVersion);
6957
}
7058

7159
private function downloadAndExtract(string $url, string $baseDir): void
@@ -94,7 +82,10 @@ private function downloadAndExtract(string $url, string $baseDir): void
9482
$tar->extractTo(directory: $baseDir, overwrite: true);
9583
@exec('mv ' . escapeshellarg($baseDir . '/'. $rootDirInsideTar) . '/* ' . escapeshellarg($baseDir));
9684
@exec('rm -rf ' . escapeshellarg($baseDir . '/'. $rootDirInsideTar));
85+
$cacheFile = '/.java_version_' . basename($url);
86+
@exec('rm ' . escapeshellarg($baseDir) . $cacheFile);
9787
unlink($baseDir . '/java.tar.gz');
88+
touch($baseDir . $cacheFile);
9889
if (!file_exists($baseDir . '/bin/java')) {
9990
throw new RuntimeException('Java binary not found at: ' . $baseDir . '/bin/java');
10091
}

tests/Runtime/JavaRuntimeServiceTest.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ public function testGetPathWithDownloadUrlAndNotRealDirectory(): void {
6565
$service->getPath($jsignParam);
6666
}
6767

68-
public function testGetPathWithDownloadUrlAndEmptyJavaVersion(): void {
69-
$jsignParam = new JSignParam();
70-
$service = new JavaRuntimeService();
71-
$jsignParam->setJavaVersion('');
72-
$this->expectException(InvalidArgumentException::class);
73-
$this->expectExceptionMessageMatches('/Java version required/');
74-
$service->getPath($jsignParam);
75-
}
76-
7768
public function testGetPathWithDownloadUrlWithInvalidUrl(): void {
7869
vfsStream::setup('download');
7970
mkdir('vfs://download/bin');
@@ -83,7 +74,6 @@ public function testGetPathWithDownloadUrlWithInvalidUrl(): void {
8374
$service = new JavaRuntimeService();
8475
$jsignParam->setJavaPath('vfs://download/bin/java');
8576
$jsignParam->setJavaDownloadUrl('invalid_url');
86-
$jsignParam->setJavaVersion('21.0.0');
8777
$this->expectException(InvalidArgumentException::class);
8878
$this->expectExceptionMessageMatches('/url.*invalid/');
8979
$service->getPath($jsignParam);
@@ -98,7 +88,6 @@ public function testGetPathWithDownloadUrlWith4xxError(): void {
9888
$service = new JavaRuntimeService();
9989
$jsignParam->setJavaPath('vfs://download/bin/java');
10090
$jsignParam->setJavaDownloadUrl('https://404.domain');
101-
$jsignParam->setJavaVersion('21.0.0');
10291
$this->expectException(InvalidArgumentException::class);
10392
$this->expectExceptionMessageMatches('/Failure to download/');
10493
$service->getPath($jsignParam);
@@ -125,7 +114,6 @@ public function testGetPathWithDownloadUrlWithInvalidGzipedFile(): void {
125114
$url = $server->getServerRoot();
126115
$jsignParam->setJavaDownloadUrl($url);
127116

128-
$jsignParam->setJavaVersion('21.0.0');
129117
$this->expectException(InvalidArgumentException::class);
130118
$this->expectExceptionMessageMatches('/cannot be extracted/');
131119
$service->getPath($jsignParam);
@@ -153,12 +141,48 @@ public function testGetPathWithDownloadUrlWithInvalidJavaPackage(): void {
153141
$url = $server->getServerRoot();
154142
$jsignParam->setJavaDownloadUrl($url);
155143

156-
$jsignParam->setJavaVersion('21.0.0');
157144
$this->expectException(InvalidArgumentException::class);
158145
$this->expectExceptionMessageMatches('/Invalid tar content/');
159146
$service->getPath($jsignParam);
160147
}
161148

149+
public function testGetPathWithDownloadUrlWithInvalidVersion(): void {
150+
$jsignParam = new JSignParam();
151+
$service = new JavaRuntimeService();
152+
153+
// When the version is invalid, will try to download the package
154+
$server = new MockWebServer();
155+
$server->start();
156+
$server->setResponseOfPath(
157+
'/',
158+
new Response('invalid response'),
159+
);
160+
$baseUrl = $server->getServerRoot();
161+
$url = $baseUrl . '/OpenJDK21U-jre_x64_linux_hotspot_21.0.8_9.tar.gz';
162+
$jsignParam->setJavaDownloadUrl($url);
163+
164+
$this->expectException(InvalidArgumentException::class);
165+
$this->expectExceptionMessageMatches('/cannot be extracted/');
166+
$service->getPath($jsignParam);
167+
}
168+
169+
public function testGetPathWithDownloadUrlWithValidVersion(): void {
170+
$jsignParam = new JSignParam();
171+
$service = new JavaRuntimeService();
172+
173+
$tarGzFilename = 'OpenJDK21U-jre_x64_linux_hotspot_21.0.8_9.tar.gz';
174+
$url = 'https://fake.url/' . $tarGzFilename;
175+
$jsignParam->setJavaDownloadUrl($url);
176+
177+
// When have a file with an expected name, will consider that the
178+
// downloaded java version is right
179+
touch($this->testTmpDir . '/.java_version_' . $tarGzFilename);
180+
$jsignParam->setJavaPath($this->testTmpDir . '/bin/java');
181+
182+
$javaPath = $service->getPath($jsignParam);
183+
$this->assertEquals($jsignParam->getJavaPath(), $javaPath);
184+
}
185+
162186
public function testGetPathWithoutJavaFallback(): void {
163187
mkdir($this->testTmpDir . '/bin', 0755, true);
164188

0 commit comments

Comments
 (0)