/*=============================================================================*
* 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.remote;
import java.util.Date;
import org.w3c.dom.Element;
import org.apache.muse.ws.addressing.EndpointReference;
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.resource.sg.impl.AddRequest;
import org.apache.muse.ws.resource.sg.impl.AddResponse;
import org.apache.muse.ws.resource.sg.impl.SimpleMembershipContentRule;
import org.apache.muse.ws.addressing.soap.SoapClient;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.core.Environment;
import org.apache.muse.util.xml.XmlUtils;
/**
*
* ServiceGroupClient is a WSRF-based web services client that provides an
* implementation of the (optional) wsrf-sg:Add operation as well as a few
* convenience methods that make reading through service groups easier.
*
* @author Dan Jemiolo (danj)
*
*/
public class ServiceGroupClient extends WsResourceClient
{
public ServiceGroupClient(EndpointReference destination)
{
super(destination);
}
public ServiceGroupClient(EndpointReference destination,
EndpointReference source)
{
super(destination, source);
}
public ServiceGroupClient(EndpointReference destination,
EndpointReference source,
Environment environment)
{
super(destination, source, environment);
}
public ServiceGroupClient(EndpointReference destination,
EndpointReference source,
SoapClient soapClient)
{
super(destination, source, soapClient);
}
/**
*
* Invokes the wsrf-sg:Add operation with an empty wsrf-sg:Content parameter.
*
*/
public WsResourceClient add(EndpointReference memberEPR, Date termination)
throws SoapFault
{
AddRequest add = new AddRequest(memberEPR, termination);
Element responseXML = invoke(WssgConstants.ADD_URI, add.toXML());
AddResponse response = new AddResponse(responseXML);
return response.getEntryClient();
}
/**
*
* @return An array of clients pointing to the member EPRs that are
* currently in the service group. The array may be empty.
* The clients in the array can be used immediately in order
* to communicate with the resources in the service group.
*
*/
public WsResourceClient[] getMembers()
throws SoapFault
{
Element[] entries = getResourceProperty(WssgConstants.ENTRY_QNAME);
WsResourceClient[] clients = new WsResourceClient[entries.length];
EndpointReference src = getSource();
EndpointReference dest = null;
for (int n = 0; n < entries.length; ++n)
{
//
// convert ws-sg entry XML -> EPRs -> clients
//
Element eprXML = XmlUtils.getElement(entries[n], WssgConstants.MEMBER_SERVICE_EPR_QNAME);
dest = new EndpointReference(eprXML);
clients[n] = new WsResourceClient(dest, src);
}
return clients;
}
/**
*
* @param epr
* The EPR of a resource that may or may not fit the content rules
* of the service group.
*
* @return True if the resource at the given EPR meets the restrictions of
* this service group's membership content rules.
*
*/
public boolean isMatch(EndpointReference epr)
throws SoapFault
{
//
// get content rules from service group
//
Element[] results = getResourceProperty(WssgConstants.CONTENT_RULE_QNAME);
//
// have to do this in a loop since we have to parse the MCRs
//
for (int n = 0; n < results.length; ++n)
{
//
// convert XML -> MCR and then use its isMatch() to do the work
//
MembershipContentRule mcr = new SimpleMembershipContentRule(getEndpointReference(), results[n]);
if (!mcr.isMatch(epr))
return false;
}
return true;
}
}