예제 #1
0
bool PluginManager::loadPlugin(std::string& pluginPath, std::ostream* errlog)
{
    if (sofa::helper::system::SetDirectory::GetParentDir(pluginPath.c_str()).empty() &&
        sofa::helper::system::SetDirectory::GetExtension(pluginPath.c_str()).empty())
    {
        // no path and extension -> automatically add suffix and OS-specific extension
#ifdef SOFA_LIBSUFFIX
        pluginPath += sofa_tostring(SOFA_LIBSUFFIX);
#endif
#if defined (WIN32)
        pluginPath = pluginPath + std::string(".dll");
#elif defined (__APPLE__)
        pluginPath = std::string("lib") + pluginPath + std::string(".dylib");
#else
        pluginPath = std::string("lib") + pluginPath + std::string(".so");
#endif
        //std::cout << "System-specific plugin filename: " << pluginPath << std::endl;
    }

    if( !PluginRepository.findFile(pluginPath,"",errlog) )
    {
        if (errlog) (*errlog) << "Plugin " << pluginPath << " NOT FOUND in: " << PluginRepository << std::endl;
        return false;
    }
    if(m_pluginMap.find(pluginPath) != m_pluginMap.end() )
    {
        if(errlog) (*errlog) << "Plugin " << pluginPath << " already in PluginManager" << std::endl;
        return false;
    }
    DynamicLibrary::Handle d  = DynamicLibrary::load(pluginPath);
    Plugin p;
    if( ! d.isValid() )
    {
        if (errlog) (*errlog) << "Plugin " << pluginPath << " loading FAILED with error: " << DynamicLibrary::getLastError() << std::endl;
        return false;
    }
    else
    {
        if(! getPluginEntry(p.initExternalModule,d))
        {
            if (errlog) (*errlog) << "Plugin " << pluginPath << " method initExternalModule() NOT FOUND" << std::endl;
            return false;
        }
        getPluginEntry(p.getModuleName,d);
        getPluginEntry(p.getModuleDescription,d);
        getPluginEntry(p.getModuleLicense,d);
        getPluginEntry(p.getModuleComponentList,d);
        getPluginEntry(p.getModuleVersion,d);
    }

    p.dynamicLibrary = d;
    m_pluginMap[pluginPath] = p;

    return true;
}
bool PluginManager::loadPluginByPath(const std::string& pluginPath, std::ostream* errlog)
{
    if (pluginIsLoaded(pluginPath))
    {
        const std::string msg = "Plugin already loaded: " + pluginPath;
//        Logger::getMainLogger().log(Logger::Warning, msg, "PluginManager");
        if (errlog) (*errlog) << msg << std::endl;
        return false;
    }

    if (!FileSystem::exists(pluginPath))
    {
        const std::string msg = "File not found: " + pluginPath;
        msg_error("PluginManager") << msg;
        if (errlog) (*errlog) << msg << std::endl;
        return false;
    }

    DynamicLibrary::Handle d  = DynamicLibrary::load(pluginPath);
    Plugin p;
    if( ! d.isValid() )
    {
        const std::string msg = "Plugin loading failed (" + pluginPath + "): " + DynamicLibrary::getLastError();
        msg_error("PluginManager") << msg;
        if (errlog) (*errlog) << msg << std::endl;
        return false;
    }
    else
    {
        if(! getPluginEntry(p.initExternalModule,d))
        {
            const std::string msg = "Plugin loading failed (" + pluginPath + "): function initExternalModule() not found";
            msg_error("PluginManager") << msg;
            if (errlog) (*errlog) << msg << std::endl;
            return false;
        }
        getPluginEntry(p.getModuleName,d);
        getPluginEntry(p.getModuleDescription,d);
        getPluginEntry(p.getModuleLicense,d);
        getPluginEntry(p.getModuleComponentList,d);
        getPluginEntry(p.getModuleVersion,d);
    }

    p.dynamicLibrary = d;
    m_pluginMap[pluginPath] = p;
    p.initExternalModule();

    msg_info("PluginManager") << "Loaded plugin: " << pluginPath;
    return true;
}