/*=============================================================================*
* 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.impl;
import javax.xml.namespace.QName;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.muse.core.Environment;
import org.apache.muse.core.SimpleResource;
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.soap.SoapFault;
import org.apache.muse.ws.resource.WsResource;
import org.apache.muse.ws.resource.metadata.OpenMetadataDescriptor;
import org.apache.muse.ws.resource.metadata.MetadataDescriptor;
import org.apache.muse.ws.resource.metadata.WsrmdConstants;
import org.apache.muse.ws.resource.metadata.impl.SimpleMetadataDescriptor;
import org.apache.muse.ws.resource.metadata.impl.WsrmdUtils;
import org.apache.muse.ws.resource.properties.ResourcePropertyCollection;
import org.apache.muse.ws.resource.properties.impl.SimpleResourcePropertyCollection;
import org.apache.muse.ws.resource.properties.impl.WsrpUtils;
import org.apache.muse.ws.resource.properties.schema.OpenPropertiesSchema;
import org.apache.muse.ws.resource.properties.schema.ResourcePropertiesSchema;
import org.apache.muse.ws.resource.properties.schema.impl.SimpleResourcePropertiesSchema;
import org.apache.muse.ws.wsdl.WsdlUtils;
/**
*
* SimpleResource is Muse's default implementation of a WS-RF-compliant
* resource ({@linkplain WsResource WsResource}). It parses the resource's
* WSDL to find the WSRP document schema and then creates the WSRP
* state model using Muse's default
* {@linkplain SimpleResourcePropertyCollection WSRP implementation}.
* The WSRP implementation class can be modified by overriding the
* createPropertyCollection() method in this class.
*
* @author Dan Jemiolo (danj)
*
*/
public class SimpleWsResource extends SimpleResource implements WsResource
{
//
// Used to look up all error messages
//
private static Messages _MESSAGES = MessagesFactory.get(SimpleResource.class);
private static final String _VALIDATE_WSRP_PARAM = "validate-wsrp-schema";
//
// WSRP state model
//
private ResourcePropertyCollection _properties = null;
/**
*
* @param wsdl
* The DOM document holding the resource's WSDL.
*
* @return The metadata descriptor referenced in the resource's WSDL, or
* an 'open' descriptor (no restrictions) if none is found.
*
*/
protected MetadataDescriptor createMetadataDescriptor(Document wsdl)
{
Element portTypeXML = WsdlUtils.getPortType(wsdl, getWsdlPortType());
//
// get the location of the RMD file...
//
String rmdName = XmlUtils.getAttribute(portTypeXML, WsrmdConstants.DESCRIPTOR_ATTR_QNAME);
String rmdPath = XmlUtils.getAttribute(portTypeXML, WsrmdConstants.DESCRIPTOR_LOCATION_ATTR_QNAME);
if (rmdName == null && rmdPath == null)
return OpenMetadataDescriptor.getInstance();
if ((rmdName == null || rmdPath == null) && (rmdName != rmdPath))
throw new RuntimeException(_MESSAGES.get("IncompleteMetadataAttributes"));
//
// load the doc and parse into a descriptor
//
Environment env = getEnvironment();
String path = env.createRelativePath(getWsdlPath(), rmdPath);
Document rmdDoc = env.getDocument(path);
Element descriptorXML = WsrmdUtils.getMetadataDescriptor(rmdDoc, rmdName);
if (descriptorXML == null)
{
Object[] filler = { rmdPath, rmdName };
throw new RuntimeException(_MESSAGES.get("DescriptorNotFound", filler));
}
return new SimpleMetadataDescriptor(descriptorXML);
}
/**
*
* @param wsdl
* The DOM document holding the resource's WSDL.
*
* @return The WSRP document schema from the WSDL's <em>types</em> section,
* or an 'open' schema (no restrictions) if none is found.
*
*/
protected ResourcePropertiesSchema createPropertiesSchema(Document wsdl)
{
//
// find the element that corresponds to the WS-RP name
//
QName wsrpName = WsrpUtils.getPropertiesName(wsdl, getWsdlPortType());
Element wsrpDoc = WsdlUtils.getElementDeclaration(wsdl, wsrpName);
if (wsrpDoc == null)
{
Object[] filler = { getContextPath(), getWsdlPath() };
getLog().warning(_MESSAGES.get("NoWSRPDocument", filler));
return OpenPropertiesSchema.getInstance();
}
return new SimpleResourcePropertiesSchema(wsrpName, wsrpDoc);
}
/**
*
* This method returns the concrete WSRP state model - this is not
* the implementation of the WSRP capabilities (which map SOAP
* requests to WSRP operations), but it does implement the actual
* WSRP operations and does the delegation of read/write requests
* to the capabilities defining the properties. It is available no
* matter how many of the WSRP capabilites are exposed to remote
* clients.
* <br><br>
* You can replace the default implementation by overriding
* this method to instantiate a different concrete class. You would
* then specify the name of your new sub-class using the
* <em>java-resource-class</em> element in muse.xml.
*
* @return An instance of SimpleResourcePropertyCollection.
*
*/
protected ResourcePropertyCollection createPropertyCollection()
{
return new SimpleResourcePropertyCollection();
}
public final ResourcePropertyCollection getPropertyCollection()
{
return _properties;
}
/**
*
* {@inheritDoc}
* <br><br>
* The SimpleWsResource implementation takes the following steps:
* <ol>
* <li>Create WSRP state model - createPropertyCollection()</li>
* <br>
* <li>Create WSRP document schema and apply it to the collection.</li>
* <br>
* <li>Create WSRP metadata and apply it to the collection.</li>
* <br>
* <li>Call super.initialize() to initialize capabilities. The
* WSRP collection is now available to the capabilities during
* their startup cycle.</li>
* <br>
* <li>Apply metadata by creating components needed to enforce it.</li>
* <br>
* <li>Validate WSRP document according to schema and metadata.</li>
* <br>
* </ol>
*
*/
public void initialize()
throws SoapFault
{
_properties = createPropertyCollection();
//
// get the WSDL, which has the WS-RP definition/schema
//
Document wsdl = WsdlUtils.createWSDL(getEnvironment(), getWsdlPath(), true);
ResourcePropertiesSchema schema = createPropertiesSchema(wsdl);
_properties.setSchema(schema);
MetadataDescriptor metadata = createMetadataDescriptor(wsdl);
_properties.setMetadata(metadata);
//
// use parent to initialize capabilities now that WSRP model is in place
//
super.initialize();
_properties.applyMetadata();
String validateParam = getInitializationParameter(_VALIDATE_WSRP_PARAM);
//
// validate WSRP schema - can be shut off with an init param flag
//
if (validateParam == null || validateParam.equalsIgnoreCase("true"))
_properties.validateSchema();
_properties.validateMetadata();
}
}