1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* comploader.cpp
* Copyright (C) 2003-2005 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <tnt/comploader.h>
#include <tnt/componentfactory.h>
#include <tnt/tntconfig.h>
#include <cxxtools/log.h>
namespace tnt
{
////////////////////////////////////////////////////////////////////////
// ComponentLibrary
//
log_define("tntnet.comploader")
ComponentLibrary::~ComponentLibrary()
{
for (langlibsType::iterator it = langlibs.begin();
it != langlibs.end(); ++it)
delete it->second;
}
Component* ComponentLibrary::create(
const std::string& component_name, Comploader& cl,
const Urlmapper& rootmapper)
{
log_debug("create \"" << component_name << '"');
ComponentFactory* factory;
// look for factory in my map
factoryMapType::const_iterator i = factoryMap.find(component_name);
if (i == factoryMap.end())
{
std::string factoryName = component_name;
std::string::size_type pos = factoryName.find("::");
if (pos != std::string::npos)
factoryName.replace(pos, 2, "__");
factoryName += factorySuffix;
if (getHandle() == 0)
throw cxxtools::dl::SymbolNotFound(factoryName);
// creatorsymbol not known - load it
log_debug("lookup symbol \"" << factoryName << '"');
factory = static_cast<ComponentFactory*>(sym(factoryName.c_str()).getSym());
factoryMap.insert(factoryMapType::value_type(component_name, factory));
}
else
factory = i->second;
// call the creator
Compident ci = Compident(libname, component_name);
log_debug("create \"" << ci << '"');
return factory->create(ci, rootmapper, cl);
}
LangLib* ComponentLibrary::getLangLib(const std::string& lang)
{
static cxxtools::RWLock monitor;
cxxtools::RdLock lock(monitor);
langlibsType::const_iterator it = langlibs.find(lang);
if (it != langlibs.end())
return it->second;
lock.unlock();
cxxtools::WrLock wrlock(monitor);
it = langlibs.find(lang);
if (it != langlibs.end())
return it->second;
LangLib* l = 0;
try
{
std::string n = (path.empty() ? libname : (path + '/' + libname));
l = new LangLib(n, lang);
}
catch (const unzipError& e)
{
log_warn("unzipError: " << e.what());
}
langlibs[lang] = l;
return l;
}
////////////////////////////////////////////////////////////////////////
// Comploader
//
Comploader::Comploader()
{
if (config)
{
Tntconfig::config_entries_type configLoad;
config->getConfigValues("Load", configLoad);
for (Tntconfig::config_entries_type::const_iterator it = configLoad.begin();
it != configLoad.end(); ++it)
{
if (it->params.empty())
throw std::runtime_error("missing libraryname in Load-command");
fetchLib(it->params[0]);
}
}
}
Comploader::~Comploader()
{
for (componentmap_type::iterator i = componentmap.begin();
i != componentmap.end(); ++i)
{
log_debug("drop " << i->first << " (" << i->second << ')');
i->second->drop();
}
}
cxxtools::RWLock Comploader::libraryMonitor;
Comploader::librarymap_type Comploader::librarymap;
const Tntconfig* Comploader::config = 0;
Comploader::search_path_type Comploader::search_path;
bool Comploader::staticFactoryAddEnabled = true;
Component& Comploader::fetchComp(const Compident& ci,
const Urlmapper& rootmapper)
{
log_debug("fetchComp \"" << ci << '"');
cxxtools::RdLock lock(componentMonitor);
// lookup Component
componentmap_type::iterator it = componentmap.find(ci);
if (it == componentmap.end())
{
// Component not known - lookup shared lib, fetch creator and create a new
// Component
lock.unlock();
cxxtools::WrLock wrlock(componentMonitor);
// doublecheck after getting writelock
it = componentmap.find(ci);
if (it == componentmap.end())
{
ComponentLibrary& lib = fetchLib(ci.libname);
Component* comp = lib.create(ci.compname, *this, rootmapper);
componentmap[ci] = comp;
return *comp;
}
else
{
return *(it->second);
}
}
else
{
return *(it->second);
}
}
Component* Comploader::createComp(const Compident& ci,
const Urlmapper& rootmapper)
{
log_debug("createComp \"" << ci << '"');
ComponentLibrary& lib = fetchLib(ci.libname);
Component* comp = lib.create(ci.compname, *this, rootmapper);
return comp;
}
const char* Comploader::getLangData(const Compident& ci,
const std::string& lang)
{
log_debug("getLangData(" << ci << ", \"" << lang << "\")");
ComponentLibrary& lib = fetchLib(ci.libname);
LangLib* langLib = lib.getLangLib(lang);
if (langLib)
return langLib->getData(ci.compname);
else
return 0;
}
ComponentLibrary& Comploader::fetchLib(const std::string& libname)
{
log_debug("fetchLib \"" << libname << '"');
// When first library is fetched static initializers are run,
// so we disable registation of static factories here.
staticFactoryAddEnabled = false;
cxxtools::RdLock lock(libraryMonitor);
librarymap_type::iterator it = librarymap.find(libname);
if (it == librarymap.end())
{
lock.unlock();
cxxtools::WrLock wrlock(libraryMonitor);
// doublecheck after writelock
it = librarymap.find(libname);
if (it == librarymap.end())
{
// load library
log_info("load library \"" << libname << '"');
ComponentLibrary lib;
bool found = false;
for (search_path_type::const_iterator p = search_path.begin();
p != search_path.end(); ++p)
{
try
{
log_debug("load library \"" << libname << "\" from " << *p << " dir");
lib = ComponentLibrary(*p, libname);
found = true;
break;
}
catch (const cxxtools::dl::DlopenError&)
{
}
}
if (!found)
{
try
{
log_debug("load library \"" << libname << "\" from current dir");
lib = ComponentLibrary(".", libname);
}
catch (const cxxtools::dl::DlopenError& e)
{
log_debug("library \"" << e.getLibname() << "\" in current dir not found - search lib-path");
lib = ComponentLibrary(libname);
}
}
it = librarymap.insert(librarymap_type::value_type(libname, lib)).first;
}
}
return it->second;
}
void Comploader::cleanup()
{
while (!componentmap.empty())
{
componentmap.begin()->second->drop();
componentmap.erase(componentmap.begin());
}
}
void Comploader::configure(const Tntconfig& config_)
{
config = &config_;
Tntconfig::config_entries_type compPath;
config_.getConfigValues("CompPath", compPath);
for (Tntconfig::config_entries_type::const_iterator it = compPath.begin();
it != compPath.end(); ++it)
{
if (it->params.size() > 0)
search_path.push_back(it->params[0]);
}
}
void Comploader::addStaticFactory(const std::string& component_name,
ComponentFactory* factory)
{
if (staticFactoryAddEnabled)
{
log_debug("Comploader::addStaticFactory(" << component_name << ", "
<< factory << ')');
// seach library with empty name
cxxtools::WrLock wrlock(libraryMonitor);
librarymap_type::iterator it = librarymap.find(std::string());
if (it == librarymap.end())
{
// empty library not in map - create a new empty library-object
it = librarymap.insert(
librarymap_type::value_type(std::string(),
ComponentLibrary())).first;
}
it->second.addStaticFactory(component_name, factory);
}
}
}
|