/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 *   SPDX-License-Identifier: Apache-2.0
 */

package org.apache.jena.fuseki.mod.shiro;

import java.util.Arrays;
import java.util.List;

import org.apache.jena.atlas.logging.FmtLog;
import org.apache.shiro.web.env.EnvironmentLoaderListener;
import org.apache.shiro.web.env.ResourceBasedWebEnvironment;
import org.apache.shiro.web.env.WebEnvironment;

/**
 * A Shiro {@link EnvironmentLoaderListener} that supports multiple possible
 * locations for a {@code shiro.ini} file. It will return the first found in a list
 * of possible file names.
 */
class FusekiShiroEnvironmentLoaderListener extends EnvironmentLoaderListener{

    private List<String> locations;

    /*package*/ FusekiShiroEnvironmentLoaderListener(List<String> locations) {
        this.locations = locations;
    }

    // Public lifecycle
//    @Override
//    public WebEnvironment createEnvironment(ServletContext sc) {
//        return super.createEnvironment(sc);
//    }
//    @Override
//    public WebEnvironment initEnvironment(ServletContext servletContext) throws IllegalStateException {
//        return super.initEnvironment(servletContext);
//    }
//    @Override
//    public void destroyEnvironment(ServletContext sc) {
//        super.destroyEnvironment(sc);
//    }

    /**
     * When given multiple locations for the shiro.ini file, and
     * if a {@link ResourceBasedWebEnvironment}, check the list of configuration
     * locations, testing whether the name identified an existing resource.
     * For the first resource name found to exist, reset the {@link ResourceBasedWebEnvironment}
     * to name that resource alone so the normal Shiro initialization executes.
     */
    @Override
    protected void customizeEnvironment(WebEnvironment environment) {
        if ( locations == null ) {
            super.customizeEnvironment(environment);
            return;
        }

        // Look for shiro.ini
        if ( environment instanceof ResourceBasedWebEnvironment ) {
            ResourceBasedWebEnvironment env = (ResourceBasedWebEnvironment)environment;
            String[] configLocations = env.getConfigLocations();
            if ( configLocations != null && configLocations.length > 0 ) {
                // Set some other way.
                FmtLog.info(FusekiShiro.shiroLog, "Shiro file resource %s", Arrays.asList(configLocations));
                return;
            }
            String loc = FusekiShiro.huntForShiroIni(locations);
            if  ( loc == null ) {
                FmtLog.info(FusekiShiro.shiroLog, "No Shiro file found (tried: %s)", locations);
                return;
            }
            // A location found by FMod_Shiro is logged in FMod_Shiro
            //shiroConfigLog.info("Shiro configuration: "+loc);
            String[] configLocationsHere = new String[] {loc};
            env.setConfigLocations(configLocationsHere);
        }
    }

    // customizeEnvironment/finalizeEnvironment pair
//    @Override
//    protected void finalizeEnvironment(WebEnvironment environment) {
//        super.finalizeEnvironment(environment);
//    }
}
