Exemplo n.º 1
0
  /**
   * Modifies a copy of the parent's implementation advertisement to reflect the addition or
   * replacement of services. The newServices Hashtable must have ModuleClassID keys and
   * ModuleImplAdvertisement values. I've deferred adding applications or protocols for the moment
   * [email protected] Dec 3 2001.
   *
   * @return -- advert for the new peergroup which the StdPeerGroup module will implement.
   * @param parent -- a running instance of the new group's parent
   * @param newGroupModuleSpecID -- since new or replacement services are involved
   * @param newDescription -- the new group's reason to be
   * @param newServices -- advert's for the new services
   * @throws IllegalArgumentException -- for a bad key or value type
   * @throws Exception --- if the parent can't produce a copy of its own impl advert
   */
  public static ModuleImplAdvertisement buildPeerGroupImplAdvertisement(
      StdPeerGroup parent,
      ModuleSpecID newGroupModuleSpecID,
      String newDescription,
      Map newServices)
      throws Exception {
    Map newApps = null, newProtos = null;

    // illegal types will cause an IllegalArgumentException
    typeCheckKeys(newServices);
    typeCheckValues(newServices);
    // get a copy of parent's general purpose advert as a template
    ModuleImplAdvertisement implAdv = parent.getAllPurposePeerGroupImplAdvertisement();

    implAdv.setDescription(newDescription);
    implAdv.setModuleSpecID(newGroupModuleSpecID);
    // extract embedded ad for standard modules
    TextElement paramElement = (TextElement) implAdv.getParam();
    StdPeerGroupParamAdv paramAdv = new StdPeerGroupParamAdv(paramElement);
    // alter services
    Map services = paramAdv.getServices();

    typeCheckKeys(services);
    // mergeTables will override old services with new if base classes are the same
    services = mergeTables(services, newServices);
    paramAdv.setServices(services);
    paramElement = (TextElement) paramAdv.getDocument(MimeMediaType.XMLUTF8);
    implAdv.setParam(paramElement);
    return implAdv;
  }
Exemplo n.º 2
0
  /**
   * Use this form to fully populate a ModuleImplAdvertisement. A ModuleImplAdvertisement has an
   * optional field, "param" which is neglected here. If needed it should be set with advert's
   * setParam method. (See {@link ModuleImplAdvertisement}.)
   *
   * @param msid -- the module spec id
   * @param code -- the module's fully qualified classname, "net.jxta.impl.wire.MyNewThing"
   * @param compat -- a compatibility statement
   * @param description -- something like "MyNewThing Module, J2SE Implementation"
   * @param provider -- something like "jxta.org"
   * @param uri -- currently ornamental, eventually where to find binaries.
   * @return -- a custom advert, fully populated except for "param" field.
   */
  public static ModuleImplAdvertisement buildModuleImplAdvertisement(
      ModuleSpecID msid,
      String code,
      Element compat,
      String description,
      String provider,
      String uri) {
    ModuleImplAdvertisement miadv = buildModuleImplAdvertisement(msid, code, compat);

    miadv.setDescription(description);
    miadv.setProvider(provider);
    miadv.setUri(uri);
    return miadv;
  }
Exemplo n.º 3
0
  /**
   * Compat's (compatibility statements) serve to narrow the search for a ModuleImplAdvertisement.
   * Basically you want something compatible with your group's implementation. Use this form for
   * compatibilty with the current StdPeerGroup.
   *
   * @return -- boilerplate compat for StdPeerGroup
   */
  public static XMLDocument buildCompat() {

    try {
      PeerGroup wpg = PeerGroup.globalRegistry.lookupInstance(PeerGroupID.worldPeerGroupID);

      ModuleImplAdvertisement implAdv = wpg.getAllPurposePeerGroupImplAdvertisement();

      wpg.unref();

      XMLDocument compat = (XMLDocument) implAdv.getCompat();

      return compat;
    } catch (Exception e) {
      // but if it doesn't work default to what was current on Feb 22 2006.
      return buildCompat("JDK1.4.1", "V2.0 Ref Impl");
    }
  }
Exemplo n.º 4
0
  /**
   * A ModuleImplAdvertisement represents one of any number of published implementations of a given
   * specification. Use this form with for a development boilerplate. Use buildCompat() for a compat
   * boilerplate. (See {@link ModuleImplAdvertisement}.)
   *
   * @param msid -- the module spec id
   * @param code -- the module's fully qualified classname, "net.jxta.impl.wire.MyNewThing"
   * @param compat -- a compatibility statement. Use buildCompat() for a boilerplate.
   * @return -- a development boilerplate with custom compatibility.
   */
  public static ModuleImplAdvertisement buildModuleImplAdvertisement(
      ModuleSpecID msid, String code, Element compat) {
    ModuleImplAdvertisement miadv =
        (ModuleImplAdvertisement)
            AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType());

    miadv.setCompat(compat);
    miadv.setModuleSpecID(msid);
    miadv.setCode(code);
    miadv.setDescription(code + " Module, J2SE Implementation");
    miadv.setProvider("jxta.org");
    miadv.setUri("http://download.jxta.org");
    return miadv;
  }
