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

com.nerdvision.agent.GrpcBreakpoint Maven / Gradle / Ivy

package com.nerdvision.agent;

import com.nerdvision.agent.api.IBreakpoint;
import com.nerdvision.agent.snapshot.WatchValue;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GrpcBreakpoint implements IBreakpoint
{
    private static final Logger LOGGER = LoggerFactory.getLogger( GrpcBreakpoint.class );

    private final String id;
    private final String relPath;
    private final long lineNo;
    private final int fireCount;
    private final Map args;
    private final long rateLimit;
    private final String condition;
    private final List watchers;
    private final String srcType;
    private final String workspaceId;
    private final boolean isCf;
    private final String type;
    private final String logMessage;

    private String estimatedClassRoot;
    private int hitCount = 0;
    private long lastHitTime = 0;


    public GrpcBreakpoint( final String id,
                           final String relPath,
                           final long lineNo,
                           final int fireCount,
                           final String srcType,
                           final String workspaceId,
                           final String condition,
                           final List watchers,
                           final String type,
                           final String logMessage,
                           final Map args )
    {
        this.id = id;
        this.relPath = relPath;
        this.lineNo = lineNo;
        this.fireCount = fireCount;
        this.srcType = srcType;
        this.workspaceId = workspaceId;
        this.condition = condition;
        this.watchers = watchers;
        this.args = args;
        this.type = type;
        this.logMessage = logMessage;
        this.rateLimit = Long.parseLong( args.get( "rate_limit" ) == null ? "0" : args.get( "rate_limit" ) );
        this.isCf = Utils.isCfClassFile( this.relPath );
    }


    @Override
    public String getId()
    {
        return id;
    }


    @Override
    public String getEstimatedClassRoot()
    {
        if( this.estimatedClassRoot == null )
        {
            this.estimatedClassRoot = asFullClassName();
        }
        return this.estimatedClassRoot;
    }


    @Override
    public boolean checkRate()
    {
        LOGGER.trace( "checkRate {} > {}", this.hitCount, this.fireCount );

        if( this.fireCount != -1 && this.hitCount >= this.fireCount )
        {
            return false;
        }

        if( this.lastHitTime == 0 )
        {
            this.lastHitTime = System.currentTimeMillis();
            return true;
        }
        else if( (System.currentTimeMillis() - this.lastHitTime) >= this.rateLimit )
        {
            this.lastHitTime = System.currentTimeMillis();
            return true;
        }
        return false;
    }


    @Override
    public String getCondition()
    {
        return this.condition;
    }


    @Override
    public List getWatchers()
    {
        return new ArrayList<>( this.watchers );
    }


    @Override
    public int getIntArgs( final String key, final int def )
    {
        final String arg = getArg( key );
        if( arg == null )
        {
            return def;
        }
        return Integer.parseInt( arg );
    }


    @Override
    public String getSrcType()
    {
        return this.srcType;
    }


    @Override
    public String getWorkspaceId()
    {
        return this.workspaceId;
    }


    @Override
    public boolean isCf()
    {
        return this.isCf;
    }


    @Override
    public void incrementHitCount()
    {
        this.hitCount += 1;
    }


    @Override
    public String getType()
    {
        return this.type;
    }


    @Override
    public String getLogMessage()
    {
        return this.logMessage;
    }


    private String asFullClassName()
    {
        return parseFullClassName( this.getRelPath(), this.getArg( "src_root" ) );
    }


    static String parseFullClassName( final String rawRelPath, final String srcRootArg )
    {
        if( rawRelPath.endsWith( ".cfm" ) || rawRelPath.endsWith( ".cfc" ) )
        {
            return "cfm";
        }

        if( rawRelPath.endsWith( ".jsp" ) )
        {
            return "jsp";
        }

        final int endIndex = rawRelPath.lastIndexOf( '.' );
        final String relPath;
        // users can install tracepoints in files that do not have extensions
        // this just handles it so we have a file/class name for them
        // even though they wont be installed by the agent
        if( endIndex == -1 )
        {
            relPath = rawRelPath;
        }
        else
        {
            relPath = rawRelPath.substring( 0, endIndex );
        }
        if( srcRootArg != null )
        {
            if( relPath.startsWith( srcRootArg ) )
            {
                return Utils.trim( relPath.substring( srcRootArg.length() ), "/" );
            }
        }
        else if( relPath.contains( "/src/main/" ) )
        {
            final String mainDir = relPath.substring( relPath.indexOf( "/src/main/" ) + 11 );
            final int i = mainDir.indexOf( '/' );
            return Utils.trim( mainDir.substring( i ), "/" );
        }
        else if( relPath.contains( "/src/test/" ) )
        {
            final String mainDir = relPath.substring( relPath.indexOf( "/src/test/" ) + 11 );
            final int i = mainDir.indexOf( '/' );
            return Utils.trim( mainDir.substring( i ), "/" );
        }
        final String trim = Utils.trim( relPath, "/" );
        // this is just to ensure the file name is never empty
        // this only happens on non class files such as '.gitignore'
        // rather then return empty name we return the raw path
        if( trim.isEmpty() )
        {
            return Utils.trim( rawRelPath, "/" );
        }
        return trim;
    }


    @Override
    public String getRelPath()
    {
        return relPath;
    }


    @Override
    public long getLineNo()
    {
        return lineNo;
    }


    @Override
    public Map getArgs()
    {
        return args;
    }


    @Override
    public String getArg( final String key )
    {
        return this.args.get( key );
    }


    @Override
    public String toString()
    {
        return "GrpcBreakpoint{" +
                "id='" + id + '\'' +
                ", relPath='" + relPath + '\'' +
                ", lineNo=" + lineNo +
                ", fireCount=" + fireCount +
                ", args=" + args +
                ", rateLimit=" + rateLimit +
                ", condition='" + condition + '\'' +
                ", watchers=" + watchers +
                ", srcType='" + srcType + '\'' +
                ", workspaceId='" + workspaceId + '\'' +
                ", isCf=" + isCf +
                ", estimatedClassRoot='" + estimatedClassRoot + '\'' +
                ", hitCount=" + hitCount +
                ", lastHitTime=" + lastHitTime +
                '}';
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy