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

    ObjectName name = null;
    Object container = environment.getNamingResources().getContainer();
    if (container instanceof Server) {
      name =
          new ObjectName(
              domain + ":type=Environment" + ",resourcetype=Global,name=" + environment.getName());
    } 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=Environment"
                  + ",resourcetype=Context,path="
                  + path
                  + ",host="
                  + host.getName()
                  + ",name="
                  + environment.getName());
    } else if (container instanceof DefaultContext) {
      container = ((DefaultContext) container).getParent();
      if (container instanceof Host) {
        Host host = (Host) container;
        name =
            new ObjectName(
                domain
                    + ":type=Environment"
                    + ",resourcetype=HostDefaultContext,host="
                    + host.getName()
                    + ",name="
                    + environment.getName());
      } else if (container instanceof Engine) {
        name =
            new ObjectName(
                domain
                    + ":type=Environment"
                    + ",resourcetype=ServiceDefaultContext,name="
                    + environment.getName());
      }
    }

    return (name);
  }
  /**
   * 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);
        }
      }
    }
  }
  /** Set the specified environment entries in the naming context. */
  public void addEnvironment(ContextEnvironment env) {

    Object value = null;
    // Instantiating a new instance of the correct object type, and
    // initializing it.
    String type = env.getType();
    try {
      if (type.equals("java.lang.String")) {
        value = env.getValue();
      } else if (type.equals("java.lang.Byte")) {
        if (env.getValue() == null) {
          value = new Byte((byte) 0);
        } else {
          value = Byte.decode(env.getValue());
        }
      } else if (type.equals("java.lang.Short")) {
        if (env.getValue() == null) {
          value = new Short((short) 0);
        } else {
          value = Short.decode(env.getValue());
        }
      } else if (type.equals("java.lang.Integer")) {
        if (env.getValue() == null) {
          value = new Integer(0);
        } else {
          value = Integer.decode(env.getValue());
        }
      } else if (type.equals("java.lang.Long")) {
        if (env.getValue() == null) {
          value = new Long(0);
        } else {
          value = Long.decode(env.getValue());
        }
      } else if (type.equals("java.lang.Boolean")) {
        value = Boolean.valueOf(env.getValue());
      } else if (type.equals("java.lang.Double")) {
        if (env.getValue() == null) {
          value = new Double(0);
        } else {
          value = Double.valueOf(env.getValue());
        }
      } else if (type.equals("java.lang.Float")) {
        if (env.getValue() == null) {
          value = new Float(0);
        } else {
          value = Float.valueOf(env.getValue());
        }
      } else if (type.equals("java.lang.Character")) {
        if (env.getValue() == null) {
          value = new Character((char) 0);
        } else {
          if (env.getValue().length() == 1) {
            value = new Character(env.getValue().charAt(0));
          } else {
            throw new IllegalArgumentException();
          }
        }
      } else {
        log(sm.getString("naming.invalidEnvEntryType", env.getName()));
      }
    } catch (NumberFormatException e) {
      log(sm.getString("naming.invalidEnvEntryValue", env.getName()));
    } catch (IllegalArgumentException e) {
      log(sm.getString("naming.invalidEnvEntryValue", env.getName()));
    }

    // Binding the object to the appropriate name
    if (value != null) {
      try {
        if (debug >= 2) log("  Adding environment entry " + env.getName());
        createSubcontexts(envCtx, env.getName());
        envCtx.bind(env.getName(), value);
      } catch (NamingException e) {
        log(sm.getString("naming.invalidEnvEntryValue", e));
      }
    }
  }
  /**
   * 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);
        }
      }
    }
  }