/*
 * 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.commons.vfs.provider.gae;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.RandomAccessContent;
import org.apache.commons.vfs.util.FileObjectUtils;
import org.apache.commons.vfs.util.RandomAccessMode;

/**
 * Creates and initializes a GaeFileSystemManager.
 * 
 * IMPORTANT! You must set the path to the webapp root directory via the
 * GaeVFS.setRootPath() method before invoking GaeVFS.getManager().
 * 
 * IMPORTANT! You must clear the ThreadLocal cache at the end of every request
 * via the GaeVFS.clearFilesCache() method. See additional comments within the
 * GaeMemcacheFilesCache class.
 *
 * @author Vince Bonfanti <vbonfanti@gmail.com>
 */
public class GaeVFS {

    private static final int DEFAULT_BLOCK_SIZE = 1024 * 32; // max 1024 x 1023
    
    static {
        // GAE doesn't set these values; Commons VFS will fail to
        // initialize if they're not set
        System.setProperty( "os.arch", "" );
        System.setProperty( "os.version", "" );
    }

    private static GaeFileSystemManager fsManager;
    private static String rootPath;
    private static int blockSize = DEFAULT_BLOCK_SIZE;

    public static GaeFileSystemManager getManager() throws IOException {
        if ( fsManager == null ) {
            if ( rootPath == null ) {
                throw new IOException( "root path not defined" );
            }
            fsManager = new GaeFileSystemManager();
            fsManager.init( rootPath );
        }
        return fsManager;
    }
    
    public static void setRootPath( String _rootPath ) {
        rootPath = _rootPath;
    }
    
    public static int getBlockSize() {
        return blockSize;
    }
    
    public void setBlockSize( int size ) {
        if ( size <= 0 ) {
            throw new IllegalArgumentException( "invalid block size: " + size );
        }
        blockSize = Math.min( size, 1023 ) * 1024; // max size is 1023 * 1024
    }
    
    public static void setBlockSize( FileObject fileObject, int size ) throws FileSystemException {
        if ( size <= 0 ) {
            throw new IllegalArgumentException( "invalid block size: " + size );
        }
        if ( fileObject instanceof GaeFileObject ) {
            ((GaeFileObject)fileObject).setBlockSize( Math.min( size, 1023 ) * 1024 );
        }
    }

    public static FileObject resolveFile( String name ) throws IOException {
        return getManager().resolveFile( name );
    }
    
    public static RandomAccessContent getRandomAccessContent( String name, String mode )
            throws IOException {
        return getRandomAccessContent( resolveFile( name ), mode );
    }

    public static RandomAccessContent getRandomAccessContent( FileObject fileObject, String mode )
            throws IOException {
        RandomAccessMode raMode;
        if ( mode.equals( "r" ) ) {
            raMode = RandomAccessMode.READ;
        // TODO: support "rws" and "rwd" modes as described for RandomAccessFile?
        } else if ( mode.equals( "rw" ) ) { // || mode.equals( "rws" ) || mode.equals( "rwd" ) ) {
            raMode = RandomAccessMode.READWRITE;
        } else {
            throw new IllegalArgumentException( "invalid mode: " + mode );
        }
        if ( !fileObject.exists() ) {
            // if opening for read/write and the file doesn't exist, try to create it
            if ( raMode == RandomAccessMode.READWRITE ) {
                fileObject.createFile();
            } else {
                throw new FileNotFoundException( fileObject.getName().getPath() );
            }
        }
        return FileObjectUtils.getAbstractFileObject( fileObject ).getRandomAccessContent( raMode );
    }

    public static void clearFilesCache() {
        if ( fsManager != null ) {
            fsManager.clearFilesCache();
        }
    }

    public static void close() {
        if ( fsManager != null ) {
            fsManager.close();
            fsManager = null;
        }
    }
}
@@ -896,61 +896,8 @@
 t;%0D%0A
-import org.apache.commons.vfs.util.FileObjectUtils;%0D%0A
 impo
@@ -946,16 +946,16 @@
 sMode;%0D%0A
+
 %0D%0A/**%0D%0A 
@@ -1495,10 +1495,11 @@
 4 * 
-32
+128
 ; //
@@ -2725,28 +2725,34 @@
 blic static 
-void
+FileObject
  setBlockSiz
@@ -3084,32 +3084,60 @@
  );%0D%0A        %7D%0D%0A
+        return fileObject;%0D%0A
     %7D%0D%0A%0D%0A    pub
@@ -4434,17 +4434,17 @@
  return 
-F
+f
 ileObjec
@@ -4448,48 +4448,20 @@
 ject
-Utils.getAbstractFileObject( fileObject 
+.getContent(
 ).ge

increase default block size to 128KB