// Start of user code Copyright
/*******************************************************************************
 * Copyright (c) 2011, 2012 IBM Corporation and others.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *  and the Eclipse Distribution License is available at
 *  http://www.eclipse.org/org/documents/edl-v10.php.
 *
 *  Contributors:
 *
 *     Sam Padgett         - initial API and implementation
 *     Michael Fiedler     - adapted for OSLC4J
 *     Jad El-khoury       - initial implementation of code generator (https://bugs.eclipse.org/bugs/show_bug.cgi?id=422448)
 *     Andrii Berezovskyi  - change URL configuration logic (Bug 509767)
 *
 * This file is generated by org.eclipse.lyo.oslc4j.codegenerator
 *******************************************************************************/
// End of user code

package se.ericsson.cf.scott.sandbox.twin.servlet;

import java.net.MalformedURLException;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration;
import org.eclipse.lyo.oslc4j.core.OSLC4JUtils;

import se.ericsson.cf.scott.sandbox.twin.TwinManager;

// Start of user code imports
import java.util.Map;
import java.util.Optional;

import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// End of user code

public class ServletListener implements ServletContextListener  {
    private static final String DEFAULT_BASE = "http://localhost:8080";
    private static final String PROPERTY_BASE = servletContextParameterName("baseurl");
    private static final Logger logger = Logger.getLogger(ServletListener.class.getName());

    //If you are using another servletName in your web.xml configuration file, modify this variable early in the method contextInitialized below
    private static String servletName = "JAX-RS Servlet";

    // Start of user code class_attributes
    // End of user code

    public ServletListener() {
        super();
    }

    @Override
    public void contextInitialized(final ServletContextEvent servletContextEvent)
    {
        // Start of user code contextInitialized_init
        servletName = "oslc-servlet";
        // End of user code

        String baseUrl = generateBasePath(servletContextEvent);
        String servletUrlPattern = "services/";
        try {
            servletUrlPattern = getServletUrlPattern(servletContextEvent);
        } catch (Exception e1) {
            logger.log(Level.SEVERE, "servletListner encountered problems identifying the servlet URL pattern.", e1);
        }
        try {
            logger.info("Setting public URI: " + baseUrl);
            OSLC4JUtils.setPublicURI(baseUrl);
            logger.info("Setting servlet path: " + servletUrlPattern);
            OSLC4JUtils.setServletPath(servletUrlPattern);
        } catch (MalformedURLException e) {
            logger.log(Level.SEVERE, "servletListner encountered MalformedURLException.", e);
        } catch (IllegalArgumentException e) {
            logger.log(Level.SEVERE, "servletListner encountered IllegalArgumentException.", e);
        }

        logger.log(Level.INFO, "servletListner contextInitialized.");

        // Establish connection to data backbone etc ...
        TwinManager.contextInitializeServletListener(servletContextEvent);

        // Start of user code contextInitialized_final
        // End of user code
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent)
    {
        // Start of user code contextDestroyed_init
        // End of user code

        // Shutdown connections to data backbone etc...
        TwinManager.contextDestroyServletListener(servletContextEvent);

        // Start of user code contextDestroyed_final
        // End of user code
    }

    // Start of user code class_methods
    @NotNull
    private static String generateBasePathContext(final ServletContextEvent servletContextEvent) {
        final ServletContext servletContext = servletContextEvent.getServletContext();
        String base = getInitParameterOrDefault(servletContext, PROPERTY_BASE, DEFAULT_BASE);
        return base + servletContext.getContextPath();
    }

    private static Optional<String> generateBasePathEnv() {
        final Map<String, String> env = System.getenv();
        if(env.containsKey(BASE_ENV_KEY)) {
            logger.info("Found {} env variable", BASE_ENV_KEY);
            return Optional.of(env.get(BASE_ENV_KEY));
        }
        return Optional.empty();
    }

    // End of user code

    private static String servletContextParameterName(String parameter) {
        return String.format("%s.%s", ServletListener.class.getPackage().getName(), parameter);
    }

    private static String generateBasePath(final ServletContextEvent servletContextEvent) {
        final ServletContext servletContext = servletContextEvent.getServletContext();
        String base = getInitParameterOrDefault(servletContext, PROPERTY_BASE, DEFAULT_BASE);
        return base + servletContext.getContextPath();
    }

    private static String getInitParameterOrDefault(ServletContext context, String propertyName, String defaultValue) {
        String base = context.getInitParameter(propertyName);
        if (base == null || base.trim().isEmpty()) {
            base = defaultValue;
        }
        return base;
    }

    private static String getServletUrlPattern(final ServletContextEvent servletContextEvent) throws Exception {
        final ServletContext servletContext = servletContextEvent.getServletContext();

        ServletRegistration servletRegistration = servletContext.getServletRegistration(servletName);
        if (servletRegistration == null) {
            throw new NoSuchElementException("no servlet with name \"" + servletName + "\" is found.");
        }
        java.util.Collection<java.lang.String> mappings = servletRegistration.getMappings();
        if (mappings.size() != 1) {
            throw new NoSuchElementException("unable to identify servlet mappings for servlet with name \"" + servletName + "\".");
        }
        String mapping = (String) mappings.toArray()[0];

        //url patterns in  most cases end with '\*'. But a url-pattern with just '\' may be found for exact matches.
        if (mapping.endsWith("*"))
            mapping = mapping.substring(0, mapping.length()-1);
        return mapping;
    }
}


@@ -1261,41 +1261,8 @@
 vel;
-%0Aimport java.util.logging.Logger;
 %0A%0Aim
@@ -2002,16 +2002,23 @@
 = Logger
+Factory
 .getLogg
@@ -2045,19 +2045,67 @@
 lass
-.getName())
+);%0A    public static final String BASE_ENV_KEY = %22LYO_BASE%22
 ;%0A%0A 
@@ -2906,34 +2906,22 @@
  logger.
-log(Level.SEVERE, 
+error(
 %22servlet
@@ -3316,34 +3316,22 @@
  logger.
-log(Level.SEVERE, 
+error(
 %22servlet
@@ -3449,26 +3449,14 @@
 ger.
-log(Level.SEVERE, 
+error(
 %22ser
@@ -3541,24 +3541,13 @@
 ger.
-log(Level.INFO, 
+info(
 %22ser
@@ -5228,233 +5228,143 @@
     
-final ServletContext servletContext = servletContextEvent.getServletContext();%0A        String base = getInitParameterOrDefault(servletContext, PROPERTY_BASE, DEFAULT_BASE);%0A        return base + servletContext.getContextPath(
+Optional%3CString%3E basePath = generateBasePathEnv();%0A        return basePath.orElseGet(() -%3E generateBasePathContext(servletContextEvent)
 );%0A 
@@ -6712,9 +6712,8 @@
     %7D%0A%7D%0A
-%0A

Revert unintended change to ServletListener