}
if (references != null) {
Context initialContext = null;
Arrays.sort(references, Utils.SERVICE_REFERENCE_COMPARATOR);
for (ServiceReference reference : references) {
InitialContextFactory factory = (InitialContextFactory) context.getService(reference);
try {
initialContext = factory.getInitialContext(environment);
} finally {
context.ungetService(reference);
}
if (initialContext != null) {
provider = new ContextProvider(reference, initialContext);
break;
}
}
}
}
} else {
// 1. lookup ContextFactory using the factory class
String filter = "(&(objectClass=javax.naming.spi.InitialContextFactory)(objectClass="+ contextFactoryClass + "))";
ServiceReference[] references = null;
try {
references = context.getServiceReferences(InitialContextFactory.class.getName(), filter);
} catch (InvalidSyntaxException e) {
NamingException ex = new NamingException("Bad filter: " + filter);
ex.initCause(e);
throw ex;
}
if (references != null && references.length > 0) {
Context initialContext = null;
Arrays.sort(references, Utils.SERVICE_REFERENCE_COMPARATOR);
ServiceReference reference = references[0];
InitialContextFactory factory = (InitialContextFactory) context.getService(reference);
try {
initialContext = factory.getInitialContext(environment);
} finally {
context.ungetService(reference);
}
if (initialContext != null) {
provider = new ContextProvider(reference, initialContext);