Package org.apache.muse.core.routing

Source Code of org.apache.muse.core.routing.RouterFilePersistence

/*=============================================================================*
*  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.core.routing;

import java.io.File;
import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.apache.muse.core.AbstractFilePersistence;
import org.apache.muse.core.Resource;
import org.apache.muse.core.ResourceManager;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.addressing.WsaConstants;
import org.apache.muse.ws.addressing.soap.SoapFault;

/**
*
* RouterFilePersistence is a component that saves router entries (EPRs and
* their resource types) to XML files on disk.
*
* @author Dan Jemiolo (danj)
*
*/

public class RouterFilePersistence
    extends AbstractFilePersistence implements RouterPersistence
{
    /**
     *
     * {@inheritDoc}
     * <br><br>
     * This implementation serializes the EPR to XML, then writes the
     * wsa:ReferenceParameters element to the file. If the EPR had no
     * reference parameters, an empty element is written.
     *
     */
    protected void createResourceFile(EndpointReference epr, Resource resource, File resourceFile)
        throws SoapFault
    {
        try
        {
            //
            // extract the wsa:ReferenceParameters XML out of the EPR and
            // save that to a file - if the EPR had no params, just make
            // an empty element (because we need a valid XML doc)
            //
            Element eprXML = epr.toXML();
            Element params = XmlUtils.getElement(eprXML, WsaConstants.PARAMETERS_QNAME);
           
            if (params == null)
                params = XmlUtils.createElement(WsaConstants.PARAMETERS_QNAME);
           
            XmlUtils.toFile(params, resourceFile);
        }
       
        catch (IOException error)
        {
            throw new SoapFault(error);
        }
    }
   
    /**
     *
     * @return The string 'resource-instance-'.
     *
     */
    protected String getFilePrefix()
    {
        return "resource-instance-";
    }
   
    /**
     *
     * {@inheritDoc}
     * <br><br>
     * This implementation treats the XML fragment as a wsa:ReferenceParameters
     * element. It creates an EPR for the given resource type (context path)
     * and then adds the reference parameters to it. Finally, it creates an
     * instance of the resource type and (re-)sets the EPR that it has constructed.
     *
     */
    protected Resource reloadResource(String contextPath, Element resourceXML)
        throws SoapFault
    {
        ResourceManager manager = getResourceManager();
       
        //
        // the XML from the file is the reference parameters of an EPR, so
        // we're going to construct the rest of the EPR XML around it and
        // then turn it into an EPR object
        //
       
        //
        // wrap parameter XML in a wsa:EndpointReference element
        //       
        Document doc = resourceXML.getOwnerDocument();
        Element eprXML = XmlUtils.createElement(doc, WsaConstants.EPR_QNAME);
        eprXML.appendChild(resourceXML);
       
        //
        // get the right address URI for the wsa:Address element - this is
        // the default URI (has the proper host/port/app) with the context
        // path for the resource type at the end
        //
        String address = manager.getEnvironment().getDefaultURI();
        int lastSlash = address.lastIndexOf('/');
        address = address.substring(0, lastSlash + 1) + contextPath;
       
        XmlUtils.setElement(eprXML, WsaConstants.ADDRESS_QNAME, address);
       
        //
        // create EPR object from XML and set it on the newly-created resource
        //
        EndpointReference epr = new EndpointReference(eprXML);
       
        Resource resource = manager.createResource(contextPath);
        resource.setEndpointReference(epr);
       
        //
        // continue initialization/registration
        //
        resource.initialize();
        manager.addResource(epr, resource);
       
        return resource;
    }
   
    /**
     *
     * This implementation checks to see if the resource type is one that
     * is being persisted, and if so, creates a file for the instance.
     *
     */
    public void resourceAdded(EndpointReference epr, Resource resource)
        throws SoapFault
    {
        String contextPath = resource.getContextPath();
       
        if (getResourceManager().isUsingPersistence(contextPath))
            createResourceFile(epr, resource);
    }
   
    /**
     *
     * This implementation checks to see if the resource type is one that
     * is being persisted, and if so, tries to delete the resource's file.
     *
     */
    public void resourceRemoved(EndpointReference epr)
        throws SoapFault
    {
        String contextPath = getContextPath(epr);
       
        if (getResourceManager().isUsingPersistence(contextPath))
            destroyResourceFile(epr);
    }
}
TOP

Related Classes of org.apache.muse.core.routing.RouterFilePersistence

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.