File indexing completed on 2025-01-18 10:17:36
0001
0002
0003
0004
0005
0006 #include "JAutoActivator.h"
0007
0008 JAutoActivator::JAutoActivator() {
0009 SetTypeName("JAutoActivator");
0010 SetCallbackStyle(CallbackStyle::ExpertMode);
0011 }
0012
0013 void JAutoActivator::AddAutoActivatedFactory(string factory_name, string factory_tag) {
0014 m_auto_activated_factories.push_back({std::move(factory_name), std::move(factory_tag)});
0015 }
0016
0017
0018
0019 std::pair<std::string, std::string> JAutoActivator::Split(std::string factory_name) {
0020
0021 std::string::size_type pos = -1;
0022 while (true) {
0023
0024 pos = factory_name.find(':', pos+1);
0025
0026 if (pos == std::string::npos) {
0027
0028 return std::make_pair(factory_name, "");
0029 }
0030 else if (factory_name[pos+1] == ':') {
0031
0032 pos += 1;
0033 }
0034 else {
0035
0036 return std::make_pair(factory_name.substr(0, pos), factory_name.substr(pos+1));
0037 }
0038 }
0039 }
0040
0041 void JAutoActivator::Init() {
0042
0043 string autoactivate_conf;
0044 if (GetApplication()->GetParameter("autoactivate", autoactivate_conf)) {
0045 try {
0046 if (!autoactivate_conf.empty()) {
0047
0048 vector <string> myfactories;
0049 string &str = autoactivate_conf;
0050 unsigned int cutAt;
0051 while ((cutAt = str.find(",")) != (unsigned int) str.npos) {
0052 if (cutAt > 0)myfactories.push_back(str.substr(0, cutAt));
0053 str = str.substr(cutAt + 1);
0054 }
0055 if (str.length() > 0)myfactories.push_back(str);
0056
0057
0058
0059
0060 for (unsigned int i = 0; i < myfactories.size(); i++) {
0061 auto pair = Split(myfactories[i]);
0062 AddAutoActivatedFactory(pair.first, pair.second);
0063 }
0064 }
0065 }
0066 catch (...) {
0067 LOG_ERROR(GetLogger()) << "Error parsing parameter 'autoactivate'. Found: " << autoactivate_conf << LOG_END;
0068 throw JException("AutoActivator could not parse parameter 'autoactivate'");
0069 }
0070 }
0071 }
0072
0073 void JAutoActivator::Process(const JEvent& event) {
0074 for (const auto &pair: m_auto_activated_factories) {
0075 auto name = pair.first;
0076 auto tag = pair.second;
0077 auto factory = event.GetFactory(name, tag);
0078 if (factory != nullptr) {
0079 factory->Create(event.shared_from_this());
0080 }
0081 else {
0082 LOG_ERROR(GetLogger()) << "Could not find factory with typename=" << name << ", tag=" << tag << LOG_END;
0083 throw JException("AutoActivator could not find factory with typename=%s, tag=%s", name.c_str(), tag.c_str());
0084 }
0085 }
0086 }
0087