/*=============================================================================*
* 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.dm.muws.impl;
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.util.xml.XmlUtils;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.ws.dm.muws.Identity;
import org.apache.muse.ws.dm.muws.MuwsConstants;
import org.apache.muse.ws.dm.muws.Participant;
import org.apache.muse.ws.dm.muws.Relationships;
import org.apache.muse.ws.resource.WsResource;
import org.apache.muse.ws.resource.basefaults.BaseFault;
import org.apache.muse.ws.resource.basefaults.WsbfUtils;
import org.apache.muse.ws.resource.ext.faults.InvalidMessageFormatFault;
import org.apache.muse.ws.resource.remote.WsResourceClient;
/**
*
* SimpleParticipant represents resources that are participating in a relationship
* through the {@linkplain Relationships Relationships} capability. It has
* the information needed to locate the participants in a relationship and
* determine their roles.
*
* @author Dan Jemiolo (danj)
*
*/
public class SimpleParticipant implements Participant
{
//
// Used to look up all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(SimpleParticipant.class);
private EndpointReference _resourceEPR = null;
private String _resourceID = null;
public String _role = null;
/**
*
* Reads the muws1:ResourceId property from a <b>local</b> resource.
*
*/
private static String getResourceId(WsResource resource)
throws BaseFault
{
Identity id = (Identity)resource.getCapability(MuwsConstants.IDENTITY_URI);
return id == null ? null : id.getResourceId();
}
/**
*
* Reads the muws1:ResourceId property from a <b>remote</b> resource.
*
*/
private static String getResourceId(WsResourceClient resource)
throws BaseFault
{
try
{
String[] results = (String[])resource.getPropertyAsObject(MuwsConstants.RESOURCE_ID_QNAME, String.class);
if (results.length == 0)
return null;
return results[0];
}
catch (SoapFault error)
{
throw WsbfUtils.convertToFault(error);
}
}
/**
*
* @param xml
* The DOM Element representing the relationship participant.
*
* @throws BaseFault
* <ul>
* <li>If the participant XML is not formatted properly.</li>
* </ul>
*
*/
public SimpleParticipant(Element xml)
throws BaseFault
{
if (xml == null)
throw new NullPointerException(_MESSAGES.get("NullParticipantElement"));
Element eprXML = XmlUtils.getElement(xml, MuwsConstants.MANAGEABILITY_EPR_QNAME);
if (eprXML == null)
throw new InvalidMessageFormatFault(_MESSAGES.get("NoParticipantEPR"));
try
{
_resourceEPR = new EndpointReference(eprXML);
}
catch (SoapFault error)
{
throw new InvalidMessageFormatFault(error);
}
_resourceID = XmlUtils.getElementText(xml, MuwsConstants.RESOURCE_ID_QNAME);
if (_resourceID == null)
throw new InvalidMessageFormatFault(_MESSAGES.get("NoParticipantID"));
_role = XmlUtils.getElementText(xml, MuwsConstants.ROLE_QNAME);
if (_role == null)
throw new InvalidMessageFormatFault(_MESSAGES.get("NoParticipantRole"));
}
/**
*
* @param resourceEPR
* The participant's EPR.
*
* @param resourceID
* The participant's MUWS ResourceId.
*
* @param role
* The URI that identifies the participant's role in the
* relationship.
*
*/
public SimpleParticipant(EndpointReference resourceEPR, String resourceID, String role)
{
if (resourceEPR == null)
throw new NullPointerException(_MESSAGES.get("NullParticipantEPR"));
if (resourceID == null)
throw new NullPointerException(_MESSAGES.get("NullParticipantID"));
if (role == null)
throw new NullPointerException(_MESSAGES.get("NullParticipantRole"));
_resourceEPR = resourceEPR;
_resourceID = resourceID;
_role = role;
}
/**
*
* @param resource
* The participant resource. This will be used to gather its EPR
* and MUWS ResourceId.
*
* @param role
* The URI that identifies the participant's role in the
* relationship.
*
* @throws BaseFault
* <ul>
* <li>If the participant resource is unavailable.</li>
* </ul>
*
*/
public SimpleParticipant(WsResource resource, String role)
throws BaseFault
{
if (resource == null)
throw new NullPointerException(_MESSAGES.get("NullParticipantResource"));
if (role == null)
throw new NullPointerException(_MESSAGES.get("NullParticipantRole"));
_resourceEPR = resource.getEndpointReference();
_resourceID = getResourceId(resource);
_role = role;
}
/**
*
* @param remote
* A proxy that points to the participant resource. This will be
* used to gather its EPR and MUWS ResourceId.
*
* @param role
* The URI that identifies the participant's role in the
* relationship.
*
* @throws BaseFault
* <ul>
* <li>If the participant resource is unavailable.</li>
* </ul>
*
*/
public SimpleParticipant(WsResourceClient remote, String role)
throws BaseFault
{
if (remote == null)
throw new NullPointerException(_MESSAGES.get("NullParticipantResource"));
if (role == null)
throw new NullPointerException(_MESSAGES.get("NullParticipantRole"));
_resourceEPR = remote.getEndpointReference();
_resourceID = getResourceId(remote);
_role = role;
}
/**
*
* @return The participant's EPR.
*
*/
public EndpointReference getManageabilityReference()
{
return _resourceEPR;
}
/**
*
* @return The participant's MUWS ResourceId.
*
* @see org.apache.muse.ws.dm.muws.Identity#getResourceId()
*
*/
public String getResourceId()
{
return _resourceID;
}
/**
*
* @return The URI identifying the role of the participant in its
* relationship.
*
*/
public String getRole()
{
return _role;
}
public Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public Element toXML(Document doc)
{
if (doc == null)
throw new NullPointerException(_MESSAGES.get("NullDocument"));
Element root = XmlUtils.createElement(doc, MuwsConstants.PARTICIPANT_QNAME);
Element eprXML = getManageabilityReference().toXML();
XmlUtils.setElement(root, MuwsConstants.MANAGEABILITY_EPR_QNAME, eprXML);
String resourceID = getResourceId();
if (resourceID != null)
XmlUtils.setElement(root, MuwsConstants.RESOURCE_ID_QNAME, resourceID);
XmlUtils.setElement(root, MuwsConstants.ROLE_QNAME, getRole());
return root;
}
}