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

org.gridkit.lab.jvm.attach.PatternJvmMatcher Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
package org.gridkit.lab.jvm.attach;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternJvmMatcher implements JavaProcessMatcher, Serializable {        
    
	private static final long serialVersionUID = 20121106L;
	
	private final Map patterns = new LinkedHashMap();

    public void matchVmName(String pattern) {
    	matchProp(":name", pattern);
    }
    
    public void matchProp(String prop, String pattern) {
    	Pattern p = Pattern.compile(pattern);
    	patterns.put(prop, p);
    }

    public void matchPropExact(String prop, String pattern) {
    	matchProp(prop, Pattern.quote(pattern));
    }

    @Override
	public boolean evaluate(JavaProcessDetails proc) {
    	if (patterns.containsKey(":name")) {
    		if (!match(":name", proc.getDescription())) {
    			return false;
    		}
    	}
        
        Properties props = proc.getSystemProperties();
        if (props == null) {
        	return false;
        }
        
        for(String prop: patterns.keySet()) {
        	if (!prop.startsWith(":")) {
        		if (!match(prop, props.getProperty(prop))) {
        			return false;
        		}
        	}
        }
            
        return true;
    }
    
    private boolean match(String prop, String value) {
    	if (value == null) {
    		return false;
    	}
		Matcher matcher = patterns.get(prop).matcher(value);
		return matcher.matches();
	}


	@Override
    public String toString() {
        return String.format("%s%s", getClass().getSimpleName(), patterns.toString());
    }
}	




© 2015 - 2024 Weber Informatics LLC | Privacy Policy