/**
 * Copyright (c) 2008-2012 EBM WebSourcing, 2012-2013 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see <http://www.gnu.org/licenses/>
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.engine.clock;

import java.util.logging.Level;

import javax.jbi.management.DeploymentException;

import org.ow2.petals.component.framework.se.AbstractServiceEngine;

public class Clock extends AbstractServiceEngine {

    private static final String serviceUnit = "serviceUnit";

    private String serviceUnitRootPath;

    private ClockService service;

    private boolean suUndeployed = true;

    public ClockService getService() {
        return this.service;
    }

    @Override
    public void doInit() {
        this.getLogger().info("init Clock service");
        this.service = new ClockService(this.getLogger());
        // serviceUnitRootPath = context.getInstallRoot() + File.separator +
        // "serviceUnit";
    }

    @Override
    public void doStart() {
        this.getLogger().info("start Clock service");
        // if (suUndeployed) {
        // deploySU();
        // }
        // startSU();
    }

    @Override
    public void doStop() {
        this.getLogger().info("stop Clock service");
        // stopSU();
    }

    @Override
    public void doShutdown() {
        this.getLogger().info("shutdown Clock service");
        // undeploySU();
    }

    private void deploySU() {
        try {
            this.getLogger().info("deploy embedded ServiceUnit");
            this.getServiceUnitManager().deploy(serviceUnit, this.serviceUnitRootPath);
            this.getServiceUnitManager().init(serviceUnit, this.serviceUnitRootPath);
            this.suUndeployed = false;
        } catch (DeploymentException e) {
            this.getLogger().log(Level.SEVERE, "Error while deploying the embedded ServiceUnit", e);
        }
    }

    private void undeploySU() {
        try {
            this.getLogger().info("undeploy embedded ServiceUnit");
            this.getServiceUnitManager().undeploy(serviceUnit, this.serviceUnitRootPath);
            this.suUndeployed = true;
        } catch (DeploymentException e) {
            this.getLogger().log(Level.SEVERE, "Error while undeploying the embedded ServiceUnit", e);
        }
    }

    private void startSU() {
        try {
            this.getLogger().info("start embedded ServiceUnit");
            this.getServiceUnitManager().start(serviceUnit);
        } catch (DeploymentException e) {
            this.getLogger().log(Level.SEVERE,
                    "Error while starting the embedded ServiceUnit, undeploy it", e);
            this.undeploySU();
        }
    }

    private void stopSU() {
        try {
            this.getLogger().info("stop embedded ServiceUnit");
            this.getServiceUnitManager().stop(serviceUnit);
        } catch (DeploymentException e) {
            this.getLogger().log(Level.SEVERE,
                    "Error while stopping the embedded ServiceUnit, undeploy it", e);
            this.undeploySU();
        }
    }

}
