Class RemoteDbDelegateInvocationHandler

java.lang.Object
org.tentackle.dbms.trip.RemoteDbDelegateInvocationHandler
All Implemented Interfaces:
InvocationHandler

public class RemoteDbDelegateInvocationHandler extends Object implements InvocationHandler
Invocation handler to capture TRIP method invocations.
Author:
harald
  • Field Details

  • Constructor Details

    • RemoteDbDelegateInvocationHandler

      public RemoteDbDelegateInvocationHandler(RemoteDbDelegateImpl<?> delegate)
      Creates an invocation handler for a RemoteDbDelegate.

      The invocation handler may be used to intercept remote method invocations. The default implementation just sets the session info into the MappedDiagnosticContext.

      Parameters:
      delegate - the remote delegate
  • Method Details

    • isCollectingStatistics

      public static boolean isCollectingStatistics()
      Returns whether statistics should be collected.
      Returns:
      true if method invocations are counted and measured per method
    • setCollectingStatistics

      public static void setCollectingStatistics(boolean collect)
      Sets whether statistics should be collected.
      Parameters:
      collect - true if method invocations are counted and measured per method
    • isLoggingInvocations

      public static boolean isLoggingInvocations()
      Returns whether each invocation should be logged.
      Returns:
      true if invocations are logged
    • setLoggingInvocations

      public static void setLoggingInvocations(boolean log)
      Sets whether each invocation should be logged.
      Parameters:
      log - true if invocations are logged
    • isLoggingInvocationDetails

      public static boolean isLoggingInvocationDetails()
      Returns whether args and return values will be logged as well.
      Returns:
      true if log arguments and return value
    • setLoggingInvocationDetails

      public static void setLoggingInvocationDetails(boolean log)
      Sets whether args and return values will be logged as well.
      Parameters:
      log - true if log arguments and return value
    • getLogMinDurationMillis

      public static long getLogMinDurationMillis()
      Gets the minimum milliseconds a method needs to execute before being logged.
      Returns:
      0 if disabled
    • setLogMinDurationMillis

      public static void setLogMinDurationMillis(long millis)
      Sets the minimum milliseconds a method needs to execute before being logged.
      Parameters:
      millis - 0 to disable
    • getLogMinReturnedCollectionSize

      public static int getLogMinReturnedCollectionSize()
      Gets the minimum size of the returned collection to get logged.
      Returns:
      0 if disabled
    • setLogMinReturnedCollectionSize

      public static void setLogMinReturnedCollectionSize(int size)
      Sets the minimum size of the returned collection to get logged.
      Parameters:
      size - 0 to disable
    • getMdcFilter

      public static Pattern getMdcFilter()
      Gets the MDC filter.
      Returns:
      the filter, null if none
    • setMdcFilter

      public static void setMdcFilter(Pattern filter)
      Sets the optional filter to log only TRIP-calls for certain MappedDiagnosticContexts.
      If set, statements are only collected for logging if the MDC is valid and the pattern matches the toString-value of the MDC.
      Parameters:
      filter - the filter, null to clear
    • getDelegate

      public RemoteDbDelegateImpl<?> getDelegate()
      Gets the delegate.
      Returns:
      the delegate
    • invoke

      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

      The default implementation just updates the MDC, invokes the method, and logs the invoked method (args and return value).

      Specified by:
      invoke in interface InvocationHandler
      Throws:
      Throwable
    • updateMDC

      protected void updateMDC()
      Updates the MappedDiagnosticContext with the session data.
    • clearMDC

      protected void clearMDC()
      Clears the MDC.
    • isMDCValid

      protected boolean isMDCValid()
      Checks whether the mapped diagnostic context is valid for statistics logging.
      Returns:
      true if loggable
    • updateThreadInfo

      protected void updateThreadInfo()
      Updates the thread-locals and sets the session's owner thread.
    • clearThreadInfo

      protected void clearThreadInfo()
      Clears the thread-locals and session's owner thread.
    • handleInvocationException

      protected RuntimeException handleInvocationException(Object proxy, Method method, Object[] args, Throwable cause)
      Handles an invocation exception.
      Figures out the real exception cause and throws it, skipping InvocationTargetExceptions. Furthermore, the exception will be logged in the server's log as well (not just the client).
      Parameters:
      proxy - the dynamic proxy
      method - the method to invoke
      args - the method's arguments
      cause - the invocation exception
    • cleanupDbAfterException

      protected void cleanupDbAfterException(Throwable cause)
      Cleans up the db after an exception.
      Invoked from handleInvocationException(Object, Method, Object[], Throwable).
      Parameters:
      cause - the cause of failure
    • invokeImpl

      protected Object invokeImpl(Object proxy, Method method, Object[] args)
      Invokes the delegate's method.

      If the invocation failed for some reason, the stacktrace is logged.

      Parameters:
      proxy - the dynamic proxy
      method - the method to invoke
      args - the method's arguments
      Returns:
      the method's return value
    • log

      protected void log(Method method, Object[] args, Object returnValue, org.tentackle.misc.TimeKeeper timeKeeper)
      Logs and counts the invocation.

      Parameters:
      method - the method executed
      args - the method args
      returnValue - the method's return value
      timeKeeper - the duration when execution was started