package org.bench4q.master.infrastructure.faultolerence;

import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl;

public class BriefAgentFault implements FaultTolerance {
	private HighAvailablePool haPool;
	private Agent agent;

	private HighAvailablePool getHaPool() {
		return haPool;
	}

	private void setHaPool(HighAvailablePool haPool) {
		this.haPool = haPool;
	}

	private Agent getAgent() {
		return agent;
	}

	private void setAgent(Agent agent) {
		this.agent = agent;
	}

	BriefAgentFault(Agent agent) {
		this.setAgent(agent);
		this.setHaPool(ApplicationContextHelper.getContext().getBean(
				HighAvailablePoolImpl.class));
	}

	public void doTolerance() {
		if (isLiving()) {
			return;
		}
		synchronized (this.getHaPool()) {
			this.getHaPool().checkHeartBeat(this.getAgent());
		}
	}

	private boolean isLiving() {
		return this.getHaPool().queryAgentStatus(this.getAgent()) != null;
	}
}
