Пример #1
0
  /**
   * Constructor taking fields in the <i>fieldName=fieldValue</i> format.
   *
   * @param fields String array with each element containing a field name and value. If this array
   *     is null or empty, then the default constructor will be executed. Null strings or empty
   *     strings will be ignored.
   *     <p>All field values should be Strings. If the field values are not Strings, the programmer
   *     will have to reset or convert these fields correctly.
   *     <p>Note: Each string should be of the form <i>fieldName=fieldValue</i>. The field name ends
   *     at the first {@code =} character; for example if the String is {@code a=b=c} then the field
   *     name is {@code a} and its value is {@code b=c}.
   * @exception RuntimeOperationsException for illegal value for field Names or field Values. The
   *     field must contain an "=". "=fieldValue", "fieldName", and "fieldValue" are illegal.
   *     FieldName cannot be null. "fieldName=" will cause the value to be null. If the descriptor
   *     construction fails for any reason, this exception will be thrown.
   */
  public DescriptorSupport(String... fields) {
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
      MODELMBEAN_LOGGER.logp(
          Level.FINEST,
          DescriptorSupport.class.getName(),
          "Descriptor(String... fields)",
          "Constructor");
    }
    init(null);
    if ((fields == null) || (fields.length == 0)) return;

    init(null);

    for (int i = 0; i < fields.length; i++) {
      if ((fields[i] == null) || (fields[i].equals(""))) {
        continue;
      }
      int eq_separator = fields[i].indexOf("=");
      if (eq_separator < 0) {
        // illegal if no = or is first character
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
          MODELMBEAN_LOGGER.logp(
              Level.FINEST,
              DescriptorSupport.class.getName(),
              "Descriptor(String... fields)",
              "Illegal arguments: field does not have " + "'=' as a name and value separator");
        }
        final String msg = "Field in invalid format: no equals sign";
        final RuntimeException iae = new IllegalArgumentException(msg);
        throw new RuntimeOperationsException(iae, msg);
      }

      String fieldName = fields[i].substring(0, eq_separator);
      String fieldValue = null;
      if (eq_separator < fields[i].length()) {
        // = is not in last character
        fieldValue = fields[i].substring(eq_separator + 1);
      }

      if (fieldName.equals("")) {
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
          MODELMBEAN_LOGGER.logp(
              Level.FINEST,
              DescriptorSupport.class.getName(),
              "Descriptor(String... fields)",
              "Illegal arguments: fieldName is empty");
        }

        final String msg = "Field in invalid format: no fieldName";
        final RuntimeException iae = new IllegalArgumentException(msg);
        throw new RuntimeOperationsException(iae, msg);
      }

      setField(fieldName, fieldValue);
    }
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
      MODELMBEAN_LOGGER.logp(
          Level.FINEST, DescriptorSupport.class.getName(), "Descriptor(String... fields)", "Exit");
    }
  }
Пример #2
0
  public void testCopyConstructor() throws Exception {
    DescriptorSupport descriptor = new DescriptorSupport((DescriptorSupport) null);
    assertTrue("Should be empty", descriptor.getFields().length == 0);
    assertFalse("Should not be valid", descriptor.isValid());

    descriptor = new DescriptorSupport(descriptor);
    assertTrue("Should be empty", descriptor.getFields().length == 0);
    assertFalse("Should not be valid", descriptor.isValid());

    descriptor.setField("name", "testName");
    descriptor.setField("descriptorType", "testType");
    descriptor = new DescriptorSupport(descriptor);
    assertTrue("Should be two fields", descriptor.getFields().length == 2);
    assertEquals("testName", descriptor.getFieldValue("name"));
    assertEquals("testType", descriptor.getFieldValue("descriptorType"));
    assertTrue("Should be valid", descriptor.isValid());
  }
Пример #3
0
  public void testSetFieldValue() throws Exception {
    String[] fields = new String[] {"name=testName", "descriptorType=testType"};
    DescriptorSupport descriptor = new DescriptorSupport(fields);
    assertEquals("testName", descriptor.getFieldValue("name"));
    descriptor.setField("name", "newName");
    assertEquals("newName", descriptor.getFieldValue("name"));
    descriptor.setField("NAME", "newNAME");
    assertEquals("newNAME", descriptor.getFieldValue("name"));

    boolean caught = false;
    try {
      descriptor.setField(null, "null");
    } catch (RuntimeOperationsException e) {
      caught = true;
    }
    assertTrue("Expected an exception for null name", caught);

    caught = false;
    try {
      descriptor.setField("", "empty");
    } catch (RuntimeOperationsException e) {
      caught = true;
    }
    assertTrue("Expected an exception for empty name", caught);

    // This is legal?
    descriptor.setField(" ", "blank");
    assertEquals("blank", descriptor.getFieldValue(" "));

    descriptor.setField("newField", "newValue");
    assertEquals("newValue", descriptor.getFieldValue("newField"));
  }
Пример #4
0
  public synchronized void setFields(String[] fieldNames, Object[] fieldValues)
      throws RuntimeOperationsException {

    if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
      MODELMBEAN_LOGGER.logp(
          Level.FINEST,
          DescriptorSupport.class.getName(),
          "setFields(fieldNames,fieldValues)",
          "Entry");
    }

    if ((fieldNames == null)
        || (fieldValues == null)
        || (fieldNames.length != fieldValues.length)) {
      if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
        MODELMBEAN_LOGGER.logp(
            Level.FINEST,
            DescriptorSupport.class.getName(),
            "setFields(fieldNames,fieldValues)",
            "Illegal arguments");
      }

      final String msg = "fieldNames and fieldValues are null or invalid";
      final RuntimeException iae = new IllegalArgumentException(msg);
      throw new RuntimeOperationsException(iae, msg);
    }

    for (int i = 0; i < fieldNames.length; i++) {
      if ((fieldNames[i] == null) || (fieldNames[i].equals(""))) {
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
          MODELMBEAN_LOGGER.logp(
              Level.FINEST,
              DescriptorSupport.class.getName(),
              "setFields(fieldNames,fieldValues)",
              "Null field name encountered at element " + i);
        }
        final String msg = "fieldNames is null or invalid";
        final RuntimeException iae = new IllegalArgumentException(msg);
        throw new RuntimeOperationsException(iae, msg);
      }
      setField(fieldNames[i], fieldValues[i]);
    }
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
      MODELMBEAN_LOGGER.logp(
          Level.FINEST,
          DescriptorSupport.class.getName(),
          "setFields(fieldNames,fieldValues)",
          "Exit");
    }
  }
Пример #5
0
  /**
   * Constructor taking field names and field values. Neither array can be null.
   *
   * @param fieldNames String array of field names. No elements of this array can be null.
   * @param fieldValues Object array of the corresponding field values. Elements of the array can be
   *     null. The <code>fieldValue</code> must be valid for the <code>fieldName</code> (as defined
   *     in method {@link #isValid isValid})
   *     <p>Note: array sizes of parameters should match. If both arrays are empty, then an empty
   *     descriptor is created.
   * @exception RuntimeOperationsException for illegal value for field Names or field Values. The
   *     array lengths must be equal. If the descriptor construction fails for any reason, this
   *     exception will be thrown.
   */
  public DescriptorSupport(String[] fieldNames, Object[] fieldValues)
      throws RuntimeOperationsException {
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
      MODELMBEAN_LOGGER.logp(
          Level.FINEST,
          DescriptorSupport.class.getName(),
          "Descriptor(fieldNames,fieldObjects)",
          "Constructor");
    }

    if ((fieldNames == null)
        || (fieldValues == null)
        || (fieldNames.length != fieldValues.length)) {
      if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
        MODELMBEAN_LOGGER.logp(
            Level.FINEST,
            DescriptorSupport.class.getName(),
            "Descriptor(fieldNames,fieldObjects)",
            "Illegal arguments");
      }

      final String msg = "Null or invalid fieldNames or fieldValues";
      final RuntimeException iae = new IllegalArgumentException(msg);
      throw new RuntimeOperationsException(iae, msg);
    }

    /* populate internal structure with fields */
    init(null);
    for (int i = 0; i < fieldNames.length; i++) {
      // setField will throw an exception if a fieldName is be null.
      // the fieldName and fieldValue will be validated in setField.
      setField(fieldNames[i], fieldValues[i]);
    }
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
      MODELMBEAN_LOGGER.logp(
          Level.FINEST,
          DescriptorSupport.class.getName(),
          "Descriptor(fieldNames,fieldObjects)",
          "Exit");
    }
  }
Пример #6
0
  private void validate(String[] names, Object[] values, boolean valid) {
    DescriptorSupport descriptor = null;
    RuntimeOperationsException caught = null;
    boolean descriptorValid = false;
    try {
      descriptor = new DescriptorSupport(names, values);
      descriptorValid = descriptor.isValid();
    } catch (RuntimeOperationsException e) {
      caught = e;
    }
    if (valid && caught != null) throw caught;
    assertEquals(
        "Expected "
            + valid
            + " for new Descriptor(String[], String[]) names="
            + Arrays.asList(names)
            + " values="
            + Arrays.asList(values),
        valid,
        descriptorValid);

    caught = null;
    descriptorValid = false;
    try {
      String[] fields = new String[names.length];
      for (int i = 0; i < fields.length; i++) {
        if (values[i] == null) fields[i] = names[i] + "=";
        else fields[i] = names[i] + "=" + values[i].toString();
      }
      descriptor = new DescriptorSupport(names, values);
      descriptorValid = descriptor.isValid();
    } catch (RuntimeOperationsException e) {
      caught = e;
    }
    if (valid && caught != null) throw caught;
    assertEquals(
        "Expected "
            + valid
            + " for new Descriptor(String[], String[]) names="
            + Arrays.asList(names)
            + " values="
            + Arrays.asList(values),
        valid,
        descriptorValid);

    caught = null;
    descriptorValid = false;
    try {
      descriptor = new DescriptorSupport();
      for (int i = 0; i < names.length; i++) descriptor.setField(names[i], values[i]);
      descriptorValid = descriptor.isValid();
    } catch (RuntimeOperationsException e) {
      caught = e;
    }
    if (valid && caught != null) throw caught;
    assertEquals(
        "Expected "
            + valid
            + " for new Descriptor(String[], String[]) names="
            + Arrays.asList(names)
            + " values="
            + Arrays.asList(values),
        valid,
        descriptorValid);

    caught = null;
    descriptorValid = false;
    try {
      descriptor = new DescriptorSupport();
      descriptor.setFields(names, values);
      descriptorValid = descriptor.isValid();
    } catch (RuntimeOperationsException e) {
      caught = e;
    }
    if (valid && caught != null) throw caught;
    assertEquals(
        "Expected "
            + valid
            + " for new Descriptor(String[], String[]) names="
            + Arrays.asList(names)
            + " values="
            + Arrays.asList(values),
        valid,
        descriptorValid);
  }
Пример #7
0
  /* At some stage we should rewrite this code to be cleverer.  Using
  a StringTokenizer as we do means, first, that we accept a lot of
  bogus strings without noticing they are bogus, and second, that we
  split the string being parsed at characters like > even if they
  occur in the middle of a field value. */
  public DescriptorSupport(String inStr)
      throws MBeanException, RuntimeOperationsException, XMLParseException {
    /* parse an XML-formatted string and populate internal
     * structure with it */
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
      MODELMBEAN_LOGGER.logp(
          Level.FINEST,
          DescriptorSupport.class.getName(),
          "Descriptor(String = '" + inStr + "')",
          "Constructor");
    }
    if (inStr == null) {
      if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
        MODELMBEAN_LOGGER.logp(
            Level.FINEST,
            DescriptorSupport.class.getName(),
            "Descriptor(String = null)",
            "Illegal arguments");
      }
      final String msg = "String in parameter is null";
      final RuntimeException iae = new IllegalArgumentException(msg);
      throw new RuntimeOperationsException(iae, msg);
    }

    final String lowerInStr = inStr.toLowerCase();
    if (!lowerInStr.startsWith("<descriptor>") || !lowerInStr.endsWith("</descriptor>")) {
      throw new XMLParseException("No <descriptor>, </descriptor> pair");
    }

    // parse xmlstring into structures
    init(null);
    // create dummy descriptor: should have same size
    // as number of fields in xmlstring
    // loop through structures and put them in descriptor

    StringTokenizer st = new StringTokenizer(inStr, "<> \t\n\r\f");

    boolean inFld = false;
    boolean inDesc = false;
    String fieldName = null;
    String fieldValue = null;

    while (st.hasMoreTokens()) { // loop through tokens
      String tok = st.nextToken();

      if (tok.equalsIgnoreCase("FIELD")) {
        inFld = true;
      } else if (tok.equalsIgnoreCase("/FIELD")) {
        if ((fieldName != null) && (fieldValue != null)) {
          fieldName = fieldName.substring(fieldName.indexOf('"') + 1, fieldName.lastIndexOf('"'));
          final Object fieldValueObject = parseQuotedFieldValue(fieldValue);
          setField(fieldName, fieldValueObject);
        }
        fieldName = null;
        fieldValue = null;
        inFld = false;
      } else if (tok.equalsIgnoreCase("DESCRIPTOR")) {
        inDesc = true;
      } else if (tok.equalsIgnoreCase("/DESCRIPTOR")) {
        inDesc = false;
        fieldName = null;
        fieldValue = null;
        inFld = false;
      } else if (inFld && inDesc) {
        // want kw=value, eg, name="myname" value="myvalue"
        int eq_separator = tok.indexOf("=");
        if (eq_separator > 0) {
          String kwPart = tok.substring(0, eq_separator);
          String valPart = tok.substring(eq_separator + 1);
          if (kwPart.equalsIgnoreCase("NAME")) fieldName = valPart;
          else if (kwPart.equalsIgnoreCase("VALUE")) fieldValue = valPart;
          else { // xml parse exception
            final String msg = "Expected `name' or `value', got `" + tok + "'";
            throw new XMLParseException(msg);
          }
        } else { // xml parse exception
          final String msg = "Expected `keyword=value', got `" + tok + "'";
          throw new XMLParseException(msg);
        }
      }
    } // while tokens
    if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
      MODELMBEAN_LOGGER.logp(
          Level.FINEST, DescriptorSupport.class.getName(), "Descriptor(XMLString)", "Exit");
    }
  }