javacommons/utils/src/dynamiclibloader.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  DynamicLibLoader
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <memory>
       
    20 
       
    21 #include "logger.h"
       
    22 #include "javaoslayer.h"
       
    23 #include "libraryloaderexception.h"
       
    24 #include "dynamiclibloader.h"
       
    25 
       
    26 using namespace java::util;
       
    27 
       
    28 OS_EXPORT DynamicLibLoader::DynamicLibLoader(const char* libName) :
       
    29         mHandle(0), mCloseOnDestroy(true)
       
    30 {
       
    31     JELOG2(EUtils);
       
    32     JavaOsLayer::getOsSpecificLibName(mLibName, libName);
       
    33 }
       
    34 
       
    35 OS_EXPORT DynamicLibLoader::DynamicLibLoader(const char* libName,
       
    36         bool closeWhenDestructed) :
       
    37         mHandle(0), mCloseOnDestroy(closeWhenDestructed)
       
    38 {
       
    39     JELOG2(EUtils);
       
    40     JavaOsLayer::getOsSpecificLibName(mLibName, libName);
       
    41 }
       
    42 
       
    43 OS_EXPORT DynamicLibLoader::~DynamicLibLoader()
       
    44 {
       
    45     JELOG2(EUtils);
       
    46     if (mCloseOnDestroy)
       
    47     {
       
    48         closeLib();
       
    49     }
       
    50 }
       
    51 
       
    52 OS_EXPORT void DynamicLibLoader::openLib()
       
    53 {
       
    54     if (mHandle == 0)
       
    55     {
       
    56         LOG1(EUtils, EInfo, "Opening lib %s", mLibName.c_str());
       
    57         mHandle = JavaOsLayer::dlopen(mLibName.c_str());
       
    58         if (mHandle == 0)
       
    59         {
       
    60             std::string errorStr("Error opening lib.");
       
    61             errorStr.append(mLibName);
       
    62             throw LibraryLoaderException(OPENING_LIBRARY_FAILED,errorStr,
       
    63                                          __FILE__, __FUNCTION__, __LINE__);
       
    64         }
       
    65     }
       
    66 }
       
    67 
       
    68 OS_EXPORT void DynamicLibLoader::closeLib()
       
    69 {
       
    70     if (mHandle)
       
    71     {
       
    72         LOG1(EUtils, EInfo, "Closing lib %s", mLibName.c_str());
       
    73         JavaOsLayer::dlclose(mHandle);
       
    74         mHandle = 0;
       
    75     }
       
    76 }
       
    77 
       
    78 OS_EXPORT void* DynamicLibLoader::getFunction(const char* functionName,
       
    79         bool containsByteCode)
       
    80 {
       
    81     JELOG2(EUtils);
       
    82     LOG1(EUtils, EInfo, "Getting function %s", functionName);
       
    83     openLib(); //Ensure that the library has been opened.
       
    84     void* func = JavaOsLayer::dlsym(mHandle, functionName, containsByteCode);
       
    85     if (func == 0)
       
    86     {
       
    87         std::string errorStr("Error getting function ");
       
    88         errorStr.append(functionName);
       
    89         errorStr.append(" from library ");
       
    90         errorStr.append(mLibName);
       
    91         throw LibraryLoaderException(CANNOT_FIND_FUNCTION_FAILURE,errorStr,
       
    92                                      __FILE__, __FUNCTION__, __LINE__);
       
    93     }
       
    94     return func;
       
    95 }
       
    96 
       
    97 OS_EXPORT DynamicLibLoader*
       
    98 DynamicLibLoader::loadAndGetFunction(void*&      functionPtr,
       
    99                                      const char* libName,
       
   100                                      const char* functionName)
       
   101 {
       
   102     JELOG2(EUtils);
       
   103 
       
   104     std::auto_ptr<DynamicLibLoader> libLoader(
       
   105         new DynamicLibLoader(libName));
       
   106 
       
   107     //Throws exception if fails
       
   108     functionPtr = (libLoader->getFunction(functionName));
       
   109 
       
   110     //everything went ok so we must not free the libLoader.
       
   111     DynamicLibLoader* loader = libLoader.get();
       
   112     libLoader.release();
       
   113     return loader;
       
   114 }