package com.google.slurp.servlets;

import java.io.IOException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;

/**
 * Servlet context listener that is looked for whenever the backend server
 * receives a startup or shutdown request.
 */
@WebListener
public class ServerListener implements ServletContextListener {
  /**
   * {@inheritDoc}
   *
   * Initialize the Firebase Admin app for the server.
   */
  @Override
  public void contextInitialized(ServletContextEvent event) {
    System.out.println("Server initialized and starting up...");

    try {
      FirebaseOptions options = new FirebaseOptions.Builder()
                                    .setCredentials(GoogleCredentials.getApplicationDefault())
                                    .build();

      FirebaseApp.initializeApp(options);
    } catch (IOException error) {
      System.err.println("Error obtaining the Application Default Credentials:");
      error.printStackTrace();
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void contextDestroyed(ServletContextEvent event) {
    System.out.println("Server destroyed and shutting down...");
  }
}

@@ -527,22 +527,49 @@
 tener %7B%0A
+  public FirebaseApp app;%0A%0A
   /**%0A
-
    * %7B@i
@@ -1000,24 +1000,30 @@
 ld();%0A%0A     
+ app =
  FirebaseApp
@@ -1226,24 +1226,65 @@
 inheritDoc%7D%0A
+   *%0A   * Delete the Firebase Admin app.%0A
    */%0A  @Ove
@@ -1341,32 +1341,32 @@
 tEvent event) %7B%0A
-
     System.out.p
@@ -1413,15 +1413,34 @@
 wn...%22);
+%0A%0A    app.delete();
 %0A  %7D%0A%7D%0A

Delete FirebaseApp on server shutdown