| 1 | //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ |
|---|
| 2 | // Zen Enterprise Framework |
|---|
| 3 | // |
|---|
| 4 | // Copyright (C) 2001 - 2010 Tony Richards |
|---|
| 5 | // Copyright (C) 2008 - 2009 Matthew Alan Gray |
|---|
| 6 | // Copyright (C) 2009 Joshua Cassity |
|---|
| 7 | // |
|---|
| 8 | // This software is provided 'as-is', without any express or implied |
|---|
| 9 | // warranty. In no event will the authors be held liable for any damages |
|---|
| 10 | // arising from the use of this software. |
|---|
| 11 | // |
|---|
| 12 | // Permission is granted to anyone to use this software for any purpose, |
|---|
| 13 | // including commercial applications, and to alter it and redistribute it |
|---|
| 14 | // freely, subject to the following restrictions: |
|---|
| 15 | // |
|---|
| 16 | // 1. The origin of this software must not be misrepresented; you must not |
|---|
| 17 | // claim that you wrote the original software. If you use this software |
|---|
| 18 | // in a product, an acknowledgment in the product documentation would be |
|---|
| 19 | // appreciated but is not required. |
|---|
| 20 | // 2. Altered source versions must be plainly marked as such, and must not be |
|---|
| 21 | // misrepresented as being the original software. |
|---|
| 22 | // 3. This notice may not be removed or altered from any source distribution. |
|---|
| 23 | // |
|---|
| 24 | // Tony Richards trichards@indiezen.com |
|---|
| 25 | // Matthew Alan Gray mgray@indiezen.org |
|---|
| 26 | // Joshua Cassity jcassity@indiezen.org |
|---|
| 27 | //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ |
|---|
| 28 | #ifndef ZEN_APPSERVER_I_APPLICATION_SERVER_HPP_INCLUDED |
|---|
| 29 | #define ZEN_APPSERVER_I_APPLICATION_SERVER_HPP_INCLUDED |
|---|
| 30 | |
|---|
| 31 | #include "Configuration.hpp" |
|---|
| 32 | |
|---|
| 33 | #include <Zen/Core/Memory/managed_ptr.hpp> |
|---|
| 34 | #include <Zen/Core/Plugins/I_Configuration.hpp> |
|---|
| 35 | #include <Zen/Core/Threading/I_Condition.hpp> |
|---|
| 36 | #include <Zen/Core/Threading/I_Thread.hpp> |
|---|
| 37 | #include <Zen/Core/Scripting/I_ScriptEngine.hpp> |
|---|
| 38 | #include <Zen/Core/Event/I_EventManager.hpp> |
|---|
| 39 | |
|---|
| 40 | #include <Zen/Enterprise/Networking/I_Endpoint.hpp> |
|---|
| 41 | |
|---|
| 42 | #include <Zen/Community/SessionCommon/I_SessionService.hpp> |
|---|
| 43 | |
|---|
| 44 | #include <boost/noncopyable.hpp> |
|---|
| 45 | |
|---|
| 46 | #include <map> |
|---|
| 47 | |
|---|
| 48 | //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ |
|---|
| 49 | namespace Zen { |
|---|
| 50 | namespace Database { |
|---|
| 51 | class I_DatabaseConnection; |
|---|
| 52 | } // namespace Database |
|---|
| 53 | namespace Enterprise { |
|---|
| 54 | namespace AppServer { |
|---|
| 55 | //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ |
|---|
| 56 | class I_ProtocolService; |
|---|
| 57 | class I_ApplicationService; |
|---|
| 58 | class I_ResourceLocation; |
|---|
| 59 | class I_Message; |
|---|
| 60 | class I_Request; |
|---|
| 61 | class I_Response; |
|---|
| 62 | class I_ResponseHandler; |
|---|
| 63 | class I_SessionEvent; |
|---|
| 64 | class I_MessageRegistry; |
|---|
| 65 | |
|---|
| 66 | /// Base Application Server. |
|---|
| 67 | /// For a conversation about SOA and the Zen Enterprise Application server see |
|---|
| 68 | /// http://www.indiezen.org/wiki/irclogs/2009/03/04/#UTC2009-03-04T18:25:15 |
|---|
| 69 | class APPSERVER_DLL_LINK I_ApplicationServer |
|---|
| 70 | : boost::noncopyable |
|---|
| 71 | { |
|---|
| 72 | /// @name Forward declarations |
|---|
| 73 | /// @{ |
|---|
| 74 | public: |
|---|
| 75 | struct I_ResourceLocationVisitor; |
|---|
| 76 | /// @} |
|---|
| 77 | |
|---|
| 78 | /// @name Types |
|---|
| 79 | /// @{ |
|---|
| 80 | public: |
|---|
| 81 | typedef Memory::managed_ptr<I_ProtocolService> pProtocolService_type; |
|---|
| 82 | typedef Memory::managed_ptr<I_ApplicationService> pApplicationService_type; |
|---|
| 83 | typedef Memory::managed_ptr<I_Message> pMessage_type; |
|---|
| 84 | typedef Memory::managed_weak_ptr<I_Message> wpMessage_type; |
|---|
| 85 | typedef Memory::managed_ptr<I_Request> pRequest_type; |
|---|
| 86 | typedef Memory::managed_ptr<I_Response> pResponse_type; |
|---|
| 87 | typedef Memory::managed_ptr<I_ResponseHandler> pResponseHandler_type; |
|---|
| 88 | typedef Memory::managed_ptr<I_SessionEvent> pSessionEvent_type; |
|---|
| 89 | typedef Memory::managed_ptr<I_ResourceLocation> pResourceLocation_type; |
|---|
| 90 | typedef Memory::managed_ptr<I_MessageRegistry> pMessageRegistry_type; |
|---|
| 91 | |
|---|
| 92 | typedef Memory::managed_ptr<Networking::I_Endpoint> pEndpoint_type; |
|---|
| 93 | |
|---|
| 94 | typedef Memory::managed_ptr<Database::I_DatabaseConnection> pDatabaseConnection_type; |
|---|
| 95 | |
|---|
| 96 | typedef Zen::Plugins::I_ConfigurationElement::const_ptr_type pConfig_type; |
|---|
| 97 | |
|---|
| 98 | typedef Memory::managed_ptr<Scripting::I_ScriptEngine> pScriptEngine_type; |
|---|
| 99 | typedef Memory::managed_ptr<Scripting::I_ScriptModule> pScriptModule_type; |
|---|
| 100 | |
|---|
| 101 | typedef Event::I_EventManager::pEventService_type pEventService_type; |
|---|
| 102 | typedef std::map<std::string,std::string> config_type; |
|---|
| 103 | |
|---|
| 104 | typedef Memory::managed_ptr<Community::Common::I_SessionService> pSessionService_type; |
|---|
| 105 | /// @} |
|---|
| 106 | |
|---|
| 107 | /// @name I_ApplicationServer interface. |
|---|
| 108 | /// @{ |
|---|
| 109 | public: |
|---|
| 110 | /// Start the application server. |
|---|
| 111 | /// @return the condition variable that indicates when the application server |
|---|
| 112 | /// has fully started. |
|---|
| 113 | virtual Zen::Threading::I_Condition* start() = 0; |
|---|
| 114 | |
|---|
| 115 | /// @brief Stop the application server. |
|---|
| 116 | /// |
|---|
| 117 | /// This method will return when the application server is fully stopped and destroyed. |
|---|
| 118 | /// Do not reference this object after stop() is called. |
|---|
| 119 | virtual void stop() = 0; |
|---|
| 120 | |
|---|
| 121 | /// Register the default script engine for all application services. |
|---|
| 122 | /// Every application service that has been created or is created in the future |
|---|
| 123 | /// will use this script engine. |
|---|
| 124 | /// @param _pEngine NULL to set the default engine to none, but doing so will |
|---|
| 125 | /// not unregister the script engine to services that have already |
|---|
| 126 | /// been created. It will only prevent subsequent services from |
|---|
| 127 | /// using this script engine. |
|---|
| 128 | virtual void registerDefaultScriptEngine(pScriptEngine_type _pEngine) = 0; |
|---|
| 129 | |
|---|
| 130 | /// Get the default script engine. |
|---|
| 131 | virtual pScriptEngine_type getDefaultScriptEngine() = 0; |
|---|
| 132 | |
|---|
| 133 | /// Get the default session service. |
|---|
| 134 | virtual pSessionService_type getDefaultSessionService() = 0; |
|---|
| 135 | |
|---|
| 136 | /// Get the event service that the application server uses for publishing events. |
|---|
| 137 | virtual pEventService_type getEventService() = 0; |
|---|
| 138 | |
|---|
| 139 | /// Install multiple protocol services using the provided configuration. |
|---|
| 140 | /// The provided configuration should have zero or more <protocol/> entries which |
|---|
| 141 | /// are used to invoke installProtocol using a protocol service specified by the "type" attribute. |
|---|
| 142 | /// @param _pProtocolsConfig - The <protocols/> configuration element that contains zero or more |
|---|
| 143 | /// <protocol/> elements. |
|---|
| 144 | /// @see http://www.indiezen.org/wiki/wiki/zoss/ZenServer/Configuration |
|---|
| 145 | virtual void installProtocols(pConfig_type _pProtocolsConfig) = 0; |
|---|
| 146 | |
|---|
| 147 | /// Install a Protocol Service into this Application Server |
|---|
| 148 | /// @param _protocolName the name of the protocol. This name must be unique to this Application Server. |
|---|
| 149 | virtual void installProtocol(pProtocolService_type _pProtocolService, const std::string& _protocolName) = 0; |
|---|
| 150 | |
|---|
| 151 | /// Get a Protocol Service that's already been installed by the name. |
|---|
| 152 | /// @param _protocolName the name of the protocol used when installing the protocol service in installProtocol(). |
|---|
| 153 | /// @see I_ApplicationServer::installProtocol() |
|---|
| 154 | virtual pProtocolService_type getProtocol(const std::string& _protocolName) = 0; |
|---|
| 155 | |
|---|
| 156 | /// Install multiple application services using the provided configuration. |
|---|
| 157 | /// The provided configuration should have zero or more <application/> entries which |
|---|
| 158 | /// are used to invoke installApplication at the specified "location" attribute using an application |
|---|
| 159 | /// service specified by the "type" attribute. |
|---|
| 160 | /// @param _pAppServicesConfig - The <applications/> configuration element that contains zero or more |
|---|
| 161 | /// <application/> elements. |
|---|
| 162 | /// @see http://www.indiezen.org/wiki/wiki/zoss/ZenServer/Configuration |
|---|
| 163 | virtual void installApplications(pConfig_type _pAppServicesConfig) = 0; |
|---|
| 164 | |
|---|
| 165 | /// Install an Application into this Application Server. |
|---|
| 166 | /// @param _pApplicationService - Application service to install |
|---|
| 167 | /// @param _rootLocation - Root resource location at which this application should be installed. |
|---|
| 168 | virtual void installApplication(pApplicationService_type _pApplicationService, pResourceLocation_type _pRootLocation) = 0; |
|---|
| 169 | |
|---|
| 170 | /// Configure the application. |
|---|
| 171 | virtual void configureApplication(pApplicationService_type _pApplicationService, pConfig_type _pConfig) = 0; |
|---|
| 172 | |
|---|
| 173 | /// Get the application. |
|---|
| 174 | virtual pApplicationService_type getApplication(pResourceLocation_type _pServiceLocation) const = 0; |
|---|
| 175 | |
|---|
| 176 | /// Visit the resource locations. |
|---|
| 177 | virtual void getResourceLocations(I_ResourceLocationVisitor& _visitor) const = 0; |
|---|
| 178 | |
|---|
| 179 | /// Get the message registry. |
|---|
| 180 | virtual pMessageRegistry_type getMessageRegistry() = 0; |
|---|
| 181 | |
|---|
| 182 | /// Handle a message. |
|---|
| 183 | /// Messages are one-way notifications and are not expected to return a result. |
|---|
| 184 | virtual void handleMessage(pMessage_type _pMessage) = 0; |
|---|
| 185 | |
|---|
| 186 | /// Handle a request asynchronously. |
|---|
| 187 | /// A request is a message that requires a response, but the response can be handled |
|---|
| 188 | /// asynchronously. |
|---|
| 189 | /// @param _responseHandler dispatch object that will handle the reponse to this request. |
|---|
| 190 | virtual void handleRequest(pRequest_type _pRequest, pResponseHandler_type _pResponseHandler) = 0; |
|---|
| 191 | |
|---|
| 192 | /// Handle a session event. |
|---|
| 193 | /// A session event is an event such as a succesful authentication, (or unsuccesful |
|---|
| 194 | /// for that matter), or a login or logout request event, etc. |
|---|
| 195 | /// @see I_SessionEvent |
|---|
| 196 | virtual void handleSessionEvent(pSessionEvent_type _pSessionEvent) = 0; |
|---|
| 197 | |
|---|
| 198 | /// Install database connections. |
|---|
| 199 | /// Install one or more database connections using the provided configuration. |
|---|
| 200 | /// The provided configuration should have zero or more <database/> entries which |
|---|
| 201 | /// are used to invoke createDatabaseEntry(). |
|---|
| 202 | /// service specified by the "type" attribute. |
|---|
| 203 | /// @param _pDatabasesConfig - The <databases/> configuration element that contains zero or more |
|---|
| 204 | /// <database/> elements. |
|---|
| 205 | /// @see http://www.indiezen.org/wiki/wiki/zoss/ZenServer/Configuration |
|---|
| 206 | virtual void installDatabaseConnections(pConfig_type _pDatabasesConfig) = 0; |
|---|
| 207 | |
|---|
| 208 | /// Create a database connection entry. |
|---|
| 209 | /// Database connections are pooled by the application server. This method creates the |
|---|
| 210 | /// connection information entry by a connection name. When you call getDatabaseConnection() |
|---|
| 211 | /// and pass the same connection name, it will return the appropriate connection. |
|---|
| 212 | /// @todo Refactor to use config_type instead of pConfig_type. |
|---|
| 213 | virtual void createDatabaseEntry(const std::string& _connectionName, const std::string& _databaseType, config_type& _config) = 0; |
|---|
| 214 | |
|---|
| 215 | /// Get the database connection for the current thread. |
|---|
| 216 | virtual pDatabaseConnection_type getDatabaseConnection(const std::string& _connectionName) = 0; |
|---|
| 217 | /// @} |
|---|
| 218 | |
|---|
| 219 | /// @name Inner classes |
|---|
| 220 | /// @{ |
|---|
| 221 | public: |
|---|
| 222 | struct APPSERVER_DLL_LINK I_ResourceLocationVisitor |
|---|
| 223 | { |
|---|
| 224 | virtual void begin() = 0; |
|---|
| 225 | virtual void visit(pResourceLocation_type _pResourceLocation, pApplicationService_type _pService) = 0; |
|---|
| 226 | virtual void end() = 0; |
|---|
| 227 | }; // struct I_ResourceLocationVisitor |
|---|
| 228 | /// @} |
|---|
| 229 | |
|---|
| 230 | /// @name Static Instance |
|---|
| 231 | /// @{ |
|---|
| 232 | public: |
|---|
| 233 | /// Get an application server by name. |
|---|
| 234 | /// |
|---|
| 235 | /// The lifetime of an application server is from the time that getInstance() |
|---|
| 236 | /// returns to the time that I_ApplicationServer::stop() is called and the |
|---|
| 237 | /// application server is fully stopped. |
|---|
| 238 | static I_ApplicationServer& getInstance(const std::string& _instanceName); |
|---|
| 239 | /// @} |
|---|
| 240 | |
|---|
| 241 | /// @name Events |
|---|
| 242 | /// @{ |
|---|
| 243 | public: |
|---|
| 244 | /// @} |
|---|
| 245 | |
|---|
| 246 | /// @name 'Structors |
|---|
| 247 | /// @{ |
|---|
| 248 | protected: |
|---|
| 249 | I_ApplicationServer(); |
|---|
| 250 | virtual ~I_ApplicationServer(); |
|---|
| 251 | /// @} |
|---|
| 252 | |
|---|
| 253 | }; // interface I_ApplicationServer |
|---|
| 254 | |
|---|
| 255 | //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ |
|---|
| 256 | } // namespace AppServer |
|---|
| 257 | } // namespace Enterprise |
|---|
| 258 | } // namespace Zen |
|---|
| 259 | //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ |
|---|
| 260 | |
|---|
| 261 | #endif // ZEN_APPSERVER_I_APPLICATION_SERVER_HPP_INCLUDED |
|---|