Exemplo n.º 5
0
public class StdPeerGroupParamAdv {

  private static final Logger LOG = Logger.getLogger(StdPeerGroupParamAdv.class.getName());

  private static final String paramTag = "Parm";
  private static final String protoTag = "Proto";
  private static final String appTag = "App";
  private static final String svcTag = "Svc";
  private static final String mcidTag = "MCID";
  private static final String msidTag = "MSID";

  private static final String miaTag = ModuleImplAdvertisement.getAdvertisementType();

  // In the future we should be able to manipulate all modules regardless
  // of their kind, but right now it helps to keep them categorized
  // as follows.
  private Hashtable servicesTable = null;
  private Hashtable protosTable = null;
  private Hashtable appsTable = null;

  public StdPeerGroupParamAdv() {
    // set defaults
    servicesTable = new Hashtable();
    protosTable = new Hashtable();
    appsTable = new Hashtable();
  }

  public StdPeerGroupParamAdv(Element root) {

    initialize(root);
  }

  public Hashtable getServices() {
    return servicesTable;
  }

  public Hashtable getProtos() {
    return protosTable;
  }

  public Hashtable getApps() {
    return appsTable;
  }

  public void setServices(Hashtable servicesTable) {
    if (servicesTable == null) this.servicesTable = new Hashtable();
    else this.servicesTable = servicesTable;
  }

  public void setProtos(Hashtable protosTable) {
    if (protosTable == null) this.protosTable = new Hashtable();
    else this.protosTable = protosTable;
  }

  public void setApps(Hashtable appsTable) {
    if (appsTable == null) this.appsTable = new Hashtable();
    else this.appsTable = appsTable;
  }

  private void initialize(Element root) {

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

    TextElement doc = (TextElement) root;

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

    // set defaults
    servicesTable = new Hashtable();
    protosTable = new Hashtable();
    appsTable = new Hashtable();

    int appCount = 0;
    Enumeration modules = doc.getChildren();
    while (modules.hasMoreElements()) {
      Hashtable theTable;

      TextElement module = (TextElement) modules.nextElement();
      String tagName = module.getName();
      if (tagName.equals(svcTag)) {
        theTable = servicesTable;
      } else if (tagName.equals(appTag)) {
        theTable = appsTable;
      } else if (tagName.equals(protoTag)) {
        theTable = protosTable;
      } else continue;

      ModuleSpecID specID = null;
      ModuleClassID classID = null;
      ModuleImplAdvertisement inLineAdv = null;

      try {
        if (module.getTextValue() != null) {
          specID = (ModuleSpecID) IDFactory.fromURL(IDFactory.jxtaURL(module.getTextValue()));
        }

        // Check for children anyway.
        Enumeration fields = module.getChildren();
        while (fields.hasMoreElements()) {
          TextElement field = (TextElement) fields.nextElement();
          if (field.getName().equals(mcidTag)) {
            classID = (ModuleClassID) IDFactory.fromURL(IDFactory.jxtaURL(field.getTextValue()));
            continue;
          }
          if (field.getName().equals(msidTag)) {
            specID = (ModuleSpecID) IDFactory.fromURL(IDFactory.jxtaURL(field.getTextValue()));
            continue;
          }
          if (field.getName().equals(miaTag)) {
            inLineAdv = (ModuleImplAdvertisement) AdvertisementFactory.newAdvertisement(field);
            continue;
          }
        }
      } catch (Exception any) {
        if (LOG.isEnabledFor(Level.WARN)) LOG.warn("Broken entry; skipping", any);
        continue;
      }

      if (inLineAdv == null && specID == null) {
        if (LOG.isEnabledFor(Level.WARN)) LOG.warn("Insufficent entry; skipping");
        continue;
      }

      Object theValue;
      if (inLineAdv == null) {
        theValue = specID;
      } else {
        specID = inLineAdv.getModuleSpecID();
        theValue = inLineAdv;
      }
      if (classID == null) {
        classID = specID.getBaseClass();
      }

      // For applications, the role does not matter. We just create
      // a unique role ID on the fly.
      // When outputing the add we get rid of it to save space.

      if (theTable == appsTable) {
        // Only the first (or only) one may use the base class.
        if (appCount++ != 0) {
          classID = IDFactory.newModuleClassID(classID);
        }
      }
      theTable.put(classID, theValue);
    }
  }

