Skip to content

Commit 726d8c0

Browse files
committed
- Created search in pre-defined paths
- Created support for "filtered paths" - matching Java installations will be excluded
1 parent 8221f8e commit 726d8c0

3 files changed

Lines changed: 150 additions & 0 deletions

File tree

Core.pas

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface
3131
function GetRegistryPaths() : TVStringList;
3232
function GetEnvironmentVariables() : TVStringList;
3333
function GetFilePaths() : TVStringList;
34+
function GetFilteredPaths() : TVStringList;
3435
end;
3536

3637
{ TJavaInstallation }
@@ -73,6 +74,8 @@ TParser = class(TObject)
7374
function ParseJavaSoft(hk : HKEY; const samDesired : REGSAM) : Integer;
7475
function ParseZuluLiberica(hk : HKEY; const samDesired : REGSAM) : Integer;
7576
procedure ProcessEnvironmentVariables();
77+
procedure ProcessFilePaths();
78+
procedure ProcessFilteredPaths();
7679
procedure ProcessOSPath();
7780
procedure ProcessRegistry(const Is64 : Boolean);
7881
public
@@ -507,6 +510,51 @@ procedure TParser.ProcessEnvironmentVariables();
507510
end;
508511
end;
509512

513+
procedure TParser.ProcessFilePaths();
514+
515+
var
516+
Installation : TJavaInstallation;
517+
InstallationType : TInstallationType;
518+
FilePath, s : VString;
519+
520+
begin
521+
for FilePath in FSettings.GetFilePaths do begin
522+
InstallationType := InferTypeFromPath(FilePath);
523+
s := ResolveJavawPath(FilePath);
524+
Installation := GetJavawInfo(s);
525+
if Installation <> Nil then begin
526+
Installation.InstallationType := InstallationType;
527+
AddInstallationIfUnique(Installation, FInstallations);
528+
end;
529+
end;
530+
end;
531+
532+
procedure TParser.ProcessFilteredPaths();
533+
534+
var
535+
FilteredPaths : TVStringList;
536+
i : Integer;
537+
s, curPath : VString;
538+
539+
begin
540+
FilteredPaths := FSettings.GetFilteredPaths;
541+
if (FilteredPaths = Nil) or (FilteredPaths.Count = 0) then Exit;
542+
543+
i := 0;
544+
while i < FInstallations.Count do begin
545+
curPath := FInstallations[i].Path;
546+
for s in FilteredPaths do begin
547+
if StartsWith(s, curPath, False) then begin
548+
if FLogger.IsDebug() then FLogger.Log('Filtered out installation: ' + curPath, TLogLevel.DEBUG);
549+
FInstallations.Delete(i);
550+
Dec(i);
551+
Break;
552+
end;
553+
end;
554+
Inc(i);
555+
end;
556+
end;
557+
510558
procedure TParser.ProcessOSPath();
511559

512560
var
@@ -574,6 +622,8 @@ procedure TParser.Process();
574622
ProcessRegistry(IsWOW64(FLogger));
575623
ProcessEnvironmentVariables();
576624
ProcessOSPath();
625+
ProcessFilePaths();
626+
ProcessFilteredPaths();
577627

578628
if (FLogger <> Nil) and (FLogger.isDebug()) then begin
579629
for i := 0 to FInstallations.Count - 1 do begin

NsJavaLocator.lpr

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ TParameters = class(TObject, TSettings)
4141
FRegistryPaths : TVStringList;
4242
FEnvironmentVariables : TVStringList;
4343
FFilePaths : TVStringList;
44+
FFilteredPaths : TVStringList;
4445
FLogLevel : TLogLevel;
4546
FIsLogging : Boolean;
4647
FIsDialogDebug : Boolean;
@@ -52,13 +53,15 @@ TParameters = class(TObject, TSettings)
5253
function GetRegistryPaths() : TVStringList;
5354
function GetEnvironmentVariables() : TVStringList;
5455
function GetFilePaths() : TVStringList;
56+
function GetFilteredPaths() : TVStringList;
5557
procedure ParseParams();
5658
constructor Create();
5759
destructor Destroy(); override;
5860
published
5961
property RegistryPaths : TVStringList read GetRegistryPaths;
6062
property EnvironmentVariables : TVStringList read GetEnvironmentVariables;
6163
property FilePaths : TVStringList read GetFilePaths;
64+
property FilteredPaths : TVStringList read GetFilteredPaths;
6265
property LogLevel : TLogLevel read GetLogLevel;
6366
property IsLogging : Boolean read GetLogging;
6467
property IsDialogDebug : Boolean read GetDialogDebug;
@@ -164,6 +167,11 @@ function TParameters.GetFilePaths() : TVStringList;
164167
Result := FFilePaths;
165168
end;
166169

