/** * 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; }
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"); } } } }