summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2013-06-23 15:43:32 +0000
committerrandomdan <randomdan@localhost>2013-06-23 15:43:32 +0000
commitdd60be86753dcee901f3a74700d7f68f689bd5a6 (patch)
tree47725a698f89358420499d49649d1f433f82df92
parentMove logger initialisation and optionSource loading out of environment into t... (diff)
downloadproject2-dd60be86753dcee901f3a74700d7f68f689bd5a6.tar.bz2
project2-dd60be86753dcee901f3a74700d7f68f689bd5a6.tar.xz
project2-dd60be86753dcee901f3a74700d7f68f689bd5a6.zip
Move script resolution out of environment into script reader
-rw-r--r--project2/cgi/cgiAppEngine.cpp6
-rw-r--r--project2/cgi/cgiProgRouter.cpp2
-rw-r--r--project2/cgi/cgiStageInitial.cpp4
-rw-r--r--project2/cgi/cgiStageRequest.cpp2
-rw-r--r--project2/common/commonObjects.cpp2
-rw-r--r--project2/common/environment.cpp37
-rw-r--r--project2/common/environment.h7
-rw-r--r--project2/common/scripts.cpp34
-rw-r--r--project2/common/scripts.h8
-rw-r--r--project2/console/p2consoleMain.cpp3
-rw-r--r--project2/mail/sendmailTask.cpp2
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<ElementLoader>(&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<ElementLoader>(&datasources));
dbs->load(NULL, false);
return safeMapFind<DataSourceNotFound>(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 <pch.hpp>
#include "environment.h"
#include "optionsSource.h"
-#include "logger.h"
-#include "scriptLoader.h"
-#include <stdio.h>
-#include <fstream>
-#include <boost/filesystem/convenience.hpp>
-#include <boost/tuple/tuple_comparison.hpp>
-#include <boost/foreach.hpp>
-#include <boost/bind.hpp>
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<std::string, boost::shared_ptr<ScriptReaderLoader> > 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<ScriptReaderLoader>()) {
- 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 <string>
#include <glibmm/ustring.h>
-#include <boost/tuple/tuple.hpp>
#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<const std::string, const std::string> ScriptKey;
- typedef std::map<ScriptKey, ScriptReaderPtr> 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 <boost/filesystem/convenience.hpp>
+#include <boost/tuple/tuple_comparison.hpp>
+
+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<std::string, boost::shared_ptr<ScriptReaderLoader> > 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<ScriptReaderLoader>()) {
+ 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 <boost/intrusive_ptr.hpp>
+#include <boost/tuple/tuple.hpp>
#include <boost/function.hpp>
#include <boost/optional.hpp>
#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<std::string, SourceObject *> namedComponents;
+
+ private:
+ typedef boost::tuple<const std::string, const std::string> ScriptKey;
+ typedef std::map<ScriptKey, ScriptReaderPtr> 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<ConsoleApplicationEngine> app(new ConsoleApplicationEngine(&env, env.resolveScript(todo.get<0>(), todo.get<1>(), false)));
+ boost::intrusive_ptr<ConsoleApplicationEngine> 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();