1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 #include "GenericPlatform.h" 35 36 #include <fstream> 37 #include <locale> 38 39 #ifdef WINDOWS 40 #include <codecvt> 41 #endif //WINDOWS 42 43 44 GenericPlatform::GenericPlatform(void) { 45 } 46 47 GenericPlatform::~GenericPlatform(void) { 48 } 49 50 TString GenericPlatform::GetConfigFileName() { 51 TString result; 52 TString basedir = GetPackageAppDirectory(); 53 54 if (basedir.empty() == false) { 55 basedir = FilePath::IncludeTrailingSeparater(basedir); 56 TString appConfig = basedir + GetAppName() + _T(".cfg"); 57 58 if (FilePath::FileExists(appConfig) == true) { 59 result = appConfig; 60 } 61 else { 62 result = basedir + _T("package.cfg"); 63 64 if (FilePath::FileExists(result) == false) { 65 result = _T(""); 66 } 67 } 68 } 69 70 return result; 71 } 72 73 TString GenericPlatform::GetPackageAppDirectory() { 74 #if defined(WINDOWS) || defined(LINUX) 75 return FilePath::IncludeTrailingSeparater(GetPackageRootDirectory()) + _T("app"); 76 #endif //WINDOWS || LINUX 77 #ifdef MAC 78 return FilePath::IncludeTrailingSeparater(GetPackageRootDirectory()) + _T("Java"); 79 #endif 80 } 81 82 TString GenericPlatform::GetPackageLauncherDirectory() { 83 #if defined(WINDOWS) || defined(LINUX) 84 return GetPackageRootDirectory(); 85 #endif //WINDOWS || LINUX 86 #ifdef MAC 87 return FilePath::IncludeTrailingSeparater(GetPackageRootDirectory()) + _T("MacOS"); 88 #endif 89 } 90 91 std::list<TString> GenericPlatform::LoadFromFile(TString FileName) { 92 std::list<TString> result; 93 94 if (FilePath::FileExists(FileName) == true) { 95 std::wifstream stream(FileName.data()); 96 97 #ifdef WINDOWS 98 const std::locale empty_locale = std::locale::empty(); 99 #endif //WINDOWS 100 #ifdef POSIX 101 const std::locale empty_locale = std::locale::classic(); 102 #endif //POSIX 103 #if defined(WINDOWS) 104 const std::locale utf8_locale = std::locale(empty_locale, new std::codecvt_utf8<wchar_t>()); 105 stream.imbue(utf8_locale); 106 #endif //WINDOWS 107 108 if (stream.is_open() == true) { 109 while (stream.eof() == false) { 110 std::wstring line; 111 std::getline(stream, line); 112 113 // # at the first character will comment out the line. 114 if (line.empty() == false && line[0] != '#') { 115 result.push_back(PlatformString(line).toString()); 116 } 117 } 118 } 119 } 120 121 return result; 122 } 123 124 void GenericPlatform::SaveToFile(TString FileName, std::list<TString> Contents, bool ownerOnly) { 125 TString path = FilePath::ExtractFilePath(FileName); 126 127 if (FilePath::DirectoryExists(path) == false) { 128 FilePath::CreateDirectory(path, ownerOnly); 129 } 130 131 std::wofstream stream(FileName.data()); 132 133 FilePath::ChangePermissions(FileName.data(), ownerOnly); 134 135 #ifdef WINDOWS 136 const std::locale empty_locale = std::locale::empty(); 137 #endif //WINDOWS 138 #ifdef POSIX 139 const std::locale empty_locale = std::locale::classic(); 140 #endif //POSIX 141 #if defined(WINDOWS) 142 const std::locale utf8_locale = std::locale(empty_locale, new std::codecvt_utf8<wchar_t>()); 143 stream.imbue(utf8_locale); 144 #endif //WINDOWS || MAC 145 146 if (stream.is_open() == true) { 147 for (std::list<TString>::const_iterator iterator = Contents.begin(); iterator != Contents.end(); iterator++) { 148 TString line = *iterator; 149 stream << PlatformString(line).toUnicodeString() << std::endl; 150 } 151 } 152 } 153 154 #if defined(WINDOWS) || defined(LINUX) 155 TString GenericPlatform::GetAppName() { 156 TString result = GetModuleFileName(); 157 result = FilePath::ExtractFileName(result); 158 result = FilePath::ChangeFileExt(result, _T("")); 159 return result; 160 } 161 #endif //WINDOWS || LINUX 162 163 std::map<TString, TString> GenericPlatform::GetKeys() { 164 std::map<TString, TString> keys; 165 keys.insert(std::map<TString, TString>::value_type(CONFIG_VERSION, _T("app.version"))); 166 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINJAR_KEY, _T("app.mainjar"))); 167 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINCLASSNAME_KEY, _T("app.mainclass"))); 168 keys.insert(std::map<TString, TString>::value_type(CONFIG_CLASSPATH_KEY, _T("app.classpath"))); 169 keys.insert(std::map<TString, TString>::value_type(APP_NAME_KEY, _T("app.name"))); 170 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_ID_KEY, _T("app.preferences.id"))); 171 keys.insert(std::map<TString, TString>::value_type(JVM_RUNTIME_KEY, _T("app.runtime"))); 172 keys.insert(std::map<TString, TString>::value_type(PACKAGER_APP_DATA_DIR, _T("app.identifier"))); 173 174 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_MEMORY, _T("app.memory"))); 175 keys.insert(std::map<TString, TString>::value_type(CONFIG_SPLASH_KEY, _T("app.splash"))); 176 177 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_APPLICATION, _T("Application"))); 178 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_JVMOPTIONS, _T("JVMOptions"))); 179 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_JVMUSEROPTIONS, _T("JVMUserOptions"))); 180 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_JVMUSEROVERRIDESOPTIONS, _T("JVMUserOverrideOptions"))); 181 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_APPCDSJVMOPTIONS, _T("AppCDSJVMOptions"))); 182 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_APPCDSGENERATECACHEJVMOPTIONS, _T("AppCDSGenerateCacheJVMOptions"))); 183 keys.insert(std::map<TString, TString>::value_type(CONFIG_SECTION_ARGOPTIONS, _T("ArgOptions"))); 184 185 return keys; 186 } 187 188 #ifdef DEBUG 189 DebugState GenericPlatform::GetDebugState() { 190 DebugState result = dsNone; 191 192 if (IsNativeDebuggerPresent() == true) { 193 result = dsNative; 194 } 195 196 return result; 197 } 198 #endif //DEBUG