/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.huihoo.jfox.mx;

import java.io.File;
import java.net.URL;
import java.util.Properties;

import org.jfox.ejb.deploy.EJBDeployService;
import org.huihoo.jfox.system.State;

/**
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class EJBDeployServiceMX implements ServiceMX {

  private EJBDeployService deployService = null;

  public EJBDeployServiceMX() {
    deployService = new EJBDeployService();
  }

  public void mx_start(){
    try {
      deployService.init();
      deployService.start();
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }

  public void mx_stop(){
    try {
      deployService.stop();
      deployService.destroy();
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }

  public void mx_addDeployDirectory(String url) {
    deployService.addDeployDirectory(url);
  }

  public String[] mx_getDeployDirectories() {
    File[] directories = deployService.getDeployDirectories();
    String[] names = new String[directories.length];
    for(int i=0; i<directories.length; i++){
      names[i] = directories[i].getAbsoluteFile().getName();
    }
    return names;
  }

  public void mx_setSleepTime(long time) {
    deployService.setSleepTime(time);
  }

  public long mx_getSleepTime() {
    return deployService.getSleepTime();
  }

  public Properties getOperationDescriptions() {
    return null;
  }

  public Object getSource() {
    return deployService;
  }

  public String getMxDescription() {
    return "auto deploy the jars in the monitored deploy directories";
  }

  public String[] mx_getDeployedJars() {
    URL[] jarURLs = deployService.getDeployedJars();
    String[] jars = new String[jarURLs.length];
    for(int i=0; i<jarURLs.length; i++){
      jars[i] = jarURLs[i].toString();
    }
    return jars;
  }

  public State mx_getStat(){
    return deployService.getState();
  }

  public static void main(String[] args) {

  }
}