/*=============================================================================*
* 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 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.WsResource;
import org.apache.muse.ws.resource.basefaults.BaseFault;
import org.apache.muse.ws.resource.impl.AbstractWsResourceCapability;
import org.apache.muse.ws.resource.remote.WsResourceClient;
import org.apache.muse.ws.resource.sg.Entry;
import org.apache.muse.ws.resource.sg.ServiceGroup;
import org.apache.muse.ws.resource.sg.WssgConstants;
import org.apache.muse.ws.resource.sg.faults.ContentCreationFailedFault;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.util.xml.XmlUtils;
/**
*
* SimpleEntry is Muse's default implementation of the WS-RF ServiceGroupEntry
* capability. This capability provides all of the Entry-specific properties
* needed to quality the resource as a service group entry. Because entries are
* also serialized as properties in the service group itself, this class
* also provides XML serialization.
*
* @author Dan Jemiolo (danj)
*
*/
public class SimpleEntry
extends AbstractWsResourceCapability implements Entry
{
//
// Used to look up all exception messages
//
protected static Messages _MESSAGES = MessagesFactory.get(SimpleEntry.class);
private static final Element[] _EMPTY_CONTENT = new Element[0];
private WsResourceClient _member = null;
private WsResource _serviceGroup = null;
/**
*
* This implementation reads the content values on-the-fly, creating a
* client to the member resource and using the WSRP GetMultipleResourceProperties
* operation to discover the property values.
*
*/
public Element getContent()
throws BaseFault
{
Element[] content = _EMPTY_CONTENT;
WsResourceClient client = getMemberClient();
if (client == null)
return null;
ServiceGroup sg = (ServiceGroup)getServiceGroup().getCapability(WssgConstants.SERVICE_GROUP_URI);
QName[] contentNames = sg.getContentElements();
try
{
if (contentNames.length != 0)
content = client.getMultipleResourceProperties(contentNames);
}
catch (SoapFault fault)
{
throw new ContentCreationFailedFault(fault);
}
if (content.length == 0)
return null;
Document doc = content[0].getOwnerDocument();
Element root = XmlUtils.createElement(doc, WssgConstants.CONTENT_QNAME);
for (int n = 0; n < content.length; ++n)
root.appendChild(content[n]);
return root;
}
protected WsResourceClient getMemberClient()
{
return _member;
}
public EndpointReference getMemberEPR()
{
WsResourceClient client = getMemberClient();
return client == null ? null : client.getEndpointReference();
}
public QName[] getPropertyNames()
{
return PROPERTIES;
}
protected WsResource getServiceGroup()
{
return _serviceGroup;
}
public EndpointReference getServiceGroupEPR()
{
WsResource sg = getServiceGroup();
return sg == null ? null : sg.getEndpointReference();
}
public void setMemberEPR(EndpointReference memberEPR)
{
if (memberEPR == null)
throw new NullPointerException(_MESSAGES.get("NullResource"));
_member = new WsResourceClient(memberEPR, getServiceGroupEPR(), getEnvironment());
}
public void setServiceGroup(WsResource serviceGroup)
{
if (serviceGroup == null)
throw new NullPointerException(_MESSAGES.get("NullServiceGroup"));
_serviceGroup = serviceGroup;
}
public void shutdown()
throws SoapFault
{
//
// tell the sg that owns us to remove us from its collection
//
ServiceGroup sg = (ServiceGroup)getServiceGroup().getCapability(WssgConstants.SERVICE_GROUP_URI);
sg.removeEntry(getWsResource());
super.shutdown();
}
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, WssgConstants.ENTRY_QNAME);
EndpointReference myEPR = getResource().getEndpointReference();
XmlUtils.setElement(root, WssgConstants.SG_ENTRY_EPR_QNAME, myEPR);
EndpointReference member = getMemberEPR();
XmlUtils.setElement(root, WssgConstants.MEMBER_SERVICE_EPR_QNAME, member);
try
{
XmlUtils.setElement(root, WssgConstants.CONTENT_QNAME, getContent());
}
catch (SoapFault error)
{
Object[] filler = { error.getMessage() };
getLog().fine(_MESSAGES.get("ContentCreationFailed", filler));
}
return root;
}
}