Search in sources :

Example 1 with Connection

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();
}
Also used : Dictionary(java.util.Dictionary) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) Hashtable(java.util.Hashtable) Connection(javax.microedition.io.Connection) ConnectorService(org.osgi.service.io.ConnectorService) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with Connection

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();
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) Connection(javax.microedition.io.Connection) ConnectorService(org.osgi.service.io.ConnectorService) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 3 with Connection

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;
}
Also used : Connection(javax.microedition.io.Connection) IOException(java.io.IOException)

Example 4 with Connection

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;
}
Also used : DeviceClass(javax.bluetooth.DeviceClass) Connection(javax.microedition.io.Connection) StreamConnection(javax.microedition.io.StreamConnection) IOException(java.io.IOException) StreamConnection(javax.microedition.io.StreamConnection) BluetoothStateException(javax.bluetooth.BluetoothStateException) IOException(java.io.IOException) BluetoothStateException(javax.bluetooth.BluetoothStateException) ServiceRecord(javax.bluetooth.ServiceRecord) RemoteDevice(javax.bluetooth.RemoteDevice) UUID(javax.bluetooth.UUID) DiscoveryListener(javax.bluetooth.DiscoveryListener)

Example 5 with Connection

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);
}
Also used : ConnectionFactory(org.osgi.service.io.ConnectionFactory) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) Connection(javax.microedition.io.Connection) OutputConnection(javax.microedition.io.OutputConnection) InputConnection(javax.microedition.io.InputConnection) IOException(java.io.IOException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException)

Aggregations

Connection (javax.microedition.io.Connection)7 Dictionary (java.util.Dictionary)4 Hashtable (java.util.Hashtable)4 ConnectorService (org.osgi.service.io.ConnectorService)4 IOException (java.io.IOException)3 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)3 ServiceRegistration (org.osgi.framework.ServiceRegistration)3 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 BluetoothStateException (javax.bluetooth.BluetoothStateException)1 DeviceClass (javax.bluetooth.DeviceClass)1 DiscoveryListener (javax.bluetooth.DiscoveryListener)1 RemoteDevice (javax.bluetooth.RemoteDevice)1 ServiceRecord (javax.bluetooth.ServiceRecord)1 UUID (javax.bluetooth.UUID)1 InputConnection (javax.microedition.io.InputConnection)1 OutputConnection (javax.microedition.io.OutputConnection)1 StreamConnection (javax.microedition.io.StreamConnection)1