📄 advcooker.java
字号:
StructuredTextDocument doc = (StructuredTextDocument)
StructuredDocumentFactory.newStructuredDocument(
new MimeMediaType("text", "xml"), "Comp");
Element e = doc.createElement("Efmt", efmt );
doc.appendChild(e);
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 javadocs for 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 javadocs for 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,
Hashtable newServices)
throws IllegalArgumentException, Exception {
Hashtable 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
Hashtable 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(new MimeMediaType("text", "xml"));
implAdv.setParam(paramElement);
return implAdv;
}
public static ModuleImplAdvertisement buildPeerGroupImplAdvertisement(
PeerGroup parent, ModuleSpecID newGroupModuleSpecID, String newDescription,
Hashtable newServices, Hashtable newApps)
throws IllegalArgumentException, Exception {
Hashtable 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
Hashtable 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
Hashtable apps = paramAdv.getApps();
typeCheckKeys(apps);
apps = mergeTables(apps,newApps);
paramAdv.setApps(apps);
paramElement = (TextElement)paramAdv.getDocument(new MimeMediaType("text", "xml"));
implAdv.setParam(paramElement);
return implAdv;
}
public static ModuleImplAdvertisement
buildPeerGroupImplAdvertisement(
PeerGroup parent,
ModuleSpecID newGroupModuleSpecID,
String newDescription,
Hashtable newServices,
Hashtable newApps,
Hashtable newProtos)
throws IllegalArgumentException,
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
Hashtable 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
Hashtable apps = paramAdv.getApps();
typeCheckKeys(apps);
apps = mergeTables(apps,newApps);
paramAdv.setApps(newApps);
// alter protos
Hashtable protos = paramAdv.getProtos();
typeCheckKeys(protos);
apps = mergeTables(protos,newProtos);
paramAdv.setProtos(newProtos);
paramElement = (TextElement)paramAdv.getDocument(new MimeMediaType("text", "xml"));
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(Hashtable moduleTable)
throws IllegalArgumentException {
String badVal = "Module table value not a ModuleImplAdvertisement ";
java.util.Enumeration keys = moduleTable.keys();
while (keys.hasMoreElements()){
// 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.nextElement());
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(Hashtable moduleTable)
throws IllegalArgumentException {
String badKey = "Module table key not a ModuleClassID ";
java.util.Enumeration keys = moduleTable.keys();
while (keys.hasMoreElements()){
Object key = keys.nextElement();
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 Hashtable mergeTables(Hashtable oldServices, Hashtable newServices){
// just use brute force; we won't be doing it that often
Hashtable mergedServices = new Hashtable(oldServices);
Enumeration newKeys = newServices.keys();
while(newKeys.hasMoreElements()){
ModuleClassID key = (ModuleClassID)newKeys.nextElement();
Enumeration oldKeys = oldServices.keys();
while(oldKeys.hasMoreElements()){
ModuleClassID oldkey = (ModuleClassID)oldKeys.nextElement();
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 + -