1 /* 2 * Copyright (c) 2014, 2019, 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 "Platform.h" 27 #include "Lock.h" 28 #include "Messages.h" 29 30 #include "WindowsPlatform.h" 31 #include "LinuxPlatform.h" 32 #include "MacPlatform.h" 33 34 35 // Environment 36 StaticReadProperty<TString, &Environment::GetNewLine> Environment::NewLine; 37 38 39 Platform& Platform::GetInstance() { 40 41 #ifdef WINDOWS 42 static WindowsPlatform instance; 43 #endif // WINDOWS 44 45 #ifdef LINUX 46 static LinuxPlatform instance; 47 #endif // LINUX 48 49 #ifdef MAC 50 static MacPlatform instance; 51 #endif // MAC 52 return instance; 53 } 54 55 56 Library::Library() { 57 Initialize(); 58 } 59 60 Library::Library(const TString &FileName) { 61 Initialize(); 62 Load(FileName); 63 } 64 65 Library::~Library() { 66 Unload(); 67 } 68 69 void Library::Initialize() { 70 FModule = NULL; 71 FDependentLibraryNames = NULL; 72 FDependenciesLibraries = NULL; 73 } 74 75 void Library::InitializeDependencies() { 76 if (FDependentLibraryNames == NULL) { 77 FDependentLibraryNames = new std::vector<TString>(); 78 } 79 80 if (FDependenciesLibraries == NULL) { 81 FDependenciesLibraries = new std::vector<Library*>(); 82 } 83 } 84 85 void Library::LoadDependencies() { 86 if (FDependentLibraryNames != NULL && FDependenciesLibraries != NULL) { 87 for (std::vector<TString>::const_iterator iterator = 88 FDependentLibraryNames->begin(); 89 iterator != FDependentLibraryNames->end(); iterator++) { 90 Library* library = new Library(); 91 92 if (library->Load(*iterator) == true) { 93 FDependenciesLibraries->push_back(library); 94 } 95 } 96 97 delete FDependentLibraryNames; 98 FDependentLibraryNames = NULL; 99 } 100 } 101 102 void Library::UnloadDependencies() { 103 if (FDependenciesLibraries != NULL) { 104 for (std::vector<Library*>::const_iterator iterator = 105 FDependenciesLibraries->begin(); 106 iterator != FDependenciesLibraries->end(); iterator++) { 107 Library* library = *iterator; 108 109 if (library != NULL) { 110 library->Unload(); 111 delete library; 112 } 113 } 114 115 delete FDependenciesLibraries; 116 FDependenciesLibraries = NULL; 117 } 118 } 119 120 Procedure Library::GetProcAddress(const std::string& MethodName) const { 121 Platform& platform = Platform::GetInstance(); 122 return platform.GetProcAddress(FModule, MethodName); 123 } 124 125 bool Library::Load(const TString &FileName) { 126 bool result = true; 127 128 if (FModule == NULL) { 129 LoadDependencies(); 130 Platform& platform = Platform::GetInstance(); 131 FModule = platform.LoadLibrary(FileName); 132 133 if (FModule == NULL) { 134 Messages& messages = Messages::GetInstance(); 135 platform.ShowMessage(messages.GetMessage(LIBRARY_NOT_FOUND), 136 FileName); 137 result = false; 138 } else { 139 fname = PlatformString(FileName).toStdString(); 140 } 141 } 142 143 return result; 144 } 145 146 bool Library::Unload() { 147 bool result = false; 148 149 if (FModule != NULL) { 150 Platform& platform = Platform::GetInstance(); 151 platform.FreeLibrary(FModule); 152 FModule = NULL; 153 UnloadDependencies(); 154 result = true; 155 } 156 157 return result; 158 } 159 160 void Library::AddDependency(const TString &FileName) { 161 InitializeDependencies(); 162 163 if (FDependentLibraryNames != NULL) { 164 FDependentLibraryNames->push_back(FileName); 165 } 166 } 167 168 void Library::AddDependencies(const std::vector<TString> &Dependencies) { 169 if (Dependencies.size() > 0) { 170 InitializeDependencies(); 171 172 if (FDependentLibraryNames != NULL) { 173 for (std::vector<TString>::const_iterator iterator = 174 FDependentLibraryNames->begin(); 175 iterator != FDependentLibraryNames->end(); iterator++) { 176 TString fileName = *iterator; 177 AddDependency(fileName); 178 } 179 } 180 } 181 }