Back to index

nordugrid-arc-nox  1.1.0~rc6
Public Member Functions | Private Attributes | Static Private Attributes
Arc::ModuleManager Class Reference

Manager of shared libraries. More...

#include <ModuleManager.h>

Inheritance diagram for Arc::ModuleManager:
Inheritance graph
[legend]
Collaboration diagram for Arc::ModuleManager:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ModuleManager (XMLNode cfg)
 Cache of handles of loaded modules.
 ~ModuleManager ()
Glib::Module * load (const std::string &name, bool probe=false)
 Finds module 'name' in cache or loads corresponding loadable module.
std::string find (const std::string &name)
 Finds loadable module by 'name' looking in same places as load() does, but does not load it.
Glib::Module * reload (Glib::Module *module)
 Reload module previously loaded in probe mode.
void unload (Glib::Module *module)
 Unload module by its identifier.
void unload (const std::string &name)
 Unload module by its name.
std::string findLocation (const std::string &name)
 Finds shared library corresponding to module 'name' and returns path to it.
bool makePersistent (Glib::Module *module)
 Make sure this module is never unloaded.
bool makePersistent (const std::string &name)
 Make sure this module is never unloaded.
void setCfg (XMLNode cfg)
 Input the configuration subtree, and trigger the module loading (do almost the same as the Constructor); It is function desgined for ClassLoader to adopt the singleton pattern.

Private Attributes

std::vector< std::string > plugin_dir
plugin_cache_t plugin_cache
 collection of path to directory for modules

Static Private Attributes

static Logger logger

Detailed Description

Manager of shared libraries.

This class loads shared libraries/modules. There supposed to be created one instance of it per executable. In such circumstances it would cache handles to loaded modules and not load them multiple times.

Definition at line 46 of file ModuleManager.h.


Constructor & Destructor Documentation

Cache of handles of loaded modules.

Constructor. It is supposed to process correponding configuration subtree and tune module loading parameters accordingly.

Definition at line 19 of file ModuleManager.cpp.

{ 
  if(!cfg) return;
  ModuleManager::logger.msg(DEBUG, "Module Manager Init");
  if(!MatchXMLName(cfg,"ArcConfig")) return;
  XMLNode mm = cfg["ModuleManager"];
  for (int n = 0;;++n) {
    XMLNode path = mm.Child(n);
    if (!path) {
      break;
    }
    if (MatchXMLName(path, "Path")) {
      plugin_dir.push_back((std::string)path);
    }
  }
}

Here is the call graph for this function:

Definition at line 36 of file ModuleManager.cpp.

{
    // removes all elements from cache
    plugin_cache_t::iterator i;
    for(i=plugin_cache.begin();i!=plugin_cache.end();++i) {
        while(i->second.unload() > 0) { };
    };
    plugin_cache.clear();
}

Member Function Documentation

std::string Arc::ModuleManager::find ( const std::string &  name)

Finds loadable module by 'name' looking in same places as load() does, but does not load it.

Definition at line 88 of file ModuleManager.cpp.

{
  return findLocation(name);
}

Here is the call graph for this function:

Here is the caller graph for this function:

std::string Arc::ModuleManager::findLocation ( const std::string &  name)

Finds shared library corresponding to module 'name' and returns path to it.

Definition at line 46 of file ModuleManager.cpp.

{
  std::string path;
  std::vector<std::string>::const_iterator i = plugin_dir.begin();
  for (; i != plugin_dir.end(); i++) {
    path = Glib::Module::build_path(*i, name);
    // Loader::logger.msg(VERBOSE, "Try load %s", path);
    FILE *file = fopen(path.c_str(), "r");
    if (file == NULL) {
      continue;
    } else {
      fclose(file);
      break;
    }
  }
  if(i == plugin_dir.end()) path="";
  return path;
}

Here is the caller graph for this function:

Glib::Module * Arc::ModuleManager::load ( const std::string &  name,
bool  probe = false 
)

Finds module 'name' in cache or loads corresponding loadable module.

Definition at line 93 of file ModuleManager.cpp.

