Package org.apache.muse.ws.resource.sg.impl

Source Code of org.apache.muse.ws.resource.sg.impl.SimpleMembershipContentRule

/*=============================================================================*
*  Copyright 2006 The Apache Software Foundation
*
*  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.apache.muse.ws.resource.sg.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.namespace.QName;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.ws.resource.remote.WsResourceClient;
import org.apache.muse.ws.resource.sg.MembershipContentRule;
import org.apache.muse.ws.resource.sg.WssgConstants;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.util.xml.XmlUtils;

/**
*
* @author Dan Jemiolo (danj)
*
*/

public class SimpleMembershipContentRule implements MembershipContentRule
{
    //
    // Used to look up all exception messages
    //
    private static Messages _MESSAGES =
        MessagesFactory.get(SimpleMembershipContentRule.class);
   
    private static final QName[] _EMPTY_CONTENT = new QName[0];
   
    private QName[] _content = _EMPTY_CONTENT;
   
    private QName _portType = null;
   
    private EndpointReference _serviceGroupEPR = null;
   
    public SimpleMembershipContentRule(EndpointReference serviceGroupEPR)
    {
        _serviceGroupEPR = serviceGroupEPR;
    }
   
    public SimpleMembershipContentRule(EndpointReference serviceGroupEPR, Element xml)
    {
        this(serviceGroupEPR);
       
        if (xml == null)
            throw new NullPointerException(_MESSAGES.get("NullElement"));
       
        String portTypeName = xml.getAttribute(WssgConstants.MEMBERSHIP_INTERFACE);
       
        if (portTypeName.length() > 0)
            _portType = XmlUtils.parseQName(portTypeName, xml);
       
        String contentList = xml.getAttribute(WssgConstants.CONTENT_ELEMENTS);
       
        if (contentList.length() > 0)
        {
            String[] contentNames = contentList.split(" ");
            _content = new QName[contentNames.length];
           
            for (int n = 0; n < contentNames.length; ++n)
                _content[n] = XmlUtils.parseQName(contentNames[n], xml);
        }
    }
       
    public QName[] getContentElements()
    {
        return _content;
    }
   
    public QName getMemberInterface()
    {
        return _portType;
    }
   
    public EndpointReference getServiceGroupEPR()
    {
        return _serviceGroupEPR;
    }
   
    /**
     *
     * @param epr
     *        The EPR for the resource against which this membership content
     *        rule will be evaluated.
     *
     * @return True if the given resource's WS-RP document matches the rules
     *         in the MCR.
     *
     */
    public boolean isMatch(EndpointReference epr)
    {
        if (epr == null)
            throw new NullPointerException(_MESSAGES.get("NullResource"));
       
        EndpointReference sgEPR = getServiceGroupEPR();       
        WsResourceClient resource = new WsResourceClient(epr, sgEPR);
        QName[] content = getContentElements();
       
        try
        {
            //
            // all we have to do is try and read all the properties
            // in the list - if any of them is undefined, this will
            // fault, and we don't have a match. there is no fault if
            // the property is defined but has zero instances
            //
            // note that we only do the test if there are some property
            // names - an empty array means "any resource is allowed"
            //
            if (content.length > 0)
                resource.getMultipleResourceProperties(content);
        }
       
        //
        // in this case, there is no error - this just means that
        // the resource does not match the membership rule
        //
        catch (SoapFault error)
        {
            return false;
        }
       
        return true;
    }
   
    public void setContentElements(QName[] content)
    {
        if (content == null)
            content = _EMPTY_CONTENT;
       
        _content = content;
    }
   
    public void setMemberInterface(QName portType)
    {
        _portType = portType;
    }

    public Element toXML()
    {
        return toXML(XmlUtils.EMPTY_DOC);
    }

    public Element toXML(Document factory)
    {
        Element root = XmlUtils.createElement(factory, WssgConstants.CONTENT_RULE_QNAME);
       
        QName interfaceName = getMemberInterface();
       
        if (interfaceName != null)
        {
            root.setAttribute(WssgConstants.MEMBERSHIP_INTERFACE, XmlUtils.toString(interfaceName));
           
            String prefix = interfaceName.getPrefix();
            String namespace = interfaceName.getNamespaceURI();
            XmlUtils.setNamespaceAttribute(root, prefix, namespace);
        }
       
        QName[] content = getContentElements();
        StringBuffer contentAttr = new StringBuffer();
        Map prefixes = new HashMap();
       
        for (int n = 0; n < content.length; ++n)
        {
            contentAttr.append(XmlUtils.toString(content[n]));
           
            if (n < content.length - 1)
                contentAttr.append(' ');
           
            prefixes.put(content[n].getPrefix(), content[n].getNamespaceURI());
        }
       
        root.setAttribute(WssgConstants.CONTENT_ELEMENTS, contentAttr.toString());
       
        Iterator i = prefixes.keySet().iterator();
       
        while (i.hasNext())
        {
            String next = (String)i.next();
            XmlUtils.setNamespaceAttribute(root, next, (String)prefixes.get(next));
        }
       
        return root;
    }
}
TOP

Related Classes of org.apache.muse.ws.resource.sg.impl.SimpleMembershipContentRule

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.