/**
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 - JASS: Java Advanced tranSaction Support
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 - This module was originally developed by 
 -
 -    LSD (Distributed Systems Lab, http://lsd.ls.fi.upm.es/lsd/lsd.htm)
 -    at Universidad Politecnica de Madrid (UPM) as an ObjectWeb Consortium
 -    (http://www.objectweb.org) project.
 -
 -    This project has been partially funded by the European Commission under
 -    the IST programme of V FP grant IST-2001-37126 and by the Spanish
 -    Ministry of Science & Technology (MCyT) grants TIC2002-10376-E and
 -    TIC2001-1586-C03-02
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -  The original code and portions created by LSD are 
 -  Copyright (c) 2004 LSD (UPM)  
 -  All rights reserved.
 -  
 - Redistribution and use in source and binary forms, with or without 
 - modification, are permitted provided that the following conditions are met:
 -
 - -Redistributions of source code must retain the above copyright notice, this
 - list of conditions and the following disclaimer. 
 -
 - -Redistributions in binary form must reproduce the above copyright notice, 
 - this list of conditions and the following disclaimer in the documentation 
 - and/or other materials provided with the distribution. 
 -
 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 - POSSIBILITY OF SUCH DAMAGE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -
 -  Author: Francisco Perez Sorrosal (frperezs)
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/

package org.omg.CosActivity;

import javax.activity.ActivityCoordinator;
import javax.activity.CompletionStatus;
import javax.activity.CoordinationInformation;
import javax.activity.Outcome;
import javax.activity.Signal;
import javax.activity.SignalSetActiveException;
import javax.activity.SignalSetInactiveException;
import javax.activity.coordination.SignalSet;

public class Synchronization implements SignalSet {

	// Constants --------------------------------------------------------------
	public static final String SYNCHRONIZATION_SS_NAME =
		"org.omg.CosActivity.Synchronization";

	// Attributes -------------------------------------------------------------

	private String signalSetName;

	private Signal signal;

	private ActivityCoordinator ac;

	private int completionStatus;

	private int status;

	private Outcome outcome;

	private boolean started;

	// Constructors -----------------------------------------------------------

	public Synchronization(String signalSetName) {
		this.signalSetName = signalSetName;
		this.outcome = null;
		this.started = false;
	}

	// Public -----------------------------------------------------------------

	// Signal implementation --------------------------------------------------

	public java.lang.String getSignalSetName() {
		return this.signalSetName;
	}

	public Signal getSignal() {

		Signal signal = null;
		started = true;

		if (this.completionStatus == CompletionStatus.CompletionStatusSuccess) {

			signal =
				new Signal(
					"preCompletion",
					this.signalSetName,
					(java.io.Serializable) null);

		}
		return signal;
	}

	public CoordinationInformation setResponse(Outcome response)
		throws SignalSetInactiveException {

		if (!started)
			throw new SignalSetInactiveException();

		CoordinationInformation info = null;
		if (response.getName().equals("preCompletitionSuccess")) {
			info = new CoordinationInformation(false, false);
		}

		if (response.getName().equals("preCompletitionFailed")) {
			info = new CoordinationInformation(false, false);
		}
		return info;
	}

	public Outcome getOutcome() throws SignalSetActiveException {
		return this.outcome;
	}

	public void setCompletionStatus(int completionStatus, int status) {
		this.completionStatus = completionStatus;
		this.status = status;
	}

	public int getCompletionStatus() throws SignalSetActiveException {
		return this.completionStatus;
	}

	public void setActivityCoordinator(ActivityCoordinator coord)
		throws SignalSetActiveException {
		this.ac = coord;
	}

	public void destroy() {

	}
}
