Search in sources :

Example 1 with KeyValueHolder

use of org.apache.camel.util.KeyValueHolder in project camel by apache.

the class DefaultManagementLifecycleStrategy method removeWrappedProcessorsForRoutes.

/**
     * Removes the wrapped processors for the given routes, as they are no longer in use.
     * <p/>
     * This is needed to avoid accumulating memory, if a lot of routes is being added and removed.
     *
     * @param routes the routes
     */
private void removeWrappedProcessorsForRoutes(Collection<Route> routes) {
    // loop the routes, and remove the route associated wrapped processors, as they are no longer in use
    for (Route route : routes) {
        String id = route.getId();
        Iterator<KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor>> it = wrappedProcessors.values().iterator();
        while (it.hasNext()) {
            KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor> holder = it.next();
            RouteDefinition def = ProcessorDefinitionHelper.getRoute(holder.getKey());
            if (def != null && id.equals(def.getId())) {
                it.remove();
            }
        }
    }
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) KeyValueHolder(org.apache.camel.util.KeyValueHolder) ManagedRoute(org.apache.camel.management.mbean.ManagedRoute) Route(org.apache.camel.Route) EventDrivenConsumerRoute(org.apache.camel.impl.EventDrivenConsumerRoute)

Example 2 with KeyValueHolder

use of org.apache.camel.util.KeyValueHolder in project camel by apache.

the class MailConsumer method poll.

protected int poll() throws Exception {
    // must reset for each poll
    shutdownRunningTask = null;
    pendingExchanges = 0;
    int polledMessages = 0;
    ensureIsConnected();
    if (store == null || folder == null) {
        throw new IllegalStateException("MailConsumer did not connect properly to the MailStore: " + getEndpoint().getConfiguration().getMailStoreLogInformation());
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Polling mailbox folder: " + getEndpoint().getConfiguration().getMailStoreLogInformation());
    }
    if (getEndpoint().getConfiguration().getFetchSize() == 0) {
        LOG.warn("Fetch size is 0 meaning the configuration is set to poll no new messages at all. Camel will skip this poll.");
        return 0;
    }
    // ensure folder is open
    if (!folder.isOpen()) {
        folder.open(Folder.READ_WRITE);
    }
    try {
        int count = folder.getMessageCount();
        if (count > 0) {
            List<KeyValueHolder<String, Message>> messages = retrieveMessages();
            // we process the message and rollback due an exception
            if (getEndpoint().getConfiguration().isPeek()) {
                for (KeyValueHolder<String, Message> entry : messages) {
                    Message message = entry.getValue();
                    peekMessage(message);
                }
            }
            polledMessages = processBatch(CastUtils.cast(createExchanges(messages)));
            final MailBoxPostProcessAction postProcessor = getEndpoint().getPostProcessAction();
            if (postProcessor != null) {
                postProcessor.process(folder);
            }
        } else if (count == -1) {
            throw new MessagingException("Folder: " + folder.getFullName() + " is closed");
        }
    } catch (Exception e) {
        handleException(e);
    } finally {
        // need to ensure we release resources, but only if closeFolder or disconnect = true
        if (getEndpoint().getConfiguration().isCloseFolder() || getEndpoint().getConfiguration().isDisconnect()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Close mailbox folder {} from {}", folder.getName(), getEndpoint().getConfiguration().getMailStoreLogInformation());
            }
            try {
                if (folder.isOpen()) {
                    folder.close(true);
                }
            } catch (Exception e) {
                // some mail servers will lock the folder so we ignore in this case (CAMEL-1263)
                LOG.debug("Could not close mailbox folder: " + folder.getName() + ". This exception is ignored.", e);
            }
        }
    }
    // should we disconnect, the header can override the configuration
    boolean disconnect = getEndpoint().getConfiguration().isDisconnect();
    if (disconnect) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Disconnecting from {}", getEndpoint().getConfiguration().getMailStoreLogInformation());
        }
        try {
            store.close();
        } catch (Exception e) {
            LOG.debug("Could not disconnect from {}: " + getEndpoint().getConfiguration().getMailStoreLogInformation() + ". This exception is ignored.", e);
        }
        store = null;
        folder = null;
    }
    return polledMessages;
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) KeyValueHolder(org.apache.camel.util.KeyValueHolder) MessagingException(javax.mail.MessagingException) FolderNotFoundException(javax.mail.FolderNotFoundException)

Example 3 with KeyValueHolder

use of org.apache.camel.util.KeyValueHolder in project camel by apache.

