From 559cdfaebab676f3a33316140c64c205e0b6fcf4 Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Fri, 28 Oct 2011 15:48:30 +0100 Subject: [PATCH] Merged shared code in listener classes and made casts safe. --- .../plugins/bluetooth/AbstractListener.java | 49 ++++++++++++++++ .../plugins/bluetooth/ContactListener.java | 49 +++++----------- .../plugins/bluetooth/InvitationListener.java | 56 ++++++------------- 3 files changed, 81 insertions(+), 73 deletions(-) create mode 100644 components/net/sf/briar/plugins/bluetooth/AbstractListener.java diff --git a/components/net/sf/briar/plugins/bluetooth/AbstractListener.java b/components/net/sf/briar/plugins/bluetooth/AbstractListener.java new file mode 100644 index 0000000000..171afa4ba7 --- /dev/null +++ b/components/net/sf/briar/plugins/bluetooth/AbstractListener.java @@ -0,0 +1,49 @@ +package net.sf.briar.plugins.bluetooth; + +import java.util.concurrent.atomic.AtomicInteger; + +import javax.bluetooth.DataElement; +import javax.bluetooth.DiscoveryAgent; +import javax.bluetooth.DiscoveryListener; + +abstract class AbstractListener implements DiscoveryListener { + + protected final DiscoveryAgent discoveryAgent; + protected final AtomicInteger searches = new AtomicInteger(1); + + protected boolean finished = false; // Locking: this + + protected AbstractListener(DiscoveryAgent discoveryAgent) { + this.discoveryAgent = discoveryAgent; + } + + public void inquiryCompleted(int discoveryType) { + if(searches.decrementAndGet() == 0) { + synchronized(this) { + finished = true; + notifyAll(); + } + } + } + + public void serviceSearchCompleted(int transaction, int response) { + if(searches.decrementAndGet() == 0) { + synchronized(this) { + finished = true; + notifyAll(); + } + } + } + + protected Object getDataElementValue(Object o) { + if(o instanceof DataElement) { + // Bluecove throws an exception if the type is unknown + try { + return ((DataElement) o).getValue(); + } catch(ClassCastException e) { + return null; + } + } + return null; + } +} diff --git a/components/net/sf/briar/plugins/bluetooth/ContactListener.java b/components/net/sf/briar/plugins/bluetooth/ContactListener.java index 21b3cfc36d..e25493506e 100644 --- a/components/net/sf/briar/plugins/bluetooth/ContactListener.java +++ b/components/net/sf/briar/plugins/bluetooth/ContactListener.java @@ -4,7 +4,6 @@ import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -12,29 +11,24 @@ import javax.bluetooth.BluetoothStateException; import javax.bluetooth.DataElement; import javax.bluetooth.DeviceClass; import javax.bluetooth.DiscoveryAgent; -import javax.bluetooth.DiscoveryListener; import javax.bluetooth.RemoteDevice; import javax.bluetooth.ServiceRecord; import javax.bluetooth.UUID; import net.sf.briar.api.ContactId; -class ContactListener implements DiscoveryListener { +class ContactListener extends AbstractListener { private static final Logger LOG = Logger.getLogger(ContactListener.class.getName()); - private final AtomicInteger searches = new AtomicInteger(1); - private final DiscoveryAgent discoveryAgent; private final Map<String, ContactId> addresses; private final Map<ContactId, String> uuids; private final Map<ContactId, String> urls; - private boolean finished = false; // Locking: this - ContactListener(DiscoveryAgent discoveryAgent, Map<String, ContactId> addresses, Map<ContactId, String> uuids) { - this.discoveryAgent = discoveryAgent; + super(discoveryAgent); this.addresses = addresses; this.uuids = uuids; urls = Collections.synchronizedMap(new HashMap<ContactId, String>()); @@ -68,15 +62,6 @@ class ContactListener implements DiscoveryListener { searches.incrementAndGet(); } - public void inquiryCompleted(int discoveryType) { - if(searches.decrementAndGet() == 0) { - synchronized(this) { - finished = true; - notifyAll(); - } - } - } - public void servicesDiscovered(int transaction, ServiceRecord[] services) { for(ServiceRecord record : services) { // Do we recognise the address? @@ -92,26 +77,20 @@ class ContactListener implements DiscoveryListener { if(serviceUrl == null) continue; // Does this service have the UUID we're looking for? DataElement classIds = record.getAttributeValue(0x1); - if(classIds == null) continue; - @SuppressWarnings("unchecked") - Enumeration<DataElement> e = - (Enumeration<DataElement>) classIds.getValue(); - for(DataElement classId : Collections.list(e)) { - UUID serviceUuid = (UUID) classId.getValue(); - if(uuid.equalsIgnoreCase(serviceUuid.toString())) { - // The UUID matches - store the URL - urls.put(c, serviceUrl); + Object o = getDataElementValue(classIds); + if(o instanceof Enumeration) { + Enumeration<?> e = (Enumeration<?>) o; + for(Object o1 : Collections.list(e)) { + Object o2 = getDataElementValue(o1); + if(o2 instanceof UUID) { + UUID serviceUuid = (UUID) o2; + if(uuid.equalsIgnoreCase(serviceUuid.toString())) { + // The UUID matches - store the URL + urls.put(c, serviceUrl); + } + } } } } } - - public void serviceSearchCompleted(int transaction, int response) { - if(searches.decrementAndGet() == 0) { - synchronized(this) { - finished = true; - notifyAll(); - } - } - } } diff --git a/components/net/sf/briar/plugins/bluetooth/InvitationListener.java b/components/net/sf/briar/plugins/bluetooth/InvitationListener.java index 46265bdb2f..9ed9f10c19 100644 --- a/components/net/sf/briar/plugins/bluetooth/InvitationListener.java +++ b/components/net/sf/briar/plugins/bluetooth/InvitationListener.java @@ -2,7 +2,6 @@ package net.sf.briar.plugins.bluetooth; import java.util.Collections; import java.util.Enumeration; -import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -10,25 +9,21 @@ import javax.bluetooth.BluetoothStateException; import javax.bluetooth.DataElement; import javax.bluetooth.DeviceClass; import javax.bluetooth.DiscoveryAgent; -import javax.bluetooth.DiscoveryListener; import javax.bluetooth.RemoteDevice; import javax.bluetooth.ServiceRecord; import javax.bluetooth.UUID; -class InvitationListener implements DiscoveryListener { +class InvitationListener extends AbstractListener { private static final Logger LOG = Logger.getLogger(InvitationListener.class.getName()); - private final AtomicInteger searches = new AtomicInteger(1); - private final DiscoveryAgent discoveryAgent; private final String uuid; private String url = null; // Locking: this - private boolean finished = false; // Locking: this InvitationListener(DiscoveryAgent discoveryAgent, String uuid) { - this.discoveryAgent = discoveryAgent; + super(discoveryAgent); this.uuid = uuid; } @@ -54,15 +49,6 @@ class InvitationListener implements DiscoveryListener { } } - public void inquiryCompleted(int discoveryType) { - if(searches.decrementAndGet() == 0) { - synchronized(this) { - finished = true; - notifyAll(); - } - } - } - public void servicesDiscovered(int transaction, ServiceRecord[] services) { for(ServiceRecord record : services) { // Does this service have a URL? @@ -71,31 +57,25 @@ class InvitationListener implements DiscoveryListener { if(serviceUrl == null) continue; // Does this service have the UUID we're looking for? DataElement classIds = record.getAttributeValue(0x1); - if(classIds == null) continue; - @SuppressWarnings("unchecked") - Enumeration<DataElement> e = - (Enumeration<DataElement>) classIds.getValue(); - for(DataElement classId : Collections.list(e)) { - UUID serviceUuid = (UUID) classId.getValue(); - if(uuid.equalsIgnoreCase(serviceUuid.toString())) { - // The UUID matches - store the URL - synchronized(this) { - url = serviceUrl; - finished = true; - notifyAll(); + Object o = getDataElementValue(classIds); + if(o instanceof Enumeration) { + Enumeration<?> e = (Enumeration<?>) o; + for(Object o1 : Collections.list(e)) { + Object o2 = getDataElementValue(o1); + if(o2 instanceof UUID) { + UUID serviceUuid = (UUID) o2; + if(uuid.equalsIgnoreCase(serviceUuid.toString())) { + // The UUID matches - store the URL + synchronized(this) { + url = serviceUrl; + finished = true; + notifyAll(); + return; + } + } } - return; } } } } - - public void serviceSearchCompleted(int transaction, int response) { - if(searches.decrementAndGet() == 0) { - synchronized(this) { - finished = true; - notifyAll(); - } - } - } } -- GitLab