org.eclipse.swordfish.plugins.planner.policy.PolicyAssertionHintExtractor.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.swordfish.plugins.planner.policy.PolicyAssertionHintExtractor.java

Source

/*******************************************************************************
 * Copyright (c) 2008, 2009 SOPERA GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     SOPERA GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.swordfish.plugins.planner.policy;

import static java.util.Collections.emptyList;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.NormalizedMessage;
import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.neethi.Assertion;
import org.apache.neethi.Constants;
import org.apache.neethi.Policy;
import org.eclipse.swordfish.core.planner.strategy.Hint;
import org.eclipse.swordfish.core.planner.strategy.HintExtractor;
import org.eclipse.swordfish.core.resolver.policy.PolicyExtractor;
import org.eclipse.swordfish.internal.core.util.smx.JbiConstants;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

//TODO: Add JavaDocs
public class PolicyAssertionHintExtractor implements HintExtractor {

    private static final QName POLICY_HEADER = new QName("http://schemas.xmlsoap.org/ws/2004/09/policy", "Policy");

    private static final Log LOG = LogFactory.getLog(PolicyAssertionHintExtractor.class);

    private static final List<Hint<?>> EMPTY_LIST = emptyList();
    private PolicyExtractor policyExtractor;

    public PolicyExtractor getPolicyExtractor() {
        return policyExtractor;
    }

    public void setPolicyExtractor(PolicyExtractor policyExtractor) {
        this.policyExtractor = policyExtractor;
    }

    public PolicyAssertionHintExtractor() {

    }

    public List<Hint<?>> extractHints(MessageExchange messageExchange) {
        List<Hint<?>> hints = EMPTY_LIST;

        if (messageExchange.getStatus() != ExchangeStatus.ACTIVE) {
            LOG.warn("Couldn't extract hints from terminated (status == DONE) message exchange!");
            return hints;
        }

        Policy policy = getPolicy(messageExchange);
        if (policy != null) {
            hints = extractAssertions(policy);
        }
        return hints;
    }

    @SuppressWarnings("unchecked")
    private Policy getPolicy(MessageExchange messageExchange) {
        Policy policy = null;
        try {
            NormalizedMessage message = getNormalizedMessage(messageExchange);

            Map<QName, Node> headers = (Map<QName, Node>) message.getProperty(JbiConstants.SOAP_HEADERS);
            if (headers == null) {
                return null;
            }

            DocumentFragment policyFragment = (DocumentFragment) headers.get(POLICY_HEADER);
            if (policyFragment == null) {
                return null;
            }

            Element policyElement = (Element) policyFragment.getFirstChild();
            if (!Constants.ELEM_POLICY.equals(policyElement.getLocalName())
                    || !Constants.URI_POLICY_NS.equals(policyElement.getNamespaceURI())) {
                return null;
            }
            return policyExtractor.extractPolicy(policyElement);
        } catch (Exception ex) {
            LOG.warn(ex.getMessage(), ex);
        }
        return policy;
    }

    private NormalizedMessage getNormalizedMessage(MessageExchange exchange) {
        String messageId = exchange.getRole().equals(MessageExchange.Role.CONSUMER) ? "in" : "out";
        return exchange.getMessage(messageId);
    }

    @SuppressWarnings("unchecked")
    public List<Hint<?>> extractAssertions(Policy policy) {
        List<Hint<?>> assertions = new ArrayList<Hint<?>>();

        Iterator<Iterable<Assertion>> alternativesIter = policy.getAlternatives();
        if (alternativesIter.hasNext()) {
            Iterable<Assertion> alternative = alternativesIter.next();

            for (Assertion assertion : alternative) {
                assertions.add(new AssertionHint<Assertion>(assertion));
            }
        }
        return assertions;
    }
}