Map<String, WebFragmentEntry> webFragmentEntryMap = new HashMap<String, WebFragmentEntry>(jarURLDocumentMap.size());
boolean absoluteOrderingConfigured = webApp.getAbsoluteOrderingArray().length != 0;
Set<String> usedWebFragmentNames = new HashSet<String>();
Map<String, WebFragmentType> unnamedWebFragmentMap = new HashMap<String, WebFragmentType>();
for (String jarURL : jarURLDocumentMap.keySet()) {
WebFragmentType webFragment = jarURLDocumentMap.get(jarURL).getWebFragment();
JavaIdentifierType[] names = webFragment.getNameArray();
String webFragmentName = names.length == 0 ? null : names[0].getStringValue();
if (webFragmentName != null) {
if (webFragmentEntryMap.containsKey(webFragmentName)) {
//TODO Please correct my understanding about how to handle the duplicate web-fragment name (spec 8.2.2)
//If absolute-ordering is configured, the web-fragments of the same name are allowed, but only the first occurrence is considered
//If relative-ordering is configured, Duplicate name exception: if, when traversing the web-fragments, multiple
//members with the same <name> element are encountered, the application must
//log an informative error message including information to help fix the
//problem, and must fail to deploy. For example, one way to fix this problem is
//for the user to use absolute ordering, in which case relative ordering is ignored.
//TODO If there is no configuration for absolute-ordering in web.xml and relative-ordering in webfragment.xml files, shall we allowed duplicate names ?
if (!absoluteOrderingConfigured) {
throw new DeploymentException("Mutiple web-fragments with the same name [" + webFragmentName + "] are found in " + jarURL + " and "
+ webFragmentEntryMap.get(webFragmentName).getJarURL() + ", you might make the name unique or use absolute ordering in web.xml");
}
} else {
webFragmentEntryMap.put(webFragmentName, new WebFragmentEntry(webFragmentName, webFragmentName, webFragment, jarURL));
}
usedWebFragmentNames.add(webFragmentName);
} else {
unnamedWebFragmentMap.put(jarURL, webFragment);
}
//Add names configurations in before/after, so that we would not add an existing name for those unamed web fragment by sudden.
if (webFragment.getOrderingArray().length > 0) {
OrderingType order = webFragment.getOrderingArray()[0];
if (order.getBefore() != null) {
for (JavaIdentifierType name : order.getBefore().getNameArray()) {
usedWebFragmentNames.add(name.getStringValue());
}
}
if (order.getAfter() != null) {
for (JavaIdentifierType name : order.getAfter().getNameArray()) {
usedWebFragmentNames.add(name.getStringValue());
}
}
}
}
//Generate names for all the web fragments which are not explicitly configured
String tempNamePrefix = "geronimo-deployment";
int nameSubfix = 0;
for (String webFragmentURL : unnamedWebFragmentMap.keySet()) {
WebFragmentType webFragment = unnamedWebFragmentMap.get(webFragmentURL);
String tempWebFragmentName = tempNamePrefix + nameSubfix++;
while (usedWebFragmentNames.contains(tempWebFragmentName)) {
tempWebFragmentName = tempNamePrefix + nameSubfix++;
}
webFragmentEntryMap.put(tempWebFragmentName, new WebFragmentEntry(tempNamePrefix, null, webFragment, webFragmentURL));