}
@Override
@SuppressWarnings("unchecked")
public <T, U extends T> U newClassInstance(AtmosphereFramework framework, Class<T> classType, Class<U> classToInstantiate) throws InstantiationException, IllegalAccessException {
CreationalContext cc = null;
try {
final Iterator<Bean<?>> i = bm.getBeans(classToInstantiate).iterator();
if (!i.hasNext()) {
logger.trace("Unable to find {}. Creating the object directly.", classToInstantiate.getName());
return classToInstantiate.newInstance();
}
Bean<U> bean = (Bean<U>) i.next();
CreationalContext<U> ctx = bm.createCreationalContext(bean);
U dao = (U) bm.getReference(bean, classToInstantiate, ctx);
if (framework.objectFactory().getClass().getName().equals("org.atmosphere.inject.InjectableObjectFactory")) {
InjectableObjectFactory.class.cast(framework.objectFactory()).injectAtmosphereInternalObject(dao, classToInstantiate, framework);
}
return dao;
} catch (Exception e) {
logger.error("Unable to construct {}. Creating the object directly.", classToInstantiate.getName());
return classToInstantiate.newInstance();
} finally {
if (cc != null) cc.release();
}
}