Search in sources :

Example 1 with ConnectionFactory

use of org.osgi.service.io.ConnectionFactory in project felix by apache.

the class ConnectorServiceImpl method resolveConnectionFactory.

/**
 * <p>
 * Resolves {@link ConnectionFactory} based on IO scheme name. If multiple ConnectionFactory services register with
 * the same scheme, method select the ConnectionFactory with highest value for service.ranking service registration
 * property or if more than one ConnectionFactory service has the highest value, the ConnectionFactory service with
 * the lowest service.id is selected.
 * </p>
 *
 * @param scheme
 *            name of IO scheme.
 * @return {@link ConnectionFactory} which matched provided scheme.
 */
private ConnectionFactory resolveConnectionFactory(String scheme) {
    ServiceReference[] references = m_connFactoryTracker.getServiceReferences();
    if (references == null || references.length == 0) {
        return null;
    }
    ServiceReference matchingRef = null;
    for (int i = 0; i < references.length; i++) {
        if (containsScheme(references[i], scheme)) {
            if (matchingRef != null) {
                int matchRanking = getServiceRanking(matchingRef);
                int foundRanking = getServiceRanking(references[i]);
                if (foundRanking > matchRanking) {
                    matchingRef = references[i];
                } else if (foundRanking == matchRanking && compareServiceId(references[i], matchingRef)) {
                    matchingRef = references[i];
                }
            } else {
                matchingRef = references[i];
            }
        }
    }
    if (matchingRef != null) {
        return (ConnectionFactory) m_connFactoryTracker.getService(matchingRef);
    }
    return null;
}
Also used : ConnectionFactory(org.osgi.service.io.ConnectionFactory) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with ConnectionFactory

use of org.osgi.service.io.ConnectionFactory 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

ConnectionFactory (org.osgi.service.io.ConnectionFactory)2 IOException (java.io.IOException)1 Connection (javax.microedition.io.Connection)1 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)1 InputConnection (javax.microedition.io.InputConnection)1 OutputConnection (javax.microedition.io.OutputConnection)1 ServiceReference (org.osgi.framework.ServiceReference)1