CPPSERV


Home Projects Jobs Clientele Contact
CPPSERV Documentation Download TODO Mailing lists Bug tracker News RSS Feed Browse source

appcontext.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Ilya A. Volynets-Evenbakh                       *
00003  *   ilya@total-knowledge.com                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifndef _SERVERCONFIG_APPCONTEXT_H_
00022 #define _SERVERCONFIG_APPCONTEXT_H_
00023 
00024 #include "context.h"
00025 #include "contextregistry.h"
00026 #include "paramregistry.h"
00027 #include "globalcontext.h"
00028 #include "../ServletMap.h"
00029 #include <servlet/ServletContext.h>
00030 #include <servlet/Servlet.h>
00031 #include <ServletConfigImpl.h>
00032 #include <servletcontainer.h>
00033 #include <sptk4/CThread.h>
00034 
00035 #ifdef HAVE_LIBMAGIC
00036 #include <magic.h>
00037 #endif
00038 
00039 namespace container {
00040 namespace serverconfig {
00041 class GlobalContext;
00042 
00048 class AppContext:
00049     public Context, public servlet::ServletContext
00050 {
00051     static ContextRegistry m_contextregistry;
00052     static ParamRegistry<AppContext> m_paramregistry;
00053     time_t m_sessionTimeout;
00054     std::string m_uploadDir;
00055     util::pairlist_t m_init_params;
00056 private:
00057     static void registerParams(ParamRegistry<AppContext>&);
00058     static void registerContexts(ContextRegistry&);
00059 protected:
00060     virtual bool onSetParam(const ConfigNode&);
00061     virtual Context* getContext(const ConfigNode& node)
00062     { return m_contextregistry.getContext(node, this); }
00063     virtual bool onPreComplete();
00064     virtual bool onPostComplete();
00065     bool setSessionTimeout(const ConfigNode&);
00066     bool setUploadDir(const ConfigNode&);
00067     bool addInitParam(const ConfigNode&);
00068     bool setPhysBase(const ConfigNode&);
00069     bool setDefaultMimeType(const ConfigNode&);
00070     bool setDefaultCharacterEncoding(const ConfigNode&);
00071     bool setUriBase(const ConfigNode&);
00072 private:
00073     AppContext(const ConfigNode& node, Context* parent);
00074 public:
00075     ~AppContext();
00076     static Context* contextCreator(const ConfigNode& node, Context* parent)
00077     { return new AppContext(node, parent); }
00078     GlobalContext* getGlobalContext()
00079     { return static_cast<GlobalContext*>(getParent()); }
00080 
00081 private:
00082     typedef servlet::Servlet* (*servletcreatefunc_t)();
00083     struct ServletDesc
00084     {
00085         ServletDesc(servlet::Servlet* serv,container::ServletConfigImpl* conf,void* handle, const std::string& path, size_t maxRequestSize, size_t maxFileSize, const std::string& mime, const std::string& enc, bool cache)
00086             : m_cont(serv,conf,path, maxRequestSize, maxFileSize, mime, enc, cache)
00087             , m_h(handle)
00088         {}
00089         ServletContainer m_cont;
00090         void* m_h;
00091     };
00092 public:
00093     typedef ServletMap<ServletDesc> servletmap_t;
00094 private:
00095     servletmap_t m_maptop;
00096 
00097     static void splitServPath(const std::string& path, std::string& dir, std::string& name);
00098     void unloadServlet(const std::string& path);
00099     ServletDesc* getDesc(const std::string& path);
00100     void initServlet(const std::string& path);
00101     void destroyServlet(const std::string& path);
00102 public:
00103     ServletConfigImpl* addServlet(const std::string& path, const std::string& name, const std::string& dso, bool hidden, size_t maxRequestSize, size_t maxFileSize);
00104     void delServlet(const std::string& path);
00105     ServletContainer* getServletContainer(const std::string& path);
00106     void initServlets();
00107     void destroyServlets();
00108 
00109 private:
00110     class SessionCleaner: public sptk::CThread
00111     {
00112     private:
00113         bool m_running;
00114         int m_freq;
00115         AppContext* m_ctx;
00117         int m_trigger[2];
00118     public:
00119         SessionCleaner(AppContext* ctx,int freq);
00120         ~SessionCleaner();
00121         void stop();
00122         virtual void threadFunction();
00123         void wakeUp();
00124     };
00125     SessionCleaner *m_cleaner;
00126 private:
00127     typedef std::map<std::string, HttpSessionImpl*> sessionlist_t;
00128     sessionlist_t m_sessions;
00129     std::string m_sessionCookieName;
00130     std::string m_fileSaveTemplate;
00131     sptk::CWaiter m_sessionLock; //Used for locking session list for modification
00132     typedef std::map<std::string,boost::shared_ptr<void> > attr_t;
00133     attr_t m_attrs;
00134     typedef std::map<std::string,std::string> param_t;
00135     param_t m_params;
00141     std::string m_uribase;
00149     std::string m_physbase;
00150     std::string m_mime;
00151     std::string m_enc;
00152     bool m_cache; 
00153 #ifdef HAVE_LIBMAGIC
00154     magic_t m_mime_cookie;
00155 #endif
00156 
00157 public:
00158     HttpSessionImpl* getSession(const std::string& sid, bool create);
00159 
00163     const std::string& getSessionCookieName()
00164     {
00165         return m_sessionCookieName;
00166     }
00167     void killSession(const std::string& sid);
00168     void cleanSessions();
00169     bool isSessionValid(const std::string& sid);
00170     void addInitParam(const std::string& name, const std::string& value);
00171 
00172     void killAllSessions();
00173     const std::string& getFileSaveTemplate() const {return m_fileSaveTemplate;}
00174     const std::string& getUriBase() const { return m_uribase;}
00175 public:
00177     virtual servlet::ServletContext* getContext(const std::string& uripath);
00178     virtual int  getMajorVersion();
00179     virtual int  getMinorVersion();
00180     virtual std::string  getMimeType(const std::string& file);
00181     virtual std::auto_ptr< std::set<std::string> >  getResourcePaths(const std::string& path);
00182     virtual std::string getResource(const std::string& path);
00183     virtual std::istream& getResourceAsStream(const std::string& path);
00184     virtual servlet::RequestDispatcher* getRequestDispatcher(const std::string& path);
00185     virtual servlet::RequestDispatcher* getNamedDispatcher(const std::string& name);
00186     virtual void  log(const std::string& msg) const ;
00187     virtual void  log(const std::string& message, const std::exception& e) const ;
00188     virtual std::string  getRealPath(const std::string& path);
00189     virtual std::string  getServerInfo();
00190     virtual std::string  getInitParameter(const std::string& name);
00191     virtual std::auto_ptr< std::vector<std::string> > getInitParameterNames();
00192     virtual boost::shared_ptr<void> getAttribute(const std::string& name);
00193     virtual std::auto_ptr< std::vector<std::string> > getAttributeNames();
00194     virtual void setAttribute(const std::string& name, boost::shared_ptr<void> object);
00195     virtual void  removeAttribute(const std::string& name);
00196     virtual bool hasAttribute(const std::string& name) const;
00197     virtual std::string  getServletContextName() const;
00198 
00199 private:
00200     HttpSessionImpl* newSession();
00201     static std::string makeSName(const std::string& name);
00202 };
00203 
00204 }
00205 }
00206 #endif // _SERVERCONFIG_APPCONTEXT_H_

SourceForge.net Logo