1 23 24 package com.sun.enterprise.iiop; 25 26 31 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.logging.Logger ; 38 import java.util.logging.Level ; 39 40 import com.sun.logging.LogDomains; 41 42 import com.sun.corba.ee.pept.transport.ContactInfo; 43 import com.sun.corba.ee.spi.ior.IOR; 44 import com.sun.corba.ee.spi.orb.ORB; 45 import com.sun.corba.ee.spi.transport.CorbaContactInfoList; 46 import com.sun.corba.ee.spi.transport.IIOPPrimaryToContactInfo; 47 import com.sun.corba.ee.spi.transport.SocketInfo; 48 import com.sun.corba.ee.impl.orbutil.ORBUtility; 49 50 54 public class IIOPPrimaryToContactInfoImpl 55 implements IIOPPrimaryToContactInfo 56 { 57 58 60 private static Logger _logger = null; 61 static { 62 _logger = LogDomains.getLogger(LogDomains.CORBA_LOGGER); 63 } 64 65 68 69 public final String baseMsg = IIOPPrimaryToContactInfoImpl.class.getName(); 70 71 private Map map; 72 private boolean debugChecked; 73 private boolean debug; 74 75 public IIOPPrimaryToContactInfoImpl() 76 { 77 map = new HashMap (); 78 debugChecked = false; 79 debug = false; 80 } 81 82 public synchronized void reset(ContactInfo primary) 83 { 84 try { 85 if (debug) { 86 dprint(".reset: " + getKey(primary)); 87 } 88 map.remove(getKey(primary)); 89 } catch (Throwable t) { 90 _logger.log(Level.WARNING, 91 "Problem in " + baseMsg + ".reset", 92 t); 93 RuntimeException rte = 94 new RuntimeException (baseMsg + ".reset error"); 95 rte.initCause(t); 96 throw rte; 97 } 98 } 99 100 public synchronized boolean hasNext(ContactInfo primary, 101 ContactInfo previous, 102 List contactInfos) 103 { 104 try { 105 if (! debugChecked) { 106 debugChecked = true; 107 debug = ((ORB)primary.getBroker()).transportDebugFlag 108 || _logger.isLoggable(Level.FINE); 109 } 110 111 if (debug) { 112 dprint(".hasNext->: " 113 + formatKeyPreviousList(getKey(primary), 114 previous, 115 contactInfos)); 116 } 117 boolean result; 118 if (previous == null) { 119 result = true; 120 } else { 121 int previousIndex = contactInfos.indexOf(previous); 122 int contactInfosSize = contactInfos.size(); 123 if (debug) { 124 dprint(".hasNext: " 125 + previousIndex + " " + contactInfosSize); 126 } 127 if (previousIndex < 0) { 128 RuntimeException rte = new RuntimeException ( 132 133 134 "Problem in " + baseMsg + ".hasNext: previousIndex: " 135 + previousIndex); 136 137 _logger.log(Level.SEVERE, 138 "Problem in " + baseMsg + ".hasNext: previousIndex: " 139 + previousIndex, rte); 140 throw rte; 141 } else { 142 result = (contactInfosSize - 1) > previousIndex; 145 } 146 } 147 if (debug) { 148 dprint(".hasNext<-: " + result); 149 } 150 return result; 151 } catch (Throwable t) { 152 _logger.log(Level.WARNING, 153 "Problem in " + baseMsg + ".hasNext", 154 t); 155 RuntimeException rte = 156 new RuntimeException (baseMsg + ".hasNext error"); 157 rte.initCause(t); 158 throw rte; 159 } 160 } 161 162 public synchronized ContactInfo next(ContactInfo primary, 163 ContactInfo previous, 164 List contactInfos) 165 { 166 try { 167 String debugMsg = null; 168 169 if (debug) { 170 debugMsg = ""; 171 dprint(".next->: " 172 + formatKeyPreviousList(getKey(primary), 173 previous, 174 contactInfos)); 175 dprint(".next: map: " + formatMap(map)); 176 } 177 178 Object result = null; 179 180 if (previous == null) { 181 result = map.get(getKey(primary)); 183 if (result == null) { 184 if (debug) { 185 debugMsg = ".next<-: initialize map: "; 186 } 187 result = contactInfos.get(0); 190 map.put(getKey(primary), result); 191 } else { 192 if (debug) { 193 dprint(".next: primary mapped to: " + result); 194 } 195 int position = contactInfos.indexOf(result); 196 if (position == -1) { 197 if (debug) { 202 dprint(".next: cannot find mapped entry in current list. Removing mapped entry and trying .next again."); 203 } 204 reset(primary); 205 return next(primary, previous, contactInfos); 206 } 207 result = contactInfos.get(position); 213 if (debug) { 214 debugMsg = ".next<-: mapped: "; 215 } 216 } 217 } else { 218 result = contactInfos.get(contactInfos.indexOf(previous) + 1); 222 map.put(getKey(primary), result); 223 224 _logger.log(Level.INFO, "IIOP failover to: " + result); 225 226 if (debug) { 227 debugMsg = ".next<-: update map: " 228 + " " + contactInfos.indexOf(previous) 229 + " " + contactInfos.size() + " "; 230 } 231 } 232 if (debug) { 233 dprint(debugMsg + result); 234 } 235 return (ContactInfo) result; 236 } catch (Throwable t) { 237 _logger.log(Level.WARNING, 238 "Problem in " + baseMsg + ".next", 239 t); 240 RuntimeException rte = 241 new RuntimeException (baseMsg + ".next error"); 242 rte.initCause(t); 243 throw rte; 244 } 245 } 246 247 private Object getKey(ContactInfo contactInfo) 248 { 249 if (((SocketInfo)contactInfo).getPort() == 0) { 250 return ((CorbaContactInfoList)contactInfo.getContactInfoList()) 254 .getEffectiveTargetIOR(); 255 } else { 256 return contactInfo; 257 } 258 } 259 260 private String formatKeyPreviousList(Object key, 261 ContactInfo previous, List list) 262 { 263 String result = 264 "\n key : " + key 265 + "\n previous: " + previous 266 + "\n list:"; 267 Iterator i = list.iterator(); 268 int count = 1; 269 while (i.hasNext()) { 270 result += "\n " + count++ + " " + i.next(); 271 } 272 return result; 273 } 274 275 private String formatMap(Map map) 276 { 277 String result = ""; 278 synchronized (map) { 279 Iterator i = map.entrySet().iterator(); 280 if (! i.hasNext()) { 281 return "empty"; 282 } 283 while (i.hasNext()) { 284 Map.Entry entry = (Map.Entry ) i.next(); 285 result += 286 "\n key : " + entry.getKey() 287 + "\n value: " + entry.getValue() 288 + "\n"; 289 } 290 } 291 return result; 292 } 293 294 private void dprint(String msg) 295 { 296 299 _logger.log(Level.FINE, msg); 300 } 301 } 302 303 313 314 | Popular Tags |