📄 advcooker.java
字号:
e = doc.createElement("Bind", bind ); doc.appendChild(e); return doc; } /** * 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; } /** 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; } /** * 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 --vasha@jxta.org 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; } public static ModuleImplAdvertisement buildPeerGroupImplAdvertisement( PeerGroup parent, ModuleSpecID newGroupModuleSpecID, String newDescription, Map newServices, Map newApps) throws Exception { Map newProtos = null; // illegal types will cause an IllegalArgumentException typeCheckKeys(newServices); typeCheckValues(newServices); typeCheckKeys(newApps); typeCheckValues(newApps); // 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); // alter apps Map apps = paramAdv.getApps(); typeCheckKeys(apps); apps = mergeTables(apps,newApps); paramAdv.setApps(apps); paramElement = (TextElement)paramAdv.getDocument(MimeMediaType.XMLUTF8); implAdv.setParam(paramElement); return implAdv; } public static ModuleImplAdvertisement buildPeerGroupImplAdvertisement( PeerGroup parent, ModuleSpecID newGroupModuleSpecID, String newDescription, Map newServices, Map newApps, Map newProtos) throws Exception { // illegal types will cause an IllegalArgumentException typeCheckKeys(newServices); typeCheckValues(newServices); typeCheckKeys(newApps); typeCheckValues(newApps); typeCheckKeys(newProtos); typeCheckValues(newProtos); // 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); // alter apps Map apps = paramAdv.getApps(); typeCheckKeys(apps); apps = mergeTables(apps,newApps); paramAdv.setApps(newApps); // alter protos Map protos = paramAdv.getProtos(); typeCheckKeys(protos); apps = mergeTables(protos,newProtos); paramAdv.setProtos(newProtos); paramElement = (TextElement)paramAdv.getDocument(MimeMediaType.XMLUTF8); implAdv.setParam(paramElement); return implAdv; } /** * Module table vaules must be ModuleImplAdvertisements here. * Though StdPeerGroup allows for values of type ModuleSpecID, * the context in which they seem to apply is not our context of adding * or replacing modules, so I've prohibited them. --vasha@jxta.org dec 3 2001. * * @param moduleTable -- a Hashtable of services, applications or protocols. * @throws IllegalArgumentException -- for an invalid key or value type */ public static void typeCheckValues(Map moduleTable) { String badVal = "Module table value not a ModuleImplAdvertisement "; Iterator keys = moduleTable.keySet().iterator(); while (keys.hasNext()){ // Tables allow for ModuleSpecID values when, as I understand it, // they can load the module from the parent. I'm insisting that // NEW or ALTERNATIVE modules supply a ModuleImplAdvertisement. Object value = moduleTable.get(keys.next()); boolean legalValue = value instanceof ModuleImplAdvertisement; if(!legalValue){ throw(new IllegalArgumentException(badVal + value)); } } } /** * Module table keys must be ModuleClassID's. * * @param moduleTable -- a Hashtable of services, applications or protocols. * @throws IllegalArgumentException -- for an invalid key or value type */ public static void typeCheckKeys(Map moduleTable) { String badKey = "Module table key not a ModuleClassID "; Iterator keys = moduleTable.keySet().iterator(); while (keys.hasNext()){ Object key = keys.next(); boolean legalKey = key instanceof ModuleClassID; if(!legalKey ){ throw( new IllegalArgumentException(badKey + key)); } } } /** * Merge two hashtables of servcices, overwriting old with new if * they have the same base class id. * * @oldServices --service table of a parent group * @newServices --services to be added or substituted * @return --merged table */ private static Map mergeTables(Map oldServices, Map newServices){ // just use brute force; we won't be doing it that often Map mergedServices = new HashMap(oldServices); Iterator newKeys = newServices.keySet().iterator(); while(newKeys.hasNext()){ ModuleClassID key = (ModuleClassID)newKeys.next(); Iterator oldKeys = oldServices.keySet().iterator(); while(oldKeys.hasNext()){ ModuleClassID oldkey = (ModuleClassID)oldKeys.next(); if(oldkey.isOfSameBaseClass(key)){ mergedServices.remove(oldkey); } } mergedServices.put(key,newServices.get(key)); } return mergedServices; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -