src/jdk.jpackage/share/native/libjpackage/GenericPlatform.cpp
author herrick
Mon, 03 Dec 2018 15:06:33 -0500
branchJDK-8200758-branch
changeset 57054 32d2b2d2d353
permissions -rw-r--r--
8213747: Makefile Improvements to Lib-jdk.packager.gmk Reviewed-by: ihse, almatvee

/*
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

#include "GenericPlatform.h"

#include <fstream>
#include <locale>

#ifdef WINDOWS
#include <codecvt>
#endif // WINDOWS


GenericPlatform::GenericPlatform(void) {
}

GenericPlatform::~GenericPlatform(void) {
}

TString GenericPlatform::GetConfigFileName() {
    TString result;
    TString basedir = GetPackageAppDirectory();

    if (basedir.empty() == false) {
        basedir = FilePath::IncludeTrailingSeparator(basedir);
        TString appConfig = basedir + GetAppName() + _T(".cfg");

        if (FilePath::FileExists(appConfig) == true) {
            result = appConfig;
        }
        else {
            result = basedir + _T("package.cfg");

            if (FilePath::FileExists(result) == false) {
                result = _T("");
            }
        }
    }

    return result;
}

TString GenericPlatform::GetPackageAppDirectory() {
#if defined(WINDOWS) || defined(LINUX)
    return FilePath::IncludeTrailingSeparator(
            GetPackageRootDirectory()) + _T("app");
#endif // WINDOWS || LINUX
#ifdef MAC
    return FilePath::IncludeTrailingSeparator(
            GetPackageRootDirectory()) + _T("Java");
#endif
}

TString GenericPlatform::GetPackageLauncherDirectory() {
#if defined(WINDOWS) || defined(LINUX)
    return GetPackageRootDirectory();
#endif // WINDOWS || LINUX
#ifdef MAC
    return FilePath::IncludeTrailingSeparator(
            GetPackageRootDirectory()) + _T("MacOS");
#endif
}

TString GenericPlatform::GetPackageRuntimeBinDirectory() {
#ifdef WINDOWS
    return FilePath::IncludeTrailingSeparator(GetPackageRootDirectory()) + _T("runtime\\bin");
#endif // WINDOWS
#ifdef LINUX
    return FilePath::IncludeTrailingSeparator(GetPackageRootDirectory()) + _T("runtime/bin");
#endif // LINUX
#ifdef MAC
    return FilePath::IncludeTrailingSeparator(GetPackageRootDirectory()) + _T("Plugins/Java.runtime/Contents/Home/bin");
#endif
}

std::list<TString> GenericPlatform::LoadFromFile(TString FileName) {
    std::list<TString> result;

    if (FilePath::FileExists(FileName) == true) {
        std::wifstream stream(FileName.data());

#ifdef WINDOWS
        const std::locale empty_locale = std::locale::empty();
#endif // WINDOWS
#ifdef POSIX
        const std::locale empty_locale = std::locale::classic();
#endif // POSIX
#if defined(WINDOWS)
        const std::locale utf8_locale =
                std::locale(empty_locale, new std::codecvt_utf8<wchar_t>());
        stream.imbue(utf8_locale);
#endif // WINDOWS

        if (stream.is_open() == true) {
            while (stream.eof() == false) {
                std::wstring line;
                std::getline(stream, line);

                // # at the first character will comment out the line.
                if (line.empty() == false && line[0] != '#') {
                    result.push_back(PlatformString(line).toString());
                }
            }
        }
    }

    return result;
}

void GenericPlatform::SaveToFile(TString FileName, std::list<TString> Contents, bool ownerOnly) {
    TString path = FilePath::ExtractFilePath(FileName);

    if (FilePath::DirectoryExists(path) == false) {
        FilePath::CreateDirectory(path, ownerOnly);
    }

    std::wofstream stream(FileName.data());

    FilePath::ChangePermissions(FileName.data(), ownerOnly);

#ifdef WINDOWS
    const std::locale empty_locale = std::locale::empty();
#endif // WINDOWS
#ifdef POSIX
    const std::locale empty_locale = std::locale::classic();
#endif // POSIX
#if defined(WINDOWS)
    const std::locale utf8_locale =
            std::locale(empty_locale, new std::codecvt_utf8<wchar_t>());
    stream.imbue(utf8_locale);
#endif // WINDOWS || MAC

    if (stream.is_open() == true) {
        for (std::list<TString>::const_iterator iterator =
                Contents.begin(); iterator != Contents.end(); iterator++) {
            TString line = *iterator;
            stream << PlatformString(line).toUnicodeString() << std::endl;
        }
    }
}

#if defined(WINDOWS) || defined(LINUX)
TString GenericPlatform::GetAppName() {
    TString result = GetModuleFileName();
    result = FilePath::ExtractFileName(result);
#if defined(WINDOWS)
    result = FilePath::ChangeFileExt(result, _T(""));
#endif
    return result;
}
#endif // WINDOWS || LINUX

std::map<TString, TString> GenericPlatform::GetKeys() {
    std::map<TString, TString> keys;
    keys.insert(std::map<TString, TString>::value_type(CONFIG_VERSION,
            _T("app.version")));
    keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINJAR_KEY,
            _T("app.mainjar")));
    keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINMODULE_KEY,
            _T("app.mainmodule")));
    keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINCLASSNAME_KEY,
            _T("app.mainclass")));
    keys.insert(std::map<TString, TString>::value_type(CONFIG_CLASSPATH_KEY,
            _T("app.classpath")));
    keys.insert(std::map<TString, TString>::value_type(CONFIG_MODULEPATH_KEY,
            _T("app.modulepath")));
    keys.insert(std::map<TString, TString>::value_type(APP_NAME_KEY,
            _T("app.name")));
    keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_ID_KEY,
            _T("app.preferences.id")));
    keys.insert(std::map<TString, TString>::value_type(JVM_RUNTIME_KEY,
            _T("app.runtime")));
    keys.insert(std::map<TString, TString>::value_type(JPACKAGE_APP_DATA_DIR,
            _T("app.identifier")));
    keys.insert(std::map<TString, TString>::value_type(CONFIG_SPLASH_KEY,
            _T("app.splash")));
    keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_MEMORY,
            _T("app.memory")));
    keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_DEBUG,
            _T("app.debug")));
    keys.insert(std::map<TString,
            TString>::value_type(CONFIG_APPLICATION_INSTANCE,
            _T("app.application.instance")));
    keys.insert(std::map<TString,
            TString>::value_type(CONFIG_SECTION_APPLICATION,
            _T("Application")));
    keys.insert(std::map<TString,
            TString>::value_type(CONFIG_SECTION_JVMOPTIONS,
            _T("JVMOptions")));
    keys.insert(std::map<TString,
            TString>::value_type(CONFIG_SECTION_APPCDSJVMOPTIONS,
            _T("AppCDSJVMOptions")));
    keys.insert(std::map<TString,
            TString>::value_type(CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS,
            _T("AppCDSGenerateCacheJVMOptions")));
    keys.insert(std::map<TString,
            TString>::value_type(CONFIG_SECTION_ARGOPTIONS,
            _T("ArgOptions")));

    return keys;
}

#ifdef DEBUG
DebugState GenericPlatform::GetDebugState() {
    DebugState result = dsNone;

    if (IsNativeDebuggerPresent() == true) {
        result = dsNative;
    }

    return result;
}
#endif // DEBUG