{
  if (!Glib::Module::get_supported()) {
    return false;
  }
  // find name in plugin_cache 
  {
    plugin_cache_t::iterator p = plugin_cache.find(name);
    if (p != plugin_cache.end()) {
      ModuleManager::logger.msg(DEBUG, "Found %s in cache", name);
      p->second.load();
      return static_cast<Glib::Module*>(p->second);
    }
  }
  std::string path = findLocation(name);
  if(path.empty()) {
    ModuleManager::logger.msg(VERBOSE, "Could not locate module %s in following paths:", name);
    std::vector<std::string>::const_iterator i = plugin_dir.begin();
    for (; i != plugin_dir.end(); i++) {
      ModuleManager::logger.msg(VERBOSE, "\t%s", *i);
    }
    return NULL;
  };
  Glib::ModuleFlags flags = Glib::ModuleFlags(0);
  if(probe) flags|=Glib::MODULE_BIND_LAZY;
  Glib::Module *module = new Glib::Module(path,flags);
  if ((!module) || (!(*module))) {
    ModuleManager::logger.msg(ERROR, strip_newline(Glib::Module::get_last_error()));
    if(module) delete module;
    return NULL;
  }
  ModuleManager::logger.msg(DEBUG, "Loaded %s", path);
  (plugin_cache[name] = module).load();
  return module;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool Arc::ModuleManager::makePersistent ( Glib::Module *  module)

Make sure this module is never unloaded.

Even if unload() is called.

Definition at line 190 of file ModuleManager.cpp.

                                                     {
  for(plugin_cache_t::iterator p = plugin_cache.begin();
                               p!=plugin_cache.end();++p) {
    if(p->second == module) {
      ModuleManager::logger.msg(DEBUG, "%s made persistent", p->first);
      p->second.makePersistent();
      return true;
    }
  }
  ModuleManager::logger.msg(DEBUG, "Specified module not found in cache");
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool Arc::ModuleManager::makePersistent ( const std::string &  name)

Make sure this module is never unloaded.

Even if unload() is called.

Definition at line 173 of file ModuleManager.cpp.

                                                        {
  if (!Glib::Module::get_supported()) {
    return false;
  }
  // find name in plugin_cache
  {
    plugin_cache_t::iterator p = plugin_cache.find(name);
    if (p != plugin_cache.end()) {
      p->second.makePersistent();
      ModuleManager::logger.msg(DEBUG, "%s made persistent", name);
      return true;
    }
  }
  ModuleManager::logger.msg(DEBUG, "Not found %s in cache", name);
  return false;
}

Here is the call graph for this function:

Glib::Module * Arc::ModuleManager::reload ( Glib::Module *  module)

Reload module previously loaded in probe mode.

New module is loaded with all symbols resolved and old module handler is unloaded. In case of error old module is not unloaded.

Definition at line 129 of file ModuleManager.cpp.

{
  plugin_cache_t::iterator p = plugin_cache.begin();
  for(;p!=plugin_cache.end();++p) {
    if(p->second == omodule) break;
  }
  if(p==plugin_cache.end()) return NULL;
  Glib::ModuleFlags flags = Glib::ModuleFlags(0);
  //flags|=Glib::MODULE_BIND_LOCAL;
  Glib::Module *module = new Glib::Module(omodule->get_name(),flags);
  if ((!module) || (!(*module))) {
    ModuleManager::logger.msg(ERROR, strip_newline(Glib::Module::get_last_error()));
    if(module) delete module;
    return NULL;
  }
  p->second=module;
  delete omodule;
  return module; 
}

Here is the call graph for this function:

Here is the caller graph for this function:

Input the configuration subtree, and trigger the module loading (do almost the same as the Constructor); It is function desgined for ClassLoader to adopt the singleton pattern.

Definition at line 149 of file ModuleManager.cpp.

                                       {
  if(!cfg) return;
  ModuleManager::logger.msg(INFO, "Module Manager Init by ModuleManager::setCfg");

  if(!MatchXMLName(cfg,"ArcConfig")) return;
  XMLNode mm = cfg["ModuleManager"];
  for (int n = 0;;++n) {
    XMLNode path = mm.Child(n);
    if (!path) {
      break;
    }
    if (MatchXMLName(path, "Path")) {
      //std::cout<<"Size:"<<plugin_dir.size()<<"plugin cache size:"<<plugin_cache.size()<<std::endl;
      std::vector<std::string>::const_iterator it;
      for( it = plugin_dir.begin(); it != plugin_dir.end(); it++){
        //std::cout<<(std::string)path<<"*********"<<(*it)<<std::endl;
        if(((*it).compare((std::string)path)) == 0)break;
      }
      if(it == plugin_dir.end())
        plugin_dir.push_back((std::string)path);
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Arc::ModuleManager::unload ( Glib::Module *  module)

Unload module by its identifier.

Definition at line 65 of file ModuleManager.cpp.

{
  for(plugin_cache_t::iterator p = plugin_cache.begin();
                               p!=plugin_cache.end();++p) {
    if(p->second == module) {
      if(p->second.unload() <= 0) {
        plugin_cache.erase(p);
      }      
      break;
    }
  }
}

Here is the caller graph for this function:

void Arc::ModuleManager::unload ( const std::string &  name)

Unload module by its name.

Definition at line 78 of file ModuleManager.cpp.

{
  plugin_cache_t::iterator p = plugin_cache.find(name);
  if (p != plugin_cache.end()) {
    if(p->second.unload() <= 0) {
      plugin_cache.erase(p);
    }
  }
}

Member Data Documentation

Reimplemented in Arc::PluginsFactory, and Arc::ClassLoader.

Definition at line 49 of file ModuleManager.h.

collection of path to directory for modules

Definition at line 51 of file ModuleManager.h.

std::vector<std::string> Arc::ModuleManager::plugin_dir [private]

Definition at line 50 of file ModuleManager.h.


The documentation for this class was generated from the following files: