Class: RServiceBus::HandlerLoader

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(host, handlerManager) ⇒ HandlerLoader

Constructor

Parameters:

  • host (RServiceBus::Host)

    instance

  • appResources (Hash)

    As hash where k is the name of a resource, and v is the resource



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

#handlerListObject (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

Parameters:

  • path (String)

    directory to check

Returns:

  • (Array)

    a list of paths to files found in the given path



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

Parameters:

  • filePath (String)

    path to check - this can be a directory or file



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.

Parameters:

  • filePath (String)

    the path to be cleaned



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

Parameters:

  • filePath (String)
  • handlerName (String)


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.message
		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.

Parameters:

  • handlerName (String)

    name of the handler to instantiate

  • filePath (String)

    the path to the file to be loaded

Returns:

  • (RServiceBus::Handler)

    the loader



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

Parameters:

  • baseDir (String)

    directory to check - should not have trailing slash



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

Parameters:

  • msgName (String)

    name of message

  • baseDir (String)

    directory to check for handlers of the given msgName



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

Parameters:

  • baseDir (String)

    directory to check - should not have trailing slash



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