use of java.util.Map in project camel by apache.
the class DefaultPropertiesResolver method loadPropertiesFromRegistry.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Properties loadPropertiesFromRegistry(CamelContext context, boolean ignoreMissingLocation, PropertiesLocation location) throws IOException {
String path = location.getPath();
Properties answer;
try {
answer = context.getRegistry().lookupByNameAndType(path, Properties.class);
} catch (Exception ex) {
// just look up the Map as a fault back
Map map = context.getRegistry().lookupByNameAndType(path, Map.class);
answer = new Properties();
answer.putAll(map);
}
if (answer == null && (!ignoreMissingLocation && !location.isOptional())) {
throw new FileNotFoundException("Properties " + path + " not found in registry");
}
return answer != null ? answer : new Properties();
}
use of java.util.Map in project camel by apache.
the class FileConsumer method pollDirectory.
@Override
protected boolean pollDirectory(String fileName, List<GenericFile<File>> fileList, int depth) {
log.trace("pollDirectory from fileName: {}", fileName);
depth++;
File directory = new File(fileName);
if (!directory.exists() || !directory.isDirectory()) {
log.debug("Cannot poll as directory does not exists or its not a directory: {}", directory);
if (getEndpoint().isDirectoryMustExist()) {
throw new GenericFileOperationFailedException("Directory does not exist: " + directory);
}
return true;
}
log.trace("Polling directory: {}", directory.getPath());
File[] dirFiles = directory.listFiles();
if (dirFiles == null || dirFiles.length == 0) {
// no files in this directory to poll
if (log.isTraceEnabled()) {
log.trace("No files found in directory: {}", directory.getPath());
}
return true;
} else {
// we found some files
if (log.isTraceEnabled()) {
log.trace("Found {} in directory: {}", dirFiles.length, directory.getPath());
}
}
List<File> files = Arrays.asList(dirFiles);
for (File file : dirFiles) {
// check if we can continue polling in files
if (!canPollMoreFiles(fileList)) {
return false;
}
// trace log as Windows/Unix can have different views what the file is?
if (log.isTraceEnabled()) {
log.trace("Found file: {} [isAbsolute: {}, isDirectory: {}, isFile: {}, isHidden: {}]", new Object[] { file, file.isAbsolute(), file.isDirectory(), file.isFile(), file.isHidden() });
}
// creates a generic file
GenericFile<File> gf = asGenericFile(endpointPath, file, getEndpoint().getCharset(), getEndpoint().isProbeContentType());
if (file.isDirectory()) {
if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && isValidFile(gf, true, files)) {
// recursive scan and add the sub files and folders
String subDirectory = fileName + File.separator + file.getName();
boolean canPollMore = pollDirectory(subDirectory, fileList, depth);
if (!canPollMore) {
return false;
}
}
} else {
// Windows can report false to a file on a share so regard it always as a file (if its not a directory)
if (depth >= endpoint.minDepth && isValidFile(gf, false, files)) {
log.trace("Adding valid file: {}", file);
// matched file so add
if (extendedAttributes != null) {
Path path = file.toPath();
Map<String, Object> allAttributes = new HashMap<>();
for (String attribute : extendedAttributes) {
try {
String prefix = null;
if (attribute.endsWith(":*")) {
prefix = attribute.substring(0, attribute.length() - 1);
} else if (attribute.equals("*")) {
prefix = "basic:";
}
if (ObjectHelper.isNotEmpty(prefix)) {
Map<String, Object> attributes = Files.readAttributes(path, attribute);
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
allAttributes.put(prefix + entry.getKey(), entry.getValue());
}
}
} else if (!attribute.contains(":")) {
allAttributes.put("basic:" + attribute, Files.getAttribute(path, attribute));
} else {
allAttributes.put(attribute, Files.getAttribute(path, attribute));
}
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug("Unable to read attribute {} on file {}", attribute, file, e);
}
}
}
gf.setExtendedAttributes(allAttributes);
}
fileList.add(gf);
}
}
}
return true;
}
use of java.util.Map in project camel by apache.
the class DefaultComponentVerifier method setProperties.
protected <T> T setProperties(T instance, Map<String, Object> properties) throws Exception {
if (camelContext == null) {
throw new IllegalStateException("Camel context is null");
}
if (!properties.isEmpty()) {
final TypeConverter converter = camelContext.getTypeConverter();
IntrospectionSupport.setProperties(converter, instance, properties);
for (Map.Entry<String, Object> entry : properties.entrySet()) {
if (entry.getValue() instanceof String) {
String value = (String) entry.getValue();
if (EndpointHelper.isReferenceParameter(value)) {
IntrospectionSupport.setProperty(camelContext, converter, instance, entry.getKey(), null, value, true);
}
}
}
}
return instance;
}
use of java.util.Map in project camel by apache.
the class DefaultComponentVerifier method verifyParametersAgainstCatalog.
// *************************************
// Helpers :: Parameters validation
// *************************************
protected void verifyParametersAgainstCatalog(ResultBuilder builder, Map<String, Object> parameters) {
String scheme = defaultScheme;
if (parameters.containsKey("scheme")) {
scheme = parameters.get("scheme").toString();
}
// Grab the runtime catalog to check parameters
RuntimeCamelCatalog catalog = camelContext.getRuntimeCamelCatalog();
// Convert from Map<String, Object> to Map<String, String> as required
// by the Camel Catalog
EndpointValidationResult result = catalog.validateProperties(scheme, parameters.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> camelContext.getTypeConverter().convertTo(String.class, e.getValue()))));
if (!result.isSuccess()) {
stream(result.getUnknown()).map(option -> ResultErrorBuilder.withUnknownOption(option).build()).forEach(builder::error);
stream(result.getRequired()).map(option -> ResultErrorBuilder.withMissingOption(option).build()).forEach(builder::error);
stream(result.getInvalidBoolean()).map(entry -> ResultErrorBuilder.withIllegalOption(entry.getKey(), entry.getValue()).build()).forEach(builder::error);
stream(result.getInvalidInteger()).map(entry -> ResultErrorBuilder.withIllegalOption(entry.getKey(), entry.getValue()).build()).forEach(builder::error);
stream(result.getInvalidNumber()).map(entry -> ResultErrorBuilder.withIllegalOption(entry.getKey(), entry.getValue()).build()).forEach(builder::error);
stream(result.getInvalidEnum()).map(entry -> ResultErrorBuilder.withIllegalOption(entry.getKey(), entry.getValue()).attribute("enum.values", result.getEnumChoices(entry.getKey())).build()).forEach(builder::error);
}
}
use of java.util.Map in project camel by apache.
the class ComponentDiscoveryTest method testComponentDiscovery.
@Test
public void testComponentDiscovery() throws Exception {
CamelContext context = new DefaultCamelContext();
SortedMap<String, Properties> map = CamelContextHelper.findComponents(context);
assertNotNull("Should never return null", map);
assertTrue("Component map should never be empty", !map.isEmpty());
String[] expectedComponentNames = { "file", "vm" };
for (String expectedName : expectedComponentNames) {
Properties properties = map.get(expectedName);
assertTrue("Component map contain component: " + expectedName, properties != null);
}
Set<Map.Entry<String, Properties>> entries = map.entrySet();
for (Map.Entry<String, Properties> entry : entries) {
LOG.info("Found component " + entry.getKey() + " with properties: " + entry.getValue());
}
}
Aggregations