Package org.apache.ws.resource.impl

Source Code of org.apache.ws.resource.impl.ResourceCapabilityImpl

/*
* Copyright 2004 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.ws.resource.impl;

import org.apache.ws.resource.InvalidWsrfWsdlException;
import org.apache.ws.resource.ResourceCapability;
import org.apache.ws.resource.properties.v1_2.ResourceProperties1_2Constants;
import org.apache.ws.resource.properties.v1_2.porttype.GetResourcePropertyPortType;
import org.apache.ws.resource.properties.v1_2.porttype.GetMultipleResourcePropertiesPortType;
import org.apache.ws.resource.properties.v1_2.porttype.SetResourcePropertiesPortType;
import org.apache.ws.resource.properties.v1_2.porttype.QueryResourcePropertiesPortType;
import org.apache.ws.resource.properties.v1_3.ResourceProperties1_3Constants;
import org.apache.ws.util.WsdlUtils;
import org.apache.ws.util.WsrfWsdlUtils;

import javax.wsdl.Definition;
import javax.wsdl.Import;
import javax.wsdl.Operation;
import javax.wsdl.PortType;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* A {@link ResourceCapability} implementation.
*
* @author Ian Springer (ian DOT springer AT hp DOT com)
*/
public class ResourceCapabilityImpl
        implements ResourceCapability
{

    private Definition m_def;
    private PortType m_portType;
    private Map m_implementedResourceDefs;
    private QName[] m_propNames;
    private String[] m_customOpNames;
    private QName[] m_customPropNames;
    private QName m_propsDocName;

    /**
     * Creates a new {@link ResourceCapabilityImpl} based on the specified JWSDL definition and portType.
     *
     * @param def a JWSDL definition
     */
    public ResourceCapabilityImpl( Definition def, PortType portType ) throws InvalidWsrfWsdlException
    {
        m_def = def;
        m_portType = portType;
        initImplementedPortTypes();
        initCustomOperations();
        initPropertyNames();
        if ( !m_def.getTargetNamespace().startsWith( "http://docs.oasis-open.org/" ) )
        {
            validateOperations();
            validateProperties();
        }
    }

    protected void validateProperties() throws InvalidWsrfWsdlException
    {
        boolean isValid = true;
        List propNameList = Arrays.asList( m_propNames );
        Iterator defIter = m_implementedResourceDefs.values().iterator();
        while ( defIter.hasNext() )
        {
            ResourceCapability def = (ResourceCapability) defIter.next();
            if ( def.hasProperties() )
            {
                QName[] propNames = def.getPropertyNames();
                Set missingPropNames = new HashSet();
                for ( int i = 0; i < propNames.length; i++ )
                {
                    if ( !propNameList.contains( propNames[i] ) )
                    {
                        missingPropNames.add( propNames[i] );
                    }
                }
                if ( !missingPropNames.isEmpty() )
                {
                    System.err.println( "PortType implements the operations from the " + def.getPortType().getQName() + " portType but does not define the following required properties: " + missingPropNames );
                    isValid = false;
                }
            }
        }
        if ( !isValid )
        {
            throw new InvalidWsrfWsdlException( "PortType " + m_portType.getQName() + " does not define one or more properties required by the portTypes it implements." );
        }
    }

    protected void validateOperations()
            throws InvalidWsrfWsdlException
    {
        if ( hasProperties() )
        {
            if ( !m_implementedResourceDefs.containsKey( GetResourcePropertyPortType.NAME ) &&
                    !m_implementedResourceDefs.containsKey( ResourceProperties1_3Constants.PORT_TYPE_NAME_GET_RESOURCE_PROPERTY )
            )
            {
                throw new InvalidWsrfWsdlException( "PortType " + m_portType.getQName() + " defines a wsrp:ResourceProperties attribute but does not implement the WSRF-RP GetResourceProperty portType." );
            }
        }
        else
        {
            if ( m_implementedResourceDefs.containsKey( GetResourcePropertyPortType.NAME ) ||
                    m_implementedResourceDefs.containsKey( ResourceProperties1_3Constants.PORT_TYPE_NAME_GET_RESOURCE_PROPERTY ) ||
                    m_implementedResourceDefs.containsKey( GetMultipleResourcePropertiesPortType.NAME ) ||
                    m_implementedResourceDefs.containsKey( ResourceProperties1_3Constants.PORT_TYPE_NAME_GET_MULTIPLE_RESOURCE_PROPERTIES ) ||
                    m_implementedResourceDefs.containsKey( SetResourcePropertiesPortType.NAME ) ||
                    m_implementedResourceDefs.containsKey( ResourceProperties1_3Constants.PORT_TYPE_NAME_SET_RESOURCE_PROPERTIES ) ||
                    m_implementedResourceDefs.containsKey( QueryResourcePropertiesPortType.NAME ) ||
                    m_implementedResourceDefs.containsKey( ResourceProperties1_3Constants.PORT_TYPE_NAME_QUERY_RESOURCE_PROPERTIES )
            )
            {
                throw new InvalidWsrfWsdlException( "PortType " + m_portType.getQName() + " does not define a wsrp:ResourceProperties attribute but implements one or more WSRF-RP portTypes." );
            }
        }
    }

    public boolean hasProperties()
    {
        return m_propNames != null;
    }

    private void initPropertyNames()
    {
        m_propsDocName = WsrfWsdlUtils.getResourcePropertiesDocumentName( m_portType );
        m_propNames = WsrfWsdlUtils.getResourcePropertyNames( m_propsDocName, m_def );
        Set customPropNames = new HashSet();
        if ( hasProperties() )
        {
            Set inheritedPropNames = getInheritedPropertyNames();
            for ( int i = 0; i < m_propNames.length; i++ )
            {
                if ( !isInheritedProperty( inheritedPropNames, m_propNames[i] ) )
                {
                    customPropNames.add( m_propNames[i] );
                }
            }
        }
        m_customPropNames = (QName[]) customPropNames.toArray( new QName[0] );
    }

    public Map getImplementedResourceCapabilities()
    {
        return m_implementedResourceDefs;
    }

    /**
     * DOCUMENT_ME
     *
     * @return DOCUMENT_ME
     */
    public String[] getCustomOperationNames()
    {
        return m_customOpNames;
    }

    /**
     * @return
     */
    public QName[] getCustomPropertyNames()
    {
        return m_customPropNames;
    }

    /**
     * DOCUMENT_ME
     *
     * @return DOCUMENT_ME
     */
    public Definition getDefinition()
    {
        return m_def;
    }

    /**
     * DOCUMENT_ME
     *
     * @return DOCUMENT_ME
     */
    public PortType getPortType()
    {
        return m_portType;
    }

    private void initImplementedPortTypes() throws InvalidWsrfWsdlException
    {
        m_implementedResourceDefs = new HashMap();
        ResourceCapability[] importedResourceDefs = getImportedResourceDefinitions();
        for ( int i = 0; i < importedResourceDefs.length; i++ )
        {
            PortType importedPortType = importedResourceDefs[i].getPortType();
            if ( WsrfWsdlUtils.implementsPortType( m_portType, importedPortType ) )
            {
                m_implementedResourceDefs.put( importedPortType.getQName(),
                        importedResourceDefs[i] );
            }
        }
    }

    private ResourceCapability[] getImportedResourceDefinitions() throws InvalidWsrfWsdlException
    {
        List importedResourceDefs = new ArrayList();
        Import[] imports = getImports( m_def );
        for ( int i = 0; i < imports.length; i++ )
        {
            Definition def = imports[i].getDefinition();
            Map portTypes = def.getPortTypes();
            Iterator portTypeIter = portTypes.values().iterator();
            while ( portTypeIter.hasNext() )
            {
                PortType portType = (PortType) portTypeIter.next();
                importedResourceDefs.add( new ResourceCapabilityImpl( def, portType ) );
            }
        }
        return (ResourceCapability[]) importedResourceDefs.toArray( new ResourceCapability[0] );
    }

    /**
     * DOCUMENT_ME
     *
     * @return DOCUMENT_ME
     */
    public QName[] getPropertyNames()
    {
        return m_propNames;
    }

    /**
     * DOCUMENT_ME
     *
     * @return DOCUMENT_ME
     */
    public String toString()
    {
        StringBuffer strBuf = new StringBuffer();
        strBuf.append( "=== WS-Resource Definition defined via portType " + m_portType.getQName() + " ===\n" );
        strBuf.append( "Implemented PortTypes:\n" );
        if ( m_implementedResourceDefs.isEmpty() )
        {
            strBuf.append( "\t<NONE>\n" );
        }
        else
        {
            Iterator implementedPortTypeNameIter = m_implementedResourceDefs.keySet().iterator();
            while ( implementedPortTypeNameIter.hasNext() )
            {
                QName portTypeName = (QName) implementedPortTypeNameIter.next();
                strBuf.append( "\t" + portTypeName + "\n" );
            }
        }
        strBuf.append( "Custom Operations:\n" );
        if ( m_customOpNames.length == 0 )
        {
            strBuf.append( "\t<NONE>\n" );
        }
        else
        {
            for ( int i = 0; i < m_customOpNames.length; i++ )
            {
                strBuf.append( "\t" + m_customOpNames[i] + "\n" );
            }
        }
        strBuf.append( "Custom Properties:\n" );
        if ( !hasProperties() )
        {
            strBuf.append( "\t<NONE>\n" );
        }
        else
        {
            for ( int i = 0; i < m_customPropNames.length; i++ )
            {
                strBuf.append( "\t" + m_customPropNames[i] + "\n" );
            }
        }
        return strBuf.toString();
    }

    private Import[] getImports( Definition def )
    {
        Set importSet = new HashSet();
        Map importMap = def.getImports();
        Iterator iter = importMap.keySet().iterator();
        while ( iter.hasNext() )
        {
            String nsURI = (String) iter.next();
            Iterator importsIter = ( (List) importMap.get( nsURI ) ).iterator();
            while ( importsIter.hasNext() )
            {
                Import anImport = (Import) importsIter.next();
                Import[] importDefImports = getImports( anImport.getDefinition() );
                List imports = new ArrayList();
                imports.add( anImport );
                imports.addAll( Arrays.asList( importDefImports ) );
                for ( int i = 0; i < imports.size(); i++ )
                {
                    Import importToCheck = (Import) imports.get( i );
                    if ( !importIsRedundant( importSet, importToCheck ) )
                    {
                        importSet.add( anImport );
                    }
                }
            }
        }
        return (Import[]) importSet.toArray( new Import[0] );
    }

    private boolean importIsRedundant( Set imports, Import anImport )
    {
        boolean importIsRedundant = false;
        Iterator importIter = imports.iterator();
        while ( importIter.hasNext() )
        {
            Import anotherImport = (Import) importIter.next();
            if ( WsdlUtils.equals( anImport, anotherImport ) )
            {
                importIsRedundant = true;
                break;
            }
        }
        return importIsRedundant;
    }

    private void initCustomOperations()
    {
        Set customOpNames = new HashSet();
        Set inheritedOps = getInheritedOperations();
        List ops = m_portType.getOperations();
        for ( int i = 0; i < ops.size(); i++ )
        {
            Operation op = (Operation) ops.get( i );
            if ( !isInheritedOperation( inheritedOps, op ) )
            {
                customOpNames.add( op.getName() );
            }
        }
        m_customOpNames = (String[]) customOpNames.toArray( new String[0] );
    }

    private boolean isInheritedOperation( Set inheritedOps, Operation op )
    {
        boolean isInheritedOp = false;
        Iterator inheritedOpIter = inheritedOps.iterator();
        while ( inheritedOpIter.hasNext() )
        {
            Operation inheritedOp = (Operation) inheritedOpIter.next();
            if ( WsdlUtils.equals( op, inheritedOp ) )
            {
                isInheritedOp = true;
                break;
            }
        }
        return isInheritedOp;
    }

    private boolean isInheritedProperty( Set inheritedPropNames, QName propName )
    {
        boolean isInheritedProp = false;
        Iterator inheritedPropNameIter = inheritedPropNames.iterator();
        while ( inheritedPropNameIter.hasNext() )
        {
            QName inheritedPropName = (QName) inheritedPropNameIter.next();
            if ( inheritedPropName.equals( propName ) )
            {
                isInheritedProp = true;
                break;
            }
        }
        return isInheritedProp;
    }

    private Set getInheritedOperations()
    {
        Set specOps = new HashSet();
        Iterator defIter = m_implementedResourceDefs.values().iterator();
        while ( defIter.hasNext() )
        {
            ResourceCapability def = (ResourceCapability) defIter.next();
            List ops = def.getPortType().getOperations();
            for ( int i = 0; i < ops.size(); i++ )
            {
                specOps.add( (Operation) ops.get( i ) );
            }
        }
        return specOps;
    }

    private Set getInheritedPropertyNames()
    {
        Set inheritedPropNames = new HashSet();
        Iterator defIter = m_implementedResourceDefs.values().iterator();
        while ( defIter.hasNext() )
        {
            ResourceCapability def = (ResourceCapability) defIter.next();
            if ( def.hasProperties() )
            {
                QName[] propNames = def.getPropertyNames();
                for ( int i = 0; i < propNames.length; i++ )
                {
                    inheritedPropNames.add( propNames[i] );
                }
            }
        }
        return inheritedPropNames;
    }

    /**
     * DOCUMENT_ME
     *
     * @param args DOCUMENT_ME
     *
     * @throws Exception DOCUMENT_ME
     */
    public static void main( String[] args ) throws Exception
    {
        WSDLReader wsdlReader = WSDLFactory.newInstance().newWSDLReader();
        String wsdlPath = "C:\\Projects\\Apache\\apollo\\trunk\\target\\wsrf\\wsdl\\FileSystem.wsdl";
        Definition def = wsdlReader.readWSDL( wsdlPath );
        PortType portType = (PortType) def.getPortTypes().values().toArray()[0];
        ResourceCapability wsResourceDef = null;
        try
        {
            wsResourceDef = new ResourceCapabilityImpl( def, portType );
        }
        catch ( InvalidWsrfWsdlException iwwe )
        {
            System.err.println( iwwe );
            System.exit( 1 );
        }
        System.out.println( wsResourceDef );

        Map implementedResourceDefs = wsResourceDef.getImplementedResourceCapabilities();
        Iterator specDefs = implementedResourceDefs.values().iterator();
        while ( specDefs.hasNext() )
        {
            System.out.println( (ResourceCapability) specDefs.next() );
        }
    }

    public boolean implementsResourceCapability( QName capabilityName )
    {
        return m_implementedResourceDefs.containsKey( capabilityName );
    }

    public QName getPropertiesDocumentName()
    {
        return m_propsDocName;
    }

}
TOP

Related Classes of org.apache.ws.resource.impl.ResourceCapabilityImpl

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.