001package com.identityworksllc.iiq.common; 002 003import sailpoint.api.Meter; 004import sailpoint.tools.GeneralException; 005 006/** 007 * Starts a {@link Meter} with the given name, then invokes the callback, then 008 * finally stops the meter before returning the output. 009 */ 010public class Metered { 011 /** 012 * Callback interface where no output is required 013 */ 014 @FunctionalInterface 015 public interface MeterCallback { 016 /** 017 * Performs the action wrapped by the Meter 018 * @throws GeneralException if anything goes wrong 019 */ 020 void run() throws GeneralException; 021 } 022 023 /** 024 * Callback interface where an output is required 025 * @param <T> The output type 026 */ 027 @FunctionalInterface 028 public interface MeterCallbackWithOutput<T> { 029 /** 030 * Performs the action wrapped by the Meter, returning any output 031 * @return The output of the action 032 * @throws GeneralException if anything goes wrong 033 */ 034 T run() throws GeneralException; 035 } 036 037 /** 038 * Meters the invocation of the callback, including an output 039 * 040 * @param meterName The meter name 041 * @param callback The callback to invoke 042 * @param <T> The output type 043 * @return The output of the callback 044 * @throws GeneralException on any errors in the callback 045 */ 046 public static <T> T meter(String meterName, MeterCallbackWithOutput<T> callback) throws GeneralException { 047 Meter.enterByName(meterName); 048 try { 049 return callback.run(); 050 } finally { 051 Meter.exitByName(meterName); 052 } 053 } 054 055 /** 056 * Meters the invocation of the callback, without an output 057 * 058 * @param meterName The meter name 059 * @param callback The callback to invoke 060 * @throws GeneralException on any errors in the callback 061 */ 062 public static void meter(String meterName, MeterCallback callback) throws GeneralException { 063 Meter.enterByName(meterName); 064 try { 065 callback.run(); 066 } finally { 067 Meter.exitByName(meterName); 068 } 069 } 070 071 /** 072 * Private utility constructor 073 */ 074 private Metered() { 075 076 } 077}