170+
function TParameters.GetFilteredPaths() : TVStringList;
171+
begin
172+
Result := FFilteredPaths;
173+
end;
174+
167175
function TParameters.ReadParams() : TVStringList;
168176

169177
var
@@ -200,13 +208,16 @@ procedure TParameters.ParseParams();
200208
poRegDel = '/DELREGPATH';
201209
poEnv = '/ENVSTR';
202210
poEnvDel = '/DELENVSTR';
211+
poFilterAdd = '/ADDFILTER';
212+
poFilterDel = '/DELFILTER';
203213
poLog = '/LOG';
204214
poDialogDebug = '/DIALOGDEBUG';
205215
poLogLevel = '/LOGLEVEL';
206216

207217
var
208218
parameterList : TVStringList;
209219
i : Integer;
220+
s, dialogStr : VString;
210221
tmpLogLevel : TLogLevel;
211222

212223
begin
@@ -240,6 +251,18 @@ procedure TParameters.ParseParams();
240251
FEnvironmentVariables.RemoveMatching(parameterList[i + 1], False);
241252
Inc(i);
242253
end
254+
else if EqualStr(poFilterAdd, parameterList[i], False) then begin
255+
s := ExpandEnvStrings(parameterList[i + 1]);
256+
if (s <> '') and (s <> parameterList[i + 1]) then FFilteredPaths.AddUnique(s, False)
257+
else FFilteredPaths.AddUnique(parameterList[i + 1], False);
258+
Inc(i);
259+
end
260+
else if EqualStr(poFilterDel, parameterList[i], False) then begin
261+
FFilteredPaths.RemoveMatching(parameterList[i + 1], False);
262+
s := ExpandEnvStrings(parameterList[i + 1]);
263+
if (s <> '') and (s <> parameterList[i + 1]) then FFilteredPaths.RemoveMatching(s, False);
264+
Inc(i);
265+
end
243266
else if EqualStr(poLogLevel, parameterList[i], False) then begin
244267
tmpLogLevel := VStrToLogLevel(parameterList[i + 1]);
245268
if tmpLogLevel = TLogLevel.INVALID then
@@ -258,6 +281,40 @@ procedure TParameters.ParseParams();
258281
Inc(i);
259282
end;
260283
parameterList.Free;
284+
285+
for i := 0 to FFilePaths.Count - 1 do begin
286+
s := ExpandEnvStrings(FFilePaths[i]);
287+
if s <> FFilePaths[i] then begin
288+
FFilePaths[i] := s;
289+
end
290+
end;
291+
292+
if (FIsLogging or FIsDialogDebug) and (FLogLevel >= TLogLevel.DEBUG) then begin
293+
dialogStr := '';
294+
for i := 0 to FRegistryPaths.Count - 1 do begin
295+
if FIsLogging then LogMessage('Registry Path ' + IntToVStr(i + 1) + ': ' + FRegistryPaths[i]);
296+
if FIsDialogDebug then dialogStr := dialogStr + 'Registry Path ' + IntToVStr(i + 1) + ': ' + FRegistryPaths[i] + #13#10;
297+
end;
298+
if dialogStr <> '' then NSISDialog(dialogStr, 'Registry Paths');
299+
dialogStr := '';
300+
for i := 0 to FEnvironmentVariables.Count - 1 do begin
301+
if FIsLogging then LogMessage('Environment Variable ' + IntToVStr(i + 1) + ': ' + FEnvironmentVariables[i]);
302+
if FIsDialogDebug then dialogStr := dialogStr + 'Environment Variable ' + IntToVStr(i + 1) + ': ' + FEnvironmentVariables[i] + #13#10;
303+
end;
304+
if dialogStr <> '' then NSISDialog(dialogStr, 'Environment Variables');
305+
dialogStr := '';
306+
for i := 0 to FFilePaths.Count - 1 do begin
307+
if FIsLogging then LogMessage('File Path ' + IntToVStr(i + 1) + ': ' + FFilePaths[i]);
308+
if FIsDialogDebug then dialogStr := dialogStr + 'File Path ' + IntToVStr(i + 1) + ': ' + FFilePaths[i] +#13#10;
309+
end;
310+
if dialogStr <> '' then NSISDialog(dialogStr, 'File Paths');
311+
dialogStr := '';
312+
for i := 0 to FFilteredPaths.Count - 1 do begin
313+
if FIsLogging then LogMessage('Filtered Path ' + IntToVStr(i + 1) + ': ' + FFilteredPaths[i]);
314+
if FIsDialogDebug then dialogStr := dialogStr + 'Filtered Path ' + IntToVStr(i + 1) + ': ' + FFilteredPaths[i] + #13#10;
315+
end;
316+
if dialogStr <> '' then NSISDialog(dialogStr, 'Filtered Paths');
317+
end;
261318
end;
262319

