protected void undeployJsr77(
      MBeanServer server, VFSDeploymentUnit unit, ServiceDeployment metaData) {
    List<ServiceMetaData> beans = metaData.getServices();
    if (beans != null && beans.isEmpty() == false) {
      ListIterator<ServiceMetaData> iter = beans.listIterator(beans.size());
      while (iter.hasPrevious()) {
        ObjectName name = iter.previous().getObjectName();
        try {
          // Destroy JSR-77 MBean
          MBean.destroy(server, name.toString());
          log.debug("Destroy MBean, name: " + name);
        } catch (Throwable e) {
          log.debug("Failed to remove remove JSR-77 MBean", e);
        }
      }
    }

    // Remove JSR-77 SAR-Module
    String moduleName = unit.getSimpleName();
    try {
      ServiceModule.destroy(server, moduleName);
      log.debug("Removed JSR-77 SAR: " + moduleName);
    } catch (Throwable e) {
      log.debug("Failed to remove JSR-77 SAR: " + moduleName);
    }
  }
  protected void deployJsr77(MBeanServer server, VFSDeploymentUnit unit, ServiceDeployment metaData)
      throws Throwable {
    ObjectName sarName = ServiceModule.create(server, unit.getSimpleName(), unit.getRoot().toURL());
    if (sarName != null) {
      log.debug("Created ServiceModule: " + sarName);
    }

    List<ServiceMetaData> beans = metaData.getServices();
    if (beans != null && beans.isEmpty() == false) {
      for (ServiceMetaData bean : beans) {
        ObjectName mbeanName = bean.getObjectName();
        // Create JSR-77 MBean
        MBean.create(server, sarName.toString(), mbeanName);
        log.debug("Create MBean, name: " + mbeanName + ", SAR Module: " + sarName);
      }
    }
  }
  @SuppressWarnings("unchecked")
  public void parse(VFSDeploymentUnit unit, Object dataObj) throws Exception {
    log.debug("Deploying ruby application: " + unit);
    log.info("UNIT.root: " + unit.getRoot());
    log.info("UNIT.root.uri: " + unit.getRoot().toURI());
    log.info("UNIT.root.uri.url: " + unit.getRoot().toURI().toURL());
    log.info("UNIT.root.uri.url.ext: " + unit.getRoot().toURI().toURL().toExternalForm());

    RubyApplicationMetaData appMetaData = unit.getAttachment(RubyApplicationMetaData.class);

    if (appMetaData == null) {
      log.debug("Configuring ruby application: " + unit);
      appMetaData = new RubyApplicationMetaData();
      appMetaData.setApplicationName(unit.getSimpleName());
      unit.addAttachment(RubyApplicationMetaData.class, appMetaData);
    } else {
      log.debug("Configuring pre-existing ruby application: " + unit + "\n  " + appMetaData);
    }

    Map<String, String> app = (Map<String, String>) dataObj;

    if (appMetaData.getRoot() == null) {
      String root = getOneOf(app, "root", "RAILS_ROOT", "RACK_ROOT");

      if (root != null && !root.trim().equals("")) {
        appMetaData.setRoot(root.trim());
      }
    }

    if (appMetaData.getEnvironmentName() == null) {
      String env = getOneOf(app, "env", "RAILS_ENV", "RACK_ENV");

      if (env != null && !env.trim().equals("")) {
        appMetaData.setEnvironmentName(env.trim());
      }
    }
    log.debug("Configured ruby application: " + unit + "\n  " + appMetaData);
  }