diff --git a/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/impl/TempDir.java b/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/impl/TempDir.java deleted file mode 100644 index eb104aa04ec..00000000000 --- a/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/impl/TempDir.java +++ /dev/null @@ -1,111 +0,0 @@ - -package org.eclipse.birt.data.aggregation.impl; - -import java.io.File; - -import org.eclipse.birt.data.engine.api.DataEngineThreadLocal; - -public class TempDir -{ - - private static TempDir instance; - - private String path; - - private TempDir( String path ) - { - this.path = path; - } - - /** - * called one time during starting up the plugin - * @param path - */ - public static void createInstance( String path ) - { - instance = new TempDir( path ); - } - - /** - * valid only if createInstance is already called - * @return - */ - public static TempDir getInstance( ) - { - if( instance == null ) - { - String tempDir = System.getProperty( "java.io.tmpdir" ) - + "AggregationPlugin_temp" + File.separator; - - File f = new File( tempDir ); - if ( f.exists( ) ) - { - deleteDirectory( f ); - } - instance = new TempDir( tempDir ); - } - return instance; - } - - public String getPath( ) - { - if ( DataEngineThreadLocal.getInstance( ).getPathManager( ) != null ) - { - return DataEngineThreadLocal.getInstance( ) - .getPathManager( ) - .getTempFileName( "AggregationPlugin_temp", 0, null ); - } - else - return path; - } - - public static void release( ) - { - if (instance != null) - { - File f = new File( instance.getPath( ) ); - if ( f.exists( ) ) - { - deleteDirectory( f ); - } - instance = null; - } - } - - /** - * - * @param dir - */ - private static void deleteDirectory( File dir ) - { - File[] subFiles = dir.listFiles( ); - if ( subFiles != null ) - { - for ( int i = 0; i < subFiles.length; i++ ) - { - if ( subFiles[i].isDirectory( ) ) - { - deleteDirectory( subFiles[i] ); - } - else - { - safeDelete( subFiles[i] ); - } - - } - } - safeDelete( dir ); - } - - /** - * - * @param file - */ - private static void safeDelete( File file ) - { - if ( !file.delete( ) ) - { - file.deleteOnExit( ); - } - } -} diff --git a/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/impl/rank/BaseTopBottomAccumulator.java b/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/impl/rank/BaseTopBottomAccumulator.java index c46619ad06d..323c0d226b6 100644 --- a/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/impl/rank/BaseTopBottomAccumulator.java +++ b/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/impl/rank/BaseTopBottomAccumulator.java @@ -11,10 +11,13 @@ package org.eclipse.birt.data.aggregation.impl.rank; +import java.io.File; + import org.eclipse.birt.data.aggregation.impl.RunningAccumulator; -import org.eclipse.birt.data.aggregation.impl.TempDir; +import org.eclipse.birt.data.engine.api.DataEngineThreadLocal; import org.eclipse.birt.data.engine.cache.BasicCachedArray; import org.eclipse.birt.data.engine.core.DataException; +import org.eclipse.birt.data.engine.core.security.FileSecurity; import org.eclipse.birt.data.engine.i18n.ResourceConstants; /** @@ -35,11 +38,23 @@ public abstract class BaseTopBottomAccumulator extends RunningAccumulator { private static Boolean falseValue = Boolean.FALSE; private String tempDir; - - public BaseTopBottomAccumulator( ) + public BaseTopBottomAccumulator( ) { - this.tempDir = TempDir.getInstance( ).getPath( ); - targetValue = new BasicCachedArray(tempDir, 0); + if ( DataEngineThreadLocal.getInstance( ).getPathManager( ) != null ) + { + tempDir = DataEngineThreadLocal.getInstance( ) + .getPathManager( ) + .getTempFileName( "AggregationPlugin_temp", + this.hashCode( ), + null ) + File.separator; + } + else + { + tempDir = System.getProperty( "java.io.tmpdir" ) + + "AggregationPlugin_temp" + this.hashCode( ) + + File.separator; + } + targetValue = new BasicCachedArray( tempDir, 0 ); } /* @@ -158,4 +173,60 @@ private Boolean populateValue( ) * @return */ protected abstract int adjustNValue( double N ); + + /* + * (non-Javadoc) + * @see org.eclipse.birt.data.engine.api.aggregation.Accumulator#finish() + */ + public void finish( ) + { + //the calculation has been finish, so we can release the resources + if( this.passNo >1 ) + { + File tempFile = new File( tempDir ); + if ( !FileSecurity.fileExist( tempFile ) + || !FileSecurity.fileIsDirectory( tempFile ) ) + { + return; + } + deleteDirectory( tempFile ); + } + } + + /** + * + * @param dir + */ + private static void deleteDirectory( File dir ) + { + File[] subFiles = FileSecurity.fileListFiles( dir ); + if( subFiles != null ) + { + for( int i = 0; i < subFiles.length; i++ ) + { + if( FileSecurity.fileIsDirectory( subFiles[i] ) ) + { + deleteDirectory( subFiles[i] ); + } + else + { + safeDelete( subFiles[i] ); + } + } + } + safeDelete( dir ); + } + + + /** + * + * @param file + */ + private static void safeDelete( File file ) + { + if( !FileSecurity.fileDelete( file ) ) + { + FileSecurity.fileDeleteOnExit( file ); + } + } } diff --git a/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/plugin/AggregationPlugin.java b/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/plugin/AggregationPlugin.java index f8c7ba2203c..86d96dc1019 100644 --- a/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/plugin/AggregationPlugin.java +++ b/data/org.eclipse.birt.data.aggregation/src/org/eclipse/birt/data/aggregation/plugin/AggregationPlugin.java @@ -1,10 +1,7 @@ package org.eclipse.birt.data.aggregation.plugin; -import java.io.File; - import org.eclipse.birt.core.plugin.BIRTPlugin; -import org.eclipse.birt.data.aggregation.impl.TempDir; import org.osgi.framework.BundleContext; public class AggregationPlugin extends BIRTPlugin @@ -13,15 +10,11 @@ public class AggregationPlugin extends BIRTPlugin public void start( BundleContext context ) throws Exception { super.start( context ); - String tempDir = System.getProperty( "java.io.tmpdir" ) - + "AggregationPlugin_" + this.getBundle( ).hashCode( ) + File.separator; - TempDir.createInstance( tempDir ); } @Override public void stop( BundleContext context ) throws Exception { - TempDir.release( ); super.stop( context ); } }