Clean up exceptions
This commit is contained in:
parent
e6be7dbb8d
commit
6103198ea3
@ -21,7 +21,6 @@ import javax.usb.UsbDevice;
|
||||
import javax.usb.UsbDeviceDescriptor;
|
||||
import javax.usb.UsbDisconnectedException;
|
||||
import javax.usb.UsbException;
|
||||
import javax.usb.UsbHostManager;
|
||||
import javax.usb.UsbPort;
|
||||
import javax.usb.UsbStringDescriptor;
|
||||
import javax.usb.event.UsbDeviceEvent;
|
||||
@ -264,17 +263,7 @@ abstract class AbstractDevice implements UsbDevice
|
||||
|
||||
this.port = port;
|
||||
|
||||
final Services services;
|
||||
try
|
||||
{
|
||||
services = (Services) UsbHostManager.getUsbServices();
|
||||
}
|
||||
catch (final UsbException e)
|
||||
{
|
||||
// Can't happen. When we got here then USB services are already
|
||||
// loaded
|
||||
throw new Usb4JavaRuntimeException(e.toString(), e);
|
||||
}
|
||||
final Services services = Services.getInstance();
|
||||
|
||||
if (port == null)
|
||||
{
|
||||
|
||||
@ -9,7 +9,6 @@ import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import javax.usb.UsbException;
|
||||
import javax.usb.UsbHostManager;
|
||||
import javax.usb.UsbIrp;
|
||||
|
||||
|
||||
@ -171,16 +170,7 @@ abstract class AbstractIrpQueue<T extends UsbIrp>
|
||||
*/
|
||||
protected final Config getConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
return ((Services) UsbHostManager.getUsbServices()).getConfig();
|
||||
}
|
||||
catch (final UsbException e)
|
||||
{
|
||||
// Can't happen because we can't get to this point when USB
|
||||
// services are not available.
|
||||
throw new Usb4JavaRuntimeException(e.toString(), e);
|
||||
}
|
||||
return Services.getInstance().getConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -110,49 +110,44 @@ final class DeviceManager
|
||||
* Returns all currently connected devices.
|
||||
*
|
||||
* @return The connected devices.
|
||||
* @throws LibUsbException
|
||||
* When libusb reports an error while enumerating the devices.
|
||||
*/
|
||||
private Set<AbstractDevice> getConnectedDevices()
|
||||
private Set<AbstractDevice> getConnectedDevices() throws LibUsbException
|
||||
{
|
||||
final DeviceList devices = new DeviceList();
|
||||
final int result = LibUSB.getDeviceList(this.context, devices);
|
||||
if (result < 0)
|
||||
throw new Usb4JavaRuntimeException("Unable to get USB device list",
|
||||
throw new LibUsbException("Unable to get USB device list",
|
||||
result);
|
||||
final Set<AbstractDevice> found = new HashSet<AbstractDevice>();
|
||||
try
|
||||
{
|
||||
try
|
||||
for (Device libUsbDevice: devices)
|
||||
{
|
||||
for (Device libUsbDevice: devices)
|
||||
{
|
||||
final DeviceId id = createId(libUsbDevice);
|
||||
if (id == null) continue;
|
||||
final DeviceId id = createId(libUsbDevice);
|
||||
if (id == null) continue;
|
||||
|
||||
AbstractDevice device = this.devices.get(id);
|
||||
if (device == null)
|
||||
AbstractDevice device = this.devices.get(id);
|
||||
if (device == null)
|
||||
{
|
||||
final Device parent = LibUSB.getParent(libUsbDevice);
|
||||
final DeviceId parentId = createId(parent);
|
||||
final int speed = LibUSB.getDeviceSpeed(libUsbDevice);
|
||||
final boolean isHub = id.getDeviceDescriptor()
|
||||
.bDeviceClass() == LibUSB.CLASS_HUB;
|
||||
if (isHub)
|
||||
{
|
||||
final Device parent = LibUSB.getParent(libUsbDevice);
|
||||
final DeviceId parentId = createId(parent);
|
||||
final int speed = LibUSB.getDeviceSpeed(libUsbDevice);
|
||||
final boolean isHub = id.getDeviceDescriptor()
|
||||
.bDeviceClass() == LibUSB.CLASS_HUB;
|
||||
if (isHub)
|
||||
{
|
||||
device = new Hub(this, id, parentId,
|
||||
speed, libUsbDevice);
|
||||
}
|
||||
else
|
||||
{
|
||||
device = new NonHub(this, id,
|
||||
parentId, speed, libUsbDevice);
|
||||
}
|
||||
device = new Hub(this, id, parentId,
|
||||
speed, libUsbDevice);
|
||||
}
|
||||
else
|
||||
{
|
||||
device = new NonHub(this, id,
|
||||
parentId, speed, libUsbDevice);
|
||||
}
|
||||
found.add(device);
|
||||
}
|
||||
}
|
||||
catch (UsbException e)
|
||||
{
|
||||
throw new Usb4JavaRuntimeException(e.toString(), e);
|
||||
found.add(device);
|
||||
}
|
||||
}
|
||||
finally
|
||||
@ -224,9 +219,16 @@ final class DeviceManager
|
||||
*/
|
||||
public void scan()
|
||||
{
|
||||
final Set<AbstractDevice> found = getConnectedDevices();
|
||||
processRemovedDevices(found);
|
||||
processNewDevices(found);
|
||||
try
|
||||
{
|
||||
final Set<AbstractDevice> found = getConnectedDevices();
|
||||
processRemovedDevices(found);
|
||||
processNewDevices(found);
|
||||
}
|
||||
catch (LibUsbException e)
|
||||
{
|
||||
throw new ScanException("Unable to scan USB devices: " + e, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -236,19 +238,19 @@ final class DeviceManager
|
||||
* @param id
|
||||
* The id of the device to return. Must not be null.
|
||||
* @return device The libusb device. Never null.
|
||||
* @throws Usb4JavaRuntimeException
|
||||
* When an error occurred while searching for the device.
|
||||
* @throws DeviceNotFoundException
|
||||
* When the device was not found.
|
||||
* @throws LibUsbException
|
||||
* When libusb reported an error while enumerating USB devices.
|
||||
*/
|
||||
public Device getLibUsbDevice(final DeviceId id)
|
||||
public Device getLibUsbDevice(final DeviceId id) throws LibUsbException
|
||||
{
|
||||
if (id == null) throw new IllegalArgumentException("id must be set");
|
||||
|
||||
final DeviceList devices = new DeviceList();
|
||||
final int result = LibUSB.getDeviceList(this.context, devices);
|
||||
if (result < 0)
|
||||
throw new Usb4JavaRuntimeException("Unable to get USB device list",
|
||||
throw new LibUsbException("Unable to get USB device list",
|
||||
result);
|
||||
try
|
||||
{
|
||||
@ -265,7 +267,8 @@ final class DeviceManager
|
||||
{
|
||||
LibUSB.freeDeviceList(devices, true);
|
||||
}
|
||||
return null;
|
||||
|
||||
throw new DeviceNotFoundException(id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -12,7 +12,7 @@ import de.ailis.usb4java.libusb.LibUSB;
|
||||
*
|
||||
* @author Klaus Reimer (k@ailis.de)
|
||||
*/
|
||||
final class Usb4JavaRuntimeException extends RuntimeException
|
||||
final class DeviceManagerException extends RuntimeException
|
||||
{
|
||||
/** Serial version UID. */
|
||||
private static final long serialVersionUID = 1L;
|
||||
@ -28,7 +28,7 @@ final class Usb4JavaRuntimeException extends RuntimeException
|
||||
* @param errorCode
|
||||
* The error code.
|
||||
*/
|
||||
Usb4JavaRuntimeException(final String message, final int errorCode)
|
||||
DeviceManagerException(final String message, final int errorCode)
|
||||
{
|
||||
super(String.format("USB error %d: %s: %s", -errorCode, message,
|
||||
LibUSB.errorName(errorCode)));
|
||||
@ -43,7 +43,7 @@ final class Usb4JavaRuntimeException extends RuntimeException
|
||||
* @param cause
|
||||
* The root cause.
|
||||
*/
|
||||
Usb4JavaRuntimeException(final String message, final Throwable cause)
|
||||
DeviceManagerException(final String message, final Throwable cause)
|
||||
{
|
||||
super("USB error: " + message, cause);
|
||||
this.errorCode = 0;
|
||||
30
src/main/java/de/ailis/usb4java/ScanException.java
Normal file
30
src/main/java/de/ailis/usb4java/ScanException.java
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Klaus Reimer <k@ailis.de>
|
||||
* See LICENSE.txt for licensing information.
|
||||
*/
|
||||
|
||||
package de.ailis.usb4java;
|
||||
|
||||
/**
|
||||
* Thrown when USB device scan fails.
|
||||
*
|
||||
* @author Klaus Reimer (k@ailis.de)
|
||||
*/
|
||||
public final class ScanException extends RuntimeException
|
||||
{
|
||||
/** Serial version UID. */
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param message
|
||||
* The error message.
|
||||
* @param cause
|
||||
* The root cause.
|
||||
*/
|
||||
ScanException(final String message, final Throwable cause)
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@ -18,7 +18,7 @@ import de.ailis.usb4java.libusb.LoaderException;
|
||||
|
||||
/**
|
||||
* usb4java implementation of JSR-80 UsbServices.
|
||||
*
|
||||
*
|
||||
* @author Klaus Reimer (k@ailis.de)
|
||||
*/
|
||||
public final class Services implements UsbServices
|
||||
@ -47,7 +47,7 @@ public final class Services implements UsbServices
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
*
|
||||
* @throws UsbException
|
||||
* When properties could not be loaded.
|
||||
* @throws LoaderException
|
||||
@ -101,7 +101,7 @@ public final class Services implements UsbServices
|
||||
|
||||
/**
|
||||
* Informs listeners about a new attached device.
|
||||
*
|
||||
*
|
||||
* @param device
|
||||
* The new attached device.
|
||||
*/
|
||||
@ -112,7 +112,7 @@ public final class Services implements UsbServices
|
||||
|
||||
/**
|
||||
* Informs listeners about a detached device.
|
||||
*
|
||||
*
|
||||
* @param device
|
||||
* The detached device.
|
||||
*/
|
||||
@ -123,11 +123,34 @@ public final class Services implements UsbServices
|
||||
|
||||
/**
|
||||
* Returns the configuration.
|
||||
*
|
||||
*
|
||||
* @return The configuration.
|
||||
*/
|
||||
Config getConfig()
|
||||
{
|
||||
return this.config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the usb4java services.
|
||||
*
|
||||
* @return The usb4java services.
|
||||
*/
|
||||
static Services getInstance()
|
||||
{
|
||||
try
|
||||
{
|
||||
UsbServices services = UsbHostManager.getUsbServices();
|
||||
return (Services) services;
|
||||
}
|
||||
catch (final ClassCastException e)
|
||||
{
|
||||
throw new ServicesException("Looks like usb4java is not the "
|
||||
+ "configured USB services implementation: " + e, e);
|
||||
}
|
||||
catch (final UsbException e)
|
||||
{
|
||||
throw new Error("Unable to create USB services: " + e, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
src/main/java/de/ailis/usb4java/ServicesException.java
Normal file
30
src/main/java/de/ailis/usb4java/ServicesException.java
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Klaus Reimer <k@ailis.de>
|
||||
* See LICENSE.txt for licensing information.
|
||||
*/
|
||||
|
||||
package de.ailis.usb4java;
|
||||
|
||||
/**
|
||||
* Thrown when usb4java services could not be created.
|
||||
*
|
||||
* @author Klaus Reimer (k@ailis.de)
|
||||
*/
|
||||
public final class ServicesException extends RuntimeException
|
||||
{
|
||||
/** Serial version UID. */
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param message
|
||||
* The error message.
|
||||
* @param cause
|
||||
* The root cause.
|
||||
*/
|
||||
ServicesException(final String message, final Throwable cause)
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user