the class CamelBlueprintTestSupport method createBundleContext.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected BundleContext createBundleContext() throws Exception {
    System.setProperty("org.apache.aries.blueprint.synchronous", Boolean.toString(!useAsynchronousBlueprintStartup()));
    // load configuration file
    String[] file = loadConfigAdminConfigurationFile();
    String[][] configAdminPidFiles = new String[0][0];
    if (file != null) {
        if (file.length % 2 != 0) {
            // This needs to return pairs of filename and pid
            throw new IllegalArgumentException("The length of the String[] returned from loadConfigAdminConfigurationFile must divisible by 2, was " + file.length);
        }
        configAdminPidFiles = new String[file.length / 2][2];
        int pair = 0;
        for (int i = 0; i < file.length; i += 2) {
            String fileName = file[i];
            String pid = file[i + 1];
            if (!new File(fileName).exists()) {
                throw new IllegalArgumentException("The provided file \"" + fileName + "\" from loadConfigAdminConfigurationFile doesn't exist");
            }
            configAdminPidFiles[pair][0] = fileName;
            configAdminPidFiles[pair][1] = pid;
            pair++;
        }
    }
    // fetch initial configadmin configuration if provided programmatically
    Properties initialConfiguration = new Properties();
    String pid = setConfigAdminInitialConfiguration(initialConfiguration);
    if (pid != null) {
        configAdminPidFiles = new String[][] { { prepareInitialConfigFile(initialConfiguration), pid } };
    }
    final String symbolicName = getClass().getSimpleName();
    final BundleContext answer = CamelBlueprintHelper.createBundleContext(symbolicName, getBlueprintDescriptor(), includeTestBundle(), getBundleFilter(), getBundleVersion(), getBundleDirectives(), configAdminPidFiles);
    boolean expectReload = expectBlueprintContainerReloadOnConfigAdminUpdate();
    // must register override properties early in OSGi containers
    Properties extra = useOverridePropertiesWithPropertiesComponent();
    if (extra != null) {
        answer.registerService(PropertiesComponent.OVERRIDE_PROPERTIES, extra, null);
    }
    Map<String, KeyValueHolder<Object, Dictionary>> map = new LinkedHashMap<String, KeyValueHolder<Object, Dictionary>>();
    addServicesOnStartup(map);
    List<KeyValueHolder<String, KeyValueHolder<Object, Dictionary>>> servicesList = new LinkedList<KeyValueHolder<String, KeyValueHolder<Object, Dictionary>>>();
    for (Map.Entry<String, KeyValueHolder<Object, Dictionary>> entry : map.entrySet()) {
        servicesList.add(asKeyValueService(entry.getKey(), entry.getValue().getKey(), entry.getValue().getValue()));
    }
    addServicesOnStartup(servicesList);
    for (KeyValueHolder<String, KeyValueHolder<Object, Dictionary>> item : servicesList) {
        String clazz = item.getKey();
        Object service = item.getValue().getKey();
        Dictionary dict = item.getValue().getValue();
        log.debug("Registering service {} -> {}", clazz, service);
        ServiceRegistration<?> reg = answer.registerService(clazz, service, dict);
        if (reg != null) {
            services.add(reg);
        }
    }
    // if blueprint XML uses <cm:property-placeholder> (any update-strategy and any default properties)
    // - org.apache.aries.blueprint.compendium.cm.ManagedObjectManager.register() is called
    // - ManagedServiceUpdate is scheduled in felix.cm
    // - org.apache.felix.cm.impl.ConfigurationImpl.setDynamicBundleLocation() is called
    // - CM_LOCATION_CHANGED event is fired
    // - if BP was alredy created, it's <cm:property-placeholder> receives the event and
    // - org.apache.aries.blueprint.compendium.cm.CmPropertyPlaceholder.updated() is called,
    //   but no BP reload occurs
    // we will however wait for BP container of the test bundle to become CREATED for the first time
    // each configadmin update *may* lead to reload of BP container, if it uses <cm:property-placeholder>
    // with update-strategy="reload"
    // we will gather timestamps of BP events. We don't want to be fooled but repeated events related
    // to the same state of BP container
    Set<Long> bpEvents = new HashSet<>();
    CamelBlueprintHelper.waitForBlueprintContainer(bpEvents, answer, symbolicName, BlueprintEvent.CREATED, null);
    // must reuse props as we can do both load from .cfg file and override afterwards
    final Dictionary props = new Properties();
    // allow end user to override properties
    pid = useOverridePropertiesWithConfigAdmin(props);
    if (pid != null) {
        // we will update the configuration again
        ConfigurationAdmin configAdmin = CamelBlueprintHelper.getOsgiService(answer, ConfigurationAdmin.class);
        // passing null as second argument ties the configuration to correct bundle.
        // using single-arg method causes:
        // *ERROR* Cannot use configuration xxx.properties for [org.osgi.service.cm.ManagedService, id=N, bundle=N/jar:file:xyz.jar!/]: No visibility to configuration bound to felix-connect
        final Configuration config = configAdmin.getConfiguration(pid, null);
        if (config == null) {
            throw new IllegalArgumentException("Cannot find configuration with pid " + pid + " in OSGi ConfigurationAdmin service.");
        }
        // lets merge configurations
        Dictionary<String, Object> currentProperties = config.getProperties();
        final Dictionary newProps = new Properties();
        if (currentProperties == null) {
            currentProperties = newProps;
        }
        for (Enumeration<String> ek = currentProperties.keys(); ek.hasMoreElements(); ) {
            String k = ek.nextElement();
            newProps.put(k, currentProperties.get(k));
        }
        for (String p : ((Properties) props).stringPropertyNames()) {
            newProps.put(p, ((Properties) props).getProperty(p));
        }
        log.info("Updating ConfigAdmin {} by overriding properties {}", config, newProps);
        if (expectReload) {
            CamelBlueprintHelper.waitForBlueprintContainer(bpEvents, answer, symbolicName, BlueprintEvent.CREATED, new Runnable() {

                @Override
                public void run() {
                    try {
                        config.update(newProps);
                    } catch (IOException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                }
            });
        } else {
            config.update(newProps);
        }
    }
    return answer;
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) Properties(java.util.Properties) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) KeyValueHolder(org.apache.camel.util.KeyValueHolder) IOException(java.io.IOException) LinkedList(java.util.LinkedList) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) BundleContext(org.osgi.framework.BundleContext)

