Whitespace-only code formatting changes.

This commit is contained in:
akwizgran
2015-11-30 09:38:25 +00:00
parent 1950c13ffb
commit 027ae8340f
202 changed files with 2993 additions and 2993 deletions

View File

@@ -81,13 +81,13 @@ class BluetoothPlugin implements DuplexPlugin {
// Initialise the Bluetooth stack
try {
localDevice = LocalDevice.getLocalDevice();
} catch(UnsatisfiedLinkError e) {
} catch (UnsatisfiedLinkError e) {
// On Linux the user may need to install libbluetooth-dev
if(OsUtils.isLinux())
if (OsUtils.isLinux())
callback.showMessage("BLUETOOTH_INSTALL_LIBS");
return false;
}
if(LOG.isLoggable(INFO))
if (LOG.isLoggable(INFO))
LOG.info("Local address " + localDevice.getBluetoothAddress());
running = true;
bind();
@@ -97,7 +97,7 @@ class BluetoothPlugin implements DuplexPlugin {
private void bind() {
ioExecutor.execute(new Runnable() {
public void run() {
if(!running) return;
if (!running) return;
// Advertise the Bluetooth address to contacts
TransportProperties p = new TransportProperties();
p.put("address", localDevice.getBluetoothAddress());
@@ -107,12 +107,12 @@ class BluetoothPlugin implements DuplexPlugin {
StreamConnectionNotifier ss;
try {
ss = (StreamConnectionNotifier) Connector.open(url);
} catch(IOException e) {
if(LOG.isLoggable(WARNING))
} catch (IOException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
return;
}
if(!running) {
if (!running) {
tryToClose(ss);
return;
}
@@ -129,7 +129,7 @@ class BluetoothPlugin implements DuplexPlugin {
private String getUuid() {
String uuid = callback.getLocalProperties().get("uuid");
if(uuid == null) {
if (uuid == null) {
byte[] random = new byte[UUID_BYTES];
secureRandom.nextBytes(random);
uuid = UUID.nameUUIDFromBytes(random).toString();
@@ -142,24 +142,24 @@ class BluetoothPlugin implements DuplexPlugin {
private void tryToClose(StreamConnectionNotifier ss) {
try {
if(ss != null) ss.close();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (ss != null) ss.close();
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
private void acceptContactConnections(StreamConnectionNotifier ss) {
while(true) {
while (true) {
StreamConnection s;
try {
s = ss.acceptAndOpen();
} catch(IOException e) {
} catch (IOException e) {
// This is expected when the socket is closed
if(LOG.isLoggable(INFO)) LOG.info(e.toString());
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
return;
}
callback.incomingConnectionCreated(wrapSocket(s));
if(!running) return;
if (!running) return;
}
}
@@ -185,22 +185,22 @@ class BluetoothPlugin implements DuplexPlugin {
}
public void poll(final Collection<ContactId> connected) {
if(!running) return;
if (!running) return;
// Try to connect to known devices in parallel
Map<ContactId, TransportProperties> remote =
callback.getRemoteProperties();
for(Entry<ContactId, TransportProperties> e : remote.entrySet()) {
for (Entry<ContactId, TransportProperties> e : remote.entrySet()) {
final ContactId c = e.getKey();
if(connected.contains(c)) continue;
if (connected.contains(c)) continue;
final String address = e.getValue().get("address");
if(StringUtils.isNullOrEmpty(address)) continue;
if (StringUtils.isNullOrEmpty(address)) continue;
final String uuid = e.getValue().get("uuid");
if(StringUtils.isNullOrEmpty(uuid)) continue;
if (StringUtils.isNullOrEmpty(uuid)) continue;
ioExecutor.execute(new Runnable() {
public void run() {
if(!running) return;
if (!running) return;
StreamConnection s = connect(makeUrl(address, uuid));
if(s != null)
if (s != null)
callback.outgoingConnectionCreated(c, wrapSocket(s));
}
});
@@ -208,28 +208,28 @@ class BluetoothPlugin implements DuplexPlugin {
}
private StreamConnection connect(String url) {
if(LOG.isLoggable(INFO)) LOG.info("Connecting to " + url);
if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + url);
try {
StreamConnection s = (StreamConnection) Connector.open(url);
if(LOG.isLoggable(INFO)) LOG.info("Connected to " + url);
if (LOG.isLoggable(INFO)) LOG.info("Connected to " + url);
return s;
} catch(IOException e) {
if(LOG.isLoggable(INFO)) LOG.info("Could not connect to " + url);
} catch (IOException e) {
if (LOG.isLoggable(INFO)) LOG.info("Could not connect to " + url);
return null;
}
}
public DuplexTransportConnection createConnection(ContactId c) {
if(!running) return null;
if (!running) return null;
TransportProperties p = callback.getRemoteProperties().get(c);
if(p == null) return null;
if (p == null) return null;
String address = p.get("address");
if(StringUtils.isNullOrEmpty(address)) return null;
if (StringUtils.isNullOrEmpty(address)) return null;
String uuid = p.get("uuid");
if(StringUtils.isNullOrEmpty(uuid)) return null;
if (StringUtils.isNullOrEmpty(uuid)) return null;
String url = makeUrl(address, uuid);
StreamConnection s = connect(url);
if(s == null) return null;
if (s == null) return null;
return new BluetoothTransportConnection(this, s);
}
@@ -239,7 +239,7 @@ class BluetoothPlugin implements DuplexPlugin {
public DuplexTransportConnection createInvitationConnection(PseudoRandom r,
long timeout) {
if(!running) return null;
if (!running) return null;
// Use the invitation codes to generate the UUID
byte[] b = r.nextBytes(UUID_BYTES);
String uuid = UUID.nameUUIDFromBytes(b).toString();
@@ -250,11 +250,11 @@ class BluetoothPlugin implements DuplexPlugin {
final StreamConnectionNotifier ss;
try {
ss = (StreamConnectionNotifier) Connector.open(url);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return null;
}
if(!running) {
if (!running) {
tryToClose(ss);
return null;
}
@@ -266,8 +266,8 @@ class BluetoothPlugin implements DuplexPlugin {
// Wait for an incoming or outgoing connection
try {
StreamConnection s = socketLatch.waitForReference(timeout);
if(s != null) return new BluetoothTransportConnection(this, s);
} catch(InterruptedException e) {
if (s != null) return new BluetoothTransportConnection(this, s);
} catch (InterruptedException e) {
LOG.warning("Interrupted while exchanging invitations");
Thread.currentThread().interrupt();
} finally {
@@ -281,8 +281,8 @@ class BluetoothPlugin implements DuplexPlugin {
// Try to make the device discoverable (requires root on Linux)
try {
localDevice.setDiscoverable(GIAC);
} catch(BluetoothStateException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (BluetoothStateException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
@@ -304,8 +304,8 @@ class BluetoothPlugin implements DuplexPlugin {
DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
long now = clock.currentTimeMillis();
long end = now + timeout;
while(now < end && running && !socketLatch.isSet()) {
if(!discoverySemaphore.tryAcquire()) {
while (now < end && running && !socketLatch.isSet()) {
if (!discoverySemaphore.tryAcquire()) {
LOG.info("Another device discovery is in progress");
return;
}
@@ -314,20 +314,20 @@ class BluetoothPlugin implements DuplexPlugin {
new InvitationListener(discoveryAgent, uuid);
discoveryAgent.startInquiry(GIAC, listener);
String url = listener.waitForUrl();
if(url == null) continue;
if (url == null) continue;
StreamConnection s = connect(url);
if(s == null) continue;
if (s == null) continue;
LOG.info("Outgoing connection");
if(!socketLatch.set(s)) {
if (!socketLatch.set(s)) {
LOG.info("Closing redundant connection");
tryToClose(s);
}
return;
} catch(BluetoothStateException e) {
if(LOG.isLoggable(WARNING))
} catch (BluetoothStateException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
return;
} catch(InterruptedException e) {
} catch (InterruptedException e) {
LOG.warning("Interrupted while waiting for URL");
Thread.currentThread().interrupt();
return;
@@ -339,9 +339,9 @@ class BluetoothPlugin implements DuplexPlugin {
private void tryToClose(StreamConnection s) {
try {
if(s != null) s.close();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (s != null) s.close();
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
}
@@ -365,13 +365,13 @@ class BluetoothPlugin implements DuplexPlugin {
try {
StreamConnection s = serverSocket.acceptAndOpen();
LOG.info("Incoming connection");
if(!socketLatch.set(s)) {
if (!socketLatch.set(s)) {
LOG.info("Closing redundant connection");
s.close();
}
} catch(IOException e) {
} catch (IOException e) {
// This is expected when the socket is closed
if(LOG.isLoggable(INFO)) LOG.info(e.toString());
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
}
}
}

View File

@@ -49,8 +49,8 @@ class BluetoothTransportConnection implements DuplexTransportConnection {
public void dispose(boolean exception, boolean recognised)
throws IOException {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) stream.close();
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) stream.close();
}
}
@@ -73,8 +73,8 @@ class BluetoothTransportConnection implements DuplexTransportConnection {
}
public void dispose(boolean exception) throws IOException {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) stream.close();
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) stream.close();
}
}
}

View File

@@ -47,22 +47,22 @@ class InvitationListener implements DiscoveryListener {
try {
discoveryAgent.searchServices(null, uuids, device, this);
searches.incrementAndGet();
} catch(BluetoothStateException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (BluetoothStateException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
public void servicesDiscovered(int transaction, ServiceRecord[] services) {
for(ServiceRecord record : services) {
for (ServiceRecord record : services) {
// Does this service have a URL?
String serviceUrl = record.getConnectionURL(
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if(serviceUrl == null) continue;
if (serviceUrl == null) continue;
// Does this service have the UUID we're looking for?
Collection<String> uuids = new TreeSet<String>();
findNestedClassIds(record.getAttributeValue(0x1), uuids);
for(String u : uuids) {
if(uuid.equalsIgnoreCase(u)) {
for (String u : uuids) {
if (uuid.equalsIgnoreCase(u)) {
// The UUID matches - store the URL
url = serviceUrl;
finished.countDown();
@@ -73,30 +73,30 @@ class InvitationListener implements DiscoveryListener {
}
public void inquiryCompleted(int discoveryType) {
if(searches.decrementAndGet() == 0) finished.countDown();
if (searches.decrementAndGet() == 0) finished.countDown();
}
public void serviceSearchCompleted(int transaction, int response) {
if(searches.decrementAndGet() == 0) finished.countDown();
if (searches.decrementAndGet() == 0) finished.countDown();
}
// UUIDs are sometimes buried in nested data elements
private void findNestedClassIds(Object o, Collection<String> ids) {
o = getDataElementValue(o);
if(o instanceof Enumeration<?>) {
for(Object o1 : Collections.list((Enumeration<?>) o))
if (o instanceof Enumeration<?>) {
for (Object o1 : Collections.list((Enumeration<?>) o))
findNestedClassIds(o1, ids);
} else if(o instanceof UUID) {
} else if (o instanceof UUID) {
ids.add(o.toString());
}
}
private Object getDataElementValue(Object o) {
if(o instanceof DataElement) {
if (o instanceof DataElement) {
// Bluecove throws an exception if the type is unknown
try {
return ((DataElement) o).getValue();
} catch(ClassCastException e) {
} catch (ClassCastException e) {
return null;
}
}

View File

@@ -53,24 +53,24 @@ class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
public void run() {
try {
Collection<File> drives = finder.findRemovableDrives();
while(running) {
while (running) {
pollingLock.lock();
try {
stopPolling.await(pollingInterval, MILLISECONDS);
} finally {
pollingLock.unlock();
}
if(!running) return;
if (!running) return;
Collection<File> newDrives = finder.findRemovableDrives();
for(File f : newDrives) {
if(!drives.contains(f)) callback.driveInserted(f);
for (File f : newDrives) {
if (!drives.contains(f)) callback.driveInserted(f);
}
drives = newDrives;
}
} catch(InterruptedException e) {
} catch (InterruptedException e) {
LOG.warning("Interrupted while waiting to poll");
Thread.currentThread().interrupt();
} catch(IOException e) {
} catch (IOException e) {
callback.exceptionThrown(e);
}
}

View File

@@ -67,16 +67,16 @@ implements RemovableDriveMonitor.Callback {
try {
List<File> drives =
new ArrayList<File>(finder.findRemovableDrives());
if(drives.isEmpty()) return null;
if (drives.isEmpty()) return null;
String[] paths = new String[drives.size()];
for(int i = 0; i < paths.length; i++) {
for (int i = 0; i < paths.length; i++) {
paths[i] = drives.get(i).getPath();
}
int i = callback.showChoice(paths, "REMOVABLE_DRIVE_CHOOSE_DRIVE");
if(i == -1) return null;
if (i == -1) return null;
return drives.get(i);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return null;
}
}
@@ -95,29 +95,29 @@ implements RemovableDriveMonitor.Callback {
protected Collection<File> findFilesByName(String filename) {
List<File> matches = new ArrayList<File>();
try {
for(File drive : finder.findRemovableDrives()) {
for (File drive : finder.findRemovableDrives()) {
File[] files = drive.listFiles();
if(files != null) {
for(File f : files) {
if(f.isFile() && filename.equals(f.getName()))
if (files != null) {
for (File f : files) {
if (f.isFile() && filename.equals(f.getName()))
matches.add(f);
}
}
}
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
return Collections.unmodifiableList(matches);
}
public void driveInserted(File root) {
File[] files = root.listFiles();
if(files != null) {
for(File f : files) if(f.isFile()) createReaderFromFile(f);
if (files != null) {
for (File f : files) if (f.isFile()) createReaderFromFile(f);
}
}
public void exceptionThrown(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}

View File

@@ -31,18 +31,18 @@ public class RemovableDrivePluginFactory implements SimplexPluginFactory {
public SimplexPlugin createPlugin(SimplexPluginCallback callback) {
RemovableDriveFinder finder;
RemovableDriveMonitor monitor;
if(OsUtils.isLinux()) {
if (OsUtils.isLinux()) {
finder = new LinuxRemovableDriveFinder();
monitor = new LinuxRemovableDriveMonitor();
} else if(OsUtils.isMacLeopardOrNewer()) {
} else if (OsUtils.isMacLeopardOrNewer()) {
finder = new MacRemovableDriveFinder();
monitor = new MacRemovableDriveMonitor();
} else if(OsUtils.isMac()) {
} else if (OsUtils.isMac()) {
// JNotify requires OS X 10.5 or newer, so we have to poll
finder = new MacRemovableDriveFinder();
monitor = new PollingRemovableDriveMonitor(ioExecutor, finder,
POLLING_INTERVAL);
} else if(OsUtils.isWindows()) {
} else if (OsUtils.isWindows()) {
finder = new WindowsRemovableDriveFinder();
monitor = new PollingRemovableDriveMonitor(ioExecutor, finder,
POLLING_INTERVAL);

View File

@@ -18,17 +18,17 @@ abstract class UnixRemovableDriveFinder implements RemovableDriveFinder {
Process p = new ProcessBuilder(getMountCommand()).start();
Scanner s = new Scanner(p.getInputStream(), "UTF-8");
try {
while(s.hasNextLine()) {
while (s.hasNextLine()) {
String line = s.nextLine();
String[] tokens = line.split(" ");
if(tokens.length < 3) continue;
if (tokens.length < 3) continue;
// The general format is "/dev/foo on /bar/baz ..."
if(tokens[0].startsWith("/dev/") && tokens[1].equals("on")) {
if (tokens[0].startsWith("/dev/") && tokens[1].equals("on")) {
// The path may contain spaces so we can't use tokens[2]
String path = parseMountPoint(line);
if(isRemovableDriveMountPoint(path)) {
if (isRemovableDriveMountPoint(path)) {
File f = new File(path);
if(f.exists() && f.isDirectory()) drives.add(f);
if (f.exists() && f.isDirectory()) drives.add(f);
}
}
}

View File

@@ -33,9 +33,9 @@ JNotifyListener {
try {
Class.forName("net.contentobjects.jnotify.JNotify");
return null;
} catch(UnsatisfiedLinkError e) {
} catch (UnsatisfiedLinkError e) {
return e;
} catch(ClassNotFoundException e) {
} catch (ClassNotFoundException e) {
return e;
}
}
@@ -43,11 +43,11 @@ JNotifyListener {
public static void checkEnabled() throws IOException {
staticSynchLock.lock();
try {
if(!triedLoad) {
if (!triedLoad) {
loadError = tryLoad();
triedLoad = true;
}
if(loadError != null) throw new IOException(loadError.toString());
if (loadError != null) throw new IOException(loadError.toString());
} finally {
staticSynchLock.unlock();
}
@@ -57,8 +57,8 @@ JNotifyListener {
checkEnabled();
List<Integer> watches = new ArrayList<Integer>();
int mask = JNotify.FILE_CREATED;
for(String path : getPathsToWatch()) {
if(new File(path).exists())
for (String path : getPathsToWatch()) {
if (new File(path).exists())
watches.add(JNotify.addWatch(path, mask, false, this));
}
synchLock.lock();
@@ -87,7 +87,7 @@ JNotifyListener {
} finally {
synchLock.unlock();
}
for(Integer w : watches) JNotify.removeWatch(w);
for (Integer w : watches) JNotify.removeWatch(w);
}
public void fileCreated(int wd, String rootPath, String name) {
@@ -98,7 +98,7 @@ JNotifyListener {
} finally {
synchLock.unlock();
}
if(callback != null)
if (callback != null)
callback.driveInserted(new File(rootPath + "/" + name));
}

View File

@@ -16,13 +16,13 @@ class WindowsRemovableDriveFinder implements RemovableDriveFinder {
public Collection<File> findRemovableDrives() throws IOException {
File[] roots = File.listRoots();
if(roots == null) throw new IOException();
if (roots == null) throw new IOException();
List<File> drives = new ArrayList<File>();
for(File root : roots) {
for (File root : roots) {
try {
int type = Kernel32.INSTANCE.GetDriveType(root.getPath());
if(type == DRIVE_REMOVABLE) drives.add(root);
} catch(RuntimeException e) {
if (type == DRIVE_REMOVABLE) drives.add(root);
} catch (RuntimeException e) {
throw new IOException(e);
}
}

View File

@@ -251,23 +251,23 @@ class CountryCodes {
new HashMap<String, Country>();
static {
for(Country c : COUNTRIES) COUNTRY_MAP.put(c.iso3166, c);
for (Country c : COUNTRIES) COUNTRY_MAP.put(c.iso3166, c);
}
static String translate(String number, String callerIso, String calleeIso) {
Country from = COUNTRY_MAP.get(callerIso);
Country to = COUNTRY_MAP.get(calleeIso);
if(from == null || to == null) return null;
if (from == null || to == null) return null;
// Strip any prefixes and country codes from the number
String plusCountryCode = "+" + to.countryCode;
String iddCountryCode = to.idd + to.countryCode;
if(number.startsWith(plusCountryCode))
if (number.startsWith(plusCountryCode))
number = number.substring(plusCountryCode.length());
else if(number.startsWith(iddCountryCode))
else if (number.startsWith(iddCountryCode))
number = number.substring(iddCountryCode.length());
else if(number.startsWith(to.ndd))
else if (number.startsWith(to.ndd))
number = number.substring(to.ndd.length());
if(from == to) return from.ndd + number; // National
if (from == to) return from.ndd + number; // National
return from.idd + to.countryCode + number; // International
}

View File

@@ -69,7 +69,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
LOG.info("Starting");
try {
stateChange.acquire();
} catch(InterruptedException e) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Interrupted while waiting to start");
}
@@ -79,13 +79,13 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
// Find a suitable baud rate and initialise the modem
try {
boolean foundBaudRate = false;
for(int baudRate : BAUD_RATES) {
if(port.setParams(baudRate, 8, 1, 0)) {
for (int baudRate : BAUD_RATES) {
if (port.setParams(baudRate, 8, 1, 0)) {
foundBaudRate = true;
break;
}
}
if(!foundBaudRate) {
if (!foundBaudRate) {
tryToClose(port);
throw new IOException("No suitable baud rate");
}
@@ -93,7 +93,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
port.addEventListener(this);
port.writeBytes("ATZ\r\n".getBytes("US-ASCII")); // Reset
port.writeBytes("ATE0\r\n".getBytes("US-ASCII")); // Echo off
} catch(IOException e) {
} catch (IOException e) {
tryToClose(port);
throw e;
}
@@ -104,7 +104,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
try {
long now = clock.currentTimeMillis();
long end = now + OK_TIMEOUT;
while(now < end && !initialised) {
while (now < end && !initialised) {
initialisedStateChanged.await(end - now, MILLISECONDS);
now = clock.currentTimeMillis();
}
@@ -112,12 +112,12 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
} finally {
synchLock.unlock();
}
} catch(InterruptedException e) {
} catch (InterruptedException e) {
tryToClose(port);
Thread.currentThread().interrupt();
throw new IOException("Interrupted while initialising");
}
if(success) return true;
if (success) return true;
tryToClose(port);
return false;
} finally {
@@ -127,9 +127,9 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
private void tryToClose(SerialPort port) {
try {
if(port != null) port.closePort();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (port != null) port.closePort();
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
@@ -148,7 +148,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
// Hang up if necessary and close the port
try {
stateChange.acquire();
} catch(InterruptedException e) {
} catch (InterruptedException e) {
tryToClose(port);
Thread.currentThread().interrupt();
throw new IOException("Interrupted while waiting to stop");
@@ -166,7 +166,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
ReliabilityLayer reliability;
synchLock.lock();
try {
if(this.reliability == null) {
if (this.reliability == null) {
LOG.info("Not hanging up - already on the hook");
return;
}
@@ -183,18 +183,18 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
port.writeBytes("+++".getBytes("US-ASCII"));
clock.sleep(ESCAPE_SEQUENCE_GUARD_TIME);
port.writeBytes("ATH\r\n".getBytes("US-ASCII"));
} catch(InterruptedException e) {
} catch (InterruptedException e) {
tryToClose(port);
Thread.currentThread().interrupt();
throw new IOException("Interrupted while hanging up");
} catch(IOException e) {
} catch (IOException e) {
tryToClose(port);
throw e;
}
}
public boolean dial(String number) throws IOException {
if(!stateChange.tryAcquire()) {
if (!stateChange.tryAcquire()) {
LOG.info("Not dialling - state change in progress");
return false;
}
@@ -203,11 +203,11 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
reliabilityFactory.createReliabilityLayer(this);
synchLock.lock();
try {
if(!initialised) {
if (!initialised) {
LOG.info("Not dialling - modem not initialised");
return false;
}
if(this.reliability != null) {
if (this.reliability != null) {
LOG.info("Not dialling - call in progress");
return false;
}
@@ -220,7 +220,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
try {
String dial = "ATDT" + number + "\r\n";
port.writeBytes(dial.getBytes("US-ASCII"));
} catch(IOException e) {
} catch (IOException e) {
tryToClose(port);
throw e;
}
@@ -230,15 +230,15 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
try {
long now = clock.currentTimeMillis();
long end = now + CONNECT_TIMEOUT;
while(now < end && initialised && !connected) {
while (now < end && initialised && !connected) {
connectedStateChanged.await(end - now, MILLISECONDS);
now = clock.currentTimeMillis();
}
if(connected) return true;
if (connected) return true;
} finally {
synchLock.unlock();
}
} catch(InterruptedException e) {
} catch (InterruptedException e) {
tryToClose(port);
Thread.currentThread().interrupt();
throw new IOException("Interrupted while dialling");
@@ -258,7 +258,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
} finally {
synchLock.unlock();
}
if(reliability == null) throw new IOException("Not connected");
if (reliability == null) throw new IOException("Not connected");
return reliability.getInputStream();
}
@@ -270,14 +270,14 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
} finally {
synchLock.unlock();
}
if(reliability == null) throw new IOException("Not connected");
if (reliability == null) throw new IOException("Not connected");
return reliability.getOutputStream();
}
public void hangUp() throws IOException {
try {
stateChange.acquire();
} catch(InterruptedException e) {
} catch (InterruptedException e) {
tryToClose(port);
Thread.currentThread().interrupt();
throw new IOException("Interrupted while waiting to hang up");
@@ -292,7 +292,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
public void handleWrite(byte[] b) throws IOException {
try {
port.writeBytes(b);
} catch(IOException e) {
} catch (IOException e) {
tryToClose(port);
throw e;
}
@@ -300,20 +300,20 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
public void serialEvent(SerialPortEvent ev) {
try {
if(ev.isRXCHAR()) {
if (ev.isRXCHAR()) {
byte[] b = port.readBytes();
if(!handleData(b)) handleText(b);
} else if(ev.isDSR() && ev.getEventValue() == 0) {
if (!handleData(b)) handleText(b);
} else if (ev.isDSR() && ev.getEventValue() == 0) {
LOG.info("Remote end hung up");
hangUp();
} else {
if(LOG.isLoggable(INFO)) {
if (LOG.isLoggable(INFO)) {
LOG.info("Serial event " + ev.getEventType() + " " +
ev.getEventValue());
}
}
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
@@ -325,24 +325,24 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
} finally {
synchLock.unlock();
}
if(reliability == null) return false;
if (reliability == null) return false;
reliability.handleRead(b);
return true;
}
private void handleText(byte[] b) throws IOException {
if(lineLen + b.length > MAX_LINE_LENGTH) {
if (lineLen + b.length > MAX_LINE_LENGTH) {
tryToClose(port);
throw new IOException("Line too long");
}
for(int i = 0; i < b.length; i++) {
for (int i = 0; i < b.length; i++) {
line[lineLen] = b[i];
if(b[i] == '\n') {
if (b[i] == '\n') {
// FIXME: Use CharsetDecoder to catch invalid ASCII
String s = new String(line, 0, lineLen, "US-ASCII").trim();
lineLen = 0;
if(LOG.isLoggable(INFO)) LOG.info("Modem status: " + s);
if(s.startsWith("CONNECT")) {
if (LOG.isLoggable(INFO)) LOG.info("Modem status: " + s);
if (s.startsWith("CONNECT")) {
synchLock.lock();
try {
connected = true;
@@ -352,13 +352,13 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
}
// There might be data in the buffer as well as text
int off = i + 1;
if(off < b.length) {
if (off < b.length) {
byte[] data = new byte[b.length - off];
System.arraycopy(b, off, data, 0, data.length);
handleData(data);
}
return;
} else if(s.equals("BUSY") || s.equals("NO DIALTONE")
} else if (s.equals("BUSY") || s.equals("NO DIALTONE")
|| s.equals("NO CARRIER")) {
synchLock.lock();
try {
@@ -367,7 +367,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
} finally {
synchLock.unlock();
}
} else if(s.equals("OK")) {
} else if (s.equals("OK")) {
synchLock.lock();
try {
initialised = true;
@@ -375,13 +375,13 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
} finally {
synchLock.unlock();
}
} else if(s.equals("RING")) {
} else if (s.equals("RING")) {
ioExecutor.execute(new Runnable() {
public void run() {
try {
answer();
} catch(IOException e) {
if(LOG.isLoggable(WARNING))
} catch (IOException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
@@ -394,7 +394,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
}
private void answer() throws IOException {
if(!stateChange.tryAcquire()) {
if (!stateChange.tryAcquire()) {
LOG.info("Not answering - state change in progress");
return;
}
@@ -403,11 +403,11 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
reliabilityFactory.createReliabilityLayer(this);
synchLock.lock();
try {
if(!initialised) {
if (!initialised) {
LOG.info("Not answering - modem not initialised");
return;
}
if(this.reliability != null) {
if (this.reliability != null) {
LOG.info("Not answering - call in progress");
return;
}
@@ -419,7 +419,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
LOG.info("Answering");
try {
port.writeBytes("ATA\r\n".getBytes("US-ASCII"));
} catch(IOException e) {
} catch (IOException e) {
tryToClose(port);
throw e;
}
@@ -430,7 +430,7 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
try {
long now = clock.currentTimeMillis();
long end = now + CONNECT_TIMEOUT;
while(now < end && initialised && !connected) {
while (now < end && initialised && !connected) {
connectedStateChanged.await(end - now, MILLISECONDS);
now = clock.currentTimeMillis();
}
@@ -438,12 +438,12 @@ class ModemImpl implements Modem, WriteHandler, SerialPortEventListener {
} finally {
synchLock.unlock();
}
} catch(InterruptedException e) {
} catch (InterruptedException e) {
tryToClose(port);
Thread.currentThread().interrupt();
throw new IOException("Interrupted while answering");
}
if(success) callback.incomingCallConnected();
if (success) callback.incomingCallConnected();
else hangUpInner();
} finally {
stateChange.release();

View File

@@ -59,18 +59,18 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
}
public boolean start() {
for(String portName : serialPortList.getPortNames()) {
if(LOG.isLoggable(INFO))
for (String portName : serialPortList.getPortNames()) {
if (LOG.isLoggable(INFO))
LOG.info("Trying to initialise modem on " + portName);
modem = modemFactory.createModem(this, portName);
try {
if(!modem.start()) continue;
if(LOG.isLoggable(INFO))
if (!modem.start()) continue;
if (LOG.isLoggable(INFO))
LOG.info("Initialised modem on " + portName);
running = true;
return true;
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
return false;
@@ -78,11 +78,11 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
public void stop() {
running = false;
if(modem != null) {
if (modem != null) {
try {
modem.stop();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
}
@@ -104,18 +104,18 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
}
boolean resetModem() {
if(!running) return false;
for(String portName : serialPortList.getPortNames()) {
if(LOG.isLoggable(INFO))
if (!running) return false;
for (String portName : serialPortList.getPortNames()) {
if (LOG.isLoggable(INFO))
LOG.info("Trying to initialise modem on " + portName);
modem = modemFactory.createModem(this, portName);
try {
if(!modem.start()) continue;
if(LOG.isLoggable(INFO))
if (!modem.start()) continue;
if (LOG.isLoggable(INFO))
LOG.info("Initialised modem on " + portName);
return true;
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
running = false;
@@ -123,26 +123,26 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
}
public DuplexTransportConnection createConnection(ContactId c) {
if(!running) return null;
if (!running) return null;
// Get the ISO 3166 code for the caller's country
String fromIso = callback.getLocalProperties().get("iso3166");
if(StringUtils.isNullOrEmpty(fromIso)) return null;
if (StringUtils.isNullOrEmpty(fromIso)) return null;
// Get the ISO 3166 code for the callee's country
TransportProperties properties = callback.getRemoteProperties().get(c);
if(properties == null) return null;
if (properties == null) return null;
String toIso = properties.get("iso3166");
if(StringUtils.isNullOrEmpty(toIso)) return null;
if (StringUtils.isNullOrEmpty(toIso)) return null;
// Get the callee's phone number
String number = properties.get("number");
if(StringUtils.isNullOrEmpty(number)) return null;
if (StringUtils.isNullOrEmpty(number)) return null;
// Convert the number into direct dialling form
number = CountryCodes.translate(number, fromIso, toIso);
if(number == null) return null;
if (number == null) return null;
// Dial the number
try {
if(!modem.dial(number)) return null;
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (!modem.dial(number)) return null;
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
resetModem();
return null;
}
@@ -184,11 +184,11 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
LOG.info("Call disconnected");
try {
modem.hangUp();
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
exception = true;
}
if(exception) resetModem();
if (exception) resetModem();
disposalFinished.countDown();
}
@@ -203,8 +203,8 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
}
public void dispose(boolean exception, boolean recognised) {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) hangUp(exception);
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) hangUp(exception);
}
}
@@ -227,8 +227,8 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
}
public void dispose(boolean exception) {
if(halfClosed.getAndSet(true) || exception)
if(!closed.getAndSet(true)) hangUp(exception);
if (halfClosed.getAndSet(true) || exception)
if (!closed.getAndSet(true)) hangUp(exception);
}
}
}

View File

@@ -29,7 +29,7 @@ public class ModemPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
// This plugin is not enabled by default
String enabled = callback.getConfig().get("enabled");
if(StringUtils.isNullOrEmpty(enabled)) return null;
if (StringUtils.isNullOrEmpty(enabled)) return null;
return new ModemPlugin(modemFactory, serialPortList, callback,
MAX_LATENCY);
}

View File

@@ -15,16 +15,16 @@ class SerialPortImpl implements SerialPort {
public void openPort() throws IOException {
try {
if(!port.openPort()) throw new IOException("Failed to open port");
} catch(SerialPortException e) {
if (!port.openPort()) throw new IOException("Failed to open port");
} catch (SerialPortException e) {
throw new IOException(e);
}
}
public void closePort() throws IOException {
try {
if(!port.closePort()) throw new IOException("Failed to close port");
} catch(SerialPortException e) {
if (!port.closePort()) throw new IOException("Failed to close port");
} catch (SerialPortException e) {
throw new IOException(e);
}
}
@@ -33,16 +33,16 @@ class SerialPortImpl implements SerialPort {
int parityBits) throws IOException {
try {
return port.setParams(baudRate, dataBits, stopBits, parityBits);
} catch(SerialPortException e) {
} catch (SerialPortException e) {
throw new IOException(e);
}
}
public void purgePort(int flags) throws IOException {
try {
if(!port.purgePort(flags))
if (!port.purgePort(flags))
throw new IOException("Failed to purge port");
} catch(SerialPortException e) {
} catch (SerialPortException e) {
throw new IOException(e);
}
}
@@ -50,7 +50,7 @@ class SerialPortImpl implements SerialPort {
public void addEventListener(SerialPortEventListener l) throws IOException {
try {
port.addEventListener(l);
} catch(SerialPortException e) {
} catch (SerialPortException e) {
throw new IOException(e);
}
}
@@ -58,15 +58,15 @@ class SerialPortImpl implements SerialPort {
public byte[] readBytes() throws IOException {
try {
return port.readBytes();
} catch(SerialPortException e) {
} catch (SerialPortException e) {
throw new IOException(e);
}
}
public void writeBytes(byte[] b) throws IOException {
try {
if(!port.writeBytes(b)) throw new IOException("Failed to write");
} catch(SerialPortException e) {
if (!port.writeBytes(b)) throw new IOException("Failed to write");
} catch (SerialPortException e) {
throw new IOException(e);
}
}