-------------------------------------------------------------------------------- -- Title: LWDispatcher.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies local LUObject = require( "LUObject" ) local LUList = require( "LUList" ) local LUMap = require( "LUMap" ) local LUURI = require( "LUURI" ) local LWService = require( "LWService" ) -- define the class local super = LUObject local self = super() -- services map function self:map() if self._map == nil then self._map = LUMap() end return self._map end -- method to register a service with this dispatcher function self:registerService( aService ) if aService ~= nil then local aMap = self:map() local aPrefix = aService:prefix():lower() local someComponents = LUURI:componentsWithPath( aPrefix ) if aMap:hasData() == false then aMap:put( "/", aService ) end for _, aComponent in someComponents:iterator() do local aLocalMap = aMap:get( aComponent ) if aLocalMap == nil then aLocalMap = LUMap() aMap:put( aComponent, aLocalMap ) end aMap = aLocalMap end aMap:put( "/", aService ) end return self end -- method to access the default service function self:defaultService() local aService = self:map():get( "/" ) if aService == nil then aService = LWService:default() end return aService end -- method to find a service with a given context function self:serviceWithContext( aContext ) local anURI = aContext:request():uri() if anURI ~= nil and anURI:path() ~= nil then local aMap = self:map() local aPath = anURI:path():lower() local someComponents = LUURI:componentsWithPath( aPath ) for _, aComponent in someComponents:iterator() do local aLocalMap = aMap:get( aComponent ) if aLocalMap ~= nil then aMap = aLocalMap else break end end return aMap:get( "/" ) end return self:defaultService() end -- method to list this dispatcher services function self:services( aMap, aList ) aMap = aMap or self:map() aList = aList or LUList() for aKey, aValue in aMap:iterator() do if aValue:isKindOf( LWService ) == true then aList:add( aValue ) elseif aValue:isKindOf( LUMap ) == true then aList:addAll( self:services( aValue, aList ) ) end end return aList:distinct():order() end return self