Example 4 with KeyValueHolder

use of org.apache.camel.util.KeyValueHolder in project camel by apache.

the class MailConsumer method retrieveMessages.

/**
     * @return Messages from input folder according to the search and sort criteria stored in the endpoint
     * @throws MessagingException If message retrieval fails
     */
private List<KeyValueHolder<String, Message>> retrieveMessages() throws MessagingException {
    List<KeyValueHolder<String, Message>> answer = new ArrayList<>();
    Message[] messages;
    final SortTerm[] sortTerm = getEndpoint().getSortTerm();
    final SearchTerm searchTerm = computeSearchTerm();
    if (sortTerm != null && serverCanSort) {
        final IMAPFolder imapFolder = (IMAPFolder) folder;
        if (searchTerm != null) {
            // Sort and search using server capability
            messages = imapFolder.getSortedMessages(sortTerm, searchTerm);
        } else {
            // Only sort using server capability
            messages = imapFolder.getSortedMessages(sortTerm);
        }
    } else {
        if (searchTerm != null) {
            messages = folder.search(searchTerm, retrieveAllMessages());
        } else {
            messages = retrieveAllMessages();
        }
        // Now we can sort (emulate email sort but restrict sort terms)
        if (sortTerm != null) {
            MailSorter.sortMessages(messages, sortTerm);
        }
    }
    for (Message message : messages) {
        String key = getEndpoint().getMailUidGenerator().generateUuid(getEndpoint(), message);
        if (isValidMessage(key, message)) {
            answer.add(new KeyValueHolder<>(key, message));
        }
    }
    return answer;
}
Also used : SortTerm(com.sun.mail.imap.SortTerm) Message(javax.mail.Message) IMAPFolder(com.sun.mail.imap.IMAPFolder) ArrayList(java.util.ArrayList) KeyValueHolder(org.apache.camel.util.KeyValueHolder) SearchTerm(javax.mail.search.SearchTerm)

Example 5 with KeyValueHolder

use of org.apache.camel.util.KeyValueHolder in project camel by apache.

the class InstrumentationInterceptStrategy method wrapProcessorInInterceptors.

public Processor wrapProcessorInInterceptors(CamelContext context, ProcessorDefinition<?> definition, Processor target, Processor nextTarget) throws Exception {
    // do not double wrap it
    if (target instanceof InstrumentationProcessor) {
        return target;
    }
    // only wrap a performance counter if we have it registered in JMX by the jmx agent
    PerformanceCounter counter = registeredCounters.get(definition);
    if (counter != null) {
        InstrumentationProcessor wrapper = new InstrumentationProcessor(counter);
        wrapper.setProcessor(target);
        wrapper.setType(definition.getShortName());
        // add it to the mapping of wrappers so we can later change it to a decorated counter
        // that when we register the processor
        KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor> holder = new KeyValueHolder<ProcessorDefinition<?>, InstrumentationProcessor>(definition, wrapper);
        wrappedProcessors.put(target, holder);
        return wrapper;
    }
    return target;
}
Also used : ProcessorDefinition(org.apache.camel.model.ProcessorDefinition) KeyValueHolder(org.apache.camel.util.KeyValueHolder) ManagedPerformanceCounter(org.apache.camel.management.mbean.ManagedPerformanceCounter) PerformanceCounter(org.apache.camel.api.management.PerformanceCounter)

Aggregations

KeyValueHolder (org.apache.camel.util.KeyValueHolder)5 Message (javax.mail.Message)2 ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)2 IMAPFolder (com.sun.mail.imap.IMAPFolder)1 SortTerm (com.sun.mail.imap.SortTerm)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 FolderNotFoundException (javax.mail.FolderNotFoundException)1 MessagingException (javax.mail.MessagingException)1 SearchTerm (javax.mail.search.SearchTerm)1 Route (org.apache.camel.Route)1 PerformanceCounter (org.apache.camel.api.management.PerformanceCounter)1