Search in sources :

Example 1 with ConnectionNotFoundException

use of javax.microedition.io.ConnectionNotFoundException 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 ConnectionNotFoundException

use of javax.microedition.io.ConnectionNotFoundException in project J2ME-Loader by nikita36078.

the class MIDlet method platformRequest.

public boolean platformRequest(String url) throws ConnectionNotFoundException {
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (url.startsWith("file://")) {
            FileSystemFileConnection fileConnection = (FileSystemFileConnection) Connector.open(url);
            intent.setData(fileConnection.getURI());
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            fileConnection.close();
        } else {
            intent.setData(Uri.parse(url));
        }
        ContextHolder.getActivity().startActivity(intent);
    } catch (ActivityNotFoundException | IOException e) {
        throw new ConnectionNotFoundException();
    }
    return true;
}
Also used : ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) ActivityNotFoundException(android.content.ActivityNotFoundException) Intent(android.content.Intent) IOException(java.io.IOException) FileSystemFileConnection(org.microemu.cldc.file.FileSystemFileConnection)

Example 3 with ConnectionNotFoundException

use of javax.microedition.io.ConnectionNotFoundException in project J2ME-Loader by nikita36078.

the class ConnectorImpl method openSecure.

private Connection openSecure(String name, int mode, boolean timeouts) throws IOException {
    String className = null;
    String protocol = null;
    try {
        try {
            protocol = name.substring(0, name.indexOf(':'));
            className = "org.microemu.cldc." + protocol + ".Connection";
            Class cl = Class.forName(className);
            Object inst = cl.newInstance();
            if (inst instanceof ConnectionImplementation) {
                return ((ConnectionImplementation) inst).openConnection(name, mode, timeouts);
            } else {
                throw new ClassNotFoundException();
            }
        } catch (ClassNotFoundException e) {
            Log.d(TAG, "connection [" + protocol + "] class not found", e);
            throw new ConnectionNotFoundException("connection [" + protocol + "] class not found");
        }
    } catch (InstantiationException e) {
        Log.e(TAG, "Unable to create" + className, e);
        throw new ConnectionNotFoundException();
    } catch (IllegalAccessException e) {
        Log.e(TAG, "Unable to create" + className, e);
        throw new ConnectionNotFoundException();
    }
}
Also used : ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException)

Example 4 with ConnectionNotFoundException

use of javax.microedition.io.ConnectionNotFoundException 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)

Example 5 with ConnectionNotFoundException

use of javax.microedition.io.ConnectionNotFoundException in project felix by apache.

the class ConnectorServiceTest method testOpen.

/**
 * Tests all methods provided by {@link ConnectorService}.
 *
 * @throws Exception
 */
public void testOpen() throws Exception {
    ConnectionFactoryMock connFactory = new ConnectionFactoryMock();
    Dictionary props = new Hashtable();
    props.put(ConnectionFactory.IO_SCHEME, "file");
    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("file://test.txt", ConnectorService.READ);
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned Connection", connection);
    connection = service.open("file://test.txt", ConnectorService.WRITE);
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.WRITE, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned Connection", connection);
    connection = service.open("file://test.txt", ConnectorService.READ, true);
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(true, connFactory.isTimeout());
    assertNotNull("checks returned Connection", connection);
    try {
        connection = service.open("http://test.txt", ConnectorService.READ);
        fail("Connection shouldn't be created");
    } catch (ConnectionNotFoundException e) {
    // "expected"
    }
    try {
        service.open("file.txt");
        fail("Illegal format of uri");
    } catch (IllegalArgumentException e) {
    // expected
    }
    DataInputStream dataInStream = service.openDataInputStream("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned DataInputStream", dataInStream);
    DataOutputStream dataOutStream = service.openDataOutputStream("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.WRITE, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned DataOutputStream", dataOutStream);
    InputStream inStream = service.openInputStream("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.READ, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned InputStream", inStream);
    OutputStream outStream = service.openOutputStream("file://test.txt");
    assertEquals("file://test.txt", connFactory.getName());
    assertEquals(ConnectorService.WRITE, connFactory.getMode());
    assertEquals(false, connFactory.isTimeout());
    assertNotNull("checks returned OutputStream", outStream);
    registration.unregister();
}
Also used : Dictionary(java.util.Dictionary) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) Hashtable(java.util.Hashtable) DataOutputStream(java.io.DataOutputStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) DataOutputStream(java.io.DataOutputStream) Connection(javax.microedition.io.Connection) DataInputStream(java.io.DataInputStream) ConnectorService(org.osgi.service.io.ConnectorService) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)5 Connection (javax.microedition.io.Connection)3 IOException (java.io.IOException)2 Dictionary (java.util.Dictionary)2 Hashtable (java.util.Hashtable)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ConnectorService (org.osgi.service.io.ConnectorService)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InputConnection (javax.microedition.io.InputConnection)1 OutputConnection (javax.microedition.io.OutputConnection)1 FileSystemFileConnection (org.microemu.cldc.file.FileSystemFileConnection)1 ConnectionFactory (org.osgi.service.io.ConnectionFactory)1