Package org.apache.muse.ws.resource.sg.impl

Source Code of org.apache.muse.ws.resource.sg.impl.ServiceGroupFilePersistence

/*=============================================================================*
*  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 java.io.File;
import java.io.IOException;

import org.w3c.dom.Element;

import org.apache.muse.core.AbstractFilePersistence;
import org.apache.muse.core.Resource;
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.resource.WsResource;
import org.apache.muse.ws.resource.sg.Entry;
import org.apache.muse.ws.resource.sg.ServiceGroup;
import org.apache.muse.ws.resource.sg.ServiceGroupPersistence;
import org.apache.muse.ws.resource.sg.WssgConstants;

/**
*
* ServiceGroupFilePersistence is an implementation of file-based persistence
* of service group entry data. It stores data about current service group
* entries on disk and then re-loads them after an application restarts. This
* allows the service group to continue functioning as it was before the
* restart without the application having to somehow re-discover and re-add
* all of the members.
*
* @author Dan Jemiolo (danj)
*
*/

public class ServiceGroupFilePersistence
    extends AbstractFilePersistence implements ServiceGroupPersistence
{
    private WsResource _serviceGroup = null;
   
    protected void createResourceFile(EndpointReference epr, Resource resource, File resourceFile)
        throws SoapFault
    {
        //
        // write the service group entry XML infoset to file
        //
        Entry entry = (Entry)resource.getCapability(WssgConstants.ENTRY_URI);
        Element entryXML = entry.toXML();
       
        try
        {
            XmlUtils.toFile(entryXML, resourceFile);
        }
       
        catch (IOException error)
        {
            throw new SoapFault(error);
        }
    }
   
    protected String getFilePrefix()
    {
        return "service-group-entry-";
    }

    public WsResource getServiceGroup()
    {
        return _serviceGroup;
    }

    protected Resource reloadResource(String contextPath, Element resourceXML)
        throws SoapFault
    {
        //
        // get the EPR of the entry resource so we can look it up
        //
        Element entryEprXML = XmlUtils.getElement(resourceXML, WssgConstants.SG_ENTRY_EPR_QNAME);
        EndpointReference entryEPR = new EndpointReference(entryEprXML);
       
        //
        // get the member EPR so we can set the value on the entry resource
        //
        Element memberEprXML = XmlUtils.getElement(resourceXML, WssgConstants.MEMBER_SERVICE_EPR_QNAME);
        EndpointReference memberEPR = new EndpointReference(memberEprXML);
       
        //
        // find the entry resource, use the Entry capability to set the
        // member EPR and service group property values
        //
        WsResource entryResource = (WsResource)getResourceManager().getResource(entryEPR);       
        Entry entryCap = (Entry)entryResource.getCapability(WssgConstants.ENTRY_URI);
       
        WsResource sgResource = getServiceGroup();
       
        entryCap.setServiceGroup(sgResource);
        entryCap.setMemberEPR(memberEPR);
       
        ServiceGroup sgCap = (ServiceGroup)sgResource.getCapability(WssgConstants.SERVICE_GROUP_URI);
        sgCap.addEntry(memberEPR, entryResource);
       
        return entryResource;
    }

    public void resourceAdded(EndpointReference epr, Resource resource)
        throws SoapFault
    {
        createResourceFile(epr, resource);
    }
   
    public void resourceRemoved(EndpointReference epr)
        throws SoapFault
    {
        destroyResourceFile(epr);
    }
   
    public void setServiceGroup(WsResource serviceGroup)
    {
        _serviceGroup = serviceGroup;
    }
}
TOP

Related Classes of org.apache.muse.ws.resource.sg.impl.ServiceGroupFilePersistence

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.