  public Document getDocument(MimeMediaType encodeAs) {
    StructuredTextDocument doc = null;

    doc =
        (StructuredTextDocument)
            StructuredDocumentFactory.newStructuredDocument(encodeAs, paramTag);

    outputModules(doc, servicesTable, svcTag, encodeAs);
    outputModules(doc, protosTable, protoTag, encodeAs);
    outputModules(doc, appsTable, appTag, encodeAs);
    return doc;
  }

  private void outputModules(
      StructuredTextDocument doc, Hashtable modulesTable, String mainTag, MimeMediaType encodeAs) {

    Enumeration allClasses = modulesTable.keys();
    while (allClasses.hasMoreElements()) {
      ModuleClassID mcid = (ModuleClassID) allClasses.nextElement();
      Object val = modulesTable.get(mcid);

      // For applications, we ignore the role ID. It is not meaningfull,
      // and a new one is assigned on the fly when loading this adv.

      if (val instanceof Advertisement) {
        TextElement m = doc.createElement(mainTag);
        doc.appendChild(m);

        if (!(modulesTable == appsTable || mcid.equals(mcid.getBaseClass()))) {
          // It is not an app and there is a role ID. Output it.

          TextElement i = doc.createElement(mcidTag, mcid.toString());
          m.appendChild(i);
        }

        StructuredTextDocument advdoc =
            (StructuredTextDocument) ((Advertisement) val).getDocument(encodeAs);

        StructuredDocumentUtils.copyElements(doc, m, advdoc);

      } else if (val instanceof ModuleSpecID) {
        TextElement m;

        if (modulesTable == appsTable || mcid.equals(mcid.getBaseClass())) {

          // Either it is an app or there is no role ID.
          // So the specId is good enough.
          m = doc.createElement(mainTag, ((ModuleSpecID) val).toString());
          doc.appendChild(m);
        } else {
          // The role ID matters, so the classId must be separate.
          m = doc.createElement(mainTag);
          doc.appendChild(m);

          TextElement i;
          i = doc.createElement(mcidTag, mcid.toString());
          m.appendChild(i);

          i = doc.createElement(msidTag, ((ModuleSpecID) val).toString());
          m.appendChild(i);
        }
      } else {
        if (LOG.isEnabledFor(Level.WARN)) LOG.warn("unsupported class in modules table");
      }
    }
  }
}
Exemplo n.º 6
0
  private void initialize(Element root) {

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

    TextElement doc = (TextElement) root;

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

    // set defaults
    servicesTable = new Hashtable();
    protosTable = new Hashtable();
    appsTable = new Hashtable();

    int appCount = 0;
    Enumeration modules = doc.getChildren();
    while (modules.hasMoreElements()) {
      Hashtable theTable;

      TextElement module = (TextElement) modules.nextElement();
      String tagName = module.getName();
      if (tagName.equals(svcTag)) {
        theTable = servicesTable;
      } else if (tagName.equals(appTag)) {
        theTable = appsTable;
      } else if (tagName.equals(protoTag)) {
        theTable = protosTable;
      } else continue;

      ModuleSpecID specID = null;
      ModuleClassID classID = null;
      ModuleImplAdvertisement inLineAdv = null;

      try {
        if (module.getTextValue() != null) {
          specID = (ModuleSpecID) IDFactory.fromURL(IDFactory.jxtaURL(module.getTextValue()));
        }

        // Check for children anyway.
        Enumeration fields = module.getChildren();
        while (fields.hasMoreElements()) {
          TextElement field = (TextElement) fields.nextElement();
          if (field.getName().equals(mcidTag)) {
            classID = (ModuleClassID) IDFactory.fromURL(IDFactory.jxtaURL(field.getTextValue()));
            continue;
          }
          if (field.getName().equals(msidTag)) {
            specID = (ModuleSpecID) IDFactory.fromURL(IDFactory.jxtaURL(field.getTextValue()));
            continue;
          }
          if (field.getName().equals(miaTag)) {
            inLineAdv = (ModuleImplAdvertisement) AdvertisementFactory.newAdvertisement(field);
            continue;
          }
        }
      } catch (Exception any) {
        if (LOG.isEnabledFor(Level.WARN)) LOG.warn("Broken entry; skipping", any);
        continue;
      }

      if (inLineAdv == null && specID == null) {
        if (LOG.isEnabledFor(Level.WARN)) LOG.warn("Insufficent entry; skipping");
        continue;
      }

      Object theValue;
      if (inLineAdv == null) {
        theValue = specID;
      } else {
        specID = inLineAdv.getModuleSpecID();
        theValue = inLineAdv;
      }
      if (classID == null) {
        classID = specID.getBaseClass();
      }

      // For applications, the role does not matter. We just create
      // a unique role ID on the fly.
      // When outputing the add we get rid of it to save space.

      if (theTable == appsTable) {
        // Only the first (or only) one may use the base class.
        if (appCount++ != 0) {
          classID = IDFactory.newModuleClassID(classID);
        }
      }
      theTable.put(classID, theValue);
    }
  }