private void initReaderFromURL(final Object source, final Hints hints) throws Exception {
this.sourceURL = Utils.checkSource(source, hints);
// Preliminar check on source
if (this.sourceURL == null) {
throw new DataSourceException(
"This plugin accepts File, URL or String. The string may describe a File or an URL");
}
// Load properties file
MosaicConfigurationBean configuration = null;
try {
if (sourceURL.getProtocol().equals("file")) {
final File sourceFile = DataUtilities.urlToFile(sourceURL);
if (!sourceFile.exists()) {
throw new DataSourceException("The specified sourceURL doesn't refer to an existing file");
}
}
if (sourceURL != null) {
parentDirectory = DataUtilities.urlToFile(sourceURL);
if (!parentDirectory.isDirectory()) {
parentDirectory = parentDirectory.getParentFile();
}
}
configuration = Utils.loadMosaicProperties(sourceURL, this.locationAttributeName);
if (configuration == null) {
//
// do we have a datastore properties file? It will preempt on the shapefile
//
final File parent = DataUtilities.urlToFile(sourceURL).getParentFile();
// this can be used to look for properties files that do NOT define a datastore
final File[] properties = parent.listFiles((FilenameFilter) FileFilterUtils.and(
FileFilterUtils.notFileFilter(FileFilterUtils
.nameFileFilter("indexer.properties")), FileFilterUtils.and(
FileFilterUtils.notFileFilter(FileFilterUtils
.nameFileFilter("datastore.properties")), FileFilterUtils
.makeFileOnly(FileFilterUtils
.suffixFileFilter(".properties")))));
// do we have a valid datastore + mosaic properties pair?
final File datastoreProperties = new File(parent, "datastore.properties");
// Scan for MosaicConfigurationBeans from properties files
List<MosaicConfigurationBean> beans = new ArrayList<MosaicConfigurationBean>();
for (File propFile : properties) {
if (Utils.checkFileReadable(propFile) && Utils.loadMosaicProperties(DataUtilities.fileToURL(propFile), "") != null) {
configuration = Utils.loadMosaicProperties(DataUtilities.fileToURL(propFile), this.locationAttributeName);
if (configuration != null) {
beans.add(configuration);
}
}
}
// In case we didn't find any configuration bean and datastore properties, we can't do anything
if (beans.isEmpty() && !datastoreProperties.exists()) {
throw new DataSourceException("No mosaic properties file or datastore properties file have been found");
}
// Catalog initialization from datastore
GranuleCatalog catalog = null;
final Properties params = CatalogManager.createGranuleCatalogProperties(datastoreProperties);
// Since we are dealing with a catalog from an existing store, make sure to scan for all the typeNames on initialization
final Object typeNames=params.get(Utils.SCAN_FOR_TYPENAMES);
if (typeNames!=null){
params.put(Utils.SCAN_FOR_TYPENAMES, Boolean.valueOf(typeNames.toString()));
} else {
params.put(Utils.SCAN_FOR_TYPENAMES, Boolean.TRUE);
}
if (beans.size() > 0) {
catalog = GranuleCatalogFactory.createGranuleCatalog(sourceURL, beans.get(0).getCatalogConfigurationBean(), params, getHints());
} else {
catalog = CatalogManager.createGranuleCatalogFromDatastore(parent, datastoreProperties, true, getHints());
}
MultiLevelROIProvider rois = MultiLevelROIProviderFactory.createFootprintProvider(parent);
catalog.setMultiScaleROIProvider(rois);
if (granuleCatalog != null) {
granuleCatalog.dispose();
}
granuleCatalog = catalog;
if (granuleCatalog == null) {
throw new DataSourceException("Unable to create index for this URL " + sourceURL);
}
// Creating a RasterManager for each mosaic configuration found on disk
for (MosaicConfigurationBean bean : beans) {
// Add a RasterManager on top of this Mosaic configuration bean and initialize it
addRasterManager(bean, true);
}
} else {
// Old style code: we have a single MosaicConfigurationBean. Use that to create the catalog
granuleCatalog = CatalogManager.createCatalog(sourceURL, configuration, this.hints);
File parent = DataUtilities.urlToFile(sourceURL).getParentFile();
MultiLevelROIProvider rois = MultiLevelROIProviderFactory.createFootprintProvider(parent);
granuleCatalog.setMultiScaleROIProvider(rois);
addRasterManager(configuration, true);
}
} catch (Throwable e) {
// Dispose catalog
try {
if (granuleCatalog != null) {
granuleCatalog.dispose();
}
} catch (Throwable e1) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, e1.getLocalizedMessage(), e1);
}
} finally {
granuleCatalog = null;
}
// dispose raster managers as well
try {
disposeManagers();
} catch (Throwable e1) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, e1.getLocalizedMessage(), e1);
}
} finally {
rasterManagers = null;
}
// rethrow
throw new DataSourceException(e);
}
}