/*
 * Copyright (c) 2008, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

/*
 * $Id$
 */

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

import java.util.logging.Level;
import java.util.logging.Logger;
import jakarta.servlet.ServletContextAttributeEvent;
import jakarta.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");
	    }
	}
    }
}
