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();
}
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;
}
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();
}
}
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);
}
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();
}
Aggregations