src/jdk.packager/share/native/library/common/GenericPlatform.cpp
branchJDK-8200758-branch
changeset 57017 1b08af362a30
parent 57016 f63f13da91c0
child 57018 9d782e357916
equal deleted inserted replaced
57016:f63f13da91c0 57017:1b08af362a30
     1 /*
       
     2  * Copyright (c) 2014, 2018, 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 "GenericPlatform.h"
       
    27 
       
    28 #include <fstream>
       
    29 #include <locale>
       
    30 
       
    31 #ifdef WINDOWS
       
    32 #include <codecvt>
       
    33 #endif // WINDOWS
       
    34 
       
    35 
       
    36 GenericPlatform::GenericPlatform(void) {
       
    37 }
       
    38 
       
    39 GenericPlatform::~GenericPlatform(void) {
       
    40 }
       
    41 
       
    42 TString GenericPlatform::GetConfigFileName() {
       
    43     TString result;
       
    44     TString basedir = GetPackageAppDirectory();
       
    45 
       
    46     if (basedir.empty() == false) {
       
    47         basedir = FilePath::IncludeTrailingSeparator(basedir);
       
    48         TString appConfig = basedir + GetAppName() + _T(".cfg");
       
    49 
       
    50         if (FilePath::FileExists(appConfig) == true) {
       
    51             result = appConfig;
       
    52         }
       
    53         else {
       
    54             result = basedir + _T("package.cfg");
       
    55 
       
    56             if (FilePath::FileExists(result) == false) {
       
    57                 result = _T("");
       
    58             }
       
    59         }
       
    60     }
       
    61 
       
    62     return result;
       
    63 }
       
    64 
       
    65 TString GenericPlatform::GetPackageAppDirectory() {
       
    66 #if defined(WINDOWS) || defined(LINUX)
       
    67     return FilePath::IncludeTrailingSeparator(
       
    68             GetPackageRootDirectory()) + _T("app");
       
    69 #endif // WINDOWS || LINUX
       
    70 #ifdef MAC
       
    71     return FilePath::IncludeTrailingSeparator(
       
    72             GetPackageRootDirectory()) + _T("Java");
       
    73 #endif
       
    74 }
       
    75 
       
    76 TString GenericPlatform::GetPackageLauncherDirectory() {
       
    77 #if defined(WINDOWS) || defined(LINUX)
       
    78     return GetPackageRootDirectory();
       
    79 #endif // WINDOWS || LINUX
       
    80 #ifdef MAC
       
    81     return FilePath::IncludeTrailingSeparator(
       
    82             GetPackageRootDirectory()) + _T("MacOS");
       
    83 #endif
       
    84 }
       
    85 
       
    86 TString GenericPlatform::GetPackageRuntimeBinDirectory() {
       
    87 #ifdef WINDOWS
       
    88     return FilePath::IncludeTrailingSeparator(GetPackageRootDirectory()) + _T("runtime\\bin");
       
    89 #endif // WINDOWS
       
    90 #ifdef LINUX
       
    91     return FilePath::IncludeTrailingSeparator(GetPackageRootDirectory()) + _T("runtime/bin");
       
    92 #endif // LINUX
       
    93 #ifdef MAC
       
    94     return FilePath::IncludeTrailingSeparator(GetPackageRootDirectory()) + _T("Plugins/Java.runtime/Contents/Home/bin");
       
    95 #endif
       
    96 }
       
    97 
       
    98 std::list<TString> GenericPlatform::LoadFromFile(TString FileName) {
       
    99     std::list<TString> result;
       
   100 
       
   101     if (FilePath::FileExists(FileName) == true) {
       
   102         std::wifstream stream(FileName.data());
       
   103 
       
   104 #ifdef WINDOWS
       
   105         const std::locale empty_locale = std::locale::empty();
       
   106 #endif // WINDOWS
       
   107 #ifdef POSIX
       
   108         const std::locale empty_locale = std::locale::classic();
       
   109 #endif // POSIX
       
   110 #if defined(WINDOWS)
       
   111         const std::locale utf8_locale =
       
   112                 std::locale(empty_locale, new std::codecvt_utf8<wchar_t>());
       
   113         stream.imbue(utf8_locale);
       
   114 #endif // WINDOWS
       
   115 
       
   116         if (stream.is_open() == true) {
       
   117             while (stream.eof() == false) {
       
   118                 std::wstring line;
       
   119                 std::getline(stream, line);
       
   120 
       
   121                 // # at the first character will comment out the line.
       
   122                 if (line.empty() == false && line[0] != '#') {
       
   123                     result.push_back(PlatformString(line).toString());
       
   124                 }
       
   125             }
       
   126         }
       
   127     }
       
   128 
       
   129     return result;
       
   130 }
       
   131 
       
   132 void GenericPlatform::SaveToFile(TString FileName, std::list<TString> Contents, bool ownerOnly) {
       
   133     TString path = FilePath::ExtractFilePath(FileName);
       
   134 
       
   135     if (FilePath::DirectoryExists(path) == false) {
       
   136         FilePath::CreateDirectory(path, ownerOnly);
       
   137     }
       
   138 
       
   139     std::wofstream stream(FileName.data());
       
   140 
       
   141     FilePath::ChangePermissions(FileName.data(), ownerOnly);
       
   142 
       
   143 #ifdef WINDOWS
       
   144     const std::locale empty_locale = std::locale::empty();
       
   145 #endif // WINDOWS
       
   146 #ifdef POSIX
       
   147     const std::locale empty_locale = std::locale::classic();
       
   148 #endif // POSIX
       
   149 #if defined(WINDOWS)
       
   150     const std::locale utf8_locale =
       
   151             std::locale(empty_locale, new std::codecvt_utf8<wchar_t>());
       
   152     stream.imbue(utf8_locale);
       
   153 #endif // WINDOWS || MAC
       
   154 
       
   155     if (stream.is_open() == true) {
       
   156         for (std::list<TString>::const_iterator iterator =
       
   157                 Contents.begin(); iterator != Contents.end(); iterator++) {
       
   158             TString line = *iterator;
       
   159             stream << PlatformString(line).toUnicodeString() << std::endl;
       
   160         }
       
   161     }
       
   162 }
       
   163 
       
   164 #if defined(WINDOWS) || defined(LINUX)
       
   165 TString GenericPlatform::GetAppName() {
       
   166     TString result = GetModuleFileName();
       
   167     result = FilePath::ExtractFileName(result);
       
   168 #if defined(WINDOWS)
       
   169     result = FilePath::ChangeFileExt(result, _T(""));
       
   170 #endif
       
   171     return result;
       
   172 }
       
   173 #endif // WINDOWS || LINUX
       
   174 
       
   175 std::map<TString, TString> GenericPlatform::GetKeys() {
       
   176     std::map<TString, TString> keys;
       
   177     keys.insert(std::map<TString, TString>::value_type(CONFIG_VERSION,
       
   178             _T("app.version")));
       
   179     keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINJAR_KEY,
       
   180             _T("app.mainjar")));
       
   181     keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINMODULE_KEY,
       
   182             _T("app.mainmodule")));
       
   183     keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINCLASSNAME_KEY,
       
   184             _T("app.mainclass")));
       
   185     keys.insert(std::map<TString, TString>::value_type(CONFIG_CLASSPATH_KEY,
       
   186             _T("app.classpath")));
       
   187     keys.insert(std::map<TString, TString>::value_type(CONFIG_MODULEPATH_KEY,
       
   188             _T("app.modulepath")));
       
   189     keys.insert(std::map<TString, TString>::value_type(APP_NAME_KEY,
       
   190             _T("app.name")));
       
   191     keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_ID_KEY,
       
   192             _T("app.preferences.id")));
       
   193     keys.insert(std::map<TString, TString>::value_type(JVM_RUNTIME_KEY,
       
   194             _T("app.runtime")));
       
   195     keys.insert(std::map<TString, TString>::value_type(PACKAGER_APP_DATA_DIR,
       
   196             _T("app.identifier")));
       
   197     keys.insert(std::map<TString, TString>::value_type(CONFIG_SPLASH_KEY,
       
   198             _T("app.splash")));
       
   199     keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_MEMORY,
       
   200             _T("app.memory")));
       
   201     keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_DEBUG,
       
   202             _T("app.debug")));
       
   203     keys.insert(std::map<TString,
       
   204             TString>::value_type(CONFIG_APPLICATION_INSTANCE,
       
   205             _T("app.application.instance")));
       
   206     keys.insert(std::map<TString,
       
   207             TString>::value_type(CONFIG_SECTION_APPLICATION,
       
   208             _T("Application")));
       
   209     keys.insert(std::map<TString,
       
   210             TString>::value_type(CONFIG_SECTION_JVMOPTIONS,
       
   211             _T("JVMOptions")));
       
   212     keys.insert(std::map<TString,
       
   213             TString>::value_type(CONFIG_SECTION_APPCDSJVMOPTIONS,
       
   214             _T("AppCDSJVMOptions")));
       
   215     keys.insert(std::map<TString,
       
   216             TString>::value_type(CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS,
       
   217             _T("AppCDSGenerateCacheJVMOptions")));
       
   218     keys.insert(std::map<TString,
       
   219             TString>::value_type(CONFIG_SECTION_ARGOPTIONS,
       
   220             _T("ArgOptions")));
       
   221 
       
   222     return keys;
       
   223 }
       
   224 
       
   225 #ifdef DEBUG
       
   226 DebugState GenericPlatform::GetDebugState() {
       
   227     DebugState result = dsNone;
       
   228 
       
   229     if (IsNativeDebuggerPresent() == true) {
       
   230         result = dsNative;
       
   231     }
       
   232 
       
   233     return result;
       
   234 }
       
   235 #endif // DEBUG