001package com.nimbusds.common.jsonrpc2; 002 003 004import org.apache.logging.log4j.Logger; 005 006import java.net.URL; 007 008 009/** 010 * Logs the detected name and version of a remote JSON-RPC 2.0 web service, as 011 * reported by a {@link WsInfoRequestHandler}. To prevent blocking the detection 012 * routine is run on a separate thread. 013 * 014 * <p>Example: 015 * 016 * <pre> 017 * new Thread(new LoggingWsDetector(...)).start(); 018 * </pre> 019 */ 020public class LoggingWsDetector extends WsDetector implements Runnable { 021 022 023 /** 024 * The expected web service name. 025 */ 026 private final String expectedWsName; 027 028 029 /** 030 * The logger. 031 */ 032 private final Logger log; 033 034 035 /** 036 * The detected WS name and version, {@code null} if not detected. 037 */ 038 private WsInfo wsInfo; 039 040 041 /** 042 * Creates a new JSON-RPC 2.0 web service detector. 043 * 044 * @param url The web service HTTP(S) URL. Must not be 045 * {@code null}. 046 * @param expectedWsName The expected web service name, as reported by 047 * {@code ws.getName}. Must not be {@code null}. 048 * @param log The logger. Must not be {@code null}. 049 */ 050 public LoggingWsDetector(final URL url, final String expectedWsName, final Logger log) { 051 052 super(url); 053 054 if (expectedWsName == null) 055 throw new IllegalArgumentException("The web service name must not be null"); 056 057 this.expectedWsName = expectedWsName; 058 059 060 if (log == null) 061 throw new IllegalArgumentException("The logger must not be null"); 062 063 this.log = log; 064 } 065 066 067 /** 068 * Returns the detected web service name and version. 069 * 070 * @return The web service name and version, {@code null} if not 071 * detected. 072 */ 073 public WsInfo getWsInfo() { 074 075 return wsInfo; 076 } 077 078 079 /** 080 * Detects and logs the JSON-RPC 2.0 web service name and version. The 081 * target web service must handle {@code ws.getName} and 082 * {@code ws.getVersion} calls, as implemented by 083 * {@link WsInfoRequestHandler}. 084 * 085 * <p>Any encountered exceptions are logged at WARN level. 086 */ 087 @Override 088 public void run() { 089 090 WsInfo wsInfo; 091 092 try { 093 wsInfo = detect(); 094 095 } catch (Exception e) { 096 097 log.warn("Couldn't detect remote {} web service: {}", expectedWsName, e.getMessage()); 098 return; 099 } 100 101 if (! expectedWsName.equalsIgnoreCase(wsInfo.getName())) { 102 103 log.warn("Remote web service is not {}, detected {} {}", expectedWsName, wsInfo.getName(), wsInfo.getVersion()); 104 return; 105 } 106 107 108 log.info("Detected remote web service {} {}", wsInfo.getName(), wsInfo.getVersion()); 109 } 110}