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 "Helpers.h" 35 #include "PlatformString.h" 36 37 38 bool Helpers::SplitOptionIntoNameValue(TString option, TString& Name, TString& Value) { 39 bool result = false; 40 size_t position = option.find('='); 41 42 if (position != TString::npos) { 43 Name = option.substr(0, position); 44 Value = option.substr(position + 1, option.length() - position + 1); 45 result = true; 46 } 47 else { 48 Name = option; 49 } 50 51 return result; 52 } 53 54 TString Helpers::ReplaceString(TString subject, const TString& search, 55 const TString& replace) { 56 size_t pos = 0; 57 while((pos = subject.find(search, pos)) != TString::npos) { 58 subject.replace(pos, search.length(), replace); 59 pos += replace.length(); 60 } 61 return subject; 62 } 63 64 TString Helpers::ConvertIdToFilePath(TString Value) { 65 TString search; 66 search = '.'; 67 TString replace; 68 replace = '/'; 69 TString result = ReplaceString(Value, search, replace); 70 return result; 71 } 72 73 TString Helpers::ConvertIdToJavaPath(TString Value) { 74 TString search; 75 search = '.'; 76 TString replace; 77 replace = '/'; 78 TString result = ReplaceString(Value, search, replace); 79 search = '\\'; 80 result = ReplaceString(result, search, replace); 81 return result; 82 } 83 84 TString Helpers::ConvertPathToId(TString Value) { 85 TString search; 86 search = TRAILING_SLASH; 87 TString replace; 88 replace = '.'; 89 TString result = ReplaceString(Value, search, replace); 90 return result; 91 } 92 93 TOrderedMap Helpers::GetJVMArgsFromConfig(PropertyContainer* config) { 94 TOrderedMap result; 95 96 for (unsigned int index = 0; index < config->GetCount(); index++) { 97 TString argname = TString(_T("jvmarg.")) + PlatformString(index + 1).toString(); 98 TString argvalue; 99 100 if (config->GetValue(argname, argvalue) == false) { 101 break; 102 } 103 else if (argvalue.empty() == false) { 104 TString name; 105 TValueIndex value; 106 Helpers::SplitOptionIntoNameValue(argvalue, name, value.value); 107 result.insert(TOrderedMap::value_type(name, value)); 108 } 109 } 110 111 return result; 112 } 113 114 TOrderedMap Helpers::GetJVMUserArgsFromConfig(PropertyContainer* config) { 115 TOrderedMap result; 116 117 for (unsigned int index = 0; index < config->GetCount(); index++) { 118 TString prefix = TString(_T("jvmuserarg.")) + PlatformString(index + 1).toString(); 119 TString argname = prefix + _T(".name"); 120 TString argvalue = prefix + _T(".value"); 121 TString name; 122 TValueIndex value; 123 124 if ((config->GetValue(argname, name) == false) || (config->GetValue(argvalue, value.value) == false)) { 125 break; 126 } 127 else if ((name.empty() == false) && (value.value.empty() == false)) { 128 result.insert(TOrderedMap::value_type(name, value)); 129 } 130 } 131 132 return result; 133 } 134 135 std::map<TString, TString> Helpers::GetConfigFromJVMUserArgs(TOrderedMap OrderedMap) { 136 std::map<TString, TString> result; 137 size_t index = 0; 138 139 for (TOrderedMap::iterator iterator = OrderedMap.begin(); 140 iterator != OrderedMap.end(); 141 iterator++) { 142 TString prefix = TString(_T("jvmuserarg.")) + PlatformString(index + 1).toString(); 143 TString argname = prefix + _T(".name"); 144 TString argvalue = prefix + _T(".value"); 145 TString name = iterator->first; 146 TString value = iterator->second.value; 147 148 result.insert(std::map<TString, TString>::value_type(argname, name)); 149 result.insert(std::map<TString, TString>::value_type(argvalue, value)); 150 index++; 151 } 152 153 return result; 154 } 155 156 std::list<TString> Helpers::GetArgsFromConfig(PropertyContainer* config) { 157 std::list<TString> result; 158 159 for (unsigned int index = 0; index < config->GetCount(); index++) { 160 TString argname = TString(_T("arg.")) + PlatformString(index + 1).toString(); 161 TString argvalue; 162 163 if (config->GetValue(argname, argvalue) == false) { 164 break; 165 } 166 else if (argvalue.empty() == false) { 167 result.push_back((argvalue)); 168 } 169 } 170 171 return result; 172 } 173 174 bool comp(const TValueIndex& a, const TValueIndex& b) { 175 return a.index < b.index; 176 } 177 178 std::list<TString> Helpers::GetOrderedKeysFromMap(TOrderedMap OrderedMap) { 179 std::list<TString> result; 180 std::list<TValueIndex> indexedList; 181 182 for (TOrderedMap::iterator iterator = OrderedMap.begin(); 183 iterator != OrderedMap.end(); 184 iterator++) { 185 TValueIndex item; 186 item.value = iterator->first; 187 item.index = iterator->second.index; 188 indexedList.push_back(item); 189 } 190 191 indexedList.sort(comp); 192 193 for (std::list<TValueIndex>::const_iterator iterator = indexedList.begin(); iterator != indexedList.end(); iterator++) { 194 TString name = iterator->value; 195 result.push_back(name); 196 } 197 198 return result; 199 } 200 201 TString Helpers::NameValueToString(TString name, TString value) { 202 TString result; 203 204 if (value.empty() == true) { 205 result = name; 206 } 207 else { 208 result = name + TString(_T("=")) + value; 209 } 210 211 return result; 212 }