/**
   * Create an <code>ObjectName</code> for this <code>ContextResource</code> object.
   *
   * @param resource The resource
   * @return ObjectName The object name
   * @exception MalformedObjectNameException if a name cannot be created
   */
  protected ObjectName createObjectName(ContextResource resource)
      throws MalformedObjectNameException {

    String domain = null;
    if (container instanceof StandardServer) {
      domain = ((StandardServer) container).getDomain();
    } else if (container instanceof ContainerBase) {
      domain = ((ContainerBase) container).getDomain();
    }
    if (domain == null) {
      domain = "Catalina";
    }

    ObjectName name = null;
    String quotedResourceName = ObjectName.quote(resource.getName());
    if (container instanceof Server) {
      name =
          new ObjectName(
              domain
                  + ":type=DataSource"
                  + ",class="
                  + resource.getType()
                  + ",name="
                  + quotedResourceName);
    } else if (container instanceof Context) {
      String contextName = ((Context) container).getName();
      if (!contextName.startsWith("/")) contextName = "/" + contextName;
      Host host = (Host) ((Context) container).getParent();
      name =
          new ObjectName(
              domain
                  + ":type=DataSource"
                  + ",host="
                  + host.getName()
                  + ",context="
                  + contextName
                  + ",class="
                  + resource.getType()
                  + ",name="
                  + quotedResourceName);
    }

    return (name);
  }
  public void addContextResource(Context context, List resourceList, boolean contextBound) {
    NamingResourcesImpl namingResources = context.getNamingResources();
    ContextResource[] resources = namingResources.findResources();
    for (int i = 0; i < resources.length; i++) {
      ContextResource contextResource = resources[i];
      ApplicationResource resource = new ApplicationResource();

      logger.info("reading resource: " + contextResource.getName());
      resource.setApplicationName(context.getName());
      resource.setName(contextResource.getName());
      resource.setType(contextResource.getType());
      resource.setScope(contextResource.getScope());
      resource.setAuth(contextResource.getAuth());
      resource.setDescription(contextResource.getDescription());

      // lookupResource(resource, contextBound, false);
      resourceList.add(resource);
    }
  }
  /**
   * Set the specified resources in the naming context.
   *
   * @param resource the resource descriptor
   */
  public void addResource(ContextResource resource) {

    // Create a reference to the resource.
    Reference ref =
        new ResourceRef(
            resource.getType(),
            resource.getDescription(),
            resource.getScope(),
            resource.getAuth(),
            resource.getSingleton());
    // Adding the additional parameters, if any
    Iterator<String> params = resource.listProperties();
    while (params.hasNext()) {
      String paramName = params.next();
      String paramValue = (String) resource.getProperty(paramName);
      StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
      ref.add(refAddr);
    }
    try {
      if (logger.isDebugEnabled()) {
        logger.debug("  Adding resource ref " + resource.getName() + "  " + ref);
      }
      createSubcontexts(envCtx, resource.getName());
      envCtx.bind(resource.getName(), ref);
    } catch (NamingException e) {
      logger.error(sm.getString("naming.bindFailed", e));
    }

    if ("javax.sql.DataSource".equals(ref.getClassName()) && resource.getSingleton()) {
      try {
        ObjectName on = createObjectName(resource);
        Object actualResource = envCtx.lookup(resource.getName());
        Registry.getRegistry(null, null).registerComponent(actualResource, on, null);
        objectNames.put(resource.getName(), on);
      } catch (Exception e) {
        logger.warn(sm.getString("naming.jmxRegistrationFailed", e));
      }
    }
  }
  /**
   * Process a property change on the naming resources, by making the corresponding addition or
   * removal to the associated JNDI context.
   *
   * @param name Property name of the change to be processed
   * @param oldValue The old value (or <code>null</code> if adding)
   * @param newValue The new value (or <code>null</code> if removing)
   */
  private void processGlobalResourcesChange(String name, Object oldValue, Object newValue) {

    if (name.equals("ejb")) {
      if (oldValue != null) {
        ContextEjb ejb = (ContextEjb) oldValue;
        if (ejb.getName() != null) {
          removeEjb(ejb.getName());
        }
      }
      if (newValue != null) {
        ContextEjb ejb = (ContextEjb) newValue;
        if (ejb.getName() != null) {
          addEjb(ejb);
        }
      }
    } else if (name.equals("environment")) {
      if (oldValue != null) {
        ContextEnvironment env = (ContextEnvironment) oldValue;
        if (env.getName() != null) {
          removeEnvironment(env.getName());
        }
      }
      if (newValue != null) {
        ContextEnvironment env = (ContextEnvironment) newValue;
        if (env.getName() != null) {
          addEnvironment(env);
        }
      }
    } else if (name.equals("localEjb")) {
      if (oldValue != null) {
        ContextLocalEjb ejb = (ContextLocalEjb) oldValue;
        if (ejb.getName() != null) {
          removeLocalEjb(ejb.getName());
        }
      }
      if (newValue != null) {
        ContextLocalEjb ejb = (ContextLocalEjb) newValue;
        if (ejb.getName() != null) {
          addLocalEjb(ejb);
        }
      }
    } else if (name.equals("resource")) {
      if (oldValue != null) {
        ContextResource resource = (ContextResource) oldValue;
        if (resource.getName() != null) {
          removeResource(resource.getName());
        }
      }
      if (newValue != null) {
        ContextResource resource = (ContextResource) newValue;
        if (resource.getName() != null) {
          addResource(resource);
        }
      }
    } else if (name.equals("resourceEnvRef")) {
      if (oldValue != null) {
        ContextResourceEnvRef resourceEnvRef = (ContextResourceEnvRef) oldValue;
        if (resourceEnvRef.getName() != null) {
          removeResourceEnvRef(resourceEnvRef.getName());
        }
      }
      if (newValue != null) {
        ContextResourceEnvRef resourceEnvRef = (ContextResourceEnvRef) newValue;
        if (resourceEnvRef.getName() != null) {
          addResourceEnvRef(resourceEnvRef);
        }
      }
    } else if (name.equals("resourceLink")) {
      if (oldValue != null) {
        ContextResourceLink rl = (ContextResourceLink) oldValue;
        if (rl.getName() != null) {
          removeResourceLink(rl.getName());
        }
      }
      if (newValue != null) {
        ContextResourceLink rl = (ContextResourceLink) newValue;
        if (rl.getName() != null) {
          addResourceLink(rl);
        }
      }
    } else if (name.equals("service")) {
      if (oldValue != null) {
        ContextService service = (ContextService) oldValue;
        if (service.getName() != null) {
          removeService(service.getName());
        }
      }
      if (newValue != null) {
        ContextService service = (ContextService) newValue;
        if (service.getName() != null) {
          addService(service);
        }
      }
    }
  }