/*
 * Copyright (c) 2008, Oracle and its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * $Id$
 */

package @package@;
//package com.sun.ts.tests.common.vehicle.ejbliteservletcal;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

public class EJBLiteServletContextAttributeListener extends Client
	implements ServletContextAttributeListener {
    private static final Logger logger = Logger.getLogger(EJBLiteServletContextAttributeListener.class.getName());

    public void attributeAdded(ServletContextAttributeEvent event) {
	processEvent(event);
    }

    public void attributeRemoved(ServletContextAttributeEvent event) {
    }

    public void attributeReplaced(ServletContextAttributeEvent event) {
	processEvent(event);
    }
    
    private void processEvent(ServletContextAttributeEvent event) {
	String attrName = event.getName();
	String attrVal = null;
	if("testName".equals(attrName)) {
            attrVal = (String) event.getValue();
	    if(logger.isLoggable(Level.FINE)) {
		logger.fine("Beginning test " + attrVal + "_from_ejbliteservletcla");
	    }
	    setTestName(attrVal);
	    setModuleName(event.getServletContext().getContextPath());
	    setInjectionSupported(true);
	    String sta = getStatus(); //to trigger test run
	    String reason = getReason();
	    event.getServletContext().setAttribute("statusAndReason", sta + " " + reason);
	} else if("testEnd".equals(attrName)) {
            String testName = (String) event.getServletContext().getAttribute("testName");
	    cleanup();
	    event.getServletContext().removeAttribute("testName");
	    event.getServletContext().removeAttribute("testEnd");
	    event.getServletContext().removeAttribute("statusAndReason");
	    if(logger.isLoggable(Level.FINE)) {
		logger.fine("Finished cleanup after " + testName + "_from_ejbliteservletcla");
	    }
	}
    }
}