/*
 * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.wso2.carbon.ml.rest.api.handler;

import java.util.concurrent.TimeUnit;

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

import org.wso2.carbon.ml.core.utils.MLCoreServiceValueHolder;
import org.wso2.carbon.ml.database.exceptions.DatabaseHandlerException;

/**
 * This shutdown hook will make the REST API to wait till the resources it used, clean up.
 */
public class ShutdownListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        try {
            if (MLCoreServiceValueHolder.getInstance().getDatabaseService() != null) {
                MLCoreServiceValueHolder.getInstance().getDatabaseService().shutdown();
            }

            if (MLCoreServiceValueHolder.getInstance().getThreadExecutor() != null) {
                try {
                    MLCoreServiceValueHolder.getInstance().getThreadExecutor()
                            .awaitTermination(1000, TimeUnit.MILLISECONDS);
                    MLCoreServiceValueHolder.getInstance().getThreadExecutor().shutdown();
                } catch (InterruptedException ignore) {
                }
            }

            if (MLCoreServiceValueHolder.getInstance().getSparkContext() != null) {
                MLCoreServiceValueHolder.getInstance().getSparkContext().close();
            }
        } catch (DatabaseHandlerException e) {
        }
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
    }

}

@@ -706,24 +706,88 @@
 i.handler;%0A%0A
+import org.wso2.carbon.ml.core.utils.MLCoreServiceValueHolder;%0A%0A
 import java.
@@ -812,17 +812,16 @@
 meUnit;%0A
-%0A
 import j
@@ -904,144 +904,8 @@
 r;%0A%0A
-import org.wso2.carbon.ml.core.utils.MLCoreServiceValueHolder;%0Aimport org.wso2.carbon.ml.database.exceptions.DatabaseHandlerException;%0A%0A
 /**%0A
@@ -1145,216 +1145,8 @@
 ) %7B%0A
-        try %7B%0A            if (MLCoreServiceValueHolder.getInstance().getDatabaseService() != null) %7B%0A                MLCoreServiceValueHolder.getInstance().getDatabaseService().shutdown();%0A            %7D%0A%0A    
     
@@ -1239,22 +1239,14 @@
     
-   
- 
 try %7B%0A
-    
     
@@ -1340,20 +1340,16 @@
         
-   
- 
 .awaitTe
@@ -1388,20 +1388,16 @@
 CONDS);%0A
-    
         
@@ -1483,28 +1483,24 @@
             
-   
- 
 %7D catch (Int
@@ -1543,24 +1543,18 @@
     
-    %7D%0A  
+%7D%0A
         
   %7D%0A
@@ -1549,25 +1549,19 @@
         
-  
 %7D%0A%0A
-    
         
@@ -1636,36 +1636,32 @@
 ) %7B%0A            
-  
-  
 MLCoreServiceVal
@@ -1714,69 +1714,8 @@
 ();%0A
-            %7D%0A        %7D catch (DatabaseHandlerException e) %7B%0A
     

remove DB service shutdown from ShutdownListener