From 64fb289c4038c66f7274b00d205b68d670b5f6c8 Mon Sep 17 00:00:00 2001 From: lucio Date: Wed, 21 Feb 2024 20:22:49 +0100 Subject: [PATCH] change InnerMethodName --- pom.xml | 2 +- .../request/RequestAccounting.java | 24 +++++++++---------- .../application/request/RequestValidator.java | 8 +++---- .../smartgears/utils/InnerMethodName.java | 10 ++++---- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index e6fc952..6b2f4b1 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ maven-parent org.gcube.tools - 1.2.0 + 1.1.0 org.gcube.core diff --git a/src/main/java/org/gcube/smartgears/handlers/application/request/RequestAccounting.java b/src/main/java/org/gcube/smartgears/handlers/application/request/RequestAccounting.java index db65ce8..1f146dd 100644 --- a/src/main/java/org/gcube/smartgears/handlers/application/request/RequestAccounting.java +++ b/src/main/java/org/gcube/smartgears/handlers/application/request/RequestAccounting.java @@ -34,21 +34,21 @@ public class RequestAccounting extends RequestHandler { String context = getContext(appContext); - if (InnerMethodName.instance.get() == null) { + if (InnerMethodName.get() == null) { String calledMethod = e.request().getRequestURI().substring(e.request().getContextPath().length()); if (calledMethod.isEmpty()) calledMethod = "/"; calledMethod = e.request().getMethod() + " " + calledMethod; - InnerMethodName.instance.set(calledMethod); + InnerMethodName.set(calledMethod); } - String caller = SecretManagerProvider.instance.get() != null - ? SecretManagerProvider.instance.get().getOwner().getId() + String caller = SecretManagerProvider.get() != null + ? SecretManagerProvider.get().getOwner().getId() : UNKNOWN; startCallThreadLocal.set(System.currentTimeMillis()); log.info("REQUEST START ON {}:{}({}) CALLED FROM {}@{} IN SCOPE {} ", appContext.configuration().name(), - appContext.configuration().group(), InnerMethodName.instance.get(), caller, + appContext.configuration().group(), InnerMethodName.get(), caller, e.request().getRemoteHost(), context); } @@ -61,8 +61,8 @@ public class RequestAccounting extends RequestHandler { String context = getContext(appContext); - String caller = SecretManagerProvider.instance.get() != null - ? SecretManagerProvider.instance.get().getOwner().getId() + String caller = SecretManagerProvider.get() != null + ? SecretManagerProvider.get().getOwner().getId() : UNKNOWN; String callerQualifier = UNKNOWN; // retieves caller Ip when there is a proxy @@ -91,7 +91,7 @@ public class RequestAccounting extends RequestHandler { log.info("REQUEST SERVED ON {}:{}({}) CALLED FROM {}@{} IN SCOPE {} {}(CODE {}) IN {} millis", appContext.configuration().name(), appContext.configuration().group(), - InnerMethodName.instance.get(), caller, callerIp, context, success ? "SUCCEDED" : "FAILED", + InnerMethodName.get(), caller, callerIp, context, success ? "SUCCEDED" : "FAILED", e.response().getStatus(), durationInMillis); } catch (Exception e1) { @@ -99,7 +99,7 @@ public class RequestAccounting extends RequestHandler { throw e1; } finally { startCallThreadLocal.remove(); - InnerMethodName.instance.reset(); + InnerMethodName.reset(); } } @@ -120,7 +120,7 @@ public class RequestAccounting extends RequestHandler { serviceUsageRecord.setHost(appContext.container().configuration().hostname() + ":" + appContext.container().configuration().port()); - serviceUsageRecord.setCalledMethod(InnerMethodName.instance.get()); + serviceUsageRecord.setCalledMethod(InnerMethodName.get()); serviceUsageRecord.setCallerHost(remoteHost); serviceUsageRecord.setOperationResult(success ? OperationResult.SUCCESS : OperationResult.FAILED); serviceUsageRecord.setDuration(System.currentTimeMillis() - startCallThreadLocal.get()); @@ -134,8 +134,8 @@ public class RequestAccounting extends RequestHandler { private String getContext(ApplicationContext appContext) { String infrastructure = appContext.container().configuration().infrastructure(); String context = "/" + infrastructure; - if (SecretManagerProvider.instance.get() != null) - context = SecretManagerProvider.instance.get().getContext(); + if (SecretManagerProvider.get() != null) + context = SecretManagerProvider.get().getContext(); return context; } diff --git a/src/main/java/org/gcube/smartgears/handlers/application/request/RequestValidator.java b/src/main/java/org/gcube/smartgears/handlers/application/request/RequestValidator.java index 8135571..d09af14 100644 --- a/src/main/java/org/gcube/smartgears/handlers/application/request/RequestValidator.java +++ b/src/main/java/org/gcube/smartgears/handlers/application/request/RequestValidator.java @@ -47,7 +47,7 @@ public class RequestValidator extends RequestHandler { appContext = call.context(); - SecretManagerProvider.instance.set(getSecret(call)); + SecretManagerProvider.set(getSecret(call)); validateAgainstLifecycle(call); @@ -63,7 +63,7 @@ public class RequestValidator extends RequestHandler { @Override public void handleResponse(ResponseEvent e) { log.debug("resetting all the Thread local for this call."); - SecretManagerProvider.instance.reset(); + SecretManagerProvider.reset(); } @@ -85,7 +85,7 @@ public class RequestValidator extends RequestHandler { private void validateScopeCall() { - String context = SecretManagerProvider.instance.get().getContext(); + String context = SecretManagerProvider.get().getContext(); if (context == null) { log.warn("rejecting unscoped call to {}",appContext.name()); @@ -106,7 +106,7 @@ public class RequestValidator extends RequestHandler { private void rejectUnauthorizedCalls(RequestEvent call){ - Secret secret = SecretManagerProvider.instance.get(); + Secret secret = SecretManagerProvider.get(); if (secret == null){ log.warn("rejecting call to {}, authorization required",appContext.name()); diff --git a/src/main/java/org/gcube/smartgears/utils/InnerMethodName.java b/src/main/java/org/gcube/smartgears/utils/InnerMethodName.java index 692975d..3ab7048 100644 --- a/src/main/java/org/gcube/smartgears/utils/InnerMethodName.java +++ b/src/main/java/org/gcube/smartgears/utils/InnerMethodName.java @@ -5,8 +5,6 @@ import org.slf4j.LoggerFactory; public class InnerMethodName { - public static InnerMethodName instance = new InnerMethodName(); - private static Logger logger = LoggerFactory.getLogger(InnerMethodName.class); // Thread local variable containing each thread's ID @@ -14,26 +12,26 @@ public class InnerMethodName { new InheritableThreadLocal() { @Override protected String initialValue() { - return "UNKNOWN"; + return null; } }; private InnerMethodName(){} - public String get(){ + public static String get(){ String calledMethod = threadMethod.get(); logger.trace("getting InnerMethodName as "+calledMethod+" in thread "+Thread.currentThread().getId() ); return calledMethod; } - public void set(String calledMethod){ + public static void set(String calledMethod){ if (calledMethod==null) return; threadMethod.set(calledMethod); logger.trace("setting InnerMethodName as "+calledMethod+" in thread "+Thread.currentThread().getId() ); } - public void reset(){ + public static void reset(){ threadMethod.remove(); } }