Package org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl

Source Code of org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl.PrinterPortResource

/*=============================================================================*
*  Copyright 2004 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.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl;

import commonj.timers.Timer;
import commonj.timers.TimerListener;
import commonj.timers.TimerManager;
import org.apache.ws.resource.properties.ResourceProperty;
import org.apache.ws.resource.properties.impl.XmlBeansResourceProperty;
import org.apache.ws.resource.properties.impl.XmlBeansResourcePropertySet;
import org.apache.ws.resource.lifetime.ResourceTerminationListener;
import org.apache.ws.util.timer.TimerManagerImpl;
import org.apache.xmlbeans.XmlInteger;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.DocumentFormatSupportedDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.JobPropertiesDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.JobStateType;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.MimeMediaTypeType;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.MimeMediaTypes;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.Operations;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.OperationsSupportedDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.PrinterIsAcceptingJobsDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.PrinterNameDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.PrinterReferenceDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.PrinterStateDocument;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.PrinterStateType;
import org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03.QueuedJobCountDocument;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
* A PrinterPort WS-Resource.
* <p/>
* NOTE: This class is generated but IS meant to be modified.
*/
public class PrinterPortResource
        extends AbstractPrinterPortResource implements TimerListener
{

    private String m_name;
    private EndpointReferenceType m_epr;

    private List m_printQueue = new ArrayList();
    private Timer m_timer;
    private Random m_random = new Random(new Date().getTime());;

    /**
     * Creates a new {@link PrinterPortResource} object.
     *
     * @param name           DOCUMENT_ME
     * @param eprForResource DOCUMENT_ME
     */
    public PrinterPortResource(String name,
                               EndpointReferenceType eprForResource)
    {
        m_name = name;
        m_epr = eprForResource;
    }

    /**
     * DOCUMENT_ME
     *
     * @return DOCUMENT_ME
     */
    public EndpointReferenceType getEpr()
    {
        return m_epr;
    }

    /**
     * Initializes this resource's state (properties, etc.).
     */
    public void init()
    {
        super.init();
        ResourceProperty resourceProperty = m_propSet.get(PrinterPortPropertyQNames.PRINTER_REFERENCE);
        PrinterReferenceDocument printerRefDoc = PrinterReferenceDocument.Factory.newInstance();
        printerRefDoc.setPrinterReference(getEpr());
        resourceProperty.add(printerRefDoc);

        resourceProperty = m_propSet.get(PrinterPortPropertyQNames.PRINTER_STATE);
        PrinterStateDocument printerState = PrinterStateDocument.Factory.newInstance();
        printerState.setPrinterState(PrinterStateType.IDLE);
        resourceProperty.add(printerState);

        resourceProperty = m_propSet.get(PrinterPortPropertyQNames.PRINTER_NAME);
        PrinterNameDocument printName = PrinterNameDocument.Factory.newInstance();
        printName.setPrinterName(m_name);
        resourceProperty.add(printName);

        resourceProperty = m_propSet.get(PrinterPortPropertyQNames.PRINTER_IS_ACCEPTING_JOBS);
        PrinterIsAcceptingJobsDocument isAccepting = PrinterIsAcceptingJobsDocument.Factory.newInstance();
        isAccepting.setPrinterIsAcceptingJobs(false);
        resourceProperty.add(isAccepting);

        resourceProperty = m_propSet.get(PrinterPortPropertyQNames.QUEUED_JOB_COUNT);
        QueuedJobCountDocument queuedJobCountDocument = QueuedJobCountDocument.Factory.newInstance();
        queuedJobCountDocument.setQueuedJobCount(BigInteger.valueOf(0));
        resourceProperty.add(queuedJobCountDocument);

        resourceProperty = m_propSet.get(PrinterPortPropertyQNames.OPERATIONS_SUPPORTED);
        OperationsSupportedDocument operationsSupportedDocument =
                OperationsSupportedDocument.Factory.newInstance();
        List operationsSupported = new ArrayList();
        operationsSupported.add(Operations.PRINT_JOB); //refer to doc...
        operationsSupported.add(Operations.CREATE_JOB); //refer to doc...
        operationsSupported.add(Operations.SEND_DOCUMENT); //refer to doc...
        operationsSupportedDocument.setOperationsSupported(operationsSupported);
        resourceProperty.add(operationsSupportedDocument);

        resourceProperty = m_propSet.get(PrinterPortPropertyQNames.DOCUMENT_FORMAT_SUPPORTED);
        DocumentFormatSupportedDocument documentFormatSupportedDocument =
                DocumentFormatSupportedDocument.Factory.newInstance();
        MimeMediaTypes mimeMediaTypes =
                documentFormatSupportedDocument.addNewDocumentFormatSupported();
        mimeMediaTypes.addMimeMediaType(MimeMediaTypeType.TEXT_PLAIN);
        resourceProperty.add(documentFormatSupportedDocument);
    }

    /**
     * Adds a listener to be invoked when the resource has been terminated.
     *
     * @param listener
     */
    public void addTerminationListener(ResourceTerminationListener listener)
    {

    }

    public void addJobToQueue(JobPortResource job)
    {
        //if timer doesn't exist, create
        synchronized (this)
        {
            if (m_timer == null)
            {
                resetTimer();
                setPrinterStateProcessing();
            }
        }
        //LOG.debug( MSG.getMessage( Keys.QUEUING_JOB, job.m_id ) );
        m_printQueue.add(job);
    }

    private void setPrinterStateProcessing()
    {
        setPrinterState(PrinterStateType.PROCESSING);
    }

    private void setPrinterState(PrinterStateType.Enum printerState)
    {
        XmlBeansResourceProperty resourceProperty = (XmlBeansResourceProperty) getResourcePropertySet().get(PrinterPortPropertyQNames.PRINTER_STATE);
        PrinterStateType state = (PrinterStateType) resourceProperty.get(0);
        state.set(printerState);
    }

    public void resetTimer()
    {
        //scale and translate the random to get a number between 1 and 3 mins
        double doubleLength = m_random.nextDouble() * 120000.0 + 60000.0;
        //long doubleLength = 20000; //for debug only...
        long lengthOfTime = (long) doubleLength;
        TimerManager tm = new TimerManagerImpl();
        m_timer = tm.schedule(this, lengthOfTime);
    }

    public void timerExpired(Timer timer)
    {

        //make sure we have something in queue, remove the 0th print job
        if (m_printQueue.size() > 0)
        {
            if (!isPrinterStopped())
            {
                JobPortResource job = (JobPortResource) m_printQueue.remove(0);

                if (!isJobCancelledOrAborted(job))
                {
                    setJobState(job, JobStateType.PROCESSING);
                    //set termination time to now to make it be removed.
                    job.setTerminationTime(Calendar.getInstance());

                    XmlBeansResourcePropertySet jobPropSet = (XmlBeansResourcePropertySet) job.getResourcePropertySet();
                    ResourceProperty resourceProperty = jobPropSet.get(JobPortPropertyQNames.JOB_ID);
                    XmlInteger jobint = (XmlInteger) resourceProperty.get(0);
                    BigInteger bigIntegerValue = jobint.getBigIntegerValue();                   

                    decrementQueueCount();
                    setJobState(job, JobStateType.COMPLETED);
                }
            }
        }
        else
        {
            setPrinterStateIdleProcessing();
        }
        resetTimer();//restart timer
    }

    private boolean isJobCancelledOrAborted(JobPortResource job)
    {
        XmlBeansResourceProperty resourceProperty = (XmlBeansResourceProperty) job.getResourcePropertySet().get(JobPortPropertyQNames.JOB_STATE);
        JobStateType o = (JobStateType) resourceProperty.get(0);
        org.apache.xmlbeans.StringEnumAbstractBase stateVal = o.enumValue();
        return stateVal == JobStateType.ABORTED || stateVal == JobStateType.CANCELED || stateVal == JobStateType.COMPLETED;
    }

    private void setJobState(JobPortResource job, JobStateType.Enum state)
    {
        XmlBeansResourceProperty resourceProperty = (XmlBeansResourceProperty) job.getResourcePropertySet().get(JobPortPropertyQNames.JOB_STATE);
        JobStateType o = null;
        if (resourceProperty != null)
        {
         o = (JobStateType) resourceProperty.get(0);
         o.set(state);
        }

        resourceProperty = (XmlBeansResourceProperty) getResourcePropertySet().get(JobPortPropertyQNames.JOB_STATE);
        if (resourceProperty != null)
        {
            o = (JobStateType) resourceProperty.get(0);
            o.set(state);
        }
    }

    private void setPrinterStateIdleProcessing()
    {
        setPrinterState(PrinterStateType.IDLE);
    }

    private boolean isPrinterStopped()
    {
        XmlBeansResourceProperty resourceProperty = (XmlBeansResourceProperty) getResourcePropertySet().get(PrinterPortPropertyQNames.PRINTER_STATE);
        PrinterStateType state = (PrinterStateType) resourceProperty.get(0);
        return state.enumValue() == PrinterStateType.STOPPED;
    }

    private void decrementQueueCount()
    {
        XmlBeansResourceProperty resourceProperty = (XmlBeansResourceProperty) getResourcePropertySet().get(PrinterPortPropertyQNames.QUEUED_JOB_COUNT);
        XmlInteger count = (XmlInteger) resourceProperty.get(0);
        int intCount = count.getBigIntegerValue().intValue();
        --intCount;
        count.setBigIntegerValue(BigInteger.valueOf(intCount));
    }
}
TOP

Related Classes of org.oasisOpen.docs.wsrf.x2005.x01.wsrfInterop20Draft03Wsdl.PrinterPortResource

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.