/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.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://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */
package com.sun.jini.mahalo;

import com.sun.jini.mahalo.log.*;

import net.jini.core.transaction.server.TransactionManager;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * An <code>AbortRecord</code> represents the logged state of
 * a <code>Transaction</code> which has changed to the ABORTED
 * state.
 *
 * @author Sun Microsystems, Inc.
 *
 */
class AbortRecord implements TxnLogRecord  {
    /**
     * @serial
     */
    private ParticipantHandle[] parts;

    static final long serialVersionUID = -8121722031382234695L;

    static final Logger logger = TxnManagerImpl.participantLogger;

    /**
     * Constructs an <code>AbortRecord</code> which  represents a
     * <code>Transaction</code> which has moved to the ABORTED state.
     *
     * @param parts The array of participants joined in the transaction
     *
     * @see net.jini.core.transaction.Transaction
     * @see net.jini.core.transaction.server.TransactionParticipant
     * @see net.jini.core.transaction.server.TransactionConstants
     */
    AbortRecord(ParticipantHandle[] parts) {
        if (parts == null)
            throw new IllegalArgumentException("AbortRecord: must specify " +
                                                "a non-null parts array");
	this.parts = parts;
    }

    /**
     * Recovers the state encapsulated the <code>AbortRecord</code> to
     * the caller.
     *
     * @param tmt  The <code>TxnManagerTransaction</code> to which
     *             state is recovered.
     *
     * @see com.sun.jini.mahalo.TxnManagerTransaction
     */
    public void recover(TxnManagerTransaction tmt)
	throws CannotRecoverException
    {
        try {
            for (int i = 0; i< parts.length; i++) {
                tmt.add(parts[i]);
            }
	    tmt.modifyTxnState(ABORTED);
        } catch (InternalManagerException ime) {
            throw new CannotRecoverException("AbortRecord: recover: " +
                                                        ime.getMessage());
        }

	if (logger.isLoggable(Level.FINEST)) {
            logger.log(Level.FINEST, "AbortJob:recover recovered");
        }    
    }

    /**
     * Retrieves the set of <code>TransactionParticipant</code>s associated
     * with the recovered <code>Transaction</code>.
     *
     */
    ParticipantHandle[] getParts() {
        return parts;
    }
}
