naming.InitialContext
SequenceData = Struct.new :name, :value,
:last_updated, :last_updated_by
You need to import an InitialContext, and also create a structure that will contain the
data returned by some of the SequenceManager methods. As you might notice, this structure
contains exactly the data you have about a sequence in the service database. The actual
SequenceManager contains a few real methods, and several methods that mostly provide easy
access. First, the basic class definition looks like this:
class SequenceManager
include Singleton
CHAPTER 10 ?– AN EJB-BACKED RAILS APPLICATION 187
def initialize
ic = InitialContext.new
@connection = ic.lookup("com.bb.BBService")
end
end
Including the module Singleton means that there can only ever be one instance of
this SequenceManager in the same Rails process. This is to make it easy to handle the
InitialContext connection. You don??™t want more than one of those, because they??™re expensive.
The initialize method just creates a context, looks up the bean, and saves this as the
@connection instance variable.
As might be obvious, you need to invoke all the methods you want to call on the server
in the same manner, with the service_username and service_password. To make all this
invocation easier, add a with_user method that takes care of this:
private
def with_user(user, method, arg)
@connection.invoke(user.service_username,
user.service_password,method,arg)
end
This method makes the real call, sending the method and argument along.
Pages:
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286