Package org.axonframework.correlation

Source Code of org.axonframework.correlation.SimpleCorrelationDataProvider

/*
* Copyright (c) 2010-2014. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.axonframework.correlation;

import org.axonframework.domain.Message;
import org.axonframework.domain.MetaData;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* CorrelationDataProvider implementation defines correlation headers by the header names. The headers from messages
* with these keys are returned as correlation data.
*
* @author Allard Buijze
* @since 2.3
*/
public class SimpleCorrelationDataProvider implements CorrelationDataProvider<Message> {

    private final String[] headerNames;

    /**
     * Initializes the CorrelationDataProvider to return the meta data of messages with given <code>metaDataKeys</code>
     * as correlation data.
     *
     * @param metaDataKeys The keys of the meta data entries from messages to return as correlation data
     */
    public SimpleCorrelationDataProvider(String... metaDataKeys) {
        this.headerNames = Arrays.copyOf(metaDataKeys, metaDataKeys.length);
    }

    @Override
    public Map<String, ?> correlationDataFor(Message message) {
        if (headerNames.length == 0) {
            return Collections.emptyMap();
        }
        Map<String, Object> data = new HashMap<String, Object>();
        final MetaData metaData = message.getMetaData();
        for (String headerName : headerNames) {
            if (metaData.containsKey(headerName)) {
                data.put(headerName, metaData.get(headerName));
            }
        }
        return data;
    }
}
TOP

Related Classes of org.axonframework.correlation.SimpleCorrelationDataProvider

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.