package integration.jboss;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JBossSpringAdapter implements JBossSpringAdapterMBean {
    private static Log log = LogFactory.getLog(JBossSpringAdapter.class);
    private boolean created;
    private boolean started;
    private String springConfigPath;
    private long errorCount;
    private ClassPathXmlApplicationContext spring;
    
    public boolean isStarted() {
        return started;
    }
    
    public void setSpringConfigPath(String path){
        springConfigPath = path;
    }
    public String getSpringConfigPath(){
        return springConfigPath;
    }
    
    public long getErrorCount() {
        return errorCount;
    }
    
    public boolean isSpringLoaded() {
        return (spring != null) ? spring.isActive() : false;
    }
    
    public String[] getSpringComponents(){
        return (spring != null) ? spring.getBeanDefinitionNames() : null;
    }
    
    public long getSpringComponentCount() {
        return (spring != null) ? spring.getBeanDefinitionCount() : 0;
    }

    public void create(){
        log.info("**** CYCLE: CREATE ****");
        if(!created){
            try{
                spring = new ClassPathXmlApplicationContext(springConfigPath);
                spring.registerShutdownHook();
                created  = true;
            }catch(Exception ex){
                errorCount++;
                created=false;
                log.error("Unable to start Spring Context", ex);
            }
        }
    }

    public void destroy() {
        log.info("**** CYCLE: DESTROY ****");
        if(created){
            try{
                stop();
                if(spring.isActive()){
                    spring.close();
                    spring.destroy();
                    spring = null;
                }
                created = false;
            }catch(Throwable ex){
                log.error(" Failed to destroy component. ", ex);
            }
        }
    }

    public void start() throws Exception {
         log.info("**** CYCLE: START ****");
        if(!started){
            try{
                if(!spring.isActive()){
                    spring.refresh();
                    spring.start();
                }
                started = true;
                log.info("**** Spring context started OK.****");
            }catch(Throwable ex){
                errorCount++;
                started = false;
                log.error("**** Failed to start Spring context:", ex);
            }
        }
    }

    public void stop() {
         log.info("**** CYCLE: STOP ****");
        if(started){
            try{
                spring.close();
                started = false;
            log.info("**** Spring context stopped OK.****");
            }catch(Throwable ex){
                errorCount++;
                started = false;
                log.error("**** Failed to stop Spring context:", ex);
            }
        }
    }
}
