/*
 * 
 * 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.lease;

import com.sun.jini.proxy.MarshalledWrapper;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.rmi.MarshalledObject;
import net.jini.core.lease.Lease;
import net.jini.io.MarshalledInstance;
import net.jini.lease.LeaseRenewalSet;
import net.jini.lease.RenewalFailureEvent;

/**
 * Basic implementation of <code>RenewalFailureEvent</code> that
 * defers unmarshalling.
 * <p>
 * The <code>Lease</code> that could not be renewed and any
 * accompanying <code>Throwable</code> will not be deserialized until
 * the appropriate accessor is called.
 *
 * @author Sun Microsystems, Inc.
 * @see LeaseRenewalSet 
 */
public class BasicRenewalFailureEvent extends RenewalFailureEvent {
    private static final long serialVersionUID = 4122133697986606684L;

    /**
     * Exception, in marshalled form, returned by <code>getThrowable</code>
     * method.  May be <code>null</code>.
     *
     * @serial
     */
    private MarshalledInstance marshalledThrowable;

    /**
     * Lease, in marshalled form, returned by <code>getLease</code> method.
     *
     * @serial 
     */
    private MarshalledInstance marshalledLease;

    /**
     * Transient cache of lease returned by <code>getLease</code> method.  
     */
    private transient Lease lease = null;

    /**
     * Transient cache of exception returned by <code>getThrowable</code> 
     * method.
     */
    private transient Throwable throwable;

    /** Whether to verify codebase integrity. */
    private transient boolean verifyCodebaseIntegrity;

    /**
     * Simple constructor.  Note event id is fixed to
     * <code>LeaseRenewalSet.RENEWAL_FAILURE_EVENT_ID</code>.
     *
     * @param source the <code>LeaseRenewalSet</code> that generated the event
     * @param seqNum the sequence number of this event
     * @param handback the client handback
     * @param marshalledLease the lease which could not be renewed, in
     *        marshalled form
     * @param marshalledThrowable the first exception that was thrown in the
     *        last chain of renewal failures, in marshalled form.  May be
     *        <code>null</code> in which case <code>getThrowable</code> will
     *        return <code>null</code>.
     */
    public BasicRenewalFailureEvent(LeaseRenewalSet source, 
				    long seqNum,
				    MarshalledObject handback,
				    MarshalledInstance marshalledLease,
				    MarshalledInstance marshalledThrowable) 
    {
	super(source, seqNum, handback);
	this.marshalledThrowable = marshalledThrowable;
	this.marshalledLease = marshalledLease;
    }

    /**
     * Returns the lease that could not be renewed.  When the event is
     * deserialized, the lease is left in marshalled form.  It is only
     * unmarshalled when this call is made.  If a call to this method
     * fails, future calls will attempt to re-unmarshal the lease.
     * Once the lease is successfully unmarshalled it is cached in a
     * transient field so future calls will not result in
     * <code>IOException</code> or
     * <code>ClassNotFoundException</code>.
     *
     * @return the unmarshalled lease
     * @throws IOException if there are problems unmarshalling the lease,
     *	       usually because of some sort of class mismatch
     * @throws ClassNotFoundException if there are problems unmarshalling the
     *	       lease, usually because one of the classes associated with the
     *	       lease's implementation could not be loaded
     * @throws SecurityException if this object was unmarshalled from a stream
     *	       that required codebase integrity, and the integrity of the
     *	       lease's codebase could not be verified
     */
    public Lease getLease() throws IOException, ClassNotFoundException {
	if (lease == null) {
	    lease = (Lease) marshalledLease.get(verifyCodebaseIntegrity);
	}
	return lease;
    };

    /**
     * Returns the exception (if any) that was thrown by the last renewal
     * attempt.
     * <p>
     * When the event is deserialized, the exception is
     * left in marshalled form.  It is only unmarshalled when this call
     * is made.  If a call to this method fails, future calls will
     * attempt to re-unmarshal the exception.  Once the
     * exception is successfully unmarshalled it is
     * cached in a transient field so future calls will not result in
     * <code>IOException</code> or
     * <code>ClassNotFoundException</code>.
     *
     * @return the unmarshalled exception
     * @throws IOException if there are problems unmarshalling the exception,
     *	       usually because of some sort of class mismatch
     * @throws ClassNotFoundException if there are problems unmarshalling the
     *	       exception, usually because one of the classes associated with
     *	       the exception's implementation could not be loaded
     * @throws SecurityException if this object was unmarshalled from a stream
     *	       that required codebase integrity, and the integrity of the
     *	       exception's codebase could not be verified
     */
    public Throwable getThrowable() throws IOException, ClassNotFoundException {
	// Is there a throwable?
	if (marshalledThrowable == null)
	    return null;

	if (throwable == null) {
	    throwable =
		(Throwable) marshalledThrowable.get(verifyCodebaseIntegrity);
	}

	return throwable;
    }

    /* Set transient fields. */
    private void readObject(ObjectInputStream in)
	throws IOException, ClassNotFoundException
    {
	in.defaultReadObject();
	verifyCodebaseIntegrity = MarshalledWrapper.integrityEnforced(in);
    }
}
