Skip to content

Commit 9187d8c

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 9187d8c

6 files changed

Lines changed: 57 additions & 56 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ With specific Java or JSignPdf version:
4242
```php
4343
$params->getJSignPdfDownloadUrl('the url to download the zip here');
4444
$params->setJavaDownloadUrl('the url to download the .tar.gz here');
45-
$params->setJavaVersion('openjdk version "21.0.8" 2025-07-15 LTS')
4645
```
4746

4847
Without JSignPDF bin:

src/Runtime/JSignPdfRuntimeService.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getPath(JSignParam $params): string
3131
throw new InvalidArgumentException('The JSignPdf base dir cannot be created: '. $baseDir);
3232
}
3333
}
34-
if (!file_exists($jsignPdfPath)) {
34+
if (!file_exists($jsignPdfPath) || !self::validateVersion($params)) {
3535
self::downloadAndExtract($params);
3636
}
3737
return $jsignPdfPath;
@@ -40,6 +40,13 @@ public function getPath(JSignParam $params): string
4040
throw new InvalidArgumentException('Java not found.');
4141
}
4242

43+
private function validateVersion(JSignParam $params): bool
44+
{
45+
$jsignPdfPath = $params->getjSignPdfJarPath();
46+
$versionCacheFile = $jsignPdfPath . '/.jsignpdf_version_' . basename($params->getJSignPdfDownloadUrl());
47+
return file_exists($versionCacheFile);
48+
}
49+
4350
private function downloadAndExtract(JSignParam $params): void
4451
{
4552
$jsignPdfPath = $params->getjSignPdfJarPath();
@@ -68,10 +75,12 @@ private function downloadAndExtract(JSignParam $params): void
6875
}
6976
@exec('mv ' . escapeshellarg($baseDir . '/'. $z->getNameIndex(0)) . '/JSignPdf.jar ' . escapeshellarg($baseDir));
7077
@exec('rm -rf ' . escapeshellarg($baseDir . '/'. $z->getNameIndex(0)));
78+
@exec('rm -f ' . escapeshellarg($baseDir) . '/.jsignpdf_version_*');
7179
unlink($baseDir . '/jsignpdf.zip');
7280
if (!file_exists($baseDir . '/JSignPdf.jar')) {
7381
throw new RuntimeException('Java binary not found at: ' . $baseDir . '/bin/java');
7482
}
83+
touch($baseDir . '/.jsignpdf_version_' . basename($url));
7584
}
7685

7786
private function chunkDownload(string $url, string $destination): void

src/Runtime/JavaRuntimeService.php

Lines changed: 6 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,9 @@ 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+
@exec('rm -f ' . escapeshellarg($baseDir) . '/.java_version_*');
9786
unlink($baseDir . '/java.tar.gz');
87+
touch($baseDir . '/.java_version_' . basename($url));
9888
if (!file_exists($baseDir . '/bin/java')) {
9989
throw new RuntimeException('Java binary not found at: ' . $baseDir . '/bin/java');
10090
}

src/Sign/JSignParam.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ class JSignParam
1818
private $tempName;
1919
private $isOutputTypeBase64 = false;
2020
private string $jSignPdfJarPath;
21-
private string $javaVersion = 'openjdk version "21.0.8" 2025-07-15 LTS';
2221
private string $javaDownloadUrl = 'https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.8%2B9/OpenJDK21U-jre_x64_linux_hotspot_21.0.8_9.tar.gz';
2322
private string $jSignPdfDownloadUrl = 'https://github.com/intoolswetrust/jsignpdf/releases/download/JSignPdf_2_3_0/jsignpdf-2.3.0.zip';
24-
private ?string $jsignPdfVersion = null;
2523

2624
public function __construct()
2725
{
@@ -138,17 +136,6 @@ public function getjSignPdfJarPath()
138136
return $this->jSignPdfJarPath;
139137
}
140138

141-
public function setJsignPdfVersion(string $version): self
142-
{
143-
$this->jsignPdfVersion = $version;
144-
return $this;
145-
}
146-
147-
public function getJsignPdfVersion(): ?string
148-
{
149-
return $this->jsignPdfVersion;
150-
}
151-
152139
public function isOutputTypeBase64(): bool
153140
{
154141
return $this->isOutputTypeBase64;
@@ -196,14 +183,4 @@ public function getJSignPdfDownloadUrl(): string
196183
{
197184
return $this->jSignPdfDownloadUrl;
198185
}
199-
200-
public function setJavaVersion(string $javaVersion): self
201-
{
202-
$this->javaVersion = $javaVersion;
203-
return $this;
204-
}
205-
206-
public function getJavaVersion(): ?string {
207-
return $this->javaVersion;
208-
}
209186
}

tests/JSignPDFTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,12 @@ public function testGetVersion()
218218
mkdir('vfs://download/bin');
219219
touch('vfs://download/bin/java');
220220
chmod('vfs://download/bin/java', 0755);
221+
mkdir('vfs://download/jsignpdf_fake_path/');
222+
touch('vfs://download/jsignpdf_fake_path/.jsignpdf_version_fake_url');
221223
$params->setJavaPath('vfs://download/bin/java');
222-
$params->getJSignPdfDownloadUrl('fake_url');
224+
$params->setJSignPdfDownloadUrl('fake_url');
223225
$params->setIsUseJavaInstalled(true);
224-
$params->setjSignPdfJarPath('faje_path');
226+
$params->setjSignPdfJarPath('vfs://download/jsignpdf_fake_path');
225227
$version = $this->service->getVersion($params);
226228
$this->assertNotEmpty($version);
227229
}

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)