263320
constructor TParameters.Create();
@@ -340,9 +397,18 @@ constructor TParameters.Create();
340397
'SOFTWARE\Semeru',
341398
'SOFTWARE\BellSoft\Liberica'
342399
);
400+
StandardFilteredPaths : array [0..5] of VString = (
401+
'%commonprogramfiles%\Oracle\Java\javapath',
402+
'%commonprogramfiles(x86)%\Oracle\Java\javapath',
403+
'%commonprogramW6432%\Oracle\Java\javapath',
404+
'%ALLUSERSPROFILE%\Oracle\Java\javapath',
405+
'%SystemRoot%\system32',
406+
'%SystemRoot%\SysWOW64'
407+
);
343408

344409
var
345410
i : Integer;
411+
s : VString;
346412

347413
begin
348414
FRegistryPaths := TVStringList.Create();
@@ -352,13 +418,20 @@ constructor TParameters.Create();
352418
FEnvironmentVariables := TVStringList.Create();
353419
FEnvironmentVariables.Add('%JAVA_HOME%');
354420
FFilePaths := TVStringList.Create();
421+
FFilteredPaths := TVStringList.Create;
422+
for i := 0 to high(StandardFilteredPaths) do begin
423+
s := ExpandEnvStrings(StandardFilteredPaths[i]);
424+
// Only add filters that expand - otherwise they don't exist on the system
425+
if s <> StandardFilteredPaths[i] then FFilteredPaths.AddUnique(s, False);
426+
end;
355427
FLogLevel := TLogLevel.INFO;
356428
FIsLogging := False;
357429
FIsDialogDebug := False;
358430
end;
359431

360432
destructor TParameters.Destroy();
361433
begin
434+
FFilteredPaths.Free();
362435
FFilePaths.Free();
363436
FEnvironmentVariables.Free();
364437
FRegistryPaths.Free();

Utils.pas

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function VStrToIntDef(const ns : VString; Default : LongInt) : LongInt;
8484
function BoolToVStr(Value : Boolean) : VString;
8585
function EqualStr(str1, str2 : VString; CaseSensitive : Boolean = true) : Boolean;
8686
function EndsWith(subStr : VString; const str : VString; CaseSensitive : Boolean = true) : Boolean;
87+
function StartsWith(subStr : VString; const str : VString; CaseSensitive : Boolean = true) : Boolean;
8788
function InstallationTypeToStr(const InstallationType : TInstallationType) : VString;
8889
function ArchitectureToStr(const Architecture : TArchitecture; const BitsOnly : Boolean) : VString;
8990
function LogLevelToVStr(const LogLevel : TLogLevel) : VString;
@@ -203,6 +204,32 @@ function EndsWith(subStr : VString; const str : VString; CaseSensitive : Boolean
203204
end;
204205
end;
205206

207+
function StartsWith(subStr : VString; const str : VString; CaseSensitive : Boolean = true) : Boolean;
208+
209+
var
210+
subLen, len, i : Integer;
211+
startStr : VString;
212+
213+
begin
214+
Result := False;
215+
subLen := Length(subStr);
216+
len := Length(str);
217+
if (subLen = 0) or (len = 0) or (len < subLen) then Exit;
218+
if CaseSensitive then begin
219+
for i := 1 to subLen do if str[i] <> subStr[i] then Exit;
220+
Result := true;
221+
end
222+
else begin
223+
UniqueString(subStr);
224+
startStr := Copy(str, 1, subLen);
225+
{$IFDEF UNICODE}
226+
Result := lstrcmpiW(LPCWSTR(startStr), LPCWSTR(subStr)) = 0;
227+
{$ELSE}
228+
Result := lstrcmpiA(LPCSTR(startStr), LPCSTR(subStr)) = 0;
229+
{$ENDIF}
230+
end;
231+
end;
232+
206233
{ Utility routines }
207234

208235
function InstallationTypeToStr(const InstallationType : TInstallationType) : VString;

0 commit comments

Comments
 (0)