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