/** * Retrieves a configuration object. * * @param configClass the class that represents your configuration * @param <T> Configuration class * @return configuration object. * @throws ConfigurationException If configuration class not found. */ @Override public <T> T getConfiguration(final Class<T> configClass) throws ConfigurationException { Objects.requireNonNull(configClass); if (!configurations.containsKey(configClass)) { throw new ConfigurationException("Could not find " + configClass.getName()); } return (T) configurations.get(configClass); }
@Override protected InputStream getConfigurationStream(final Class configClass, final Name relativePath) throws ConfigurationException { // Try each configuration source filename format delimiter in attempt to load the configuration source for (final String fileNameDelimiter : fileNameDelimiters) { for (final String fileExtension : fileExtensions) { String configFile = nameToFile(configClass, relativePath, fileNameDelimiter, fileExtension); Path p; if (configPath != null) { p = configPath.resolve(configFile); } else { p = Paths.get(configFile); } LOG.debug("Getting configuration for {} from {}", configClass.getSimpleName(), p); // Check if the file exists and try to return it as an input stream if (Files.exists(p)) { try { return Files.newInputStream(p); } catch (final IOException ioe) { throw new ConfigurationException("Cannot read config file: " + configFile, ioe); } } } } throw new ConfigurationException("Cannot find config file for " + configClass.getSimpleName()); }
/** * {@inheritDoc} */ public FileConfigurationSource( final BootstrapConfiguration bootstrap, final Cipher cipher, final ServicePath servicePath, final Decoder decoder ) throws ConfigurationException { super(bootstrap, cipher, servicePath, decoder); try { configPath = FileSystems.getDefault().getPath(getConfigPath(bootstrap)); } catch (final InvalidPathException e) { throw new ConfigurationException("Invalid configuration path", e); } fileExtensions = getFileExtensions(decoder); LOG.debug("Initialised"); }
/** * Acquire a configuration class from the provider. The requested class will be a simple Java object that when returned, and can be * interacted with using getters and other standard mechanisms. Configuration classes may themselves contain other configuration * objects, which will be recursively acquired if marked @Configuration. Any fields marked @Encrypted will be decrypted, any fields * marked and any validation annotations will be processed. * * @param configClass the class that represents your configuration * @param <T> the class that represents your configuration * @return the configuration class requested, if it can be deserialised * @throws ConfigurationException if the configuration class cannot be acquired or deserialised */ @Override public final <T> T getConfiguration(final Class<T> configClass) throws ConfigurationException { Objects.requireNonNull(configClass); incrementRequests(); T config = getCompleteConfig(configClass); Set<ConstraintViolation<T>> violations = getValidator().validate(config); if (violations.isEmpty()) { return config; } else { incrementErrors(); LOG.error("Configuration constraint violations found for {}: {}", configClass.getSimpleName(), violations); throw new ConfigurationException("Configuration validation failed for " + configClass.getSimpleName()); } }
/** * Acquire, decode and decrypt a configuration object from a data stream. * * @param configClass the class representing configuration to acquire * @param <T> the class representing configuration to acquire * @return the decoded configuration object * @throws ConfigurationException if the configuration cannot be acquired */ private <T> T getConfig(final Class<T> configClass) throws ConfigurationException { Iterator<Name> it = getServicePath().descendingPathIterator(); while (it.hasNext()) { try (InputStream in = getConfigurationStream(configClass, it.next())) { return decoder.deserialise(in, configClass); } catch (final ConfigurationException e) { LOG.trace("No configuration at this path level", e); } catch (final CodecException | IOException e) { incrementErrors(); throw new ConfigurationException("Failed to get configuration for " + configClass.getSimpleName(), e); } } incrementErrors(); throw new ConfigurationException("No configuration found for " + configClass.getSimpleName()); }
} catch (final InvocationTargetException | IllegalAccessException e) { incrementErrors(); throw new ConfigurationException("Failed to get complete configuration for " + configClass.getSimpleName(), e); throw new ConfigurationException("Failed to decrypt class fields", e); throw new ConfigurationException("Failed to get complete configuration for " + configClass.getSimpleName(), e);
private String getConfigPath(final BootstrapConfiguration bootstrap) throws ConfigurationException { String ret; if (bootstrap.isConfigurationPresent(CONFIG_PATH)) { ret = bootstrap.getConfiguration(CONFIG_PATH); } else if (bootstrap.isConfigurationPresent(OLD_CONFIG_PATH)) { ret = bootstrap.getConfiguration(OLD_CONFIG_PATH); } else { throw new ConfigurationException("Configuration parameter " + CONFIG_PATH + " not present"); } return ret; } }
/** * Constructs a task message for each file in the defined directory. * * @param workflowId The Workflow ID to set on the built task. * @param projectId The ProjectId to set on the built task. Workflow ID provided should be under this project ID. * @param directory The directory to build a task message per document for. * @return The constructed task messages and their associated files. * @throws ConfigurationException If invalid configuration passed. * @throws CodecException If a codec error occurs building a task message. * @throws IOException If error occurs accessing files in directory. * @throws DataStoreException If error occurs storing data for files. */ public static List<FileAndTaskMessage> buildTaskMessagesForDirectory(Long workflowId, String projectId, String directory) throws ConfigurationException, CodecException, IOException, DataStoreException, InterruptedException { if (workflowId == null) { throw new ConfigurationException("No workflow Id specified"); } // Get list of Document objects which represent each of the files we read from disk List<DocumentAndFile> documentsToSend = PolicyDocumentsBuilder.getDocuments(directory); //Construct a task to send to the policy worker for each document. List<FileAndTaskMessage> messagesToSend = new ArrayList<>(); for (DocumentAndFile documentAndFile : documentsToSend) { TaskMessage taskMessage = buildTaskMessage(documentAndFile.getDocument(), workflowId, projectId, dataDir); messagesToSend.add(new FileAndTaskMessage(documentAndFile.getFile(), taskMessage)); } return messagesToSend; }
/** * Constructs a task message for the document at specified path. * @param workflowId The Workflow ID to set on the built task. * @param projectId The ProjectId to set on the built task. Workflow ID provided should be under this project ID. * @param documentPath Path to the document to construct task for. * @return Constructed task message and the associated file. * @throws ConfigurationException If invalid configuration passed. * @throws CodecException If a codec error occurs building a task message. * @throws IOException If error occurs accessing the file. * @throws DataStoreException If error occurs storing data for file. */ public static FileAndTaskMessage buildTaskMessageForDocument(Long workflowId, String projectId, String documentPath) throws ConfigurationException, CodecException, IOException, DataStoreException { if (workflowId == null) { throw new ConfigurationException("No workflow Id specified"); } //Build Policy document from path to document DocumentAndFile documentAndFile = PolicyDocumentsBuilder.getDocument(documentPath); TaskMessage taskMessage = buildTaskMessage(documentAndFile.getDocument(), workflowId, projectId, dataDir); return new FileAndTaskMessage(documentAndFile.getFile(), taskMessage); }
throw new ConfigurationException( "Location to read documents from is not a directory." + directory);