CORBA brokers provide support for interception of GIOP requests and replies both in the server and client sides. Although the interception model is heavily influenced by the CORBA Portable Interceptors specification, OiL does not stick to it. Like other aspects of the OiL's API, it priories a simpler model more adequate to Lua than the defined by the CORBA standard.

Interceptors

Invocation interception is done by interceptor objects, which can be register in the broker as a client interceptor or a server interceptor. Client interceptors intercept invocations issued to remote servants, while server interceptors intercept invocations received from remote applications.

A client interceptor is an object that provides either methods sendrequest or receivereply. The first method is called before an invocation request is sent to a remote servant, and the second is called after a reply is received from a remote servant. On the other hand, a server interceptor is an object that provides either methods receiverequest or sendreply, which are called before an invocation request is processed by the server, and after a reply is sent to the corresponding invoker, respectively. Each one of these methods receives a single parameter, which is a table representing the invocation. The structure and information contained in this table varies accordingly to the method being called.

However, for a single invocation issued or received, the table received by method sendrequest and receivereply are always the same. The same is true for methods receiverequest and sendreply, which means that the parameter received by the former method is the same parameter passed to the corresponding invocation of the later. This also means that any field stored in this table during the execution of the first method will be available during the execution of the other for a single invocation. For an example, consider the code below that measures the time each invocation takes to conclude.

local socket = require "socket"

TimeProfiler = {}

function TimeProfiler:receiverequest(request)
	request.start = socket.gettime()
end

function TimeProfiler:sendreply(reply)
	local time = socket.gettime() - reply.start
	print(string.format("opreation '%s' lasted %d seconds", reply.operation, time))
end

For a list of the fields available in the tables passed as parameter to the methods of interceptors, see description of method <%=link("Brokers","setinterceptor","#setinterceptor")%>.

Adding Information to Invocations

GIOP messages for requests and replies has the special field service_context, which can be used to store additional information to be send with the invocation. Basically, the service_context is a sequence of structures, each containing a tag number and a octet sequence that is used to store arbitrary data. Interceptors can define the value of field service_context in the parameter of methods sendrequest or sendreply. Similarly, the value of field service_context can be accessed through the parameter of methods receiverequest or receivereply.

To promote interoperability with other CORBA ORBs, the information contained in field service_context is usually encoded using CORBA's CDR. To help this, OiL brokers provide methods <%=link("Brokers","newencoder","#newencoder")%>() and <%=link("Brokers","newdecoder","#newdecoder")%>(stream). The example below, illustrates how to use interceptors to add authentication information in an invocation.

Client
require "oil"

oil.main(function()
	local broker = oil.init()
	
	print("Type the your username")
	local Username = io.read()
	print("Type the password")
	local Password = io.read()
	
	local AuthID = broker:loadidl"struct AuthID { string user; string pswd; };"
	
	local encoder = broker:newencoder()
	encoder:put({user=UserName,pswd=Password}, AuthID)
	local AuthData = encoder:getdata()
	
	local Authenticator = {}
	function Authenticator:sendrequest(request)
		request.service_context[#request.service_context + 1] = {
			context_id = 404,
			context_data = AuthData,
		}
	end
	
	broker:setinterceptor(Authenticator, "client")
	
	...
	
end)
Server
require "oil"

oil.main(function()
	local broker = oil.init()
	
	local AuthID = broker:loadidl[[
		struct AuthID {
			string user;
			string pswd;
		};
	]]
	
	local Validator = {}
	function Validator:receiverequest(request)
		local valid
		for _, context in ipairs(request.service_context) do
			if context.context_id == 404 then
				local decoder = broker:newdecoder(context.context_data)
				local authid = decoder:get(AuthID)
				valid = (authid.user == "admin") and (authid.pswd == "admin")
			end
		end
		if not valid then
			-- cancel request with an exception
			request.success = false
			request[1] = broker:newexcept{ "CORBA::NO_PERMISSION",
				minor_code_value = 0,
			}
		end
	end
	
	broker:setinterceptor(Validator, "server")
	
	...
	
end)

Multithreading Issues

OiL guarantees that for each single invocation on both methods of the interceptor is invoked in the same thread. However, the same thread can be reused for another invocation after the first has finished. Furthermore, while an invocation is being executed, others can be issues and the interceptor again, but in other threads. Therefore, you can use the current thread (oil.tasks.current) to identify an invocation.

Multiple Interceptors

Although CORBA brokers only allow one interceptor to be defined on the client side and one on the server side, if you need to define a chain of interceptors, you can register an interceptor that delegates interception to a chain of interceptors. The following example illustrates the implementation of a interception chain that does not guarantee the other the interceptors are invoked. The implementation use class loop.object.Publisher of LOOP library.

require "oil"
require "loop.object.Publisher"

oil.main(function()
	local broker = oil.init()
	
	InterceptionChain = loop.object.Publisher{
		Interceptor_01,
		Interceptor_02,
		Interceptor_03,
		...
	}
	
	broker:setinterceptor(InterceptionChain, "server")
	
	...
	
end)