OW_dyldSharedLibraryLoader.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00035 #include "OW_config.h"
00036 #if defined(OW_USE_DYLD)
00037 #include "OW_dyldSharedLibraryLoader.hpp"
00038 #include "OW_dyldSharedLibrary.hpp"
00039 #include "OW_Format.hpp"
00040 
00041 namespace OW_NAMESPACE
00042 {
00043 
00044 std::ostream& operator<<(std::ostream& o, NSObjectFileImageReturnCode code)
00045 {
00046 #define SIMPLE_NSOBJ_CASE(X) case X: o << ""#X
00047    switch (code)
00048    {
00049       SIMPLE_NSOBJ_CASE(NSObjectFileImageFailure);
00050       break;
00051       SIMPLE_NSOBJ_CASE(NSObjectFileImageSuccess);
00052       break;
00053       SIMPLE_NSOBJ_CASE(NSObjectFileImageInappropriateFile);
00054       break;
00055       SIMPLE_NSOBJ_CASE(NSObjectFileImageArch);
00056       break;
00057       SIMPLE_NSOBJ_CASE(NSObjectFileImageFormat);
00058       break;
00059       SIMPLE_NSOBJ_CASE(NSObjectFileImageAccess);
00060       break;
00061    }
00062 #undef SIMPLE_NSOBJ_CASE
00063    return o;
00064 }
00065 
00067 SharedLibraryRef
00068 dyldSharedLibraryLoader::loadSharedLibrary(const String& filename,
00069    const LoggerRef& logger) const
00070 {
00071    OW_LOG_DEBUG(logger, Format("Load request for %1 received.", filename));
00072    NSObjectFileImage image = 0;
00073    NSObjectFileImageReturnCode dsoerr = NSCreateObjectFileImageFromFile(filename.c_str(), &image);
00074    const char* err_msg = NULL;
00075    NSModule libhandle = NULL; 
00076 
00077    if (dsoerr == NSObjectFileImageSuccess)
00078    {
00079       libhandle = NSLinkModule(image, filename.c_str(), NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE);      
00080       if (!libhandle)
00081       {
00082          NSLinkEditErrors errors;
00083          int errorNumber;
00084          const char *fileName;
00085          NSLinkEditError(&errors, &errorNumber, &fileName, &err_msg);
00086       }
00087       NSDestroyObjectFileImage(image);
00088    }
00089    else if ((dsoerr == NSObjectFileImageFormat ||
00090                dsoerr == NSObjectFileImageInappropriateFile) &&
00091                NSAddLibrary(filename.c_str()) == TRUE)
00092    {
00093       OW_LOG_ERROR(logger, Format("NSCreateObject: %1 failed with error \"%2\"",
00094                                           filename, dsoerr));
00095       // libhandle = (NSModule)DYLD_LIBRARY_HANDLE;
00096    }
00097    else
00098    {
00099       err_msg = "cannot create object file image or add library";
00100       OW_LOG_ERROR(logger, Format("NSCreateObject: %1 failed with error %2",
00101                                           filename, dsoerr));
00102    }
00103 
00104 
00105 
00106    if (libhandle)
00107    {
00108       try
00109       {
00110          return SharedLibraryRef( new dyldSharedLibrary(libhandle,
00111                                                                                filename));
00112       }
00113       catch (...)
00114       {
00115          NSUnLinkModule(libhandle, FALSE);
00116          throw;
00117       }
00118    }
00119    else
00120    {
00121       OW_LOG_ERROR(logger, Format("dyldSharedLibraryLoader::loadSharedLibrary:"
00122                                           " %1", err_msg));
00123       return SharedLibraryRef( 0 );
00124    }
00125 }
00127 SharedLibraryLoaderRef
00128 SharedLibraryLoader::createSharedLibraryLoader()
00129 {
00130    return SharedLibraryLoaderRef(new dyldSharedLibraryLoader);
00131 }
00133 dyldSharedLibraryLoader::~dyldSharedLibraryLoader()
00134 {
00135 }
00136 
00137 } // end namespace OW_NAMESPACE
00138 
00139 #endif // #if defined(OW_USE_DYLD)

Generated on Thu Feb 9 08:47:58 2006 for openwbem by  doxygen 1.4.6