/** Create and initialize the JNDI naming context. */
  private void createNamingContext() throws NamingException {

    // Creating the comp subcontext
    if (container instanceof Server) {
      compCtx = namingContext;
      envCtx = namingContext;
    } else {
      compCtx = namingContext.createSubcontext("comp");
      envCtx = compCtx.createSubcontext("env");
    }

    int i;

    if (log.isDebugEnabled()) log.debug("Creating JNDI naming context");

    if (namingResources == null) {
      namingResources = new NamingResourcesImpl();
      namingResources.setContainer(container);
    }

    // Resource links
    ContextResourceLink[] resourceLinks = namingResources.findResourceLinks();
    for (i = 0; i < resourceLinks.length; i++) {
      addResourceLink(resourceLinks[i]);
    }

    // Resources
    ContextResource[] resources = namingResources.findResources();
    for (i = 0; i < resources.length; i++) {
      addResource(resources[i]);
    }

    // Resources Env
    ContextResourceEnvRef[] resourceEnvRefs = namingResources.findResourceEnvRefs();
    for (i = 0; i < resourceEnvRefs.length; i++) {
      addResourceEnvRef(resourceEnvRefs[i]);
    }

    // Environment entries
    ContextEnvironment[] contextEnvironments = namingResources.findEnvironments();
    for (i = 0; i < contextEnvironments.length; i++) {
      addEnvironment(contextEnvironments[i]);
    }

    // EJB references
    ContextEjb[] ejbs = namingResources.findEjbs();
    for (i = 0; i < ejbs.length; i++) {
      addEjb(ejbs[i]);
    }

    // WebServices references
    ContextService[] services = namingResources.findServices();
    for (i = 0; i < services.length; i++) {
      addService(services[i]);
    }

    // Binding a User Transaction reference
    if (container instanceof Context) {
      try {
        Reference ref = new TransactionRef();
        compCtx.bind("UserTransaction", ref);
        ContextTransaction transaction = namingResources.getTransaction();
        if (transaction != null) {
          Iterator<String> params = transaction.listProperties();
          while (params.hasNext()) {
            String paramName = params.next();
            String paramValue = (String) transaction.getProperty(paramName);
            StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
            ref.add(refAddr);
          }
        }
      } catch (NameAlreadyBoundException e) {
        // Ignore because UserTransaction was obviously
        // added via ResourceLink
      } catch (NamingException e) {
        logger.error(sm.getString("naming.bindFailed", e));
      }
    }

    // Binding the resources directory context
    if (container instanceof Context) {
      try {
        compCtx.bind("Resources", ((Context) container).getResources());
      } catch (NamingException e) {
        logger.error(sm.getString("naming.bindFailed", e));
      }
    }
  }
  /** Create and initialize the JNDI naming context. */
  private void createNamingContext() throws NamingException {

    // Creating the comp subcontext
    if (container instanceof Server) {
      compCtx = namingContext;
      envCtx = namingContext;
    } else {
      compCtx = namingContext.createSubcontext("comp");
      envCtx = compCtx.createSubcontext("env");
    }

    int i;

    if (debug >= 1) log("Creating JNDI naming context");

    if (namingResources == null) {
      namingResources = new NamingResources();
      namingResources.setContainer(container);
    }

    // Resource links
    ContextResourceLink[] resourceLinks = namingResources.findResourceLinks();
    for (i = 0; i < resourceLinks.length; i++) {
      addResourceLink(resourceLinks[i]);
    }

    // Resources
    ContextResource[] resources = namingResources.findResources();
    for (i = 0; i < resources.length; i++) {
      addResource(resources[i]);
    }

    // Resources Env
    String[] resourceEnvRefs = namingResources.findResourceEnvRefs();
    for (i = 0; i < resourceEnvRefs.length; i++) {
      String key = resourceEnvRefs[i];
      String type = namingResources.findResourceEnvRef(key);
      addResourceEnvRef(key, type);
    }

    // Environment entries
    ContextEnvironment[] contextEnvironments = namingResources.findEnvironments();
    for (i = 0; i < contextEnvironments.length; i++) {
      addEnvironment(contextEnvironments[i]);
    }

    // EJB references
    ContextEjb[] ejbs = namingResources.findEjbs();
    for (i = 0; i < ejbs.length; i++) {
      addEjb(ejbs[i]);
    }

    // Binding a User Transaction reference
    if (container instanceof Context) {
      try {
        Reference ref = new TransactionRef();
        compCtx.bind("UserTransaction", ref);
        addAdditionalParameters(namingResources, ref, "UserTransaction");
      } catch (NamingException e) {
        log(sm.getString("naming.bindFailed", e));
      }
    }

    // Binding the resources directory context
    if (container instanceof Context) {
      try {
        compCtx.bind("Resources", ((Container) container).getResources());
      } catch (NamingException e) {
        log(sm.getString("naming.bindFailed", e));
      }
    }
  }