Version 4

    Often it's useful for Auditing or security purposes to get the IP Address of the client making a remote EJB3 Call. This information is not readily available in the Context of an EJB3 Bean.  Here's a hack to be able to achieve this in JBoss 4.x.

     

    Note:  This should be fixed and be part of the invocation context in jboss 5.x.  You can see that in EJBTHREE-902 , Remoting has already fixed this in 2.4, which is in jboss 5 and should be piced up and put into EJB3.  At this writing the EJB3 plumming had not been put in yet.

     

    Hack Alert

    This is not pretty, but in conferring with EJB3 and Remoting developers, this seems like the only way to do this prior to JBoss 5.  You must parse the remote address from the Server Thread name.  When you invoke a method on an EJB from a client, you go through remoting which launches a worker thread.  The Worker thread is named what thread it is followed by the ip address of the client making the remoting connection.

     

    Normally the thread name looks like...

     

    WorkerThread#0[192.168.0.108:55208]

     

    So we can use the thread name and parse out the ip address.  The code will look something like this...

     

    private String getCurrentClientIpAddress()
        {
            
            String currentThreadName = Thread.currentThread().getName();
            System.out.println("Threadname: "+currentThreadName);
            int begin = currentThreadName.indexOf('[') +1;
            int end = currentThreadName.indexOf(']')-1;
            String remoteClient = currentThreadName.substring(begin, end);
            return remoteClient;
        }
    

     

    Attached is a project that shows how to implement an EJB3 Stateless Session Bean with an interceptor using the @Interceptors annotation on the SLSB.  This also uses the @AroundInvoke annotation to be able to define the interceptor method.

     

    Please see the example project and the ReadMe.txt inside the example.