private void linkServiceRefs(
     DeploymentUnit unit, ServiceReferencesMetaData serviceRefs, Context envCtx)
     throws NamingException {
   if (unit instanceof VFSDeploymentUnit) {
     VFSDeploymentUnit vfsUnit = (VFSDeploymentUnit) unit;
     ClassLoader loader = unit.getClassLoader();
     UnifiedVirtualFile vfsRoot = new VirtualFileAdaptor(vfsUnit.getRoot());
     for (ServiceReferenceMetaData sref : serviceRefs) {
       String refName = sref.getServiceRefName();
       new ServiceReferenceHandler().bindServiceRef(envCtx, refName, vfsRoot, loader, sref);
     }
   }
 }
  public void deploy(VFSDeploymentUnit unit) throws DeploymentException {
    VirtualFile file = unit.getMetaDataFile("ruby.yml");

    if (file != null) {
      try {
        RubyRuntimeMetaData runtimeMetaData = unit.getAttachment(RubyRuntimeMetaData.class);
        if (runtimeMetaData != null) {
          parse(unit, file, runtimeMetaData);
        }
      } catch (Exception e) {
        throw new DeploymentException(e);
      }
    }
  }
  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);
      }
    }
  }
  /**
   * A template pattern implementation of the deploy() method. This method calls the {@link
   * #performDeploy(WebApplication, String, WebDescriptorParser) performDeploy()} method to perform
   * the container specific deployment steps and registers the returned WebApplication in the
   * deployment map. The steps performed are:
   *
   * <p>ClassLoader appClassLoader = thread.getContextClassLoader(); URLClassLoader warLoader =
   * URLClassLoader.newInstance(empty, appClassLoader); thread.setContextClassLoader(warLoader);
   * WebDescriptorParser webAppParser = ...; WebMetaData metaData = di.metaData; // Create JACC
   * permissions, contextID, etc. ... WebApplication warInfo = new WebApplication(metaData);
   * performDeploy(warInfo, warUrl, webAppParser); deploymentMap.put(warUrl, warInfo);
   * thread.setContextClassLoader(appClassLoader);
   *
   * <p>The subclass performDeploy() implementation needs to invoke processEnc(loader, warInfo) to
   * have the JNDI java:comp/env namespace setup before any web app component can access this
   * namespace.
   *
   * <p>Also, an MBean for each servlet deployed should be created and its JMX ObjectName placed
   * into the DeploymentInfo.mbeans list so that the JSR77 layer can create the approriate model
   * view. The servlet MBean needs to provide access to the min, max and total time in milliseconds.
   * Expose this information via MinServiceTime, MaxServiceTime and TotalServiceTime attributes to
   * integrate seemlessly with the JSR77 factory layer.
   *
   * @param unit The deployment info that contains the context-root element value from the J2EE
   *     application/module/web application.xml descriptor. This may be null if war was is not being
   *     deployed as part of an enterprise application. It also contains the URL of the web
   *     application war.
   */
  public synchronized WebApplication start(DeploymentUnit unit, JBossWebMetaData metaData)
      throws Exception {
    Thread thread = Thread.currentThread();
    ClassLoader appClassLoader = thread.getContextClassLoader();
    WebApplication webApp = null;
    try {
      // Create a classloader for the war to ensure a unique ENC
      ClassLoader warLoader = unit.getClassLoader();
      thread.setContextClassLoader(warLoader);
      String webContext = metaData.getContextRoot();

      // Get the war URL
      URL warUrl = unit.getAttachment("org.jboss.web.expandedWarURL", URL.class);
      if (warUrl == null && unit instanceof VFSDeploymentUnit) {
        VFSDeploymentUnit vdu = VFSDeploymentUnit.class.cast(unit);
        warUrl = VFSUtils.getRealURL(vdu.getRoot());
      }

      // Dynamic WebMetaData deployments might not provide an URL
      // We use the DEploymentUnit name as identifier instead.
      // The JAXWS Endpoint API for example does this.
      String warURLString = (warUrl != null ? warUrl.toExternalForm() : unit.getName());

      // Strip any jar: url syntax. This should be be handled by the vfs
      if (warURLString.startsWith("jar:"))
        warURLString = warURLString.substring(4, warURLString.length() - 2);

      log.debug("webContext: " + webContext);
      log.debug("warURL: " + warURLString);

      // Register the permissions with the JACC layer
      String contextID = metaData.getJaccContextID();
      if (contextID == null) contextID = unit.getSimpleName();
      metaData.setJaccContextID(contextID);

      webApp = new WebApplication(metaData);
      webApp.setClassLoader(warLoader);
      webApp.setDeploymentUnit(unit);
      performDeploy(webApp, warURLString);
    } finally {
      thread.setContextClassLoader(appClassLoader);
    }
    return webApp;
  }
 @Override
 public void deploy(VFSDeploymentUnit unit, T metaData) throws DeploymentException {
   if (isIntegrationDeployment(unit)) {
     try {
       VirtualFile integration = VFS.getRoot(integrationURL);
       unit.addClassPath(integration);
     } catch (IOException e) {
       throw DeploymentException.rethrowAsDeploymentException("Error adding integration path.", e);
     }
   }
 }
  @SuppressWarnings("unchecked")
  public void deploy(VFSDeploymentUnit unit) throws DeploymentException {
    TorqueBoxMetaData globalMetaData = unit.getAttachment(TorqueBoxMetaData.class);

    log.debug("Global torquebox.yml: " + globalMetaData);

    Object data = null;

    if (globalMetaData != null) {
      data = globalMetaData.getSection(getSectionName());
      log.debug("Global data section for " + getSectionName() + ": " + data);
    }

    if (data == null && isSupportsStandalone()) {
      VirtualFile metaDataFile = getMetaDataFile(unit, getFileName());

      if ((metaDataFile != null) && metaDataFile.exists()) {
        log.warn("Usage of " + getFileName() + " is deprecated.  Please use torquebox.yml.");
        InputStream in = null;
        try {
          in = metaDataFile.openStream();
          Yaml yaml = new Yaml();
          data = (Map<String, ?>) yaml.load(in);
        } catch (YAMLException e) {
          log.warn("Error parsing: " + metaDataFile + ": " + e.getMessage());
          data = null;
        } catch (IOException e) {
          throw new DeploymentException(e);
        } finally {
          if (in != null) {
            try {
              in.close();
            } catch (IOException e) {
              throw new DeploymentException(e);
            }
          }
        }
      }
    }

    if (data == null) {
      return;
    }

    try {
      parse(unit, data);
    } catch (DeploymentException e) {
      throw e;
    } catch (Exception e) {
      throw new DeploymentException(e);
    }
  }
  @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);
  }