Skip to content

Commit 75a87aa

Browse files
feat(setup-check): add JavaSetupCheck
Adds JavaSetupCheck to verify Java installation, path, version and encoding. Related issue: #6590 Type: Feature Checklist: - [x] Implement ISetupCheck interface - [x] Use SetupCheckUtils trait - [x] Verify Java path exists - [x] Check Java version matches required version - [x] Check native.encoding for UTF-8 - [x] Return SetupResult with appropriate severity and translated messages - [x] Add unit tests covering all scenarios
1 parent c586f71 commit 75a87aa

2 files changed

Lines changed: 632 additions & 0 deletions

File tree

lib/SetupCheck/JavaSetupCheck.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Libresign\SetupCheck;
10+
11+
use OCA\Libresign\Helper\JavaHelper;
12+
use OCA\Libresign\Service\Install\SignSetupService;
13+
use OCA\Libresign\Service\Install\InstallService;
14+
use OCP\IL10N;
15+
use OCP\IURLGenerator;
16+
use OCP\App\IAppManager;
17+
use OCP\SetupCheck\ISetupCheck;
18+
use OCP\SetupCheck\SetupResult;
19+
use OCP\IConfig;
20+
use Psr\Log\LoggerInterface;
21+
22+
class JavaSetupCheck implements ISetupCheck
23+
{
24+
use SetupCheckUtils;
25+
26+
private IL10N $l10n;
27+
private JavaHelper $javaHelper;
28+
private IConfig $systemConfig;
29+
30+
private SignSetupService $signSetupService;
31+
private IURLGenerator $urlGenerator;
32+
private IAppManager $appManager;
33+
private LoggerInterface $logger;
34+
35+
public function __construct(
36+
IL10N $l10n,
37+
JavaHelper $javaHelper,
38+
SignSetupService $signSetupService,
39+
IURLGenerator $urlGenerator,
40+
IAppManager $appManager,
41+
LoggerInterface $logger,
42+
IConfig $systemConfig
43+
) {
44+
$this->l10n = $l10n;
45+
$this->javaHelper = $javaHelper;
46+
$this->signSetupService = $signSetupService;
47+
$this->urlGenerator = $urlGenerator;
48+
$this->appManager = $appManager;
49+
$this->logger = $logger;
50+
$this->systemConfig = $systemConfig;
51+
}
52+
53+
public function getName(): string
54+
{
55+
return $this->l10n->t('Java');
56+
}
57+
58+
public function getCategory(): string
59+
{
60+
return 'system';
61+
}
62+
63+
public function run(): SetupResult
64+
{
65+
$debugEnabled = $this->systemConfig->getSystemValueBool('debug', false);
66+
$javaPath = $this->javaHelper->getJavaPath();
67+
68+
if (!$javaPath) {
69+
return SetupResult::error(
70+
$this->l10n->t('Java not installed'),
71+
$this->l10n->t('Run occ libresign:install --java')
72+
);
73+
}
74+
75+
$verifyResult = $this->verifyResourceIntegrity('java', $debugEnabled);
76+
if (!empty($verifyResult)) {
77+
[$errorMsg, $tip] = $this->getErrorAndTipFromVerify($verifyResult, 'java', $debugEnabled, $this->l10n);
78+
return SetupResult::error($errorMsg, $tip);
79+
}
80+
81+
if (!file_exists($javaPath)) {
82+
return SetupResult::error(
83+
$this->l10n->t('Java binary not found: %s', [$javaPath]),
84+
$this->l10n->t('Run occ libresign:install --java')
85+
);
86+
}
87+
88+
exec($javaPath . ' -version 2>&1', $output, $returnCode);
89+
if (empty($output)) {
90+
return SetupResult::error(
91+
$this->l10n->t('Failed to execute Java. Sounds that your operational system is blocking the JVM.'),
92+
'https://github.com/LibreSign/libresign/issues/2327#issuecomment-1961988790'
93+
);
94+
}
95+
if ($returnCode !== 0) {
96+
return SetupResult::error(
97+
$this->l10n->t('Failure to check Java version.'),
98+
$this->l10n->t('Run occ libresign:install --java')
99+
);
100+
}
101+
102+
$javaVersion = trim($output[0] ?? '');
103+
if ($javaVersion !== InstallService::JAVA_VERSION) {
104+
return SetupResult::error(
105+
$this->l10n->t('Invalid java version. Found: %s expected: %s', [$javaVersion, InstallService::JAVA_VERSION]),
106+
$this->l10n->t('Run occ libresign:install --java')
107+
);
108+
}
109+
110+
exec($javaPath . ' -XshowSettings:properties -version 2>&1', $encodingOutput);
111+
$fullOutput = implode("\n", $encodingOutput);
112+
preg_match('/native.encoding = (?<encoding>.*?)(\n|$)/', $fullOutput, $matches);
113+
$encoding = $matches['encoding'] ?? null;
114+
115+
if (!$encoding) {
116+
return SetupResult::error(
117+
$this->l10n->t('Java encoding not found.'),
118+
sprintf('The command %s need to have native.encoding', $javaPath . ' -XshowSettings:properties -version')
119+
);
120+
}
121+
122+
if (!str_contains($encoding, 'UTF-8')) {
123+
$detectedEncoding = trim($encoding);
124+
$phpLocale = setlocale(LC_CTYPE, 0) ?: 'not set';
125+
$phpLcAll = getenv('LC_ALL') ?: 'not set';
126+
$phpLang = getenv('LANG') ?: 'not set';
127+
128+
$tip = sprintf(
129+
"Java detected encoding \"%s\" but UTF-8 is required.\n\n"
130+
. "**Current PHP environment:**\n"
131+
. "- LC_CTYPE: %s\n"
132+
. "- LC_ALL: %s\n"
133+
. "- LANG: %s\n\n"
134+
. "**To fix this issue:**\n"
135+
. "1. Set LC_ALL and LANG environment variables (e.g., LC_ALL=en_US.UTF-8) for your web server user\n"
136+
. "2. Restart your web server after making changes\n"
137+
. "3. Verify with command: `locale charmap` (should return UTF-8)\n\n"
138+
. 'For more details, see: [Issue #4872](https://github.com/LibreSign/libresign/issues/4872)',
139+
$detectedEncoding,
140+
$phpLocale,
141+
$phpLcAll,
142+
$phpLang
143+
);
144+
return SetupResult::info(
145+
$this->l10n->t('Non-UTF-8 encoding detected: %s. This may cause issues with accented or special characters', [$detectedEncoding]),
146+
$tip
147+
);
148+
}
149+
150+
$message = $this->l10n->t('Java version: %s', [$javaVersion]) . "\n" .
151+
$this->l10n->t('Java binary: %s', [$javaPath]);
152+
return SetupResult::success($message);
153+
}
154+
}

0 commit comments

Comments
 (0)