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

io.sealights.agents.plugin.BuildEndNotifier Maven / Gradle / Ivy

There is a newer version: 4.0.1125
Show newest version
package io.sealights.agents.plugin;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.apache.maven.execution.BuildFailure;
import org.apache.maven.execution.BuildSuccess;
import org.apache.maven.execution.BuildSummary;
import org.apache.maven.execution.ExecutionListener;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;

import io.sealights.onpremise.agents.java.agent.plugins.engine.lifecycle.BuildEndInfo;
import io.sealights.onpremise.agents.java.agent.plugins.engine.lifecycle.BuildEndListener;

public enum BuildEndNotifier implements InvocationHandler {
	INSTANCE;
	
    private static final String METHOD_NAME_OF_SESSION_ENDED = "sessionEnded";
    private MavenSession session;
    private ExecutionListener mavenExecutionListener;
    private BuildEndListener listener;
    private boolean isInitialized = false;
    
    public synchronized boolean initialize(MavenSession session, BuildEndListener listener) {
        if (isInitialized) {
        	return false;
        }
        
        this.session = session;
        this.listener = listener;
        MavenExecutionRequest request = session.getRequest();
        mavenExecutionListener = session.getRequest().getExecutionListener();
        Object instance = Proxy.newProxyInstance(
        		getClass().getClassLoader(), 
        		new Class[]{ExecutionListener.class},
                this);
        request.setExecutionListener((ExecutionListener) instance);
        isInitialized = true;
        return true;
    }

    public Object invoke(Object target, Method method, Object[] args) throws Throwable {
        if (isEndOfBuild(method)) {
            MavenProject project = session.getResult().getProject();
            BuildSummary buildSummary = session.getResult().getBuildSummary(project);
            BuildEndInfo buildInfo = new BuildEndInfo();
            buildInfo.setBuildDurationMSec(buildSummary.getTime());
            buildInfo.setSuccessful(buildSummary instanceof BuildSuccess);
            
            if (buildSummary instanceof BuildSuccess) {
            	buildInfo.setSuccessful(true);
            } 
            if (buildSummary instanceof BuildFailure) {
            	BuildFailure failure = (BuildFailure) buildSummary;
            	buildInfo.setFailureReason(failure.toString());
            }
            listener.notifyBuildEndInfo(buildInfo);
        }
        return method.invoke(mavenExecutionListener, args);
    }
    
    private boolean isEndOfBuild(Method method) {
        return method.getName().equals(METHOD_NAME_OF_SESSION_ENDED);
    }

 }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy