Ejemplo n.º 1
0
  /**
   * {@inheritDoc}
   *
   * @param raw Description of the Parameter
   * @return Description of the Return Value
   */
  protected boolean handleElement(Element raw) {

    if (super.handleElement(raw)) {
      return true;
    }

    XMLElement elem = (XMLElement) raw;

    if (DEST_PID_TAG.equals(elem.getName())) {
      try {
        URI pID = new URI(elem.getTextValue());

        setDestPeerID((PeerID) IDFactory.fromURI(pID));
      } catch (URISyntaxException badID) {
        throw new IllegalArgumentException("Bad PeerID in advertisement");
      } catch (ClassCastException badID) {
        throw new IllegalArgumentException("ID in advertisement is not a peer id");
      }
      return true;
    }

    if (elem.getName().equals("Dst")) {
      for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements(); ) {
        TextElement aXpt = (TextElement) eachXpt.nextElement();

        AccessPointAdvertisement xptAdv =
            (AccessPointAdvertisement) AdvertisementFactory.newAdvertisement(aXpt);

        setDest(xptAdv);
      }
      return true;
    }

    if (elem.getName().equals("Hops")) {
      Vector hops = new Vector();

      for (Enumeration eachXpt = elem.getChildren(); eachXpt.hasMoreElements(); ) {
        TextElement aXpt = (TextElement) eachXpt.nextElement();

        AccessPointAdvertisement xptAdv =
            (AccessPointAdvertisement) AdvertisementFactory.newAdvertisement(aXpt);

        hops.addElement(xptAdv);
      }
      setHops(hops);
      return true;
    }

    return false;
  }
Ejemplo n.º 2
0
  /**
   * Process an individual element from the document during parse. Normally, implementations will
   * allow the base advertisments a chance to handle the element before attempting ot handle the
   * element themselves. ie.
   *
   * <p>
   *
   * <p>
   *
   * <pre><code>
   *  protected boolean handleElement(Element elem) {
   * <p/>
   *      if (super.handleElement()) {
   *           // it's been handled.
   *           return true;
   *           }
   * <p/>
   *      <i>... handle elements here ...</i>
   * <p/>
   *      // we don't know how to handle the element
   *      return false;
   *      }
   *  </code></pre>
   *
   * @param elem the element to be processed.
   * @return true if the element was recognized, otherwise false.
   */
  protected boolean handleElement(XMLElement elem) {

    String value = elem.getTextValue();

    if (null == value) {
      return false;
    }

    value = value.trim();

    if (0 == value.length()) {
      return false;
    }

    if (elem.getName().equals(typeTag)) {
      setDiscoveryType(Integer.parseInt(value));
      return true;
    }
    if (elem.getName().equals(thresholdTag)) {
      setThreshold(Integer.parseInt(value));
      return true;
    }
    if (elem.getName().equals(peerAdvTag)) {
      try {
        XMLDocument asDoc =
            (XMLDocument)
                StructuredDocumentFactory.newStructuredDocument(
                    MimeMediaType.XMLUTF8, new StringReader(value));
        PeerAdvertisement adv = (PeerAdvertisement) AdvertisementFactory.newAdvertisement(asDoc);
        setPeerAdvertisement(adv);
        return true;
      } catch (IOException failed) {
        IllegalArgumentException failure = new IllegalArgumentException("Bad Peer Advertisement");
        failure.initCause(failed);

        throw failure;
      }
    }
    if (elem.getName().equals(queryAttrTag)) {
      setAttr(value);
      return true;
    }
    if (elem.getName().equals(queryValueTag)) {
      setValue(value);
      return true;
    }

    // element was not handled
    return false;
  }
Ejemplo n.º 3
0
  /**
   * Intialize a Discovery Query from a portion of a structured document.
   *
   * @param root document to intialize from
   */
  protected void initialize(Element root) {

    if (!XMLElement.class.isInstance(root)) {
      throw new IllegalArgumentException(getClass().getName() + " only supports XMLElement");
    }

    XMLElement doc = (XMLElement) root;

    if (!doc.getName().equals(getAdvertisementType())) {
      throw new IllegalArgumentException(
          "Could not construct : "
              + getClass().getName()
              + "from doc containing a "
              + doc.getName());
    }

    setDiscoveryType(-1); // force illegal value;

    Enumeration<XMLElement> elements = doc.getChildren();

    while (elements.hasMoreElements()) {
      XMLElement elem = elements.nextElement();

      if (!handleElement(elem)) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
          LOG.fine("Unhandled Element : " + elem.toString());
        }
      }
    }

    // sanity check time!

    if ((DiscoveryService.PEER != getDiscoveryType())
        && (DiscoveryService.GROUP != getDiscoveryType())
        && (DiscoveryService.ADV != getDiscoveryType())) {
      throw new IllegalArgumentException("Type is not one of the required values.");
    }
    if (getThreshold() < 0) {
      throw new IllegalArgumentException("Threshold must not be less than zero.");
    }
    if ((getDiscoveryType() != DiscoveryService.PEER) && (getThreshold() == 0)) {
      throw new IllegalArgumentException("Threshold may not be zero.");
    }
    if ((null == getAttr()) && (null != getValue())) {
      throw new IllegalArgumentException("Value specified without attribute.");
    }
  }
Ejemplo n.º 4
0
  /**
   * Private constructor. Use instantiator
   *
   * @param root Description of the Parameter
   */
  private RouteAdv(Element root) {
    if (!XMLElement.class.isInstance(root)) {
      throw new IllegalArgumentException(getClass().getName() + " only supports XLMElement");
    }

    XMLElement doc = (XMLElement) root;

    String doctype = doc.getName();

    String typedoctype = "";
    Attribute itsType = doc.getAttribute("type");

    if (null != itsType) {
      typedoctype = itsType.getValue();
    }

    if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
      throw new IllegalArgumentException(
          "Could not construct : "
              + getClass().getName()
              + "from doc containing a "
              + doc.getName());
    }

    Enumeration elements = doc.getChildren();

    while (elements.hasMoreElements()) {
      XMLElement elem = (XMLElement) elements.nextElement();

      if (!handleElement(elem)) {
        if (LOG.isEnabledFor(Level.DEBUG)) {
          LOG.debug("Unhandled Element: " + elem.toString());
        }
      }
    }

    // HACK Compatibility

    setDestPeerID(getDestPeerID());

    // Sanity Check!!!

    if (hasALoop()) {
      throw new IllegalArgumentException("Route contains a loop!");
    }
  }
Ejemplo n.º 5
0
  /** {@inheritDoc} */
  @Override
  protected boolean handleElement(Element raw) {

    if (super.handleElement(raw)) {
      return true;
    }

    XMLElement elem = (XMLElement) raw;

    String tag = elem.getName();

    if (tag.equals(ProxyOffTag)) {
      proxyEnabled = false;
      return true;
    }

    if (tag.equals(ServerOffTag)) {
      serverEnabled = false;
      return true;
    }

    if (tag.equals(ClientOffTag)) {
      clientEnabled = false;
      return true;
    }

    String value = elem.getTextValue();

    if (null == value) {
      return false;
    }

    value = value.trim();

    if (0 == value.length()) {
      return false;
    }

    if (tag.equals(ProtocolTag)) {
      setProtocol(value);
      return true;
    }

    if (tag.equals(IntfAddrTag)) {
      setInterfaceAddress(value);
      return true;
    }

    if (tag.equals(ConfModeTag)) {
      setConfigMode(value);
      return true;
    }

    if (tag.equals(PortTag)) {
      setPort(Integer.parseInt(value.trim()));
      return true;
    }

    if (tag.equals(ProxyTag)) {
      proxy = value;
      return true;
    }

    if (tag.equals(ServerTag)) {
      server = value;
      return true;
    }

    return false;
  }
Ejemplo n.º 6
0
  /**
   * Private constructor for xml serialized instances. Use the instantiator.
   *
   * @param doc The XML serialization of the advertisement.
   */
  private HTTPAdv(XMLElement doc) {
    String doctype = doc.getName();

    String typedoctype = "";
    Attribute itsType = doc.getAttribute("type");

    if (null != itsType) {
      typedoctype = itsType.getValue();
    }

    if (!doctype.equals(getAdvertisementType()) && !getAdvertisementType().equals(typedoctype)) {
      throw new IllegalArgumentException(
          "Could not construct : "
              + getClass().getName()
              + "from doc containing a "
              + doc.getName());
    }

    Attribute attr = doc.getAttribute(FlagsTag);

    if (attr != null) {
      String options = attr.getValue();

      publicAddressOnly = (options.indexOf(PublicAddressOnlyAttr) != -1);
    }

    Enumeration elements = doc.getChildren();

    while (elements.hasMoreElements()) {
      XMLElement elem = (XMLElement) elements.nextElement();

      if (!handleElement(elem)) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
          LOG.fine("Unhandled Element: " + elem.toString());
        }
      }
    }

    // Sanity Check!!!

    // For consistency we force the flags to "disabled" for items we do not
    // have data for. However, the flags truely matter only when there is
    // data.
    if (proxy == null) {
      proxyEnabled = false;
    }

    if (serverEnabled && (0 == listenPort)) {
      throw new IllegalArgumentException(
          "Dynmaic port selection not supported with incoming connections.");
    }

    if ((listenPort < -1) || (listenPort > 65535)) {
      throw new IllegalArgumentException("Illegal Listen Port Value");
    }

    if (!Arrays.asList(CONFIGMODES).contains(configMode)) {
      throw new IllegalArgumentException("Unsupported configuration mode.");
    }

    // XXX 20050118 bondolo Some versions apparently don't initialize this field. Eventually make it
    // required.
    if (null == getProtocol()) {
      setProtocol("http");
    }
  }