You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@continuum.apache.org by ba...@apache.org on 2015/04/10 02:02:06 UTC
svn commit: r1672524 - in /continuum/trunk/continuum-api/src:
main/java/org/apache/continuum/utils/file/
test/java/org/apache/continuum/utils/shell/
Author: batkinson
Date: Fri Apr 10 00:02:06 2015
New Revision: 1672524
URL: http://svn.apache.org/r1672524
Log:
Added service for common file system operations to api.
Added:
continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/
continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/DefaultFileSystemManager.java
continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/FileSystemManager.java
Modified:
continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java
Added: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/DefaultFileSystemManager.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/DefaultFileSystemManager.java?rev=1672524&view=auto
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/DefaultFileSystemManager.java (added)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/DefaultFileSystemManager.java Fri Apr 10 00:02:06 2015
@@ -0,0 +1,117 @@
+package org.apache.continuum.utils.file;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.
+ */
+
+import org.apache.commons.io.IOUtils;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.util.FileUtils;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+@Component( role = FileSystemManager.class )
+public class DefaultFileSystemManager
+ implements FileSystemManager
+{
+ public void wipeDir( File dir )
+ throws IOException
+ {
+ FileUtils.cleanDirectory( dir );
+ }
+
+ public void removeDir( File dir )
+ throws IOException
+ {
+ FileUtils.deleteDirectory( dir );
+ }
+
+ public void copyDir( File source, File dest )
+ throws IOException
+ {
+ FileUtils.copyDirectoryStructure( source, dest );
+ }
+
+ public void delete( File target )
+ throws IOException
+ {
+ FileUtils.forceDelete( target );
+ }
+
+ public String fileContents( File file )
+ throws IOException
+ {
+ return FileUtils.fileRead( file );
+ }
+
+ public byte[] fileBytes( File file )
+ throws IOException
+ {
+ return org.apache.commons.io.FileUtils.readFileToByteArray( file );
+ }
+
+ public void copyFile( File source, File dest )
+ throws IOException
+ {
+ FileUtils.copyFile( source, dest );
+ }
+
+ public void writeFile( File file, String contents )
+ throws IOException
+ {
+ FileUtils.fileWrite( file, contents );
+ }
+
+ public void writeFile( File file, InputStream input )
+ throws IOException
+ {
+ FileOutputStream fout = null;
+ try
+ {
+ fout = new FileOutputStream( file );
+ IOUtils.copy( input, fout );
+ }
+ finally
+ {
+ if ( fout != null )
+ {
+ fout.close();
+ }
+ }
+ }
+
+ public File createTempFile( String prefix, String suffix, File parent )
+ {
+ return FileUtils.createTempFile( prefix, suffix, parent );
+ }
+
+ public void copyFileToDir( File file, File dest )
+ throws IOException
+ {
+ FileUtils.copyFileToDirectory( file, dest );
+ }
+
+ public void deleteOnExit( File fileOrDir )
+ throws IOException
+ {
+ FileUtils.forceDeleteOnExit( fileOrDir );
+ }
+}
Added: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/FileSystemManager.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/FileSystemManager.java?rev=1672524&view=auto
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/FileSystemManager.java (added)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/file/FileSystemManager.java Fri Apr 10 00:02:06 2015
@@ -0,0 +1,147 @@
+package org.apache.continuum.utils.file;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * A service interface for handling common file system related tasks. By depending on this interface rather than static
+ * utility methods, components can have consistent, bug-fixed and easily-stubbed file system operations. By limiting
+ * coupling to utility libraries, it also allows for easier upgrades for utility libraries.
+ */
+public interface FileSystemManager
+{
+ /**
+ * Removes contents contained in the specified directory, leaving the directory intact.
+ *
+ * @param dir the directory to clean out
+ * @throws IOException
+ */
+ void wipeDir( File dir )
+ throws IOException;
+
+ /**
+ * Removes the specified directory and all contents, if any.
+ *
+ * @param dir the directory to remove
+ * @throws IOException
+ */
+ void removeDir( File dir )
+ throws IOException;
+
+ /**
+ * Copies the source directory and its contents to the specified destination.
+ *
+ * @param source directory to copy
+ * @param dest location to copy the directory to
+ */
+ void copyDir( File source, File dest )
+ throws IOException;
+
+ /**
+ * Forcibly removes the target directory or file, including contents. Also handles symlinks.
+ *
+ * @param fileOrDir file or directory to delete
+ * @throws IOException
+ */
+ void delete( File fileOrDir )
+ throws IOException;
+
+ /**
+ * Reads the entire contents of the specified file.
+ *
+ * @param file the file to read
+ * @return contents of the file, as String
+ * @throws IOException
+ */
+ String fileContents( File file )
+ throws IOException;
+
+ /**
+ * Reads the entire contents of the specified file.
+ *
+ * @param file the file to read
+ * @return contents of the file, as a byte array
+ * @throws IOException
+ */
+ byte[] fileBytes( File file )
+ throws IOException;
+
+ /**
+ * Copies the specified file to a file at the specified destination.
+ *
+ * @param source the file to copy
+ * @param dest the destination of the copy
+ * @throws IOException
+ */
+ void copyFile( File source, File dest )
+ throws IOException;
+
+ /**
+ * Writes a String as content for the specified file.
+ *
+ * @param file file to write to
+ * @param contents contents to write
+ * @throws IOException
+ */
+ void writeFile( File file, String contents )
+ throws IOException;
+
+ /**
+ * Writes contents of InputStream as content for the specified file.
+ *
+ * @param file file to write to
+ * @param input opened stream to read contents from, will not be closed automatically
+ * @throws IOException
+ */
+ void writeFile( File file, InputStream input )
+ throws IOException;
+
+ /**
+ * Create a new temporary file under the specified parent directory.
+ *
+ * @param prefix prefix to start file with
+ * @param suffix suffix to end file name with
+ * @param parent the directory to create the temp file in
+ * @return
+ */
+ File createTempFile( String prefix, String suffix, File parent );
+
+ /**
+ * Copies the specified file to the destination directory.
+ *
+ * @param file the file to copy
+ * @param dest the destination directory to put the copy
+ * @throws IOException
+ */
+ void copyFileToDir( File file, File dest )
+ throws IOException;
+
+ /**
+ * Registers the given file or directory for removal upon JVM shutdown.
+ *
+ * @param fileOrDir the file or dir to remove when JVM shuts down
+ * @throws IOException
+ */
+ void deleteOnExit( File fileOrDir )
+ throws IOException;
+}
Modified: continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java?rev=1672524&r1=1672523&r2=1672524&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java (original)
+++ continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/FileOutputConsumerTest.java Fri Apr 10 00:02:06 2015
@@ -1,6 +1,26 @@
package org.apache.continuum.utils.shell;
-import org.apache.commons.io.FileUtils;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.
+ */
+
+import org.apache.continuum.utils.file.DefaultFileSystemManager;
+import org.apache.continuum.utils.file.FileSystemManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -20,6 +40,8 @@ public class FileOutputConsumerTest
FileOutputConsumer consumer;
+ FileSystemManager fsManager;
+
@Before
public void setUp()
throws IOException
@@ -28,6 +50,7 @@ public class FileOutputConsumerTest
outputFile.delete(); // we want to test whether it is created
assertFalse( "file should not exist", outputFile.exists() );
consumer = new FileOutputConsumer( outputFile );
+ fsManager = new DefaultFileSystemManager();
}
@After
@@ -96,7 +119,7 @@ public class FileOutputConsumerTest
private void assertFileEmpty()
throws IOException
{
- assertEquals( "file should have been empty", "", FileUtils.readFileToString( outputFile ) );
+ assertEquals( "file should have been empty", "", fsManager.fileContents( outputFile ) );
}
private void assertFileContents( String... contents )
@@ -109,6 +132,6 @@ public class FileOutputConsumerTest
finalContents.append( String.format( "%s%n", line ) );
}
assertEquals( "file did not contain expected contents", finalContents.toString(),
- FileUtils.readFileToString( outputFile ) );
+ fsManager.fileContents( outputFile ) );
}
}