package foodplanet.management;

import java.util.Date;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource(objectName="foodplanet.http.listener:name=generic", description="Management for Http")
public class HttpAccessMbeanImpl implements HttpAccessMbean{
    private long reqCount;
    private boolean started;
    private String stoppedView;
    private Date dateStarted;
    private Date dateStopped;
    private Date lastRequestDate;
    
    public HttpAccessMbeanImpl() {
    }   
    
    public void incRequestCount() {
        lastRequestDate = new Date();
        reqCount++;
    }
    
    @ManagedAttribute
    public long getRequestCount() {
        return reqCount;
    }

    @ManagedOperation
    public void start() {
        dateStarted = new Date();
        started = true;
    }

    @ManagedOperation
    public void stop() {
        dateStopped = new Date();
        started = false;
    }

    @ManagedAttribute
    public boolean isStarted() {
        return started;
    }
    
    @ManagedAttribute
    public void setStoppedView(String viewName) {
        this.stoppedView = viewName;
    }
    
    @ManagedAttribute
    public String getStoppedView() {
        return this.stoppedView;
    }

    public String getDateStarted() {
        return (dateStarted != null) ? dateStarted.toString() : "UNKNOWN";
    }

    public String getDateStopped() {
         return (dateStopped != null) ? dateStopped.toString() : "UNKNOWN";
    }

    public String getLastRequestDate() {
     return (lastRequestDate != null) ? lastRequestDate.toString() : "UNKNOWN";
    }
}
