Exemplo n.º 1
0
  /**
   * Create an <code>ObjectName</code> for this <code>ContextResource</code> object.
   *
   * @param domain Domain in which this name is to be created
   * @param resource The ContextResource to be named
   * @exception MalformedObjectNameException if a name cannot be created
   */
  public static ObjectName createObjectName(String domain, ContextResource resource)
      throws MalformedObjectNameException {

    ObjectName name = null;
    String encodedResourceName = encodeStr(resource.getName());
    Object container = resource.getNamingResources().getContainer();
    if (container instanceof Server) {
      name =
          new ObjectName(
              domain
                  + ":type=Resource"
                  + ",resourcetype=Global,class="
                  + resource.getType()
                  + ",name="
                  + encodedResourceName);
    } else if (container instanceof Context) {
      String path = ((Context) container).getPath();
      if (path.length() < 1) path = "/";
      Host host = (Host) ((Context) container).getParent();
      name =
          new ObjectName(
              domain
                  + ":type=Resource"
                  + ",resourcetype=Context,path="
                  + path
                  + ",host="
                  + host.getName()
                  + ",class="
                  + resource.getType()
                  + ",name="
                  + encodedResourceName);
    } else if (container instanceof DefaultContext) {
      container = ((DefaultContext) container).getParent();
      if (container instanceof Host) {
        Host host = (Host) container;
        name =
            new ObjectName(
                domain
                    + ":type=Resource"
                    + ",resourcetype=HostDefaultContext,host="
                    + host.getName()
                    + ",class="
                    + resource.getType()
                    + ",name="
                    + encodedResourceName);
      } else if (container instanceof Engine) {
        name =
            new ObjectName(
                domain
                    + ":type=Resource"
                    + ",resourcetype=ServiceDefaultContext,class="
                    + resource.getType()
                    + ",name="
                    + encodedResourceName);
      }
    }

    return (name);
  }
  /** Set the specified resources in the naming context. */
  public void addResource(ContextResource resource) {

    // Create a reference to the resource.
    Reference ref =
        new ResourceRef(
            resource.getType(), resource.getDescription(),
            resource.getScope(), resource.getAuth());
    // Adding the additional parameters, if any
    addAdditionalParameters(resource.getNamingResources(), ref, resource.getName());
    try {
      if (debug >= 2) {
        log("  Adding resource ref " + resource.getName());
        log("  " + ref);
      }
      createSubcontexts(envCtx, resource.getName());
      envCtx.bind(resource.getName(), ref);
    } catch (NamingException e) {
      log(sm.getString("naming.bindFailed", e));
    }
  }
  /**
   * Process a property change on the global naming resources, by making the corresponding addition
   * or removal to OpenEJB.
   *
   * @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) {

    // NOTE - It seems that the Context for global JNDI resources
    // is left in read-write mode, so we do not have to change it here

    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);
        }
      }
    }
  }
  /**
   * Process a property change on the global 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) {

    // NOTE - It seems that the Context for global JNDI resources
    // is left in read-write mode, so we do not have to change it here

    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) {
        String update = (String) oldValue;
        int colon = update.indexOf(':');
        removeResourceEnvRef(update.substring(0, colon));
      }
      if (newValue != null) {
        String update = (String) newValue;
        int colon = update.indexOf(':');
        addResourceEnvRef(update.substring(0, colon), update.substring(colon + 1));
      }
    } 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("resourceParams")) {
      String resourceParamsName = null;
      ResourceParams rp = null;
      if (oldValue != null) {
        rp = (ResourceParams) oldValue;
      }
      if (newValue != null) {
        rp = (ResourceParams) newValue;
      }
      if (rp != null) {
        resourceParamsName = rp.getName();
      }
      if (resourceParamsName != null) {
        ContextEjb ejb = namingResources.findEjb(resourceParamsName);
        if (ejb != null) {
          removeEjb(resourceParamsName);
          addEjb(ejb);
        }
        ContextResource resource = namingResources.findResource(resourceParamsName);
        if (resource != null) {
          removeResource(resourceParamsName);
          addResource(resource);
        }
        String resourceEnvRefValue = namingResources.findResourceEnvRef(resourceParamsName);
        if (resourceEnvRefValue != null) {
          removeResourceEnvRef(resourceParamsName);
          addResourceEnvRef(resourceParamsName, resourceEnvRefValue);
        }
        ContextResourceLink resourceLink = namingResources.findResourceLink(resourceParamsName);
        if (resourceLink != null) {
          removeResourceLink(resourceParamsName);
          addResourceLink(resourceLink);
        }
      }
    }
  }