All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.woozooha.adonistrack.domain.Context Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.woozooha.adonistrack.domain;

import java.io.Serializable;
import java.util.Stack;

/**
 * Invocation context.
 * 
 * @author woozoo73
 */
public class Context implements Serializable {

    private static final long serialVersionUID = 1L;

    private static ThreadLocal ENDPOINT_INVOCATION_CONTEXT = new ThreadLocal();

    private static ThreadLocal> INVOCATION_STACK_CONTEXT = new ThreadLocal>() {
        @Override
        protected Stack initialValue() {
            return new Stack();
        }
    };

    public static Invocation getEndpointInvocation() {
        return ENDPOINT_INVOCATION_CONTEXT.get();
    }

    public static void setEndpointInvocation(Invocation invocation) {
        ENDPOINT_INVOCATION_CONTEXT.set(invocation);
    }

    public static void addToInvocationStack(Invocation invocation) {
        Stack stack = INVOCATION_STACK_CONTEXT.get();
        stack.add(invocation);
    }

    public static Invocation popFromInvocationStack() {
        Stack stack = INVOCATION_STACK_CONTEXT.get();
        return stack.pop();
    }

    public static Invocation peekFromInvocationStack() {
        Stack stack = INVOCATION_STACK_CONTEXT.get();
        if (stack.isEmpty()) {
            return null;
        }

        return stack.peek();
    }

    public static Invocation dump() {
        Invocation invocation = ENDPOINT_INVOCATION_CONTEXT.get();
        invocation.calculateChildDurationPercentage();

        ENDPOINT_INVOCATION_CONTEXT.set(null);
        INVOCATION_STACK_CONTEXT.set(new Stack());

        return invocation;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy