Ensure the semaphore is released if a RuntimeException is thrown.

This commit is contained in:
akwizgran
2012-12-16 00:16:19 +00:00
parent 7887331d1c
commit a7acec8f72

View File

@@ -137,49 +137,54 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
LOG.info("Previous poll still in progress"); LOG.info("Previous poll still in progress");
return; return;
} }
// Get the ISO 3166 code for the caller's country try {
String callerIso = callback.getLocalProperties().get("iso3166"); // Get the ISO 3166 code for the caller's country
if(StringUtils.isNullOrEmpty(callerIso)) return; String callerIso = callback.getLocalProperties().get("iso3166");
// Call contacts one at a time in a random order if(StringUtils.isNullOrEmpty(callerIso)) return;
Map<ContactId, TransportProperties> remote = // Call contacts one at a time in a random order
callback.getRemoteProperties(); Map<ContactId, TransportProperties> remote =
List<ContactId> contacts = new ArrayList<ContactId>(remote.keySet()); callback.getRemoteProperties();
Collections.shuffle(contacts); List<ContactId> contacts =
Iterator<ContactId> it = contacts.iterator(); new ArrayList<ContactId>(remote.keySet());
while(it.hasNext() && running) { Collections.shuffle(contacts);
ContactId c = it.next(); Iterator<ContactId> it = contacts.iterator();
// Get the ISO 3166 code for the callee's country while(it.hasNext() && running) {
TransportProperties properties = remote.get(c); ContactId c = it.next();
if(properties == null) continue; // Get the ISO 3166 code for the callee's country
String calleeIso = properties.get("iso3166"); TransportProperties properties = remote.get(c);
if(StringUtils.isNullOrEmpty(calleeIso)) continue; if(properties == null) continue;
// Get the callee's phone number String calleeIso = properties.get("iso3166");
String number = properties.get("number"); if(StringUtils.isNullOrEmpty(calleeIso)) continue;
if(StringUtils.isNullOrEmpty(number)) continue; // Get the callee's phone number
// Convert the number into direct dialling form String number = properties.get("number");
number = CountryCodes.translate(number, callerIso, calleeIso); if(StringUtils.isNullOrEmpty(number)) continue;
if(number == null) continue; // Convert the number into direct dialling form
// Dial the number number = CountryCodes.translate(number, callerIso, calleeIso);
try { if(number == null) continue;
if(!modem.dial(number)) continue; // Dial the number
} catch(IOException e) { try {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if(!modem.dial(number)) continue;
if(resetModem()) continue; } catch(IOException e) {
break; if(LOG.isLoggable(WARNING))
} LOG.log(WARNING, e.toString(), e);
if(LOG.isLoggable(INFO)) LOG.info("Outgoing call connected"); if(resetModem()) continue;
ModemTransportConnection conn = new ModemTransportConnection(); break;
callback.outgoingConnectionCreated(c, conn); }
try { if(LOG.isLoggable(INFO)) LOG.info("Outgoing call connected");
conn.waitForDisposal(); ModemTransportConnection conn = new ModemTransportConnection();
} catch(InterruptedException e) { callback.outgoingConnectionCreated(c, conn);
if(LOG.isLoggable(WARNING)) try {
LOG.warning("Interrupted while polling"); conn.waitForDisposal();
Thread.currentThread().interrupt(); } catch(InterruptedException e) {
break; if(LOG.isLoggable(WARNING))
LOG.warning("Interrupted while polling");
Thread.currentThread().interrupt();
break;
}
} }
} finally {
polling.release();
} }
polling.release();
} }
public DuplexTransportConnection createConnection(ContactId c) { public DuplexTransportConnection createConnection(ContactId c) {