-
Notifications
You must be signed in to change notification settings - Fork 4
Системная информация. Часть1. #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
7a62c52
1c53365
9576e29
b66da2e
faa7569
1cdf6ac
f7026f3
4a505dd
cd08dfc
69c2556
19d4403
638c6f1
1c9e58b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package com.github.otymko.jos.runtime.context.global; | ||
|
|
||
| import com.github.otymko.jos.core.annotation.ContextMethod; | ||
| import com.github.otymko.jos.core.annotation.GlobalContextClass; | ||
| import com.github.otymko.jos.runtime.context.AttachableContext; | ||
| import com.github.otymko.jos.runtime.context.type.ValueFactory; | ||
| import com.github.otymko.jos.runtime.context.type.collection.V8Map; | ||
| import com.github.otymko.jos.runtime.machine.info.ContextInfo; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.TreeMap; | ||
|
|
||
| @GlobalContextClass | ||
| public class SystemEnvironmentGlobalContext implements AttachableContext { | ||
| public static final ContextInfo INFO = ContextInfo.createByClass(SystemEnvironmentGlobalContext.class); | ||
| private static final Map<String, String> environmentVariables = getEnvironmentVariablesSnapshot(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, мне не очень нравится текущее решение с static field, т.к. при многократном использовании контекста будет будут просачиваться значения из других. Давай пока сделаем синглтон + todo на исправление с DI, потом переделаем.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нужен какой-то стейт на процесс выполнения ( |
||
|
|
||
| private static Map<String, String> getEnvironmentVariablesSnapshot() { | ||
| final var result = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER); | ||
| result.putAll(System.getenv()); | ||
| return result; | ||
| } | ||
|
|
||
| @ContextMethod(name = "ПеременныеСреды", alias = "EnvironmentVariables") | ||
| public V8Map environmentVariables() { | ||
| var result = V8Map.constructor(); | ||
| for (var envEntry: environmentVariables.entrySet()) { | ||
| result.insert(ValueFactory.create(envEntry.getKey()), | ||
| ValueFactory.create(envEntry.getValue())); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @ContextMethod(name = "ПолучитьПеременнуюСреды", alias = "getEnvironmentVariable") | ||
| public String getEnvironmentVariable(String name) { | ||
| return environmentVariables.getOrDefault(name, ""); | ||
| } | ||
|
|
||
| @ContextMethod(name = "УстановитьПеременнуюСреды", alias = "SetEnvironmentVariable") | ||
| public void setEnvironmentVariable(String name, String value) { | ||
| environmentVariables.put(name, value); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, как это работает дальше? Ну изменили мы у себя кеш, а на систему как это влияет?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @otymko На систему никак не влияет.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, а в оскрипте как?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @otymko в дотнете оно из коробки умеет ставить переменные среды в ОС https://learn.microsoft.com/ru-ru/dotnet/api/system.environment.setenvironmentvariable?view=net-7.0 |
||
| } | ||
|
|
||
| @Override | ||
| public ContextInfo getContextInfo() { | ||
| return INFO; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package com.github.otymko.jos.runtime.context.type.env; | ||
|
|
||
| import com.github.otymko.jos.core.annotation.EnumClass; | ||
| import com.github.otymko.jos.core.annotation.EnumValue; | ||
| import com.github.otymko.jos.runtime.context.EnumType; | ||
| import com.github.otymko.jos.runtime.context.type.enumeration.EnumerationContext; | ||
|
|
||
| /** | ||
| * Содержит известные поддерживаемые типы платформ | ||
| */ | ||
| @EnumClass(name = "ТипПлатформы", alias = "PlatformType") | ||
| public enum PlatformType implements EnumType { | ||
|
|
||
| /** | ||
| * Операционная система семейства linux, архитектура i386 | ||
| */ | ||
| @EnumValue(name = "Linux_x86", alias = "Linux_x86") | ||
|
dmpas marked this conversation as resolved.
|
||
| LINUX_X86, | ||
|
|
||
| /** | ||
| * Операционная система семейства linux, архитектура amd64 | ||
| */ | ||
| @EnumValue(name = "Linux_x86_64", alias = "Linux_x86_64") | ||
| LINUX_X86_64, | ||
|
|
||
| /** | ||
| * Операционная система Mac OS X, архитектура i386 | ||
| */ | ||
| @EnumValue(name = "MacOS_x86", alias = "MacOS_x86") | ||
| MACOS_X86, | ||
|
|
||
| /** | ||
| * Операционная система семейства Mac OS X, архитектура amd64 | ||
| */ | ||
| @EnumValue(name = "MacOS_x86_64", alias = "MacOS_x86_64") | ||
| MACOS_X86_64, | ||
|
|
||
| /** | ||
| * Операционная система семейства WinNT, архитектура i386 | ||
| */ | ||
| @EnumValue(name = "Windows_x86", alias = "Windows_x86") | ||
| WINDOWS_X86, | ||
|
|
||
| /** | ||
| * Операционная система семейства WinNT, архитектура amd64 | ||
| */ | ||
| @EnumValue(name = "Windows_x86_64", alias = "Windows_x86_64") | ||
| WINDOWS_X86_64, | ||
|
|
||
| /** | ||
| * Неизвестная операционная система | ||
| */ | ||
| @EnumValue(name = "Unknown", alias = "Unknown") | ||
| UNKNOWN; | ||
|
|
||
| public static final EnumerationContext INFO = new EnumerationContext(PlatformType.class); | ||
|
|
||
| /** | ||
| * Определяет тип платформы по строковому представлению операционной системы | ||
| * @param osName Строковое представление операционной системы | ||
| * @param is64 Признак 64-битной архитектуры | ||
| * @return Определенный тип платформы | ||
| */ | ||
| public static PlatformType parse(String osName, boolean is64) { | ||
| var prepared = osName.toUpperCase(); | ||
| if (prepared.contains("WINDOWS")) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, узоры: скобочки { } |
||
| return is64 ? WINDOWS_X86_64 : WINDOWS_X86; | ||
| if (prepared.contains("LINUX")) | ||
| return is64 ? LINUX_X86_64 : LINUX_X86; | ||
| if (prepared.contains("MAC OS")) | ||
| return is64 ? MACOS_X86_64 : MACOS_X86; | ||
| return PlatformType.UNKNOWN; | ||
| } | ||
|
|
||
| @Override | ||
| public EnumerationContext getContextInfo() { | ||
| return INFO; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package com.github.otymko.jos.runtime.context.type.env; | ||
|
|
||
| import com.github.otymko.jos.core.annotation.EnumClass; | ||
| import com.github.otymko.jos.runtime.context.EnumType; | ||
| import com.github.otymko.jos.runtime.context.type.enumeration.EnumerationContext; | ||
|
|
||
| /** | ||
| * Содержит известные поддерживаемые специальные типы каталогов | ||
| */ | ||
| @EnumClass(name = "СпециальнаяПапка", alias = "SpecialFolder") | ||
| public enum SpecialFolder implements EnumType { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, заглушка же, верно? А кинь плиз issue если нет и сюда TODO |
||
| ; | ||
| public static final EnumerationContext INFO = new EnumerationContext(SpecialFolder.class); | ||
| @Override | ||
| public EnumerationContext getContextInfo() { | ||
| return INFO; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package com.github.otymko.jos.runtime.context.type.env; | ||
|
|
||
| import com.github.otymko.jos.core.PropertyAccessMode; | ||
| import com.github.otymko.jos.core.annotation.ContextClass; | ||
| import com.github.otymko.jos.core.annotation.ContextConstructor; | ||
| import com.github.otymko.jos.core.annotation.ContextProperty; | ||
| import com.github.otymko.jos.exception.MachineException; | ||
| import com.github.otymko.jos.runtime.context.ContextValue; | ||
| import com.github.otymko.jos.runtime.machine.info.ContextInfo; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.jar.Attributes; | ||
| import java.util.jar.Manifest; | ||
|
|
||
| /** | ||
| * Содержит описание ключевых параметров среды исполнения | ||
| */ | ||
| @NoArgsConstructor | ||
| @ContextClass(name = "СистемнаяИнформация", alias = "SystemInfo") | ||
| public class V8SystemInfo extends ContextValue { | ||
| public static final ContextInfo INFO = ContextInfo.createByClass(V8SystemInfo.class); | ||
|
|
||
| /** | ||
| * Создает новый объект СистеимнаяИнформация | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, опечатка в "СистеИмная" |
||
| * | ||
| * @return СистемнаяИнформация | ||
| */ | ||
| @ContextConstructor | ||
| public static V8SystemInfo constructor() { | ||
| return new V8SystemInfo(); | ||
| } | ||
|
|
||
| /** | ||
| * Возвращает версию движка среды исполнения | ||
| * | ||
| * @return Строка с версией движка | ||
| */ | ||
| @ContextProperty(name = "Версия", alias = "Version", accessMode = PropertyAccessMode.READ_ONLY) | ||
| public String getVersion() { | ||
| var manifestStream = Thread.currentThread() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, давай вынесем в утилиты? Я думаю это значение еще нам понадобится. |
||
| .getContextClassLoader() | ||
| .getResourceAsStream("META-INF/MANIFEST.MF"); | ||
| var manifest = new Manifest(); | ||
| try { | ||
| manifest.read(manifestStream); | ||
| } catch (IOException e) { | ||
| return ""; | ||
| } | ||
| var versionFromFile = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION); | ||
| return versionFromFile == null ? "" : versionFromFile; | ||
| } | ||
|
|
||
| /** | ||
| * Возвразщает имя компьютера среды исполнения | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, опечатка в "ВозвраЗщает" |
||
| * | ||
| * @return Имя компьютера | ||
| */ | ||
| @ContextProperty(name = "ИмяКомпьютера", alias = "MachineName") | ||
| public String getMachineName() { | ||
| throw MachineException.operationNotImplementedException(); | ||
| } | ||
|
|
||
| /** | ||
| * Возвращает имя и версию операцинной системы среды исполнения | ||
| * | ||
| * @return Имя и версия операционной системы | ||
| */ | ||
| @ContextProperty(name = "ВерсияОС", alias = "OSVersion") | ||
| public String getOsVersion() { | ||
| return String.format("%s %s", | ||
| System.getProperty("os.name"), | ||
| System.getProperty("os.version")); | ||
| } | ||
|
|
||
| /** | ||
| * Возвращает тип платформы среды исполнения | ||
| * @return Тип платформы | ||
| */ | ||
| @ContextProperty(name = "ТипПлатформы", alias = "PlatformType") | ||
| public PlatformType getPlatformType() { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, давай тоже вынесем в утилиты. |
||
| var is64 = getIs64bitOperatingSystem(); | ||
| var osName = System.getProperty("os.name"); | ||
| return PlatformType.parse(osName, is64); | ||
| } | ||
|
|
||
| /** | ||
| * Возвращает пользователя операционной системы, от имени которого исполняется скрипт | ||
| * @return Имя пользователя ОС | ||
| */ | ||
| @ContextProperty(name = "ПользовательОС", alias = "OSUser") | ||
| public String getOsUser() { | ||
| return System.getProperty("user.name"); | ||
| } | ||
|
|
||
| /** | ||
| * Возвращает признак 64-битной операционной системы | ||
| * | ||
| * @return Признак 64-битной ОС | ||
| */ | ||
| @ContextProperty(name = "Это64БитнаяОперационнаяСистема", alias = "Is64bitOperatingSystem") | ||
| public boolean getIs64bitOperatingSystem() { | ||
| var arch = System.getProperty("os.arch"); | ||
| return arch.endsWith("64"); | ||
| } | ||
|
|
||
| /** | ||
| * Возвращает количество процессоров текущей среды исполнения | ||
| * | ||
| * @return Количество процессоров | ||
| */ | ||
| @ContextProperty(name = "КоличествоПроцессоров", alias = "ProcessorCount") | ||
| public int getProcessorCount() { | ||
| return Runtime.getRuntime().availableProcessors(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, кстати, в v8 возвращается количество физический или логических процессоров? |
||
| } | ||
|
|
||
| /** | ||
| * Возвращает размер страницы оперативной памяти текущей среды исполнения | ||
| * | ||
| * @return Размер страницы | ||
| */ | ||
| @ContextProperty(name = "РазмерСтраницы", alias = "SystemPageSize") | ||
| public int getSystemPageSize() { | ||
| throw MachineException.operationNotImplementedException(); | ||
| } | ||
|
|
||
| /** | ||
| * Возвращает время работы с момента загрузки системы | ||
| * | ||
| * @return Время в миллисекундах | ||
| */ | ||
| @ContextProperty(name = "ВремяРаботыСМоментаЗагрузки", alias = "TickCount") | ||
| public long getTickCount() { | ||
| return System.currentTimeMillis(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmpas, System.currentTimeMillis возвращает не ВремяРаботыСМоментаЗагрузки. Смотри как в https://github.com/oshi/oshi работает getSystemUptime
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно здесь закинуть нон-саппортед, а а отдельной задаче реализовать.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @otymko getSystemUptime - там ад, если честно. Куча |
||
| } | ||
|
|
||
| @Override | ||
| public ContextInfo getContextInfo() { | ||
| return INFO; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmpas, добавь, плиз, jdoc