You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java实现CORBA分布式应用:运行时获取客户端IP及请求日志方法问询

Hey there! Great question—since you're working with Java CORBA, there are definitely reliable ways to capture client IP addresses and log requests on the server side. Let me break this down into actionable steps for you:

1. 获取客户端IP地址

Java CORBA doesn't expose client IP directly through the standard API, but you can access it via the underlying transport connection context. The approach varies slightly based on your ORB implementation (most beginners use Sun/Oracle's ORB which comes with the JDK), so here's a practical example:

import org.omg.CORBA.ORB;
import com.sun.corba.se.spi.orb.ORB;
import com.sun.corba.se.spi.transport.Connection;
import com.sun.corba.se.spi.transport.CorbaConnection;

public class MyServant extends MyPOA {
    private ORB orb;

    public MyServant(ORB orb) {
        this.orb = orb;
    }

    @Override
    public void myBusinessMethod() {
        // Cast to the Sun-specific ORB implementation to access transport details
        com.sun.corba.se.spi.orb.ORB sunORB = (com.sun.corba.se.spi.orb.ORB) orb;
        Connection activeConnection = sunORB.getTransportManager().getCurrentConnection();
        
        if (activeConnection instanceof CorbaConnection) {
            CorbaConnection corbaConn = (CorbaConnection) activeConnection;
            String clientIP = corbaConn.getPeerAddress().getHostAddress();
            System.out.println("Incoming request from client IP: " + clientIP);
        }
    }
}

Note: If you're using a different ORB like JacORB, check its documentation for equivalent methods to access the active connection context— the core idea of pulling IP from the transport layer stays the same.

2. 实现请求日志记录

You have two solid options here, depending on how scalable you want your logging to be:

Option 1: Manual per-method logging (quick for small apps)

For a tiny application, just add log statements directly in your servant's business methods, combining it with the IP logic above:

@Override
public String sayHello(String username) {
    String clientIP = getClientIP(); // Wrap the IP logic into a reusable method
    System.out.printf("[REQUEST] Method: sayHello | Client IP: %s | Params: %s%n", clientIP, username);
    
    // Your business logic here
    String response = "Hello, " + username;
    
    System.out.printf("[RESPONSE] Method: sayHello | Client IP: %s | Result: %s%n", clientIP, response);
    return response;
}

Option 2: Global request interception (clean for larger apps)

If you have multiple servant methods, avoid repetitive code by using CORBA's Portable Interceptor to log every request/response automatically:

Step 1: Implement the server request interceptor

import org.omg.PortableInterceptor.ServerRequestInterceptor;
import org.omg.PortableInterceptor.ServerRequestInfo;
import com.sun.corba.se.spi.orb.ORB;
import com.sun.corba.se.spi.transport.Connection;
import com.sun.corba.se.spi.transport.CorbaConnection;

public class RequestLoggingInterceptor implements ServerRequestInterceptor {
    private ORB orb;

    public RequestLoggingInterceptor(ORB orb) {
        this.orb = orb;
    }

    @Override
    public String name() {
        return "RequestLoggingInterceptor";
    }

    @Override
    public void destroy() {}

    @Override
    public void receive_request(ServerRequestInfo requestInfo) {
        String clientIP = getClientIP();
        String methodName = requestInfo.operation();
        // Extract parameters (adjust based on your method's argument types)
        org.omg.CORBA.Any[] params = requestInfo.arguments();
        StringBuilder paramStr = new StringBuilder();
        for (org.omg.CORBA.Any param : params) {
            paramStr.append(param.extract_string()).append(", ");
        }
        
        System.out.printf("[INTERCEPTED REQUEST] Method: %s | Client IP: %s | Params: %s%n",
                methodName, clientIP, paramStr.toString().trim());
    }

    @Override
    public void send_reply(ServerRequestInfo requestInfo) {
        String clientIP = getClientIP();
        String methodName = requestInfo.operation();
        org.omg.CORBA.Any result = requestInfo.result();
        
        System.out.printf("[INTERCEPTED RESPONSE] Method: %s | Client IP: %s | Result: %s%n",
                methodName, clientIP, result.extract_string());
    }

    @Override
    public void send_exception(ServerRequestInfo requestInfo) {
        String clientIP = getClientIP();
        String methodName = requestInfo.operation();
        System.out.printf("[INTERCEPTED EXCEPTION] Method: %s | Client IP: %s | Error: %s%n",
                methodName, clientIP, requestInfo.exception_id());
    }

    // Implement other required interceptor methods as empty stubs
    @Override
    public void receive_request_service_contexts(ServerRequestInfo ri) {}
    @Override
    public void send_other(ServerRequestInfo ri) {}

    private String getClientIP() {
        com.sun.corba.se.spi.orb.ORB sunORB = (com.sun.corba.se.spi.orb.ORB) orb;
        Connection conn = sunORB.getTransportManager().getCurrentConnection();
        if (conn instanceof CorbaConnection) {
            return ((CorbaConnection) conn).getPeerAddress().getHostAddress();
        }
        return "Unknown IP";
    }
}

Step 2: Register the interceptor with your ORB

import org.omg.PortableInterceptor.ORBInitializer;
import org.omg.PortableInterceptor.ORBInitInfo;

public class LoggingORBInitializer implements ORBInitializer {
    @Override
    public void pre_init(ORBInitInfo info) {}

    @Override
    public void post_init(ORBInitInfo info) {
        try {
            RequestLoggingInterceptor interceptor = new RequestLoggingInterceptor(info.orb());
            info.add_server_request_interceptor(interceptor);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Step 3: Enable the initializer when starting your server

public class CORBAServerMain {
    public static void main(String[] args) {
        // Register the interceptor initializer with the ORB
        System.setProperty(
            "org.omg.PortableInterceptor.ORBInitializerClass." + LoggingORBInitializer.class.getName(),
            LoggingORBInitializer.class.getName()
        );

        // Initialize ORB, POA, and register your servant as usual
        ORB orb = ORB.init(args, null);
        // ... rest of your server setup code
    }
}

Hope this helps you get up and running with logging and client IP tracking! Let me know if you hit any snags adapting this to your specific app.

内容的提问来源于stack exchange,提问作者Moh Lamine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:16:53