Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.arjuna.wst11.stub;
  
  import com.arjuna.wst.*;
 
 
 public class ParticipantStub implements ParticipantPersistableParticipant
 {
     private static final QName QNAME_TWO_PC_PARTICIPANT = new QName("twoPCParticipant") ;
     private CoordinatorEngine coordinator ;
 
     public ParticipantStub(final String idfinal boolean durablefinal W3CEndpointReference twoPCParticipant)
         throws Exception
     {
         // id will be supplied as null during recovery in which case we can delay creation
         // of the coordinator until restore_state is called
         
         if (id != null) {
              = new CoordinatorEngine(iddurabletwoPCParticipant) ;
         }
     }
 
     public Vote prepare()
         throws WrongStateExceptionSystemException
     {
         /*
          * null - aborted or read only
          * Active - illegal state
          * Preparing - no answer
          * Prepared - illegal state
          * PreparedSuccess - prepared
          * Committing - illegal state
          * Aborting - aborting
          */
         final State state = .prepare() ;
         if (state == .)
         {
             return new Prepared() ;
         }
         else if (state == .)
         {
             return new Aborted() ;
         }
         else if (state == null)
         {
             if (.isReadOnly())
             {
                 return new ReadOnly() ;
             }
             else
             {
                 return new Aborted() ;
             }
         }
         else if (state == .)
         {
             // typically means no response from the remote end.
             // throw a comm exception to distinguish this case from the
             // one where the remote end itself threw a SystemException.
 
             throw new SystemCommunicationException() ;
         }
         else
         {
             throw new WrongStateException() ;
         }
     }
 
     public void commit()
         throws WrongStateExceptionSystemException
     {
         /*
          * null - committed
          * Active - illegal state
          * Preparing - illegal state
          * Prepared - illegal state
          * PreparedSuccess - illegal state
          * Committing - no response
          * Aborting - illegal state
          */
         final State state = .commit() ;
         if (state != null)
         {
             if (state == .)
            {
                // typically means no response from the remote end.
                // throw a comm exception to distinguish this case from the
                // one where the remote end itself threw a SystemException.
                throw new SystemCommunicationException();
            }
            else
            {
                throw new WrongStateException() ;
            }
        }
    }
    public void rollback()
        throws WrongStateExceptionSystemException
    {
        /*
         * null - aborted
         * Active - illegal state
         * Preparing - illegal state
         * Prepared - illegal state
         * PreparedSuccess - illegal state
         * Committing - illegal state
         * Aborting - no response
         */
        final State state = .rollback() ;
        if (state != null)
        {
            if (state == .)
            {
                throw new SystemCommunicationException() ;
            }
            else
            {
                throw new WrongStateException() ;
            }
        }
    }
    public void unknown()
        throws SystemException
    {
        error() ;
    }
    public void error()
        throws SystemException
    {
        try
        {
            rollback() ;
        }
        catch (final WrongStateException wse) {} // ignore
    }
    public boolean saveState(final OutputObjectState oos)
    {
        try
        {
            oos.packString(.getId()) ;
            oos.packBoolean(.isDurable()) ;
            State state = .getState();
            // participants in the heuristic list may get saved in any state
            if (state == .) {
                oos.packInt(0);
            } else if (state == .) {
                oos.packInt(1);
            } else if (state == . ||
                        state == .) {
                oos.packInt(2);
            } else if (state == .) {
                oos.packInt(3);
            } else { // COMMITTING or none
                oos.packInt(4);
            }
            // n.b. just use toString() for the endpoint -- it uses the writeTo() method which calls a suitable marshaller
            final StringWriter sw = new StringWriter() ;
            final XMLStreamWriter writer = SoapUtils.getXMLStreamWriter(sw) ;
            StreamHelper.writeStartElement(writer) ;
            String eprefText = .getParticipant().toString();
            writer.writeCData(eprefText);
            StreamHelper.writeEndElement(writernullnull) ;
            writer.close() ;
            sw.close();
            String tmp = writer.toString();
            String swString = sw.toString();
            oos.packString(swString) ;
            return true ;
        }
        catch (final Throwable th)
        {
            return false ;
        }
    }
    public boolean restoreState(final InputObjectState ios)
    {
        State state;
        try
        {
            final String id = ios.unpackString() ;
            final boolean durable = ios.unpackBoolean() ;
            final int stateTag = ios.unpackInt();
            switch (stateTag)
            {
                case 0:
                    state = .;
                    break;
                case 1:
                    state = .;
                    break;
                case 2:
                    state = .;
                    break;
                case 3:
                    state = .;
                    break;
                default:
                    state = .;
                    break;
            }
            final String eprValue = ios.unpackString() ;
            // this should successfully reverse the save process
            final XMLStreamReader reader = SoapUtils.getXMLStreamReader(new StringReader(eprValue)) ;
            StreamHelper.checkNextStartTag(reader) ;
            String eprefText = reader.getElementText();
            StreamSource source = new StreamSource(new StringReader(eprefText));
            final W3CEndpointReference endpointReference = new W3CEndpointReference(source);
            // if we already have a coordinator from a previous recovery scan or because
            // we had a heuristic outcoe then reuse it with luck it will have been committed
            // or aborted between the last scan and this one
            // note that whatever happens it will not have been removed from the table
            // because it is marked as recovered
             = (CoordinatorEngine)CoordinatorProcessorImpl.getProcessor().getCoordinator(id);
            if ( == null) {
                // no entry found so recreate one with the saved state
                 = new CoordinatorEngine(iddurableendpointReferencetruestate) ;
            }
            return true ;
        }
        catch (final Throwable th)
        {
            return false ;
        }
    }
New to GrepCode? Check out our FAQ X