@SuppressWarnings("unchecked")
protected static <U> AbstractDataSource<U> getSource(final IKey key, boolean readImmediately,
boolean forgetImmediately) {
AbstractDataSource<U> result = null;
if (key != null) {
final IDataSourceProducer producer = DataSourceProducerProvider.getProducer(key.getSourceProduction());
try {
if (producer == null) {
throw new KeyCompatibilityException(key, producer);
} else if (producer instanceof IConstantSourceProducer) {
IConstantSourceProducer constantSourceProducer = (IConstantSourceProducer) producer;
AbstractDataSource<U> former = (AbstractDataSource<U>) constantSourceProducer
.getExistingSource(key);
if (producer instanceof AbstractRefreshingManager<?>) {
result = (AbstractDataSource<U>) ((AbstractRefreshingManager<?>) producer).createDataSource(
key, false, readImmediately);
} else {
result = (AbstractDataSource<U>) producer.createDataSource(key);
}
if (forgetImmediately && (former == null)) {
constantSourceProducer.removeDataSource(key);
}
} else {
result = (AbstractDataSource<U>) producer.createDataSource(key);
}
} catch (final Exception e) {
Logger.getLogger(Mediator.LOGGER_ACCESS).log(Level.SEVERE,
"Failed to create source for key: " + (key == null ? null : key.getInformationKey()), e);
}