org.eclipse.swordfish.internal.resolver.policy.helpers.WsPolicyBuilder.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.swordfish.internal.resolver.policy.helpers.WsPolicyBuilder.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.internal.resolver.policy.helpers;

import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedList;

import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.cxf.ws.policy.AssertionBuilderRegistry;
import org.apache.cxf.ws.policy.PolicyBuilderImpl;
import org.apache.cxf.ws.policy.PolicyConstants;
import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertionBuilder;
import org.apache.cxf.ws.policy.builder.xml.XMLPrimitiveAssertionBuilder;

import org.apache.neethi.Policy;
import org.eclipse.swordfish.core.resolver.policy.builder.PlatformPolicyBuilder;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import org.w3c.dom.Element;

public class WsPolicyBuilder implements PlatformPolicyBuilder<Policy>, InitializingBean {

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

    private PolicyBuilderImpl policyBuilder;
    private PrimitiveAssertionBuilder assertionBuilder;
    private boolean isInitialized = false;

    public synchronized Policy buildPolicy(Element element) throws Exception {
        return policyBuilder.getPolicy(element);
    }

    public synchronized Policy buildPolicy(InputStream stream) throws Exception {
        return policyBuilder.getPolicy(stream);
    }

    public synchronized void registerPolicyAssertions(Collection<QName> assertions) {
        Collection<QName> assertionsToRegister = new LinkedList<QName>();
        Collection<QName> assertionsToUnregister = new LinkedList<QName>();

        if (assertionBuilder == null) {
            assertionBuilder = new XMLPrimitiveAssertionBuilder();
        } else {
            assertionsToUnregister.addAll(assertionBuilder.getKnownElements());
            assertionsToUnregister.removeAll(assertions);
        }
        assertionsToRegister.addAll(assertions);
        assertionBuilder.setKnownElements(assertionsToRegister);

        AssertionBuilderRegistry builderRegistry = policyBuilder.getAssertionBuilderRegistry();
        builderRegistry.setIgnoreUnknownAssertions(false);

        for (QName elem : assertionsToUnregister) {
            builderRegistry.unregister(elem);
        }

        for (QName elem : assertionsToRegister) {
            builderRegistry.register(elem, assertionBuilder);
        }
    }

    public synchronized void setPolicyNamespace(String namespace) {
        if (!isInitialized) {
            PolicyConstants constants = new PolicyConstants();
            constants.setNamespace(namespace);
            policyBuilder.getBus().setExtension(constants, PolicyConstants.class);
            isInitialized = true;
        } else {
            policyBuilder.getBus().getExtension(PolicyConstants.class).setNamespace(namespace);
        }
    }

    public synchronized void setPolicyBuilder(PolicyBuilderImpl policyBuilder) {
        this.policyBuilder = policyBuilder;
        isInitialized = false;
    }

    public PolicyBuilderImpl getPolicyBuilder() {
        return policyBuilder;
    }

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(policyBuilder, "Platform policy builder must be set");
    }

}