src/jdk.packager/share/native/library/common/Platform.cpp
author kcr
Fri, 10 Aug 2018 14:56:29 -0700
branchJDK-8200758-branch
changeset 56854 aedce3eaaf17
parent 56821 565d54ca1f41
child 56982 e094d5483bd6
permissions -rw-r--r--
8209377: Add proper copyright headers on all jdk.packager source files Reviewed-by: herrick

/*
 * 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 "Platform.h"
#include "Lock.h"
#include "Messages.h"

#include "WindowsPlatform.h"
#include "LinuxPlatform.h"
#include "MacPlatform.h"


// Environment
StaticReadProperty<TString, &Environment::GetNewLine> Environment::NewLine;


//--------------------------------------------------------------------------------------------------

Platform& Platform::GetInstance() {
    //Lock lock(true);
#ifdef WINDOWS
    static WindowsPlatform instance;
#endif // WINDOWS
#ifdef LINUX
    static LinuxPlatform instance;
#endif // LINUX
#ifdef MAC
    static MacPlatform instance;
#endif // MAC
    return instance;
}

//--------------------------------------------------------------------------------------------------


Library::Library() {
    Initialize();
}

Library::Library(const TString &FileName) {
    Initialize();
    Load(FileName);
}

Library::~Library() {
    Unload();
}

void Library::Initialize() {
    FModule = NULL;
    FDependentLibraryNames = NULL;
    FDependenciesLibraries = NULL;
}

void Library::InitializeDependencies() {
    if (FDependentLibraryNames == NULL) {
        FDependentLibraryNames = new std::vector<TString>();
    }

    if (FDependenciesLibraries == NULL) {
        FDependenciesLibraries = new std::vector<Library*>();
    }
}

void Library::LoadDependencies() {
    if (FDependentLibraryNames != NULL && FDependenciesLibraries != NULL) {
        for (std::vector<TString>::const_iterator iterator = FDependentLibraryNames->begin();
                iterator != FDependentLibraryNames->end(); iterator++) {
            Library* library = new Library();

            if (library->Load(*iterator) == true) {
                FDependenciesLibraries->push_back(library);
            }
        }

        delete FDependentLibraryNames;
        FDependentLibraryNames = NULL;
    }
}

void Library::UnloadDependencies() {
    if (FDependenciesLibraries != NULL) {
        for (std::vector<Library*>::const_iterator iterator = FDependenciesLibraries->begin();
                iterator != FDependenciesLibraries->end(); iterator++) {
            Library* library = *iterator;

            if (library != NULL) {
                library->Unload();
                delete library;
            }
        }

        delete FDependenciesLibraries;
        FDependenciesLibraries = NULL;
    }
}

Procedure Library::GetProcAddress(const std::string& MethodName) const {
    Platform& platform = Platform::GetInstance();
    return platform.GetProcAddress(FModule, MethodName);
}

bool Library::Load(const TString &FileName) {
    bool result = true;

    if (FModule == NULL) {
        LoadDependencies();
        Platform& platform = Platform::GetInstance();
        FModule = platform.LoadLibrary(FileName);

        if (FModule == NULL) {
            Messages& messages = Messages::GetInstance();
            platform.ShowMessage(messages.GetMessage(LIBRARY_NOT_FOUND), FileName);
            result = false;
        } else {
            fname = PlatformString(FileName).toStdString();
        }
    }

    return result;
}

bool Library::Unload() {
    bool result = false;

    if (FModule != NULL) {
        Platform& platform = Platform::GetInstance();
        platform.FreeLibrary(FModule);
        FModule = NULL;
        UnloadDependencies();
        result = true;
    }

    return result;
}

void Library::AddDependency(const TString &FileName) {
    InitializeDependencies();

    if (FDependentLibraryNames != NULL) {
        FDependentLibraryNames->push_back(FileName);
    }
}

void Library::AddDependencies(const std::vector<TString> &Dependencies) {
    if (Dependencies.size() > 0) {
        InitializeDependencies();

        if (FDependentLibraryNames != NULL) {
            for (std::vector<TString>::const_iterator iterator = FDependentLibraryNames->begin();
                iterator != FDependentLibraryNames->end(); iterator++) {
                TString fileName = *iterator;
                AddDependency(fileName);
            }
        }
    }
}
//--------------------------------------------------------------------------------------------------