jdk

Форк
0
177 строк · 5.7 Кб
1
/*
2
 * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
10
 *
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
 * version 2 for more details (a copy is included in the LICENSE file that
15
 * accompanied this code).
16
 *
17
 * You should have received a copy of the GNU General Public License version
18
 * 2 along with this work; if not, write to the Free Software Foundation,
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
 *
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
 * or visit www.oracle.com if you need additional information or have any
23
 * questions.
24
 */
25

26
#include <algorithm>
27
#include "AppLauncher.h"
28
#include "JvmLauncher.h"
29
#include "CfgFile.h"
30
#include "Log.h"
31
#include "Dll.h"
32
#include "Toolbox.h"
33
#include "SysInfo.h"
34
#include "FileUtils.h"
35

36

37
AppLauncher::AppLauncher() {
38
    setInitJvmFromCmdlineOnly(false);
39
    launcherPath = SysInfo::getProcessModulePath();
40
    args = SysInfo::getCommandArgs();
41
}
42

43

44
namespace {
45

46
struct find_jvmlib {
47
    find_jvmlib(const tstring& v): runtimePath(v) {
48
    }
49

50
    bool operator () (const tstring& jvmLibName) const {
51
        const tstring path = FileUtils::mkpath() << runtimePath << jvmLibName;
52
        return FileUtils::isFileExists(path);
53
    }
54

55
private:
56
    const tstring& runtimePath;
57
};
58

59
tstring findJvmLib(const CfgFile& cfgFile, const tstring& defaultRuntimePath,
60
        const tstring_array& jvmLibNames) {
61
    const CfgFile::Properties& appOptions = cfgFile.getProperties(
62
            SectionName::Application);
63

64
    const CfgFile::Properties::const_iterator runtimePathProp = appOptions.find(
65
            PropertyName::runtime);
66
    tstring runtimePath;
67
    if (runtimePathProp != appOptions.end()) {
68
        runtimePath = CfgFile::asString(*runtimePathProp);
69
    } else {
70
        runtimePath = defaultRuntimePath;
71
        LOG_TRACE(tstrings::any()
72
                << "Property \"" << PropertyName::runtime.name()
73
                << "\" not found in \"" << SectionName::Application.name()
74
                << "\" section of launcher config file."
75
                << " Using Java runtime from \""
76
                << runtimePath << "\" directory");
77
    }
78

79
    const tstring_array::const_iterator jvmLibNameEntry = std::find_if(
80
            jvmLibNames.begin(),
81
            jvmLibNames.end(),
82
            find_jvmlib(runtimePath));
83

84
    if (jvmLibNameEntry == jvmLibNames.end()) {
85
        JP_THROW(tstrings::any() << "Failed to find JVM in \""
86
            << runtimePath
87
            << "\" directory.");
88
    }
89

90
    return FileUtils::mkpath() << runtimePath << *jvmLibNameEntry;
91
}
92
} // namespace
93

94
bool AppLauncher::libEnvVariableContainsAppDir() const {
95
    tstring value = SysInfo::getEnvVariable(std::nothrow,
96
            libEnvVarName, tstring());
97
#ifdef _WIN32
98
    value = tstrings::toLower(value);
99
#endif
100

101
    const tstring_array tokens = tstrings::split(value,
102
            tstring(1, FileUtils::pathSeparator));
103
    return tokens.end() != std::find(tokens.begin(), tokens.end(),
104
#ifdef _WIN32
105
        tstrings::toLower(appDirPath)
106
#else
107
        appDirPath
108
#endif
109
    );
110
}
111

112
Jvm* AppLauncher::createJvmLauncher() const {
113
    const tstring cfgFilePath = getCfgFilePath();
114

115
    LOG_TRACE(tstrings::any() << "Launcher config file path: \""
116
            << cfgFilePath << "\"");
117

118
    CfgFile::Macros macros;
119
    macros[_T("$APPDIR")] = appDirPath;
120
    macros[_T("$BINDIR")] = FileUtils::dirname(launcherPath);
121
    macros[_T("$ROOTDIR")] = imageRoot;
122

123
    CfgFile cfgFile = CfgFile::load(cfgFilePath).expandMacros(macros);
124

125
    if (!args.empty()) {
126
        // Override default launcher arguments.
127
        cfgFile.setPropertyValue(SectionName::ArgOptions,
128
            PropertyName::arguments, args);
129
    }
130

131
    std::unique_ptr<Jvm> jvm(new Jvm());
132

133
    if (!libEnvVariableContainsAppDir()) {
134
        (*jvm).addEnvVariable(libEnvVarName, SysInfo::getEnvVariable(
135
                std::nothrow, libEnvVarName)
136
                + FileUtils::pathSeparator
137
                + appDirPath);
138
    }
139

140
    (*jvm)
141
        .setPath(findJvmLib(cfgFile, defaultRuntimePath, jvmLibNames))
142
        .addArgument(launcherPath);
143

144
    if (initJvmFromCmdlineOnly) {
145
        tstring_array::const_iterator argIt = args.begin();
146
        const tstring_array::const_iterator argEnd = args.end();
147
        for (; argIt != argEnd; ++argIt) {
148
            (*jvm).addArgument(*argIt);
149
        }
150
    } else {
151
        (*jvm).initFromConfigFile(cfgFile);
152
    }
153

154
    return jvm.release();
155
}
156

157

158
void AppLauncher::launch() const {
159
    std::unique_ptr<Jvm>(createJvmLauncher())->launch();
160
}
161

162

163
tstring AppLauncher::getCfgFilePath() const {
164
    tstring_array::const_iterator it = cfgFileLookupDirs.begin();
165
    tstring_array::const_iterator end = cfgFileLookupDirs.end();
166
    const tstring cfgFileName = FileUtils::stripExeSuffix(
167
            FileUtils::basename(launcherPath)) + _T(".cfg");
168
    for (; it != end; ++it) {
169
        const tstring cfgFilePath = FileUtils::mkpath() << *it << cfgFileName;
170
        LOG_TRACE(tstrings::any() << "Check [" << cfgFilePath << "] file exit");
171
        if (FileUtils::isFileExists(cfgFilePath)) {
172
            return cfgFilePath;
173
        }
174
    }
175

176
    return FileUtils::mkpath() << appDirPath << cfgFileName;
177
}
178

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.