Class: RServiceBus::HandlerLoader
- Inherits:
-
Object
- Object
- RServiceBus::HandlerLoader
- Defined in:
- lib/rservicebus/HandlerLoader.rb
Overview
Given a directory, this class is responsible for finding msgnames, handlernames, and loading handlers
Instance Attribute Summary collapse
-
#handlerList ⇒ Object
readonly
Returns the value of attribute handlerList.
Instance Method Summary collapse
-
#getListOfFilesForDir(path) ⇒ Array
This method is overloaded for unit tests.
-
#getMsgName(filePath) ⇒ Object
Extract the top level dir or file name as it is the msg name.
-
#getRequirePath(filePath) ⇒ Object
Cleans the given path to ensure it can be used for as a parameter for the require statement.
-
#initialize(host, handlerManager) ⇒ HandlerLoader
constructor
Constructor.
-
#loadHandler(msgName, filePath, handlerName) ⇒ Object
Wrapper function.
-
#loadHandlerFromFile(handlerName, filePath) ⇒ RServiceBus::Handler
Instantiate the handler named in handlerName from the file name in filePath Exceptions will be raised if encountered when loading handlers.
-
#loadHandlersFromPath(baseDir) ⇒ Object
Entry point for loading handlers.
-
#loadHandlersFromSecondLevelPath(msgName, baseDir) ⇒ Object
Multiple handlers for the same msg can be placed inside a top level directory.
-
#loadHandlersFromTopLevelPath(baseDir) ⇒ Object
Load top level handlers from the given directory.
Constructor Details
#initialize(host, handlerManager) ⇒ HandlerLoader
Constructor
28 29 30 31 32 33 34 |
# File 'lib/rservicebus/HandlerLoader.rb', line 28 def initialize( host, handlerManager ) @host = host @handlerManager = handlerManager @listOfLoadedPaths = Hash.new end |
Instance Attribute Details
#handlerList ⇒ Object (readonly)
Returns the value of attribute handlerList.
9 10 11 |
# File 'lib/rservicebus/HandlerLoader.rb', line 9 def handlerList @handlerList end |
Instance Method Details
#getListOfFilesForDir(path) ⇒ Array
This method is overloaded for unit tests
110 111 112 113 114 115 |
# File 'lib/rservicebus/HandlerLoader.rb', line 110 def getListOfFilesForDir( path ) list = Dir[path + '/*']; RServiceBus.rlog "HandlerLoader.getListOfFilesForDir. path: #{path}, list: #{list}" return list end |
#getMsgName(filePath) ⇒ Object
Extract the top level dir or file name as it is the msg name
145 146 147 148 149 150 151 152 153 |
# File 'lib/rservicebus/HandlerLoader.rb', line 145 def getMsgName( filePath ) baseName = File.basename( filePath ) extName = File.extname( baseName ) fileName = baseName.sub( extName, '') msgName = fileName return msgName end |
#getRequirePath(filePath) ⇒ Object
Cleans the given path to ensure it can be used for as a parameter for the require statement.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rservicebus/HandlerLoader.rb', line 39 def getRequirePath( filePath ) unless filePath.start_with?('/') then filePath = './' + filePath end if File.exists?( filePath ) then return filePath.sub( '.rb', '') end abort( 'Filepath, ' + filePath + ", given for MessageHandler require doesn't exist" ); end |
#loadHandler(msgName, filePath, handlerName) ⇒ Object
Wrapper function
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rservicebus/HandlerLoader.rb', line 80 def loadHandler(msgName, filePath, handlerName) if @listOfLoadedPaths.has_key?( filePath ) then RServiceBus.log "Not reloading, #{filePath}" return end begin RServiceBus.rlog 'filePath: ' + filePath RServiceBus.rlog 'handlerName: ' + handlerName handler = self.loadHandlerFromFile( handlerName, filePath ) RServiceBus.log 'Loaded Handler: ' + handlerName @handlerManager.addHandler( msgName, handler ) @listOfLoadedPaths[filePath] = 1 rescue Exception => e puts 'Exception loading handler from file: ' + filePath puts e. puts e.backtrace[0] abort() end end |
#loadHandlerFromFile(handlerName, filePath) ⇒ RServiceBus::Handler
Instantiate the handler named in handlerName from the file name in filePath Exceptions will be raised if encountered when loading handlers. This is a load time activity, so handlers should load correctly. As much information as possible is returned to enable the handler to be fixed, or configuration corrected.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rservicebus/HandlerLoader.rb', line 59 def loadHandlerFromFile( handlerName, filePath ) requirePath = self.getRequirePath( filePath ) require requirePath begin handler = Object.const_get(handlerName).new(); rescue Exception => e puts 'Expected class name: ' + handlerName + ', not found after require: ' + requirePath puts '**** Check in ' + filePath + ' that the class is named : ' + handlerName puts '( In case its not that )' raise e end return handler end |
#loadHandlersFromPath(baseDir) ⇒ Object
Entry point for loading handlers
179 180 181 182 183 |
# File 'lib/rservicebus/HandlerLoader.rb', line 179 def loadHandlersFromPath(baseDir) self.loadHandlersFromTopLevelPath(baseDir) return self end |
#loadHandlersFromSecondLevelPath(msgName, baseDir) ⇒ Object
Multiple handlers for the same msg can be placed inside a top level directory. The msg name is than taken from the directory, and the handlers from the files inside that directory
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rservicebus/HandlerLoader.rb', line 123 def loadHandlersFromSecondLevelPath(msgName, baseDir) self.getListOfFilesForDir(baseDir).each do |filePath| unless filePath.end_with?('.') then extName = File.extname(filePath) if !File.directory?(filePath) && extName == '.rb' then fileName = File.basename(filePath).sub('.rb', '') handlerName = "MessageHandler_#{msgName}_#{fileName}" self.loadHandler(msgName, filePath, handlerName) end end end return self end |
#loadHandlersFromTopLevelPath(baseDir) ⇒ Object
Load top level handlers from the given directory
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rservicebus/HandlerLoader.rb', line 158 def loadHandlersFromTopLevelPath(baseDir) RServiceBus.rlog "HandlerLoader.loadHandlersFromTopLevelPath. baseDir: #{baseDir}" self.getListOfFilesForDir(baseDir).each do |filePath| unless filePath.end_with?('.') then msgName = self.getMsgName(filePath) if File.directory?(filePath) then self.loadHandlersFromSecondLevelPath(msgName, filePath) else handlerName = "MessageHandler_#{msgName}" self.loadHandler(msgName, filePath, handlerName) end end end return self end |