/**
 * 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
 *
 *     http://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.
 */

package org.apache.jena.fuseki.server;

import javax.servlet.ServletContext ;
import javax.servlet.ServletContextEvent ;
import javax.servlet.ServletContextListener ;

import org.apache.jena.fuseki.Fuseki ;
import org.apache.jena.tdb.StoreConnection ;

public class FusekiServerListener implements ServletContextListener {

    public FusekiServerListener() { }
    
    public static ServerInitialConfig initialSetup = null ;

    private boolean initialized = false ;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext() ;
        String x = servletContext.getContextPath() ;
        if ( ! x.isEmpty() ) 
            Fuseki.configLog.info("Context path = "+x) ;
//        String x = System.getProperty("user.dir") ;
//        Path currentRelativePath = Paths.get("");
//        String s = currentRelativePath.toAbsolutePath().toString();
//        confLog.info("dir1 = "+x+" : dir2 = "+s) ;
        init() ;
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
//        DataAccessPointRegistry.get().forEach((key, dap) -> {
//            ??
//        }) ;
        // But in flight-transactions?
        StoreConnection.reset();
    }

    public synchronized void init() {
        if ( initialized )
            return ;
        initialized = true ;

        try {
            FusekiServer.init() ; 
            if ( ! FusekiServer.serverInitialized ) {
                Fuseki.serverLog.error("Failed to initialize : Server not running") ;
                return ;
            }
            // The command line code sets initialSetup. In a non-commandline startup,
            // initialSetup is null. Set to include a possible config.ttl in the BASE area.
            if ( initialSetup == null ) {
                initialSetup = new ServerInitialConfig() ;
                String cfg = FusekiEnv.FUSEKI_BASE.resolve(FusekiServer.DFT_CONFIG).toAbsolutePath().toString() ;
                initialSetup.fusekiServerConfigFile = cfg ;
            }

            if ( initialSetup != null ) {
                FusekiServer.initializeDataAccessPoints(initialSetup, FusekiServer.dirConfiguration.toString()) ;
            } else {
                Fuseki.serverLog.error("No configuration") ;
                System.exit(0) ;
            }
        } catch (Throwable th) { 
            Fuseki.serverLog.error("Exception in initialization: {}", th.getMessage()) ;
            throw th ;
        }
    }
}


@@ -2392,32 +2392,45 @@
 ;%0A            %7D%0A
+            %0A
             // T
@@ -2468,16 +2468,31 @@
 alSetup.
+%0A            //
  In a no
@@ -2513,16 +2513,39 @@
 startup,
+ initialSetup is null. 
 %0A       
@@ -2549,18 +2549,20 @@
         
-//
+if (
  initial
@@ -2571,71 +2571,89 @@
 tup 
-is
+==
  null
-. Set to include a possible config.ttl in the BASE area.
+ )%0A                initialSetup = new ServerInitialConfig() ;%0A            
 %0A   
@@ -2670,32 +2670,56 @@
 f ( initialSetup
+.fusekiCmdLineConfigFile
  == null ) %7B%0A   
@@ -2735,50 +2735,72 @@
     
-initialSetup = new ServerInitialConfig() ;
+// If not command line configuration file, use the run area one.
 %0A   

Use run/config.ttl if the command line did not give one.