26 internal record
struct IkvmPropEntry(string BasePath, string Value);
28 static readonly IDictionary<string, string> user =
new Dictionary<string, string>();
29 static readonly Lazy<Dictionary<string, IkvmPropEntry>> ikvm =
new Lazy<Dictionary<string, IkvmPropEntry>>(GetIkvmProperties);
30 static readonly Lazy<Dictionary<string, string>> init =
new Lazy<Dictionary<string, string>>(GetInitProperties);
31 static readonly Lazy<string> homePath =
new Lazy<string>(GetHomePath);
38 public static IDictionary<string, string>
User => user;
43 internal static IReadOnlyDictionary<string, IkvmPropEntry> Ikvm => ikvm.Value;
48 internal static IReadOnlyDictionary<string, string> Init => init.Value;
53 internal static string HomePath => homePath.Value;
59 static IEnumerable<string> GetIkvmPropertiesSearchPathsIter()
61 if (AppContext.BaseDirectory is
string basePath && !
string.IsNullOrEmpty(basePath))
62 yield
return basePath;
64 if (AppDomain.CurrentDomain.BaseDirectory is
string appBasePath && !
string.IsNullOrEmpty(appBasePath))
65 yield
return appBasePath;
69 if (typeof(
Properties).
Assembly.Location is
string runtimeAssemblyPath && !
string.IsNullOrEmpty(runtimeAssemblyPath))
70 foreach (var parent
in GetParentDirs(runtimeAssemblyPath))
79 static IEnumerable<string> GetParentDirs(
string path)
81 while (
string.IsNullOrWhiteSpace(path = Path.GetDirectoryName(path)) ==
false)
89 static IEnumerable<string> GetIkvmPropertiesSearchPaths()
91 return GetIkvmPropertiesSearchPathsIter().Distinct();
98 static Dictionary<string, IkvmPropEntry> GetIkvmProperties()
100 var props =
new Dictionary<string, IkvmPropEntry>();
102 foreach (var basePath
in GetIkvmPropertiesSearchPaths())
104 var ikvmPropertiesPath = Path.Combine(basePath,
"ikvm.properties");
105 if (File.Exists(ikvmPropertiesPath))
107 LoadProperties(basePath, File.ReadAllLines(ikvmPropertiesPath), props);
119 static string GetHomePath()
122 if (
User.TryGetValue(
"ikvm.home", out var userHomePath) && !
string.IsNullOrWhiteSpace(userHomePath))
123 if (Directory.Exists(Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, userHomePath))))
124 return Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, userHomePath));
127 if (Ikvm.TryGetValue(
"ikvm.home", out var ikvmHomeEntry) && !
string.IsNullOrWhiteSpace(ikvmHomeEntry.Value))
128 if (Directory.Exists(Path.GetFullPath(Path.Combine(ikvmHomeEntry.BasePath, ikvmHomeEntry.Value))))
129 return Path.GetFullPath(Path.Combine(ikvmHomeEntry.BasePath, ikvmHomeEntry.Value));
133 if (AppContext.GetData(
"IKVM.Home") is
string confHome && !
string.IsNullOrWhiteSpace(confHome))
134 if (Directory.Exists(Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, confHome))))
135 return Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, confHome));
143 if (ConfigurationManager.AppSettings[
"ikvm:ikvm.home"] is
string confHome && !
string.IsNullOrWhiteSpace(confHome))
144 if (Directory.Exists(Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, confHome))))
145 return Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, confHome));
147 catch (ConfigurationException)
154 if (
User.TryGetValue(
"ikvm.home.root", out var userHomeRoot) && !
string.IsNullOrWhiteSpace(userHomeRoot))
155 if (ResolveHomePathFromRoot(Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, userHomeRoot))) is
string userHomeRootPath)
156 return userHomeRootPath;
159 if (Ikvm.TryGetValue(
"ikvm.home.root", out var ikvmHomeRootEntry) && !
string.IsNullOrWhiteSpace(ikvmHomeRootEntry.Value))
160 if (ResolveHomePathFromRoot(Path.GetFullPath(Path.Combine(ikvmHomeRootEntry.BasePath, ikvmHomeRootEntry.Value))) is
string ikvmHomeRootPath)
161 return ikvmHomeRootPath;
165 if (AppContext.GetData(
"IKVM.Home.Root") is
string confHomeRoot && !
string.IsNullOrWhiteSpace(confHomeRoot))
166 if (ResolveHomePathFromRoot(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, confHomeRoot)) is
string confHomeRootPath)
167 return confHomeRootPath;
175 if (ConfigurationManager.AppSettings[
"ikvm:ikvm.home.root"] is
string confHomeRoot && !
string.IsNullOrWhiteSpace(confHomeRoot))
176 if (ResolveHomePathFromRoot(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, confHomeRoot)) is
string confHomeRootPath)
177 return confHomeRootPath;
179 catch (ConfigurationException)
186 if (
string.IsNullOrWhiteSpace(AppContext.BaseDirectory) ==
false)
187 if (ResolveHomePathFromRoot(Path.GetFullPath(Path.Combine(AppContext.BaseDirectory,
"ikvm"))) is
string appHomeRootPath)
188 return appHomeRootPath;
191 if (
string.IsNullOrWhiteSpace(AppDomain.CurrentDomain.BaseDirectory) ==
false)
192 if (ResolveHomePathFromRoot(Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"ikvm"))) is
string domainHomeRootPath)
193 return domainHomeRootPath;
203 static string ResolveHomePathFromRoot(
string homePathRoot)
206 if (Directory.Exists(homePathRoot))
210 var ikvmHomePath = Path.GetFullPath(Path.Combine(homePathRoot, rid));
211 if (Directory.Exists(ikvmHomePath))
224 static void LoadProperties(
string basePath, IEnumerable<string> lines, Dictionary<string, IkvmPropEntry> props)
226 foreach (var l
in lines)
228 var a = l.Split(
new[] {
'=' }, 2, StringSplitOptions.RemoveEmptyEntries);
230 props[a[0].Trim()] =
new IkvmPropEntry(basePath, a[1]?.Trim() ??
"");
238 static Dictionary<string, string> GetInitProperties()
240#if FIRST_PASS || IMPORTER || EXPORTER
241 throw new NotImplementedException();
243 var p =
new Dictionary<string, string>();
244 InitSystemProperties(p);
253 static void InitSystemProperties(Dictionary<string, string> p)
255#if FIRST_PASS || IMPORTER || EXPORTER
256 throw new NotImplementedException();
258 p[
"openjdk.version"] = Constants.openjdk_version;
259 p[
"java.vm.name"] = Constants.java_vm_name;
260 p[
"java.vm.version"] = Constants.java_vm_version;
261 p[
"java.vm.vendor"] = Constants.java_vm_vendor;
262 p[
"java.vm.specification.name"] =
"Java Virtual Machine Specification";
263 p[
"java.vm.specification.version"] = Constants.java_vm_specification_version;
264 p[
"java.vm.specification.vendor"] = Constants.java_vm_specification_vendor;
265 p[
"java.vm.info"] =
"compiled mode";
266 p[
"java.runtime.name"] = Constants.java_runtime_name;
267 p[
"java.runtime.version"] = Constants.java_runtime_version;
270 p[
"ikvm.home"] = HomePath;
271 p[
"java.home"] = HomePath;
272 p[
"java.library.path"] = GetLibraryPath();
273 p[
"java.ext.dirs"] = Path.Combine(HomePath,
"lib",
"ext");
274 p[
"java.endorsed.dirs"] = Path.Combine(HomePath,
"lib",
"endorsed");
275 p[
"sun.boot.library.path"] = GetBootLibraryPath();
276 p[
"sun.boot.class.path"] = VfsTable.GetAssemblyClassesPath(Vfs.Context, Context.Resolver.ResolveBaseAssembly().AsReflection(), HomePath);
277 p[
"sun.cds.enableSharedLookupCache"] =
"false";
280 p[
"sun.nio.MaxDirectMemorySize"] =
"-1";
283 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
284 p[
"jdk.lang.Process.launchMechanism"] =
"FORK";
290 foreach (
string key
in ConfigurationManager.AppSettings)
291 if (key.StartsWith(
"ikvm:"))
292 p[key.Substring(5)] = ConfigurationManager.AppSettings[key];
294 catch (ConfigurationException)
301 if (AppContext.GetData(
"IKVM.Properties") is
string ikvmPropertiesContext && !
string.IsNullOrWhiteSpace(ikvmPropertiesContext))
303 foreach (var ikvmSystemProperty
in ikvmPropertiesContext.Split(
';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
305 if (AppContext.GetData(ikvmSystemProperty) is
string ikvmPropertyValue)
307 p[ikvmSystemProperty] = ikvmPropertyValue;
315 foreach (var kvp
in user)
316 p[kvp.Key] = kvp.Value;
324 static string GetLibraryPath()
326#if FIRST_PASS || IMPORTER || EXPORTER
327 throw new NotImplementedException();
329 var libraryPath =
new List<string>();
334 var windir = SafeGetEnvironmentVariable(
"SystemRoot");
336 libraryPath.Add(Path.Combine(windir,
"Sun",
"Java",
"bin"));
340 libraryPath.Add(Environment.SystemDirectory);
342 catch (SecurityException)
348 libraryPath.Add(windir);
350 var path = SafeGetEnvironmentVariable(
"PATH");
352 foreach (var i
in path.Split(Path.PathSeparator))
360 libraryPath.Add(Path.Combine(
"/usr/java/packages/lib/", IntPtr.Size == 4 ?
"i386" :
"amd64"));
361 libraryPath.Add(
"/lib");
362 libraryPath.Add(
"/usr/lib");
365 var ld_library_path = SafeGetEnvironmentVariable(
"LD_LIBRARY_PATH");
366 if (ld_library_path !=
null)
367 foreach (var i
in ld_library_path.Split(Path.PathSeparator).Reverse())
368 libraryPath.Insert(0, i);
373 var home = SafeGetEnvironmentVariable(
"HOME");
375 libraryPath.Add(Path.Combine(home,
"Library/Java/Extensions"));
377 libraryPath.Add(
"/Library/Java/Extensions");
378 libraryPath.Add(
"/Network/Library/Java/Extensions");
379 libraryPath.Add(
"/System/Library/Java/Extensions");
380 libraryPath.Add(
"/usr/lib/java");
383 var javaLibraryPath = SafeGetEnvironmentVariable(
"JAVA_LIBRARY_PATH");
384 if (javaLibraryPath !=
null)
385 foreach (var i
in javaLibraryPath.Split(Path.PathSeparator))
389 var dyldLibraryPath = SafeGetEnvironmentVariable(
"DYLD_LIBRARY_PATH");
390 if (dyldLibraryPath !=
null)
391 foreach (var i
in dyldLibraryPath.Split(Path.PathSeparator))
395 libraryPath.Add(home);
397 libraryPath.Add(
".");
402 var l =
new List<string>();
404 foreach (var d
in GetIkvmPropertiesSearchPaths())
408 l.Add(Path.Combine(d,
"runtimes", rid,
"native"));
411 libraryPath.InsertRange(0, l);
419 libraryPath.Add(
".");
421 return string.Join(Path.PathSeparator.ToString(), libraryPath.Distinct());
429 static IEnumerable<string> GetBootLibraryPathsIter()
431 yield
return Path.Combine(HomePath,
"bin");
438 static string GetBootLibraryPath()
440 return string.Join(Path.PathSeparator.ToString(), GetBootLibraryPathsIter());