/*
 * Copyright 2009 New Atlanta Communications, LLC
 *
 * Licensed 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 com.newatlanta.appengine.servlet;

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

import com.newatlanta.commons.vfs.provider.gae.GaeVFS;
import com.newatlanta.h2.store.fs.FileSystemGae;

/**
 * Handles servlet lifecycle events related to GaeVFS. Must be configured within
 * <tt>web.xml</tt>:
 * <p><code>
 * &lt;listener><br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;listener-class>com.newatlanta.appengine.servlet.GaeVfsServletEventListener&lt;/listener-class><br>
 * &lt;/listener><br>
 * </code><p>
 * 
 * @author <a href="mailto:vbonfanti@gmail.com">Vince Bonfanti</a>
 */
public class GaeVfsServletEventListener implements ServletContextListener {

    /**
     * Registers with H2.
     */
    public void contextInitialized( ServletContextEvent event ) {
        try {
            // use reflection in case H2 is not installed
            Class<?> fileSystemClass = Class.forName( "org.h2.store.fs.FileSystem" );
            fileSystemClass.getMethod( "register", fileSystemClass ).invoke( null,
                                            FileSystemGae.getInstance() );
            GaeVFS.log.info( "Successfully registered GaeVFS with H2" );
        } catch ( Exception e ) {
            GaeVFS.log.info( "Failed to register GaeVFS with H2: " + e );
        }
        
        // user.home is needed by H2 to locate the ".h2.server.properties" file
        String userDir = System.getProperty( "user.dir" );
        System.setProperty( "user.home", userDir + "/WEB-INF/" );
        GaeVFS.log.info( "Context initialized: " + userDir );
    }

    /**
     * Closes GaeVFS when the servlet is destroyed.
     */
    public void contextDestroyed( ServletContextEvent event ) {
        GaeVFS.close();
        GaeVFS.log.info( "Context destroyed: " + System.getProperty( "user.dir" ) );
    }
}
@@ -755,24 +755,59 @@
 istener;%0D%0A%0D%0A
+import org.h2.engine.Constants;%0D%0A%0D%0A
 import com.n
@@ -899,16 +899,55 @@
 temGae;%0D
+%0Aimport com.newatlanta.nio.file.Paths;%0D
 %0A%0D%0A/**%0D%0A
@@ -1516,32 +1516,92 @@
 vent event ) %7B%0D%0A
+        String userDir = System.getProperty( %22user.dir%22 );%0D%0A
         try %7B%0D%0A 
@@ -1991,131 +1991,18 @@
     
-%7D catch ( Exception e ) %7B%0D%0A            GaeVFS.log.info( %22Failed to register GaeVFS with H2: %22 + e );%0D%0A        %7D%0D%0A    
     %0D%0A
+    
     
@@ -2078,32 +2078,36 @@
 %22 file%0D%0A        
+    
 String userDir =
@@ -2097,37 +2097,74 @@
  String user
-Dir =
+Home = userDir + %22/WEB-INF/%22;%0D%0A           
  System.
 getProperty(
@@ -2143,33 +2143,33 @@
          System.
-g
+s
 etProperty( %22use
@@ -2174,82 +2174,406 @@
 ser.
-dir%22 );%0D%0A        System.setProperty
+home%22, userHome );%0D%0A            %0D%0A            // copy %22h2.server.properties%22 to %22.h2.server.properties%22%0D%0A            Paths.get
 ( 
-%22
 user
-.home%22, userDir + %22/WEB-INF/%22 );
+Home + Constants.SERVER_PROPERTIES_FILE.substring( 1 ) ).copyTo(%0D%0A                    Paths.get( userHome + Constants.SERVER_PROPERTIES_FILE ) );%0D%0A        %7D catch ( Exception e ) %7B%0D%0A            GaeVFS.log.info( %22Failed to register GaeVFS with H2: %22 + e );%0D%0A        %7D
 %0D%0A  

support for H2 console