From dd60be86753dcee901f3a74700d7f68f689bd5a6 Mon Sep 17 00:00:00 2001 From: randomdan Date: Sun, 23 Jun 2013 15:43:32 +0000 Subject: Move script resolution out of environment into script reader --- project2/cgi/cgiAppEngine.cpp | 6 +++--- project2/cgi/cgiProgRouter.cpp | 2 +- project2/cgi/cgiStageInitial.cpp | 4 ++-- project2/cgi/cgiStageRequest.cpp | 2 +- project2/common/commonObjects.cpp | 2 +- project2/common/environment.cpp | 37 ------------------------------------- project2/common/environment.h | 7 ------- project2/common/scripts.cpp | 34 ++++++++++++++++++++++++++++++++++ project2/common/scripts.h | 8 ++++++++ project2/console/p2consoleMain.cpp | 3 ++- project2/mail/sendmailTask.cpp | 2 +- 11 files changed, 53 insertions(+), 54 deletions(-) diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp index 0022a88..654c2ce 100644 --- a/project2/cgi/cgiAppEngine.cpp +++ b/project2/cgi/cgiAppEngine.cpp @@ -73,7 +73,7 @@ CgiApplicationEngine::process() const currentStage = currentStage.get<0>()->run(); } catch (const CheckHost::CheckFailure & cf) { - currentStage = NextStage(new PresentStage(_env->resolveScript(_env->presentRoot, cf.failedCheck->present(), false))); + currentStage = NextStage(new PresentStage(ScriptReader::resolveScript(_env->presentRoot, cf.failedCheck->present(), false))); } catch (const ScriptNotFound & nf) { if (_env->notFoundPresent.empty() || triedNotFound) { @@ -81,7 +81,7 @@ CgiApplicationEngine::process() const } else { triedNotFound = true; - currentStage = NextStage(new CustomNotFoundStage(nf, _env->resolveScript(_env->errorPresentRoot, _env->notFoundPresent, false))); + currentStage = NextStage(new CustomNotFoundStage(nf, ScriptReader::resolveScript(_env->errorPresentRoot, _env->notFoundPresent, false))); } } catch (const std::exception & ex) { @@ -90,7 +90,7 @@ CgiApplicationEngine::process() const } else { triedNotFound = true; - currentStage = NextStage(new CustomErrorStage(ex, _env->resolveScript(_env->errorPresentRoot, _env->onErrorPresent, false))); + currentStage = NextStage(new CustomErrorStage(ex, ScriptReader::resolveScript(_env->errorPresentRoot, _env->onErrorPresent, false))); } } } while (currentStage.get<0>()); diff --git a/project2/cgi/cgiProgRouter.cpp b/project2/cgi/cgiProgRouter.cpp index 089b94f..d187de2 100644 --- a/project2/cgi/cgiProgRouter.cpp +++ b/project2/cgi/cgiProgRouter.cpp @@ -35,7 +35,7 @@ class RoutingTable { } void setRouteScript() { - routeScript = Environment::getCurrent()->resolveScript(Environment::getCurrent()->datasourceRoot, routeScriptPath, true); + routeScript = ScriptReader::resolveScript(Environment::getCurrent()->datasourceRoot, routeScriptPath, true); routeScript->loader.addLoadTarget(routeScript->root(), Storer::into(&routes)); routes.clear(); routeScript->load(NULL, true); diff --git a/project2/cgi/cgiStageInitial.cpp b/project2/cgi/cgiStageInitial.cpp index 1fe4820..d5fe424 100644 --- a/project2/cgi/cgiStageInitial.cpp +++ b/project2/cgi/cgiStageInitial.cpp @@ -8,10 +8,10 @@ CgiApplicationEngine::InitialStage::run() { const CgiEnvironment * e = env(); if (e->getRequestMethod() == "POST") { - return NextStage(new RequestStage(e->resolveScript(e->requestRoot, e->router->route(), false))); + return NextStage(new RequestStage(ScriptReader::resolveScript(e->requestRoot, e->router->route(), false))); } else { - return NextStage(new PresentStage(e->resolveScript(e->presentRoot, e->router->isDefault() ? e->defaultPresent : e->router->route(), false))); + return NextStage(new PresentStage(ScriptReader::resolveScript(e->presentRoot, e->router->isDefault() ? e->defaultPresent : e->router->route(), false))); } } diff --git a/project2/cgi/cgiStageRequest.cpp b/project2/cgi/cgiStageRequest.cpp index 8fa06bf..37faaae 100644 --- a/project2/cgi/cgiStageRequest.cpp +++ b/project2/cgi/cgiStageRequest.cpp @@ -22,7 +22,7 @@ CgiApplicationEngine::RequestStage::run() runChecks(); execute(); if (!present().isNull()) { - return NextStage(new PresentStage(env()->resolveScript(env()->presentRoot, present(), false)), this); + return NextStage(new PresentStage(ScriptReader::resolveScript(env()->presentRoot, present(), false)), this); } return NextStage(NULL, this); } diff --git a/project2/common/commonObjects.cpp b/project2/common/commonObjects.cpp index 9b45baa..c18db3c 100644 --- a/project2/common/commonObjects.cpp +++ b/project2/common/commonObjects.cpp @@ -29,7 +29,7 @@ CommonObjects::getSource(const std::string & name) const CommonObjects::DataSources::const_iterator CommonObjects::loadDataSource(const std::string & name) const { - ScriptReaderPtr dbs = Environment::getCurrent()->resolveScript(Environment::getCurrent()->datasourceRoot, name, true); + ScriptReaderPtr dbs = ScriptReader::resolveScript(Environment::getCurrent()->datasourceRoot, name, true); dbs->loader.addLoadTarget(dbs->root(), Storer::into(&datasources)); dbs->load(NULL, false); return safeMapFind(datasources, name); diff --git a/project2/common/environment.cpp b/project2/common/environment.cpp index 0b9a1fe..a996408 100644 --- a/project2/common/environment.cpp +++ b/project2/common/environment.cpp @@ -1,14 +1,6 @@ #include #include "environment.h" #include "optionsSource.h" -#include "logger.h" -#include "scriptLoader.h" -#include -#include -#include -#include -#include -#include const Environment * Environment::currentEnv(NULL); @@ -44,32 +36,3 @@ Environment::getCurrent() return currentEnv; } -ScriptReaderPtr -Environment::resolveScript(const std::string & group, const std::string & name, bool ii) const -{ - typedef std::map > ReaderLoaders; - boost::filesystem::path e(name); - while (!e.empty()) { - ScriptKey sk(group, e.string()); - ScriptCache::const_iterator i = scriptCache.find(sk); - if (i != scriptCache.end() && i->second->isCurrent()) { - return i->second; - } - else { - BOOST_FOREACH(const ReaderLoaders::value_type & rl, *Plugable::objLoaders()) { - ScriptReaderPtr rs = rl.second->resolveScript(group, e.string()); - if (rs) { - return (scriptCache[sk] = rs); - } - } - } - e = e.parent_path(); - } - if (ii) { - throw DependencyNotFound(group, name); - } - else { - throw ScriptNotFound(group, name); - } -} - diff --git a/project2/common/environment.h b/project2/common/environment.h index 7b5e976..7289bb0 100644 --- a/project2/common/environment.h +++ b/project2/common/environment.h @@ -3,7 +3,6 @@ #include #include -#include #include "options.h" #include "exceptions.h" #include "scripts.h" @@ -22,16 +21,10 @@ class Environment { virtual std::string getServerName() const = 0; virtual const Glib::ustring & platform() const = 0; - ScriptReaderPtr resolveScript(const std::string & group, const std::string & name, bool ii) const; - INITOPTIONS; private: static const Environment * currentEnv; - typedef boost::tuple ScriptKey; - typedef std::map ScriptCache; - mutable ScriptCache scriptCache; - public: static std::string datasourceRoot; static std::string scriptNamespace; diff --git a/project2/common/scripts.cpp b/project2/common/scripts.cpp index e2f1370..cb5d035 100644 --- a/project2/common/scripts.cpp +++ b/project2/common/scripts.cpp @@ -1,5 +1,9 @@ #include "scripts.h" #include "variables/fixed.h" +#include +#include + +ScriptReader::ScriptCache ScriptReader::scriptCache; ScriptNode::ScriptNode(ScriptReaderPtr s) : script(s) @@ -42,3 +46,33 @@ ScriptReader::load(const CommonObjects * co, bool childrenOnly) const { loader.collectAll(co, childrenOnly, root()); } + +ScriptReaderPtr +ScriptReader::resolveScript(const std::string & group, const std::string & name, bool ii) +{ + typedef std::map > ReaderLoaders; + boost::filesystem::path e(name); + while (!e.empty()) { + ScriptKey sk(group, e.string()); + ScriptCache::const_iterator i = scriptCache.find(sk); + if (i != scriptCache.end() && i->second->isCurrent()) { + return i->second; + } + else { + BOOST_FOREACH(const ReaderLoaders::value_type & rl, *Plugable::objLoaders()) { + ScriptReaderPtr rs = rl.second->resolveScript(group, e.string()); + if (rs) { + return (scriptCache[sk] = rs); + } + } + } + e = e.parent_path(); + } + if (ii) { + throw DependencyNotFound(group, name); + } + else { + throw ScriptNotFound(group, name); + } +} + diff --git a/project2/common/scripts.h b/project2/common/scripts.h index f571d0a..ed4d9fd 100644 --- a/project2/common/scripts.h +++ b/project2/common/scripts.h @@ -3,6 +3,7 @@ #include "intrusivePtrBase.h" #include +#include #include #include #include "scriptLoader.h" @@ -58,8 +59,15 @@ class ScriptReader : public virtual IntrusivePtrBase { virtual time_t modifiedTime() const = 0; mutable LoaderBase loader; + static ScriptReaderPtr resolveScript(const std::string & group, const std::string & name, bool ii); + friend class SourceObject; mutable std::map namedComponents; + + private: + typedef boost::tuple ScriptKey; + typedef std::map ScriptCache; + static ScriptCache scriptCache; }; /// Base class to implement script reader modules diff --git a/project2/console/p2consoleMain.cpp b/project2/console/p2consoleMain.cpp index d443ab0..57008b6 100644 --- a/project2/console/p2consoleMain.cpp +++ b/project2/console/p2consoleMain.cpp @@ -17,7 +17,8 @@ main(int argc, char ** argv) BOOST_FOREACH(const ConsoleEnvironment::ToDo & todo, env.todoList()) { Plugable::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1)); Logger()->messagebf(LOG_DEBUG, "%s: Beginning script '%s/%s'", __FUNCTION__, todo.get<0>(), todo.get<1>()); - boost::intrusive_ptr app(new ConsoleApplicationEngine(&env, env.resolveScript(todo.get<0>(), todo.get<1>(), false))); + boost::intrusive_ptr app(new ConsoleApplicationEngine(&env, + ScriptReader::resolveScript(todo.get<0>(), todo.get<1>(), false))); Logger()->messagef(LOG_DEBUG, "%s: Processing file", __FUNCTION__); app->process(); diff --git a/project2/mail/sendmailTask.cpp b/project2/mail/sendmailTask.cpp index 836b61a..10bfbdd 100644 --- a/project2/mail/sendmailTask.cpp +++ b/project2/mail/sendmailTask.cpp @@ -195,7 +195,7 @@ SendMailTask::execute() const parts->parts.insert(new Header("Content-Transfer-Encoding", "binary")); parts->parts.insert(new BoundaryEnd()); - ViewHostPtr vsp = new EmailViewHost(parts, Environment::getCurrent()->resolveScript("emails", present(), false)->root()); + ViewHostPtr vsp = new EmailViewHost(parts, ScriptReader::resolveScript("emails", present(), false)->root()); vsp->executeViews(); vsp->doTransforms(); parts->part = parts->parts.begin(); -- cgit v1.2.3