apache > ws.apache
Pubscribe
 

Modify the Resource Class

Introduction

In this step of the tutorial, the generated Resource class (FilesystemResource) is modified to implement the init method as well as several methods for the filesystem's custom operations. The Resource class is the stateful instance-representation of your Web service. The resource maintains the resource id and the ResourcePropertySet. The resource id is the unique identifier for an instance of your Web service. It allows you to have multiple resource instances, each with their own states, fronted by the same Web service. The stateful properties are represented by the ResourcePropertySet. The ResourcePropertySet is the Java representation of the Resource Properties document defined in the types section of your WSDL file.

In addition, this class is used to expose resource properties as notification topics and register the exposed topics.

Modify the Resource Class

Open WORK_DIR/generated/filesystem/src/java/org/apache/ws/resource/example/filesystem/FilesystemResource.java and replace the public void init() method with the following method. You will also need to copy the variable and custom operation methods below.

    private example.filesystem.backend.FileSystem m_filesystem;

    /**
     * Initializes this resource's state (properties, etc.).
     */
    public void init()
    {

        super.init();

        /*
         * This is where you should associate the backend instance with
         * the resource instance for a given id.
         */
        m_filesystem = new example.filesystem.backend.UnixFileSystem( m_id );

        /*
         * The resource property set which contains all of resource properties that were defined in the WSDL.
         */
        org.apache.ws.resource.properties.ResourcePropertySet resourcePropertySet = getResourcePropertySet();
        org.apache.ws.resource.properties.ResourceProperty resourceProperty = null;

        try
        {
            /*
             * Initialize each of our properties by calling resourceProperty.add(propElem) and/or resourceProperty.setCallback(callback)...
             */
            resourceProperty = resourcePropertySet.get( FilesystemPropertyQNames.DEVICESPECIALFILE );
            DeviceSpecialFileDocument deviceDocXBean = DeviceSpecialFileDocument.Factory.newInstance();
            deviceDocXBean.setDeviceSpecialFile( m_filesystem.getDeviceSpecialFile() );
            resourceProperty.add( deviceDocXBean );
            resourceProperty.getMetaData().setReadOnly( true );

            resourceProperty = resourcePropertySet.get( FilesystemPropertyQNames.TYPE );
            TypeDocument typeDocXBean = TypeDocument.Factory.newInstance();
            typeDocXBean.setType( m_filesystem.getType() );
            resourceProperty.add( typeDocXBean );
            resourceProperty.getMetaData().setReadOnly( true );
            
            BackupFrequencyDocument backupDocXBean = BackupFrequencyDocument.Factory.newInstance();
            backupDocXBean.setBackupFrequency( m_filesystem.getBackupFrequency() );
            resourceProperty = resourcePropertySet.get( FilesystemPropertyQNames.BACKUPFREQUENCY );
            resourceProperty.add( backupDocXBean );
            resourceProperty.setCallback( new example.filesystem.callback.BackupFrequencyCallback( m_filesystem ) );

            CommentDocument commentDocXBean = CommentDocument.Factory.newInstance();
            commentDocXBean.setComment( m_filesystem.getComment() );
            resourceProperty = resourcePropertySet.get( FilesystemPropertyQNames.COMMENT );
            resourceProperty.add( commentDocXBean );
            resourceProperty.setCallback( new example.filesystem.callback.CommentCallback( m_filesystem ) );

            FsckPassNumberDocument fsckDocXBean = FsckPassNumberDocument.Factory.newInstance();
            fsckDocXBean.setFsckPassNumber( m_filesystem.getFsckPassNumber() );
            resourceProperty = resourcePropertySet.get( FilesystemPropertyQNames.FSCKPASSNUMBER );
            resourceProperty.add( fsckDocXBean );
            resourceProperty.setCallback( new example.filesystem.callback.FsckPassNumberCallback( m_filesystem ) );

            MountPointDirectoryDocument mountPointDocXBean = MountPointDirectoryDocument.Factory.newInstance();
            mountPointDocXBean.setMountPointDirectory( m_filesystem.getMountPoint() );
            resourceProperty = resourcePropertySet.get( FilesystemPropertyQNames.MOUNTPOINTDIRECTORY );
            resourceProperty.add( mountPointDocXBean );
            resourceProperty.setCallback( new example.filesystem.callback.MountPointCallback( m_filesystem ) );

            OptionsDocument optionsDocXBean =
                    OptionsDocument.Factory.newInstance();
            org.apache.ws.resource.example.filesystem.OptionsDocument.Options options =
                    optionsDocXBean.addNewOptions();
            java.util.List backendOptions =
                    m_filesystem.getOptions();
            for ( int i = 0; i < backendOptions.size(); i++ )
            {
                options.addOption( (String) backendOptions.get( i ) );
            }

            resourceProperty = resourcePropertySet.get( FilesystemPropertyQNames.OPTIONS );
            resourceProperty.add( optionsDocXBean );
            resourceProperty.setCallback( new example.filesystem.callback.OptionsCallback( m_filesystem ) );
        }
        catch ( Exception e )
        {
            throw new javax.xml.rpc.JAXRPCException(
                    "There was a problem in initializing your resource properties.  Please check your init() method. Cause: " +
                    e.getLocalizedMessage() );
        }

        // Resource Property {http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceLifetime-1.2-draft-01.xsd}TerminationTime is implemented by the framework.
        // Resource Property {http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceLifetime-1.2-draft-01.xsd}CurrentTime is implemented by the framework.
        // Resource Property {http://docs.oasis-open.org/wsn/2004/06/wsn-WS-BaseNotification-1.2-draft-01.xsd}FixedTopicSet is implemented by the framework.
        // Resource Property {http://docs.oasis-open.org/wsn/2004/06/wsn-WS-BaseNotification-1.2-draft-01.xsd}Topic is implemented by the framework.
        // Resource Property {http://docs.oasis-open.org/wsn/2004/06/wsn-WS-BaseNotification-1.2-draft-01.xsd}TopicExpressionDialects is implemented by the framework.

    }

    public void mount() throws Exception
    {
        m_filesystem.mount();
    }

    public void unmount() throws Exception
    {
        m_filesystem.unmount();
    }

    public boolean isMounted()
    {
        return m_filesystem.isMounted();
    }

go to the previous step Back go to the next step Next