Search in sources :

Example 1 with RepositoryConfigException

use of org.openrdf.repository.config.RepositoryConfigException in project stanbol by apache.

the class RdfIndexingSource method loadRepositoryConfig.

/**
 * @param repoConfigFile
 * @return
 */
private RepositoryConfig loadRepositoryConfig(File repoConfigFile) {
    Repository configRepo = new SailRepository(new MemoryStore());
    RepositoryConnection con = null;
    try {
        configRepo.initialize();
        con = configRepo.getConnection();
        // We need to load the configuration into a context
        org.openrdf.model.URI configContext = con.getValueFactory().createURI("urn:stanbol.entityhub:indexing.source.sesame:config.context");
        RDFFormat format = Rio.getParserFormatForFileName(repoConfigFile.getName());
        try {
            con.add(new InputStreamReader(new FileInputStream(repoConfigFile), Charset.forName("UTF-8")), baseUri, format, configContext);
        } catch (RDFParseException e) {
            throw new IllegalArgumentException("Unable to parsed '" + repoConfigFile + "' using RDF format '" + format + "'!", e);
        } catch (IOException e) {
            throw new IllegalArgumentException("Unable to access '" + repoConfigFile + "'!", e);
        }
        con.commit();
    } catch (RepositoryException e) {
        throw new IllegalStateException("Unable to load '" + repoConfigFile + "' to inmemory Sail!", e);
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException e) {
            /* ignore */
            }
        }
    }
    Set<String> repoNames;
    RepositoryConfig repoConfig;
    try {
        repoNames = RepositoryConfigUtil.getRepositoryIDs(configRepo);
        if (repoNames.size() == 1) {
            repoConfig = RepositoryConfigUtil.getRepositoryConfig(configRepo, repoNames.iterator().next());
            repoConfig.validate();
        } else if (repoNames.size() > 1) {
            throw new IllegalArgumentException("Repository configuration file '" + repoConfigFile + "' MUST only contain a single repository configuration!");
        } else {
            throw new IllegalArgumentException("Repository configuration file '" + repoConfigFile + "' DOES NOT contain a repository configuration!");
        }
    } catch (RepositoryException e) {
        throw new IllegalStateException("Unable to read RepositoryConfiguration form the " + "in-memory Sail!", e);
    } catch (RepositoryConfigException e) {
        throw new IllegalArgumentException("Repository Configuration in '" + repoConfigFile + "is not valid!", e);
    } finally {
        try {
            configRepo.shutDown();
        } catch (RepositoryException e) {
        /* ignore */
        }
    }
    if (repoConfig.getRepositoryImplConfig() == null) {
        throw new IllegalArgumentException("Missing RepositoryImpl config for " + "config " + repoConfig.getID() + " of file " + repoConfigFile + "!");
    }
    return repoConfig;
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryConfig(org.openrdf.repository.config.RepositoryConfig) InputStreamReader(java.io.InputStreamReader) SailRepository(org.openrdf.repository.sail.SailRepository) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) RepositoryConfigException(org.openrdf.repository.config.RepositoryConfigException) FileInputStream(java.io.FileInputStream) MemoryStore(org.openrdf.sail.memory.MemoryStore) Repository(org.openrdf.repository.Repository) SailRepository(org.openrdf.repository.sail.SailRepository) URI(org.openrdf.model.URI) RDFFormat(org.openrdf.rio.RDFFormat) RDFParseException(org.openrdf.rio.RDFParseException)

Example 2 with RepositoryConfigException

use of org.openrdf.repository.config.RepositoryConfigException in project stanbol by apache.

the class RdfIndexingSource method setConfiguration.

