use of javax.microedition.io.Connection in project felix by apache.
the class ConnectorServiceTest method testMultipleScheme.
/**
* Tests ConnectionFactory service which support 3 different schemes.
*
* @throws Exception
*/
public void testMultipleScheme() throws Exception {
ConnectionFactoryMock connFactory = new ConnectionFactoryMock();
Dictionary props = new Hashtable();
props.put(ConnectionFactory.IO_SCHEME, new String[] { "file", "http", "sms" });
ServiceRegistration registration = registerConnectionFactory(connFactory, props);
ConnectorService service = getConnectorService();
Connection connection = service.open("file://test.txt");
assertEquals("file://test.txt", connFactory.getName());
assertEquals(ConnectorService.READ_WRITE, connFactory.getMode());
assertEquals(false, connFactory.isTimeout());
assertNotNull("checks returned connection", connection);
connection = service.open("http://test.txt", ConnectorService.READ);
assertEquals("http://test.txt", connFactory.getName());
assertEquals(ConnectorService.READ, connFactory.getMode());
assertEquals(false, connFactory.isTimeout());
assertNotNull("checks returned connection", connection);
connection = service.open("sms://test.txt", ConnectorService.READ);
assertEquals("sms://test.txt", connFactory.getName());
assertEquals(ConnectorService.READ, connFactory.getMode());
assertEquals(false, connFactory.isTimeout());
assertNotNull("checks returned connection", connection);
try {
connection = service.open("ftp://test.txt", ConnectorService.READ);
fail("Connection shouldn't be created");
} catch (ConnectionNotFoundException e) {
// "expected"
}
registration.unregister();
}
use of javax.microedition.io.Connection in project felix by apache.
the class ConnectorServiceTest method testLowestServiceId.
/**
* Registers two ConnectionFactory services with same IO_SCHEME. Connector Service should pickup service with lowest
* service.id.
*
* @throws Exception
*/
public void testLowestServiceId() throws Exception {
ConnectionFactoryMock connFactory = new ConnectionFactoryMock();
Dictionary props = new Hashtable();
props.put(ConnectionFactory.IO_SCHEME, "file");
ServiceRegistration registration = registerConnectionFactory(connFactory, props);
ConnectionFactoryMock connFactory2 = new ConnectionFactoryMock();
ServiceRegistration registration2 = registerConnectionFactory(connFactory2, props);
ConnectorService service = getConnectorService();
Connection connection = service.open("file://test.txt");
assertEquals("uri checks for highest service.id", null, connFactory2.getName());
assertEquals("uri checks for lowest service.id", "file://test.txt", connFactory.getName());
assertEquals(ConnectorService.READ_WRITE, connFactory.getMode());
assertEquals(false, connFactory.isTimeout());
assertNotNull("checks returned Connection", connection);
Long serviceId1 = (Long) registration.getReference().getProperty(Constants.SERVICE_ID);
Long serviceId2 = (Long) registration2.getReference().getProperty(Constants.SERVICE_ID);
assertTrue(serviceId1.longValue() < serviceId2.longValue());
registration.unregister();
registration2.unregister();
}
use of javax.microedition.io.Connection in project J2ME-Loader by nikita36078.
the class FileSystemConnectorImpl method open.
@Override
public Connection open(final String name, int mode, boolean timeouts) throws IOException {
// file://<host>/<path>
if (!name.startsWith(PROTOCOL)) {
throw new IOException("Invalid Protocol " + name);
}
Connection con = new FileSystemFileConnection(fsRoot, name.substring(PROTOCOL.length()), FileSystemConnectorImpl.this);
openConnection.add(con);
return con;
}
use of javax.microedition.io.Connection in project JMRI by JMRI.
the class LocoNetBluetoothAdapter method openPort.
@Override
public String openPort(String portName, String appName) {
int[] responseCode = new int[] { -1 };
Exception[] exception = new Exception[] { null };
try {
// Find the RemoteDevice with this name.
RemoteDevice[] devices = LocalDevice.getLocalDevice().getDiscoveryAgent().retrieveDevices(DiscoveryAgent.PREKNOWN);
if (devices != null) {
for (RemoteDevice device : devices) {
if (device.getFriendlyName(false).equals(portName)) {
Object[] waitObj = new Object[0];
// Start a search for a serialport service (UUID 0x1101)
LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(new int[] { 0x0100 }, new UUID[] { new UUID(0x1101) }, device, new DiscoveryListener() {
@Override
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
synchronized (waitObj) {
for (ServiceRecord service : servRecord) {
// Service found, get url for connection.
String url = service.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if (url == null) {
continue;
}
try {
// Open connection.
Connection conn = Connector.open(url, Connector.READ_WRITE);
if (conn instanceof StreamConnection) {
// The connection should be a StreamConnection, otherwise it's a one way communication.
StreamConnection stream = (StreamConnection) conn;
in = stream.openInputStream();
out = stream.openOutputStream();
opened = true;
// Port is open, let openPort continue.
//waitObj.notify();
} else {
throw new IOException("Could not establish a two-way communication");
}
} catch (IOException IOe) {
exception[0] = IOe;
}
}
if (!opened) {
exception[0] = new IOException("No service found to connect to");
}
}
}
@Override
public void serviceSearchCompleted(int transID, int respCode) {
synchronized (waitObj) {
// Search for services complete, if the port was not opened, save the response code for error analysis.
responseCode[0] = respCode;
// Search completer, let openPort continue.
waitObj.notify();
}
}
@Override
public void inquiryCompleted(int discType) {
}
@Override
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
}
});
synchronized (waitObj) {
// Wait until either the port is open on the search has returned a response code.
while (!opened && responseCode[0] == -1) {
try {
// Wait for search to complete.
waitObj.wait();
} catch (InterruptedException Ie) {
Ie.printStackTrace();
}
}
}
break;
}
}
}
} catch (BluetoothStateException BSe) {
log.error("Exception when using bluetooth");
return BSe.getLocalizedMessage();
} catch (IOException IOe) {
log.error("Unknown IOException when establishing connection to " + portName);
return IOe.getLocalizedMessage();
}
if (!opened) {
ConnectionStatus.instance().setConnectionState(portName, ConnectionStatus.CONNECTION_DOWN);
if (exception[0] != null) {
log.error("Exception when connecting to " + portName);
return exception[0].getLocalizedMessage();
}
switch(responseCode[0]) {
case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
log.error("Bluetooth connection " + portName + " not opened, unknown error");
return "Unknown error: failed to connect to " + portName;
case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
log.error("Bluetooth device " + portName + " could not be reached");
return "Could not find " + portName;
case DiscoveryListener.SERVICE_SEARCH_ERROR:
log.error("Error when searching for " + portName);
return "Error when searching for " + portName;
case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
log.error("No serial service found on " + portName);
return "Invalid bluetooth device: " + portName;
case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
log.error("Service search on " + portName + " ended prematurely");
return "Search for " + portName + " ended unexpectedly";
default:
log.warn("Unhandled response code: {}", responseCode[0]);
break;
}
log.error("Unknown error when connecting to " + portName);
return "Unknown error when connecting to " + portName;
}
// normal operation
return null;
}
use of javax.microedition.io.Connection in project felix by apache.
the class ConnectorServiceImpl method open.
/**
* @see org.osgi.service.io.ConnectorService#open(String, int, boolean)
*/
public Connection open(String name, int mode, boolean timeouts) throws IOException {
if (name == null) {
throw new IllegalArgumentException("URI for the connection can't be null!");
}
// resolving scheme name
int index = name.indexOf(":");
if (index == -1) {
throw new IllegalArgumentException("Can't resolve scheme name");
}
String scheme = name.substring(0, index);
ConnectionFactory connFactory = resolveConnectionFactory(scheme);
Connection connection = null;
if (connFactory != null) {
connection = connFactory.createConnection(name, mode, timeouts);
}
// if connection is not provided go to javax.microedition.io.Connector
if (connection == null) {
try {
connection = Connector.open(name, mode, timeouts);
} catch (Exception ex) {
}
} else {
return connection;
}
throw new ConnectionNotFoundException("Failed to create connection " + name);
}
Aggregations