1 /* 2 * Copyright (c) 2014, 2015, 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 "Helpers.h" 35 #include "PlatformString.h" 36 #include "PropertyFile.h" 37 38 39 bool Helpers::SplitOptionIntoNameValue(TString option, TString& Name, TString& Value) { 40 bool result = false; 41 Name = _T(""); 42 Value = _T(""); 43 unsigned int index = 0; 44 45 for (; index < option.length(); index++) { 46 TCHAR c = option[index]; 47 48 switch (c) { 49 case '=': { 50 index++; 51 result = true; 52 break; 53 } 54 55 case '\\': { 56 if (index + 1 < option.length()) { 57 c = option[index + 1]; 58 59 switch (c) { 60 case '\\': { 61 index++; 62 Name += '\\'; 63 break; 64 } 65 66 case '=': { 67 index++; 68 Name += '='; 69 break; 70 } 71 } 72 73 } 74 75 continue; 76 } 77 78 default: { 79 Name += c; 80 continue; 81 } 82 } 83 84 break; 85 } 86 87 if (result) { 88 Value = option.substr(index, index - option.length()); 89 } 90 91 return true; 92 } 93 94 95 TString Helpers::ReplaceString(TString subject, const TString& search, 96 const TString& replace) { 97 size_t pos = 0; 98 while((pos = subject.find(search, pos)) != TString::npos) { 99 subject.replace(pos, search.length(), replace); 100 pos += replace.length(); 101 } 102 return subject; 103 } 104 105 TString Helpers::ConvertIdToFilePath(TString Value) { 106 TString search; 107 search = '.'; 108 TString replace; 109 replace = '/'; 110 TString result = ReplaceString(Value, search, replace); 111 return result; 112 } 113 114 TString Helpers::ConvertIdToJavaPath(TString Value) { 115 TString search; 116 search = '.'; 117 TString replace; 118 replace = '/'; 119 TString result = ReplaceString(Value, search, replace); 120 search = '\\'; 121 result = ReplaceString(result, search, replace); 122 return result; 123 } 124 125 TString Helpers::ConvertJavaPathToId(TString Value) { 126 TString search; 127 search = '/'; 128 TString replace; 129 replace = '.'; 130 TString result = ReplaceString(Value, search, replace); 131 return result; 132 } 133 134 OrderedMap<TString, TString> Helpers::GetJVMArgsFromConfig(IPropertyContainer* config) { 135 OrderedMap<TString, TString> result; 136 137 for (unsigned int index = 0; index < config->GetCount(); index++) { 138 TString argname = TString(_T("jvmarg.")) + PlatformString(index + 1).toString(); 139 TString argvalue; 140 141 if (config->GetValue(argname, argvalue) == false) { 142 break; 143 } 144 else if (argvalue.empty() == false) { 145 TString name; 146 TString value; 147 Helpers::SplitOptionIntoNameValue(argvalue, name, value); 148 result.Append(name, value); 149 } 150 } 151 152 return result; 153 } 154 155 OrderedMap<TString, TString> Helpers::GetJVMUserArgsFromConfig(IPropertyContainer* config) { 156 OrderedMap<TString, TString> result; 157 158 for (unsigned int index = 0; index < config->GetCount(); index++) { 159 TString prefix = TString(_T("jvmuserarg.")) + PlatformString(index + 1).toString(); 160 TString argname = prefix + _T(".name"); 161 TString argvalue = prefix + _T(".value"); 162 TString name; 163 TString value; 164 165 if ((config->GetValue(argname, name) == false) || (config->GetValue(argvalue, value) == false)) { 166 break; 167 } 168 else if ((name.empty() == false) && (value.empty() == false)) { 169 result.Append(name, value); 170 } 171 } 172 173 return result; 174 } 175 176 std::list<TString> Helpers::GetArgsFromConfig(IPropertyContainer* config) { 177 std::list<TString> result; 178 179 for (unsigned int index = 0; index < config->GetCount(); index++) { 180 TString argname = TString(_T("arg.")) + PlatformString(index + 1).toString(); 181 TString argvalue; 182 183 if (config->GetValue(argname, argvalue) == false) { 184 break; 185 } 186 else if (argvalue.empty() == false) { 187 result.push_back((argvalue)); 188 } 189 } 190 191 return result; 192 } 193 194 void AppendToIni(PropertyFile &Source, IniFile* Destination, TString Key) { 195 TString value; 196 197 if (Source.GetValue(Key, value) == true) { 198 Platform& platform = Platform::GetInstance(); 199 std::map<TString, TString> keys = platform.GetKeys(); 200 Destination->Append(keys[CONFIG_SECTION_APPLICATION], Key, value); 201 } 202 } 203 204 void Helpers::LoadOldConfigFile(TString FileName, IniFile* Container) { 205 PropertyFile propertyFile; 206 207 if (propertyFile.LoadFromFile(FileName) == true) { 208 Platform& platform = Platform::GetInstance(); 209 210 std::map<TString, TString> keys = platform.GetKeys(); 211 212 // Application Section 213 AppendToIni(propertyFile, Container, keys[CONFIG_MAINJAR_KEY]); 214 AppendToIni(propertyFile, Container, keys[CONFIG_MAINCLASSNAME_KEY]); 215 AppendToIni(propertyFile, Container, keys[CONFIG_CLASSPATH_KEY]); 216 AppendToIni(propertyFile, Container, keys[APP_NAME_KEY]); 217 AppendToIni(propertyFile, Container, keys[CONFIG_APP_ID_KEY]); 218 AppendToIni(propertyFile, Container, keys[JVM_RUNTIME_KEY]); 219 AppendToIni(propertyFile, Container, keys[PACKAGER_APP_DATA_DIR]); 220 221 AppendToIni(propertyFile, Container, keys[CONFIG_APP_MEMORY]); 222 AppendToIni(propertyFile, Container, keys[CONFIG_SPLASH_KEY]); 223 224 // JVMOptions Section 225 OrderedMap<TString, TString> JVMArgs = Helpers::GetJVMArgsFromConfig(&propertyFile); 226 Container->AppendSection(keys[CONFIG_SECTION_JVMOPTIONS], JVMArgs); 227 228 // JVMUserOptions Section 229 OrderedMap<TString, TString> defaultJVMUserArgs = Helpers::GetJVMUserArgsFromConfig(&propertyFile); 230 Container->AppendSection(keys[CONFIG_SECTION_JVMUSEROPTIONS], defaultJVMUserArgs); 231 232 // ArgOptions Section 233 std::list<TString> args = Helpers::GetArgsFromConfig(&propertyFile); 234 OrderedMap<TString, TString> convertedArgs; 235 236 for (std::list<TString>::iterator iterator = args.begin(); iterator != args.end(); iterator++) { 237 TString arg = *iterator; 238 TString name; 239 TString value; 240 241 if (Helpers::SplitOptionIntoNameValue(arg, name, value) == true) { 242 convertedArgs.Append(name, value); 243 } 244 } 245 246 Container->AppendSection(keys[CONFIG_SECTION_ARGOPTIONS], convertedArgs); 247 } 248 } 249 250 void Helpers::LoadOldUserConfigFile(TString FileName, IniFile* Container) { 251 PropertyFile propertyFile; 252 Container = NULL; 253 254 if (propertyFile.LoadFromFile(FileName) == true) { 255 Container = new IniFile(); 256 Platform& platform = Platform::GetInstance(); 257 258 std::map<TString, TString> keys = platform.GetKeys(); 259 260 // JVMUserOverridesOptions Section 261 OrderedMap<TString, TString> defaultJVMUserArgs = Helpers::GetJVMUserArgsFromConfig(&propertyFile); 262 Container->AppendSection(keys[CONFIG_SECTION_JVMUSEROVERRIDESOPTIONS], defaultJVMUserArgs); 263 } 264 } 265 266 std::list<TString> Helpers::MapToNameValueList(OrderedMap<TString, TString> Map) { 267 std::list<TString> result; 268 std::vector<TString> keys = Map.GetKeys(); 269 270 for (OrderedMap<TString, TString>::const_iterator iterator = Map.begin(); iterator != Map.end(); iterator++) { 271 pair<TString, TString> *item = *iterator; 272 TString key = item->first; 273 TString value = item->second; 274 275 if (value.length() == 0) { 276 result.push_back(key); 277 } else { 278 result.push_back(key + _T('=') + value); 279 } 280 } 281 282 return result; 283 } 284 285 286 TString Helpers::NameValueToString(TString name, TString value) { 287 TString result; 288 289 if (value.empty() == true) { 290 result = name; 291 } 292 else { 293 result = name + TString(_T("=")) + value; 294 } 295 296 return result; 297 }