@Override
public void setConfiguration(Map<String, Object> config) {
    IndexingConfig indexingConfig = (IndexingConfig) config.get(IndexingConfig.KEY_INDEXING_CONFIG);
    // (0) parse the baseUri
    Object value = config.get(PARAM_BASE_URI);
    baseUri = value == null ? DEFAULT_BASE_URI : value.toString();
    // (1) init the Sesame Repository from the RDF config
    value = config.get(PARAM_REPOSITORY_CONFIG);
    File repoConfigFile = indexingConfig.getConfigFile(value != null ? value.toString() : DEFAULT_REPOSITORY_CONFIG);
    if (repoConfigFile.isFile()) {
        // read the config (an RDF file)
        this.repoConfig = loadRepositoryConfig(repoConfigFile);
    } else {
        throw new IllegalArgumentException("The configured Sesame Repository configuration file " + repoConfigFile + " is missing. Please use the '" + PARAM_REPOSITORY_CONFIG + "' paramteter to configure the actual configuration file (relative " + "to the config '" + indexingConfig.getConfigFolder() + "'folder)");
    }
    RepositoryFactory factory = RepositoryRegistry.getInstance().get(repoConfig.getRepositoryImplConfig().getType());
    if (factory == null) {
        throw new IllegalStateException("Unable to initialise Repository (id: " + repoConfig.getID() + ", title: " + repoConfig.getTitle() + ", impl: " + repoConfig.getRepositoryImplConfig().getType() + ") because no " + "RepositoryFactory is present for the specified implementation!");
    }
    try {
        repository = factory.getRepository(repoConfig.getRepositoryImplConfig());
        sesameFactory = repository.getValueFactory();
        repository.initialize();
        // we created it, so we do shut it down
        shutdownRepository = true;
    } catch (RepositoryConfigException e) {
        throw new IllegalStateException("Unable to initialise Repository (id: " + repoConfig.getID() + ", title: " + repoConfig.getTitle() + ", impl: " + repoConfig.getRepositoryImplConfig().getType() + ")!", e);
    } catch (RepositoryException e) {
        throw new IllegalStateException("Unable to initialise Repository (id: " + repoConfig.getID() + ", title: " + repoConfig.getTitle() + ", impl: " + repoConfig.getRepositoryImplConfig().getType() + ")!", e);
    }
    // (2) init the resourceLoader
    loader = new ResourceLoader(new RdfResourceImporter(repository, baseUri), indexingConfig.isFailOnError());
    value = config.get(PARAM_IMPORTED_FOLDER);
    // set the folder for imported files
    String importedFolderName;
    if (value != null && !value.toString().isEmpty()) {
        importedFolderName = value.toString();
    } else {
        importedFolderName = DEFAULT_IMPORTED_FOLDER_NAME;
    }
    File importedFolder = new File(indexingConfig.getSourceFolder(), importedFolderName);
    log.info("Imported RDF File Folder: {}", importedFolder);
    this.loader.setImportedDir(importedFolder);
    // check if importing is deactivated
    // default is true
    boolean importSource = true;
    value = config.get(PARAM_IMPORT_SOURCE);
    if (value != null) {
        importSource = Boolean.parseBoolean(value.toString());
    }
    if (importSource) {
        // if we need to import ... check the source config
        log.info("Importing RDF data from:");
        value = config.get(PARAM_SOURCE_FILE_OR_FOLDER);
        if (value == null) {
            // if not set use the default
            value = DEFAULT_SOURCE_FOLDER_NAME;
        }
        for (String source : value.toString().split(",")) {
            File sourceFileOrDirectory = indexingConfig.getSourceFile(source);
            if (sourceFileOrDirectory.exists()) {
                // register the configured source with the ResourceLoader
                this.loader.addResource(sourceFileOrDirectory);
            } else {
                if (FilenameUtils.getExtension(source).isEmpty()) {
                    // RDF files.
                    if (!sourceFileOrDirectory.mkdirs()) {
                        log.warn("Unable to create directory {} configured to improt RDF data from. " + "You will need to create this directory manually before copying the" + "RDF files into it.", sourceFileOrDirectory);
                        this.loader.addResource(sourceFileOrDirectory);
                    }
                } else {
                    log.warn("Unable to find RDF source {} within the indexing Source folder {}", source, indexingConfig.getSourceFolder());
                }
            }
        }
        if (log.isInfoEnabled()) {
            for (String registeredSource : loader.getResources(ResourceState.REGISTERED)) {
                log.info(" > " + registeredSource);
            }
        }
    } else {
        log.info("Importing RDF data deactivated by parameer {}={}" + PARAM_IMPORT_SOURCE, value);
    }
}
Also used : ResourceLoader(org.apache.stanbol.entityhub.indexing.core.source.ResourceLoader) IndexingConfig(org.apache.stanbol.entityhub.indexing.core.config.IndexingConfig) RepositoryException(org.openrdf.repository.RepositoryException) RepositoryFactory(org.openrdf.repository.config.RepositoryFactory) RepositoryConfigException(org.openrdf.repository.config.RepositoryConfigException) File(java.io.File)

Aggregations

RepositoryException (org.openrdf.repository.RepositoryException)2 RepositoryConfigException (org.openrdf.repository.config.RepositoryConfigException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 IndexingConfig (org.apache.stanbol.entityhub.indexing.core.config.IndexingConfig)1 ResourceLoader (org.apache.stanbol.entityhub.indexing.core.source.ResourceLoader)1 URI (org.openrdf.model.URI)1 Repository (org.openrdf.repository.Repository)1 RepositoryConnection (org.openrdf.repository.RepositoryConnection)1 RepositoryConfig (org.openrdf.repository.config.RepositoryConfig)1 RepositoryFactory (org.openrdf.repository.config.RepositoryFactory)1 SailRepository (org.openrdf.repository.sail.SailRepository)1 RDFFormat (org.openrdf.rio.RDFFormat)1 RDFParseException (org.openrdf.rio.RDFParseException)1 MemoryStore (org.openrdf.sail.memory.MemoryStore)1