/*=============================================================================*
* 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.addressing;
import org.apache.ws.XmlObjectWrapper;
import org.apache.ws.resource.ResourceKey;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.xmlbeans.XmlObject;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.AttributedQName;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.AttributedURI;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.ReferencePropertiesType;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.ServiceNameType;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.ReferenceParametersType;
import javax.xml.namespace.QName;
/**
* This class wraps XmlBean-generated XmlBeansEndpointReference types for use throughout the system.
*
* As a new version of the schema is generated, a new constructor should be added to encompass
* the new version.
*
* Note..Use fully qualified class name in constructors for different versions.
*
* @author Sal Campana
*/
public class XmlBeansEndpointReference
implements XmlObjectWrapper,
EndpointReference
{
private XmlObject m_xmlObjectEPR;
private String m_address;
private QName m_portTypeQName;
private String m_servicePortName;
private QName m_serviceQName;
private Object[] m_referenceProps;
private String m_addressingVersionURI;
private Object[] m_referenceParameters;
private ResourceKey m_resourceKey;
/**
* Constructs an EPR (and the underlying XmlObject representation) given the params.
*
* @param address - Endpoint Address. Must Not Be Null
* @param addressingURI - WS-Addressing URI - Must Not Be Null
*/
public XmlBeansEndpointReference(String address,
String addressingURI)
{
if(address == null) throw new IllegalArgumentException("Address must not be null!");
if(addressingURI == null) throw new IllegalArgumentException("WS-Addressing addresingURI must not be null!");
m_address = address;
m_addressingVersionURI = addressingURI;
}
/**
* Creates a new {@link XmlBeansEndpointReference} object.
*
* @param epr org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType XMLBean generated type
*/
public XmlBeansEndpointReference( org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType epr )
{
m_xmlObjectEPR = epr;
if(epr.getAddress()!=null)
m_address = epr.getAddress( ).getStringValue( );
if(epr.isSetPortType())
m_portTypeQName = epr.getPortType( ).getQNameValue( );
if(epr.isSetServiceName() && epr.getServiceName( ).isSetPortName())
m_servicePortName = epr.getServiceName( ).getPortName( );
if(epr.isSetServiceName())
m_serviceQName = epr.getServiceName( ).getQNameValue( );
if(epr.isSetReferenceProperties())
m_referenceProps = XmlBeanUtils.getChildElements(epr.getReferenceProperties());
}
/**
* Creates a new {@link XmlBeansEndpointReference} object.
*
* @param epr org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType XMLBean generated type
*/
public XmlBeansEndpointReference( org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType epr )
{
m_xmlObjectEPR = epr;
if(epr.getAddress()!=null)
m_address = epr.getAddress( ).getStringValue( );
if(epr.isSetPortType())
m_portTypeQName = epr.getPortType( ).getQNameValue( );
if(epr.isSetServiceName() && epr.getServiceName( ).isSetPortName())
m_servicePortName = epr.getServiceName( ).getPortName( );
if(epr.isSetServiceName() )
m_serviceQName = epr.getServiceName( ).getQNameValue( );
if(epr.isSetReferenceProperties())
m_referenceProps = XmlBeanUtils.getChildElements(epr.getReferenceProperties());
if(epr.isSetReferenceParameters())
m_referenceParameters = XmlBeanUtils.getChildElements(epr.getReferenceParameters());
}
/**
* Returns the Address from the EPR as a String.
*
* @return Address
*/
public String getAddress( )
{
return m_address;
}
/**
* Returns the PortName associated with the Service in the EPR as a String
*
* @return Service's Port Name
*/
public String getPortName( )
{
return m_servicePortName;
}
/**
* Returns the PortType QName
*
* @return PortType QName
*/
public QName getPortType( )
{
return m_portTypeQName;
}
/**
* Returns the ReferenceProperties directly from the underlying XmlBean-generated class.
* The signature returns Object to make it generic for other impls.
*
* @return Object[] containing the Reference Property elements
*/
public Object[] getReferenceProperties( )
{
return m_referenceProps;
}
/**
* Returns the ReferenceParameters Object.
*
* @return Object[] The ReferenceParameters
*/
public Object[] getReferenceParameters()
{
return m_referenceParameters;
}
/**
* Service QName
*
* @return Service QName
*/
public QName getServiceName( )
{
return m_serviceQName;
}
/**
* The underlying XmlBean-Generate EPR
*
* @return The generated EPR
*/
public XmlObject getXmlObject( )
{
XmlObject epr = null;
if(m_xmlObjectEPR != null)
{
epr = m_xmlObjectEPR;
}
else if(m_addressingVersionURI != null && m_address != null)
{
if(org.apache.ws.addressing.v2003_03.AddressingConstants.NSURI_ADDRESSING_SCHEMA.equals(m_addressingVersionURI))
{
EndpointReferenceType endpointReferenceType = build2003_03_EPR();
m_xmlObjectEPR = endpointReferenceType;
epr = endpointReferenceType;
}
else if(org.apache.ws.addressing.v2004_08_10.AddressingConstants.NSURI_ADDRESSING_SCHEMA.equals(m_addressingVersionURI))
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType endpointReferenceType = build2004_08_EPR();
m_xmlObjectEPR = endpointReferenceType;
epr = endpointReferenceType;
}
}
return epr;
}
public XmlObject getXmlObject( String namespace )
{
XmlObject epr = null;
if(namespace != null && m_address != null)
{
if(org.apache.ws.addressing.v2003_03.AddressingConstants.NSURI_ADDRESSING_SCHEMA.equals(namespace))
{
EndpointReferenceType endpointReferenceType = build2003_03_EPR();
m_xmlObjectEPR = endpointReferenceType;
epr = endpointReferenceType;
}
else if(org.apache.ws.addressing.v2004_08_10.AddressingConstants.NSURI_ADDRESSING_SCHEMA.equals(namespace))
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType endpointReferenceType = build2004_08_EPR();
m_xmlObjectEPR = endpointReferenceType;
epr = endpointReferenceType;
}
}
return epr;
}
private EndpointReferenceType build2003_03_EPR()
{
EndpointReferenceType endpointReferenceType = EndpointReferenceType.Factory.newInstance();
AttributedURI attributedURI = endpointReferenceType.addNewAddress();
attributedURI.setStringValue(m_address);
if(m_portTypeQName != null)
{
AttributedQName attributedQName = endpointReferenceType.addNewPortType();
attributedQName.setQNameValue(m_portTypeQName);
}
if(m_servicePortName != null)
{
ServiceNameType serviceNameType = endpointReferenceType.addNewServiceName();
serviceNameType.setPortName(m_servicePortName);
}
ReferencePropertiesType referencePropertiesType = endpointReferenceType.addNewReferenceProperties();
if(m_referenceProps != null)
{
addToProperties(referencePropertiesType, m_referenceProps);
}
if(m_resourceKey != null)
{
XmlObject resourceKey = XmlBeanUtils.addChildElement(referencePropertiesType, m_resourceKey.getName());
XmlBeanUtils.setValue(resourceKey, (String)m_resourceKey.getValue());
}
return endpointReferenceType;
}
private org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType build2004_08_EPR()
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType endpointReferenceType = org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType.Factory.newInstance();
org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI attributedURI = endpointReferenceType.addNewAddress();
attributedURI.setStringValue(m_address);
if(m_portTypeQName != null)
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedQName attributedQName = endpointReferenceType.addNewPortType();
attributedQName.setQNameValue(m_portTypeQName);
}
if(m_servicePortName != null)
{
org.xmlsoap.schemas.ws.x2004.x08.addressing.ServiceNameType serviceNameType = endpointReferenceType.addNewServiceName();
serviceNameType.setPortName(m_servicePortName);
}
org.xmlsoap.schemas.ws.x2004.x08.addressing.ReferencePropertiesType referencePropertiesType = endpointReferenceType.addNewReferenceProperties();
if(m_referenceProps != null)
{
addToProperties(referencePropertiesType, m_referenceProps);
}
if(m_resourceKey != null)
{
XmlObject resourceKey = XmlBeanUtils.addChildElement(referencePropertiesType, m_resourceKey.getName());
XmlBeanUtils.setValue(resourceKey, (String)m_resourceKey.getValue());
}
if(m_referenceParameters != null)
{
ReferenceParametersType referenceParametersType = endpointReferenceType.addNewReferenceParameters();
addToProperties(referenceParametersType, m_referenceParameters);
}
return endpointReferenceType;
}
private void addToProperties(XmlObject propertiesType, Object[] referenceProps)
{
for (int i = 0; i < referenceProps.length; i++)
{
Object referenceProp = referenceProps[i];
XmlBeanUtils.addChildElement(propertiesType, (XmlObject)referenceProp);
}
}
public void setPortTypeQName(QName portTypeQName)
{
m_portTypeQName = portTypeQName;
}
public void setServicePortName(String servicePortName)
{
m_servicePortName = servicePortName;
}
public void setServiceQName(QName serviceQName)
{
m_serviceQName = serviceQName;
}
/**
* @param referenceProps XmlObject[] of refernece properties to be added
*/
public void setReferenceProperties(Object[] referenceProps)
{
if( !(referenceProps instanceof XmlObject[]) ) throw new IllegalArgumentException("Refernece Props are expected to be an XmlObject[]!");
m_referenceProps = referenceProps;
}
/**
* @param referenceParameters XmlObject[] of refernece parameters to be added
*/
public void setReferenceParameters(Object[] referenceParameters)
{
if( !(referenceParameters instanceof XmlObject[]) ) throw new IllegalArgumentException("Refernece Params are expected to be an XmlObject[]!");
m_referenceParameters = referenceParameters;
}
/**
* Adds the ResourceKey which will be added to the ReferenceProperties
*
* @param resourceKey
*/
public void setResourceKey(ResourceKey resourceKey)
{
m_resourceKey = resourceKey;
}
}