public Object generateId()

in safeguard-impl/src/main/java/org/apache/safeguard/impl/interceptor/IdGeneratorInterceptor.java [52:91]


    public Object generateId(final InvocationContext context) throws Exception {
        final long id = idGenerator.incrementAndGet();
        context.getContextData().put(IdGeneratorInterceptor.class.getName(), id);

        final Map<Key, Counters> counters = cache.getCounters();
        final Key key = new Key(context, cache.getUnwrappedCache().getUnwrappedCache());
        // todo: ?context.getContextData().put(IdGeneratorInterceptor.class.getName() + ".key_" + id, key);

        Counters methodCounters = counters.get(key);
        if (methodCounters == null) {
            methodCounters = cache.create(context.getMethod());
            final Counters existing = counters.putIfAbsent(key, methodCounters);
            if (existing != null) {
                methodCounters = existing;
            }
        }

        methodCounters.total.inc();
        try {
            final Object proceed = context.proceed();
            if (CompletionStage.class.isInstance(proceed)) {  // todo: integrate with futures
                final Counters countersRef = methodCounters;
                final CompletionStage<?> completionStage = CompletionStage.class.cast(proceed);
                return completionStage.exceptionally(e -> {
                    countersRef.failed.inc();
                    if (RuntimeException.class.isInstance(e)) {
                        throw RuntimeException.class.cast(e);
                    }
                    if (Error.class.isInstance(e)) {
                        throw Error.class.cast(e);
                    }
                    throw new IllegalStateException(e);
                });
            }
            return proceed;
        } catch (final Exception | Error e) {
            methodCounters.failed.inc();
            throw e;
        }
    }