Search in sources :

Example 11 with Map

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();
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Properties(java.util.Properties) Map(java.util.Map) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 12 with Map

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;
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) IOException(java.io.IOException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with Map

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;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) Map(java.util.Map)

Example 14 with Map

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);
    }
}
Also used : EndpointHelper(org.apache.camel.util.EndpointHelper) CamelContext(org.apache.camel.CamelContext) ComponentVerifier(org.apache.camel.ComponentVerifier) NoSuchOptionException(org.apache.camel.NoSuchOptionException) EndpointValidationResult(org.apache.camel.catalog.EndpointValidationResult) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) TypeConverter(org.apache.camel.TypeConverter) IntrospectionSupport(org.apache.camel.util.IntrospectionSupport) Map(java.util.Map) CamelContextHelper(org.apache.camel.util.CamelContextHelper) Optional(java.util.Optional) StreamUtils.stream(org.apache.camel.util.StreamUtils.stream) RuntimeCamelCatalog(org.apache.camel.catalog.RuntimeCamelCatalog) EndpointValidationResult(org.apache.camel.catalog.EndpointValidationResult) RuntimeCamelCatalog(org.apache.camel.catalog.RuntimeCamelCatalog)

Example 15 with Map

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());
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Properties(java.util.Properties) Map(java.util.Map) SortedMap(java.util.SortedMap) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Aggregations

Map (java.util.Map)15646 HashMap (java.util.HashMap)9529 ArrayList (java.util.ArrayList)3619 List (java.util.List)2988 Test (org.junit.Test)2558 Set (java.util.Set)1837 HashSet (java.util.HashSet)1646 IOException (java.io.IOException)1486 Iterator (java.util.Iterator)1307 LinkedHashMap (java.util.LinkedHashMap)1284 TreeMap (java.util.TreeMap)1022 ImmutableMap (com.google.common.collect.ImmutableMap)879 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)729 File (java.io.File)662 Collection (java.util.Collection)576 Collectors (java.util.stream.Collectors)436 ConcurrentMap (java.util.concurrent.ConcurrentMap)375 LinkedList (java.util.LinkedList)333 SSOException (com.iplanet.sso.SSOException)294 Collections (java.util.Collections)288