public Protocol getAppropriateProtocolBySite(RemoteSite remoteSite,
boolean allowReuse) throws ProtocolException {
Protocol protocol = null;
if ((allowReuse && ((protocol = reuseProtocols.get(remoteSite.getURL())) == null))
|| !allowReuse) {
ProtocolFactory protocolFactory = this.urlAndProtocolFactory
.get(remoteSite.getURL());
if (protocolFactory == null) {
LinkedList<Class<ProtocolFactory>> protocolClasses = pi
.getProtocolClassesForProtocolType(remoteSite.getURL()
.getProtocol());
for (Class<ProtocolFactory> clazz : protocolClasses) {
try {
if ((protocol = (protocolFactory = clazz.newInstance())
.newInstance()) != null) {
if (!connect(protocol, remoteSite, true)) {
LOG.log(Level.WARNING, "ProtocolFactory "
+ protocolFactory.getClass()
.getCanonicalName()
+ " is not compatible with server at "
+ remoteSite.getURL());
protocol = null;
} else {
this.urlAndProtocolFactory.put(remoteSite
.getURL(), protocolFactory);
break;
}
}
} catch (Exception e) {
LOG.log(Level.WARNING,
"Failed to instanciate protocol " + clazz
+ " for " + remoteSite.getURL());
}
}
if (protocol == null)
throw new ProtocolException(
"Failed to get appropriate protocol for "
+ remoteSite);
} else {
connect(protocol = protocolFactory.newInstance(), remoteSite,
false);
}
if (allowReuse)
this.reuseProtocols.put(remoteSite.getURL(), protocol);
}