// WorkingFiles.java

/**
*    Copyright (C) 2008 10gen Inc.
*  
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*  
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*  
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package ed.io;

import java.io.*;

public class WorkingFiles {
    
    private static final String TMP_DIR = "/tmp/jxp-" + System.getProperty( "user.name" ) + "/";
    private static final File TMP_FILE = new File( TMP_DIR );
    static {
        TMP_FILE.mkdirs();
    }

    public static final String getTmpDir(){
        return TMP_DIR;
    }

    public static File getTypeDir( String type ){
        File f = new File( TMP_FILE , type );
        f.mkdirs();
        return f;
    }

    public static File getTMPFile( String type , String name ){
        name = FileUtil.clean( name );

        while ( name.startsWith( "/" ) )
            name = name.substring(1);
        
        File f = new File( getTypeDir( type ) , name );
        
        if ( name.contains( "/" ) )
            f.getParentFile().mkdirs();

        return f;
    }
    
}

@@ -720,16 +720,36 @@
 va.io.*;
+%0Aimport java.util.*;
 %0A%0Apublic
@@ -771,24 +771,608 @@
 iles %7B%0A    %0A
+    private static final String APP_NAME;%0A    static %7B%0A        String className = %22none%22;%0A        %0A        for ( Map.Entry%3CThread,StackTraceElement%5B%5D%3E e : Thread.getAllStackTraces().entrySet() )%7B%0A            StackTraceElement%5B%5D stack = e.getValue();%0A            if ( stack == null %7C%7C stack.length == 0 )%0A                continue;%0A            %0A            StackTraceElement last = stack%5Bstack.length-1%5D;%0A            if ( %22main%22.equals( last.getMethodName() ) )%0A                className = last.getClassName().replaceAll( %22%5E.*%5C%5C.%22 , %22%22 );%0A        %7D%0A%0A        APP_NAME = className;%0A    %7D%0A
     private 
@@ -1450,16 +1450,33 @@
 ame%22 ) +
+ %22-%22 + APP_NAME +
  %22/%22;%0A  

put appname in tmp path so multiple apps can run on same machine without interfering