Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsModulesLoader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-Today Guy Deleeuw
3  *
4  * See the LICENSE file for terms of use.
5 */
6 
7 #include <fstream>
8 
9 #include <json/json.h>
10 #include <json/reader.h>
11 #include <json/value.h>
12 
13 #include <Include/WsGlobalConfig.h>
14 #include <Logger/WsLogger.h>
16 
17 #include "WsModulesLoader.h"
18 
20 { }
21 
23 {
24  for (int iModule = 0; iModule < m_vModules.size(); ++iModule) {
25  WsModule* curModule = m_vModules[iModule]->module;
26  delete curModule;
27  }
28 }
29 
31 {
32  Json::Value root;
33  Json::Reader reader;
34  std::ifstream plugins(GlobalConfig::PluginsPropertiesPath.c_str(), std::ifstream::binary);
35  bool bOk = reader.parse(plugins, root, false);
36  if ( !bOk ) {
37  LOG(DEBUG) << reader.getFormatedErrorMessages();
38  return false;
39  }
40  //Json::Value module = modules[iMod]; // crash
41  Json::Value modules = root["modules"];
42  Json::Value::Members mbrs = modules.getMemberNames();
43  bool bDebug = false;
44 #if GDDEBUG == 1
45  bDebug = true;
46 #endif
47  for (int iMod = 0; iMod < mbrs.size(); ++iMod) {
48  LOG(DEBUG) << "WsModulesLoader::load Loading module " << mbrs[iMod] << " debug mode = " << bDebug;
49  Json::Value module = modules[mbrs[iMod]];
50  Json::Value::Members attributes = module.getMemberNames();
51  for (int iAtt = 0; iAtt < attributes.size(); ++iAtt) {
52  if ( attributes[iAtt] == "soName" ) {
53  std::string soName = module.get(attributes[iAtt], "").asString();
54  if ( bDebug )
55  boost::replace_first(soName, ".so", "d.so");
56  // Important not RTLD_NOW because a derived module can also load a shared object
57  // and in this step is make in the dynlib initialization process (_init, _fini Ctor/Dtor)
58  void* hndl = dlopen(soName.c_str(), RTLD_LAZY);
59  if ( hndl == NULL ) {
60  LOG(ERROR) << "WsModulesLoader::load dlopen error soName = " << soName << " error = " << dlerror();
61  continue;
62  }
63  LOG(DEBUG) << "WsModulesLoader::load Loading module " << soName;
64  pf_wsModule func = (pf_wsModule) dlsym(hndl, "buildModule");
65  WsModule* pModule = func();
66  WsModuleLoader* ml = new WsModuleLoader();
67  ml->dlfcnHandler = hndl;
68  ml->module = pModule;
69  m_vModules.push_back(ml);
70  pModule->setSoName(soName);
71  // std::string p = WApplication::instance()->appRoot() + WApplication::instance()->internalPath();
72  // pModule->setSysPath(p);
73  for (int iAtt2 = 0; iAtt2 < attributes.size(); ++iAtt2) {
74  if ( attributes[iAtt2] == "moduleName" ) {
75  std::string moduleName = module.get(attributes[iAtt2], "").asString();
76  pModule->setModuleName(moduleName);
77  }
78  if ( attributes[iAtt2] == "fileName" ) {
79  std::string fileName = module.get(attributes[iAtt2], "").asString();
80  pModule->setFileName(fileName);
81  }
82  if ( attributes[iAtt2] == "extension" || attributes[iAtt2] == "extensions" ) {
83  std::string extensions = module.get(attributes[iAtt2], "").asString();
84  pModule->setExtensions(extensions);
85  }
86  if ( attributes[iAtt2] == "prefix" ) {
87  std::string prefix = module.get(attributes[iAtt2], "").asString();
88  pModule->setPrefix(prefix);
89  }
90  if ( attributes[iAtt2] == "hideImages" ) {
91  std::string hideImages = module.get(attributes[iAtt2], "false").asString();
92  if ( hideImages == "true" ) pModule->setHideImages(true);
93  }
94  if ( attributes[iAtt2] == "options" ) {
95  Json::Value options = module[attributes[iAtt2]];
96  Json::Value::Members optAttrs = options.getMemberNames();
97  for (int iOpt = 0; iOpt < optAttrs.size(); ++iOpt) {
98  Json::Value val = options[optAttrs[iOpt]];
99  LOG(DEBUG) << "WsModuleLoader::load options " << optAttrs[iOpt] << " value = " << val.asString();
100  if ( val.type() == Json::nullValue ) continue;
101  if ( val.type() == Json::intValue )
102  pModule->setOption(optAttrs[iOpt], val.asInt());
103  if ( val.type() == Json::uintValue )
104  pModule->setOption(optAttrs[iOpt], val.asUInt());
105  if ( val.type() == Json::realValue )
106  pModule->setOption(optAttrs[iOpt], val.asDouble());
107  if ( val.type() == Json::stringValue )
108  pModule->setOption(optAttrs[iOpt], val.asString());
109  if ( val.type() == Json::booleanValue )
110  pModule->setOption(optAttrs[iOpt], val.asBool());
111  }
112  }
113  }
114  if ( module["loadOnStartup"] != Json::Value::null && module["loadOnStartup"].asBool()) {
115  LOG(DEBUG) << "WsModuleLoader::load running Module " << pModule->moduleName();
116  //fprintf(stderr, "WsModuleLoader :: running Module %s\n",pModule->moduleName().c_str());
117  pModule->createContents();
118  pModule->setLoaded();
119  } else {
120  LOG(DEBUG) << "WsModuleLoader::load running Module else clause " << module["loadOnStartup"].asString();
121  //fprintf(stderr, "WsModuleLoader ::else clause%s\n", module["loadOnStartup"].asString().c_str() );
122  }
123  }
124  }
125  }
126  return true;
127 }
128 
129 const std::vector<WsModuleLoader*>& WsModulesLoader::modules()
130 {
131  return m_vModules;
132 }
133 
134 const WsModule* WsModulesLoader::module(const std::string& moduleName)
135 {
136  for (int iModule = 0; iModule < modules().size(); ++iModule) {
137  WsModule* curModule = modules()[iModule]->module;
138  if ( curModule->moduleName() != moduleName ) continue;
139  return curModule;
140  }
141  return 0;
142 }
143 
144 std::string WsModulesLoader::pathWithoutPrefix(const std::string& path)
145 {
146  std::string remP = removePrefix(path);
147  if ( remP == "" ) remP = "/";
148  return remP;
149 }
150 
151 std::string WsModulesLoader::removePrefix(const std::string& path)
152 {
153  // TODO : en attendant de mettre ces éléménts en Modules
154  std::string sRes(path);
155  std::string searchPath = WsLayoutProperties::instance()->get("global", "search_path", "/Search");
156  // LOG(DEBUG)<<"WsModulesLoader::removePrefix() path = " << path;
157  if ( path.compare(0, searchPath.size(), searchPath ) == 0 ) return ""; // Search
158  if ( path.compare(0, 5, "/Logo" ) == 0 ) return sRes.assign(path, 5, path.size() - 5);
159  if ( path.compare(0, 8, "/SiteMap" ) == 0 ) return sRes.assign(path, 8, path.size() - 8);
160  if ( path.compare(0, 5, "/Edit" ) == 0 ) return sRes.assign(path, 5, path.size() - 5);
161  if ( path.compare(0, 11, "/FileUpload" ) == 0 ) return sRes.assign(path, 11, path.size() - 11);
162  if ( path.compare(0, 10, "/FolderNew" ) == 0 ) return sRes.assign(path, 10, path.size() - 10);
163  if ( path.compare(0, 13, "/FolderDelete" ) == 0 ) return sRes.assign(path, 13, path.size() - 13);
164  if ( path.compare(0, 11, "/FolderEdit" ) == 0 ) return sRes.assign(path, 11, path.size() - 11);
165  for (int iModule = 0; iModule < modules().size(); ++iModule) {
166  WsModule* curModule = modules()[iModule]->module;
167  if ( curModule->prefix().size() < 1 ) continue;
168  if ( path.compare(0, curModule->prefix().size(), curModule->prefix() ) == 0)
169  return curModule->pathWithoutPrefix(path);
170  }
171  return path;
172 }
173 
174 std::string WsModulesLoader::checkPath(const std::string& path)
175 {
176  std::string sRes(path);
177  for (int iModule = 0; iModule < modules().size(); ++iModule) {
178  WsModule* curModule = modules()[iModule]->module;
179  sRes = curModule->checkPath(path);
180  if ( sRes != path ) break;
181  }
182  return sRes;
183 }
184 
WsModule * module
std::string pathWithoutPrefix(const std::string &path)
#define DEBUG
Definition: WsLogger.h:27
const std::vector< WsModuleLoader * > & modules()
void setLoaded()
Some modules can be loaded on startup, no more actions is required by the content widget...
Definition: WsModule.cpp:50
std::vector< WsModuleLoader * > m_vModules
std::string checkPath(const std::string &path)
std::string pathWithoutPrefix(const std::string &path)
return the relative path without the prefix : example pathWithoutPrefix("/SiteMap/Test") return "/Tes...
Definition: WsOption.cpp:150
const std::string PluginsPropertiesPath
void setFileName(const std::string &fileName)
Set/Get the file name that trigger the module : exemple "Staff List".
Definition: WsOption.cpp:115
void setHideImages(const bool bHide)
Set/Get the flag to specified if the images is hided.
Definition: WsOption.cpp:164
virtual std::string checkPath(const std::string &currentPath)
Return the currentPath, or another path when some action is required for the currentPath example /New...
Definition: WsOption.cpp:90
#define LOG
Definition: WsLogger.h:22
virtual Wt::WWidget * createContents(Wt::WContainerWidget *parent=0) const =0
Create the contents.
std::string removePrefix(const std::string &path)
const std::string & moduleName() const
Definition: WsOption.cpp:100
WsModule *(* pf_wsModule)()
Example of the main function extern "C" { WsModule* buildModule() { return new WsModule(); } }...
Definition: WsModule.h:126
void setOption(const std::string &attribute, boost::any value)
Set an options if previously set, update the value.
Definition: WsOption.cpp:47
void setPrefix(const std::string &prefix)
Set/Get prefix for a virtual path, like "/SiteMap".
Definition: WsOption.cpp:140
void setModuleName(const std::string &name)
Set/Get the module name.
Definition: WsOption.cpp:95
const std::string & prefix() const
Definition: WsOption.cpp:145
const std::string Value
Definition: WsRequestType.h:39
std::string get(const std::string &section, const std::string &id, const std::string &def)
void setSoName(const std::string &soName)
Set/Get the module so name or path ex. libgd.so or /usr/lib/libgd.so.
Definition: WsOption.cpp:105
static WsLayoutProperties * instance()
Get the singleton instance.
const WsModule * module(const std::string &moduleName)
void setExtensions(const std::string &extensions)
Set/Get the file extension managed by the module : exemple .ods|.odp.
Definition: WsOption.cpp:125
#define ERROR
Definition: WsLogger.h:42