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);
     }
   }
 }
  @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);
  }
  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;
  }