Protocol above_prot=null;
for(final Protocol prot: protocols) {
if(prot instanceof TP) {
String singleton_name=((TP)prot).getSingletonName();
if(singleton_name != null && singleton_name.length() > 0 && cluster_name != null) {
TP transport=(TP)prot;
final Map<String, Protocol> up_prots=transport.getUpProtocols();
synchronized(up_prots) {
Set<String> keys=up_prots.keySet();
if(keys.contains(cluster_name))
throw new IllegalStateException("cluster '" + cluster_name + "' is already connected to singleton " +
"transport: " + keys);
for(Iterator<Map.Entry<String,Protocol>> it=up_prots.entrySet().iterator(); it.hasNext();) {
Map.Entry<String,Protocol> entry=it.next();
Protocol tmp=entry.getValue();
if(tmp == above_prot) {
it.remove();
}
}
if(above_prot != null) {
TP.ProtocolAdapter ad=new TP.ProtocolAdapter(cluster_name, prot.getName(), above_prot, prot,
transport.getThreadNamingPattern(),
transport.getLocalAddress());
ad.setProtocolStack(above_prot.getProtocolStack());
above_prot.setDownProtocol(ad);
up_prots.put(cluster_name, ad);
}
}
synchronized(singletons) {
Tuple<TP,Short> val=singletons.get(singleton_name);
if(val == null) {
singletons.put(singleton_name, new Tuple<TP,Short>(transport,(short)1));
}
else {
short num_starts=val.getVal2();
val.setVal2((short)(num_starts +1));
if(num_starts >= 1) {
if(above_prot != null)
above_prot.up(new Event(Event.SET_LOCAL_ADDRESS, transport.getLocalAddress()));
continue;
}
}
}
}