//first just match on service,request
List matches = new ArrayList();
for (Iterator itr = services.iterator(); itr.hasNext();) {
Service sBean = (Service) itr.next();
if (sBean.getId().equalsIgnoreCase(id)) {
matches.add(sBean);
}
}
if (matches.isEmpty()) {
return null;
}
Service sBean = null;
//if multiple, use version to filter match
if (matches.size() > 1) {
List vmatches = new ArrayList(matches);
//match up the version
if (version != null) {
//version specified, look for a match
for (Iterator itr = vmatches.iterator(); itr.hasNext();) {
Service s = (Service) itr.next();
if (version.equals(s.getVersion())) {
continue;
}
itr.remove();
}
if (vmatches.isEmpty()) {
//no matching version found, drop out and next step
// will sort to return highest version
vmatches = new ArrayList(matches);
}
}
//if still multiple matches use namespace, if available, to filter
if (namespace != null && vmatches.size() > 1) {
List nmatches = new ArrayList(vmatches);
for (Iterator itr = nmatches.iterator(); itr.hasNext();) {
Service s = (Service) itr.next();
if (s.getNamespace() != null && !s.getNamespace().equals(namespace)) {
//service declares namespace, kick it out if there is no match
itr.remove();
}
else {
//service does not declare namespace, leave it along
}
}
if (!nmatches.isEmpty()) {
vmatches = nmatches;
}
}
//multiple services found, sort by version
if (vmatches.size() > 1) {
//use highest version
Comparator comparator = new Comparator() {
public int compare(Object o1, Object o2) {
Service s1 = (Service) o1;
Service s2 = (Service) o2;
return s1.getVersion().compareTo(s2.getVersion());
}
};
Collections.sort(vmatches, comparator);
}