import edu.umd.cs.mtc.MultithreadedTestCase;
import edu.umd.cs.mtc.TestFramework;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicInteger;
+//import org.apache.commons.io.IOUtils;
+
/**
* @author Olivier Lamy
*/
try
{
lock.getFile().delete();
- FileUtils.copyFile( largeJar, lock.getFile() );
+ Files.copy( largeJar.toPath(), lock.getFile().toPath() );
}
finally
{
try
{
lock.getFile().delete();
- FileUtils.copyFile( largeJar, lock.getFile() );
+ Files.copy( largeJar.toPath(), lock.getFile().toPath() );
}
finally
{
Lock lock = fileLockManager.readFileLock( this.file );
try
{
- IOUtils.copy( new FileInputStream( lock.getFile() ),
- new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
+ Files.copy( Paths.get( lock.getFile().getPath() ),
+ new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
}
finally
{
try
{
lock.getFile().delete();
- FileUtils.copyFile( largeJar, lock.getFile() );
+ Files.copy( largeJar.toPath(), lock.getFile().toPath() );
}
finally
{
try
{
lock.getFile().delete();
- FileUtils.copyFile( largeJar, lock.getFile() );
+ Files.copy( largeJar.toPath(), lock.getFile().toPath() );
}
finally
{
Lock lock = fileLockManager.readFileLock( this.file );
try
{
- IOUtils.copy( new FileInputStream( lock.getFile() ),
- new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
+ Files.copy( lock.getFile().toPath(),
+ new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
}
finally
{
try
{
lock.getFile().delete();
- FileUtils.copyFile( largeJar, lock.getFile() );
+ Files.copy( largeJar.toPath(), lock.getFile().toPath() );
}
finally
{
Lock lock = fileLockManager.readFileLock( this.file );
try
{
- IOUtils.copy( new FileInputStream( lock.getFile() ),
- new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
+ Files.copy( lock.getFile().toPath(), new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
}
finally
{
try
{
lock.getFile().delete();
- FileUtils.copyFile( largeJar, lock.getFile() );
+ Files.copy( largeJar.toPath(), lock.getFile().toPath() );
}
finally
{
Lock lock = fileLockManager.readFileLock( this.file );
try
{
- IOUtils.copy( new FileInputStream( lock.getFile() ),
- new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
+ Files.copy( lock.getFile().toPath(),
+ new FileOutputStream( File.createTempFile( "foo", ".jar" ) ) );
}
finally
{
* under the License.
*/
-import edu.umd.cs.mtc.MultithreadedTestCase;
-import edu.umd.cs.mtc.TestFramework;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
/**
* @author Olivier Lamy
fileLockManager.clearLockFiles();
}
- @Test( expected = FileLockTimeoutException.class )
+ @Test(expected = FileLockTimeoutException.class)
public void testTimeout()
throws Throwable
{
File file = new File( System.getProperty( "buildDirectory" ), "foo.txt" );
+ Files.deleteIfExists( file.toPath() );
+
File largeJar = new File( System.getProperty( "basedir" ), "src/test/cassandra-all-2.0.3.jar" );
Lock lock = fileLockManager.writeFileLock( file );
- FileUtils.copyFile( largeJar, lock.getFile() );
+ Files.copy( largeJar.toPath(), lock.getFile().toPath(), StandardCopyOption.REPLACE_EXISTING );
lock = fileLockManager.writeFileLock( file );
import org.apache.archiva.xml.XMLWriter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
+import java.nio.file.Files;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
throws RepositoryMetadataException
{
boolean thrown = false;
- FileWriter writer = null;
- try
+ try (FileWriter writer = new FileWriter( outputFile ))
{
- writer = new FileWriter( outputFile );
write( metadata, writer );
writer.flush();
}
}
finally
{
- IOUtils.closeQuietly( writer );
if ( thrown )
{
FileUtils.deleteQuietly( outputFile );
* under the License.
*/
-import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import java.util.Map;
/**
- * XMLReader - a set of common xml utility methods for reading content out of an xml file.
- *
- *
+ * XMLReader - a set of common xml utility methods for reading content out of an xml file.
*/
public class XMLReader
{
this.documentType = type;
this.xmlUrl = url;
- InputStream in = null;
SAXReader reader = new SAXReader();
-
- try
+
+ try (InputStream in = url.openStream())
{
- in = url.openStream();
InputStreamReader inReader = new InputStreamReader( in, "UTF-8" );
LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader );
this.document = reader.read( latinReader );
{
throw new XMLException( "Unable to open stream to " + url + ": " + e.getMessage(), e );
}
- finally
- {
- IOUtils.closeQuietly( in );
- }
Element root = this.document.getRootElement();
if ( root == null )
if ( !StringUtils.equals( root.getName(), documentType ) )
{
- throw new XMLException( "Invalid " + documentType + " xml: Unexpected root element <" + root.getName()
- + ">, expected <" + documentType + ">" );
+ throw new XMLException(
+ "Invalid " + documentType + " xml: Unexpected root element <" + root.getName() + ">, expected <"
+ + documentType + ">" );
}
}
{
// Unknown evaluated type.
throw new XMLException( ".getElement( Expr: " + xpathExpr + " ) resulted in non-Element type -> ("
- + evaluated.getClass().getName() + ") " + evaluated );
+ + evaluated.getClass().getName() + ") " + evaluated );
}
}
else
{
// Unknown evaluated type.
- throw new XMLException( ".getElementText( Node, Expr: " + xpathExpr
- + " ) resulted in non-Element type -> (" + evaluated.getClass().getName() + ") " + evaluated );
+ throw new XMLException( ".getElementText( Node, Expr: " + xpathExpr + " ) resulted in non-Element type -> ("
+ + evaluated.getClass().getName() + ") " + evaluated );
}
}
{
// Unknown evaluated type.
throw new XMLException( ".getElementText( Expr: " + xpathExpr + " ) resulted in non-Element type -> ("
- + evaluated.getClass().getName() + ") " + evaluated );
+ + evaluated.getClass().getName() + ") " + evaluated );
}
}
{
// Unknown evaluated type.
throw new XMLException( ".getElementList( Expr: " + xpathExpr + " ) resulted in non-List type -> ("
- + evaluated.getClass().getName() + ") " + evaluated );
+ + evaluated.getClass().getName() + ") " + evaluated );
}
}
// zip entry of the path -> path must a real file entry of the archive
JarFile jarFile = new JarFile( file );
ZipEntry zipEntry = jarFile.getEntry( path );
- InputStream inputStream = jarFile.getInputStream( zipEntry );
- try
+ try (InputStream inputStream = jarFile.getInputStream( zipEntry ))
{
return new ArtifactContent( IOUtils.toString( inputStream ), repoId );
}
finally
{
closeQuietly( jarFile );
- IOUtils.closeQuietly( inputStream );
}
}
return new ArtifactContent( FileUtils.readFileToString( file ), repoId );
import org.apache.archiva.redback.rest.api.services.UtilServices;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.rest.api.services.CommonServices;
-import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Olivier Lamy
*/
-@Service( "commonServices#rest" )
+@Service("commonServices#rest")
public class DefaultCommonServices
implements CommonServices
{
private void loadResource( final Properties finalProperties, String resourceName, String locale )
throws IOException
{
- InputStream is = null;
Properties properties = new Properties();
- try
+ try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName ))
{
- is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName );
if ( is != null )
{
properties.load( is );
}
}
}
- finally
- {
- IOUtils.closeQuietly( is );
- }
}
@Override
private void loadFromString( String propsStr, Properties properties )
throws ArchivaRestServiceException
{
- InputStream inputStream = null;
- try
+ try (InputStream inputStream = new ByteArrayInputStream( propsStr.getBytes() ))
{
- inputStream = new ByteArrayInputStream( propsStr.getBytes() );
properties.load( inputStream );
}
catch ( IOException e )
throw new ArchivaRestServiceException( e.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
}
- finally
- {
- IOUtils.closeQuietly( inputStream );
- }
}
import org.apache.archiva.security.common.ArchivaRoleConstants;
import org.apache.archiva.xml.XMLException;
import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.index.context.IndexingContext;
import org.slf4j.Logger;
import javax.inject.Named;
import javax.ws.rs.core.Response;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
+import java.nio.file.Files;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
* @author Olivier Lamy
* @since 1.4-M1
*/
-@Service( "repositoriesService#rest" )
+@Service("repositoriesService#rest")
public class DefaultRepositoriesService
extends AbstractRestService
implements RepositoriesService
private Logger log = LoggerFactory.getLogger( getClass() );
@Inject
- @Named( value = "taskExecutor#indexing" )
+ @Named(value = "taskExecutor#indexing")
private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor;
@Inject
private RepositoryContentFactory repositoryFactory;
@Inject
- @Named( value = "archivaTaskScheduler#repository" )
+ @Named(value = "archivaTaskScheduler#repository")
private ArchivaTaskScheduler scheduler;
@Inject
private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
@Inject
- @Named( value = "repositorySessionFactory" )
+ @Named(value = "repositorySessionFactory")
protected RepositorySessionFactory repositorySessionFactory;
@Inject
* Cache used for namespaces
*/
@Inject
- @Named( value = "cache#namespaces" )
+ @Named(value = "cache#namespaces")
private Cache<String, Collection<String>> namespacesCache;
private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
{
throw new ArchivaRestServiceException(
"artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId()
- + " and redeployment blocked", null );
+ + " and redeployment blocked", null
+ );
}
else
{
private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums )
throws IOException
{
- FileOutputStream out = new FileOutputStream( new File( targetPath, targetFilename ) );
- FileInputStream input = new FileInputStream( sourceFile );
-
- try
- {
- IOUtils.copy( input, out );
- }
- finally
- {
- IOUtils.closeQuietly( out );
- IOUtils.closeQuietly( input );
- }
+ Files.copy( sourceFile.toPath(), new File( targetPath, targetFilename ).toPath() );
if ( fixChecksums )
{
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
-
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
+import java.nio.file.Files;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
File pomFile = new File( targetPath, filename );
MavenXpp3Writer writer = new MavenXpp3Writer();
- FileWriter w = new FileWriter( pomFile );
- try
+
+ try (FileWriter w = new FileWriter( pomFile ))
{
writer.write( w, projectModel );
}
- finally
- {
- IOUtils.closeQuietly( w );
- }
return pomFile;
}
private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums )
throws IOException
{
- FileOutputStream out = null;
- FileInputStream input = null;
- try
- {
- out = new FileOutputStream( new File( targetPath, targetFilename ) );
- input = new FileInputStream( sourceFile );
- IOUtils.copy( input, out );
- }
- finally
- {
- IOUtils.closeQuietly( out );
- IOUtils.closeQuietly( input );
- }
+ Files.copy( sourceFile.toPath(), new File( targetPath, targetFilename ).toPath() );
if ( fixChecksums )
{
import org.apache.archiva.admin.model.beans.RemoteRepository;
import org.apache.archiva.redback.rest.api.services.RoleManagementService;
import org.apache.archiva.security.common.ArchivaRoleConstants;
-import org.apache.commons.compress.utils.IOUtils;
+import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
import org.apache.commons.io.FileUtils;
import org.apache.maven.wagon.providers.http.HttpWagon;
import org.apache.maven.wagon.repository.Repository;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
+import java.nio.file.Files;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
-import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
/**
* @author Olivier Lamy
protected String getSpringConfigLocation()
{
return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
- } @Override
+ }
+ @Override
@Before
public void startServer()
}
-
-
public static class RedirectServlet
extends HttpServlet
{
throws ServletException, IOException
{
File jar = new File( System.getProperty( "basedir" ), "src/test/junit-4.9.jar" );
- IOUtils.copy( new FileInputStream( jar ), resp.getOutputStream() );
+ Files.copy( jar.toPath(), resp.getOutputStream() );
}
}
import javax.servlet.http.HttpServletResponse;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public ArchivaDavResource( String localResource, String logicalResource, ManagedRepository repository,
String remoteAddr, String principal, DavSession session,
ArchivaDavResourceLocator locator, DavResourceFactory factory, MimeTypes mimeTypes,
- List<AuditListener> auditListeners, RepositoryArchivaTaskScheduler scheduler , FileLockManager fileLockManager )
+ List<AuditListener> auditListeners, RepositoryArchivaTaskScheduler scheduler,
+ FileLockManager fileLockManager )
{
this( localResource, logicalResource, repository, session, locator, factory, mimeTypes, auditListeners,
scheduler, fileLockManager );
if ( !isCollection() && outputContext.hasStream() )
{
Lock lock = fileLockManager.readFileLock( localResource );
- FileInputStream is = null;
- try
+ try (InputStream is = Files.newInputStream( lock.getFile().toPath() ))
{
- // Write content to stream
- is = new FileInputStream( lock.getFile() );
IOUtils.copy( is, outputContext.getOutputStream() );
}
- finally
- {
- IOUtils.closeQuietly( is );
- fileLockManager.release( lock );
- }
}
else if ( outputContext.hasStream() )
{
return null;
}
- @SuppressWarnings ("unchecked")
+ @SuppressWarnings("unchecked")
@Override
public MultiStatusResponse alterProperties( List changeList )
throws DavException
if ( isCollection() && inputContext.hasStream() ) // New File
{
- FileOutputStream stream = null;
- try
+ try (OutputStream stream = Files.newOutputStream( localFile.toPath() ))
{
- stream = new FileOutputStream( localFile );
IOUtils.copy( inputContext.getInputStream(), stream );
}
catch ( IOException e )
{
throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e );
}
- finally
- {
- IOUtils.closeQuietly( stream );
- }
// TODO: a bad deployment shouldn't delete an existing file - do we need to write to a temporary location first?
long expectedContentLength = inputContext.getContentLength();
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* MimeTypes
- *
- *
*/
-@Service( "mimeTpes" )
+@Service("mimeTpes")
public class MimeTypes
{
private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
load( resource );
}
- public void load( File file )
- {
- if ( !file.exists() || !file.isFile() || !file.canRead() )
- {
- log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : not a readable file." );
- return;
- }
-
- FileInputStream fis = null;
-
- try
- {
- fis = new FileInputStream( file );
- }
- catch ( FileNotFoundException e )
- {
- log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : " + e.getMessage(), e );
- }
- finally
- {
- IOUtils.closeQuietly( fis );
- }
- }
-
public void load( String resourceName )
{
ClassLoader cloader = this.getClass().getClassLoader();
throw new IllegalStateException( "Unable to find resource " + resourceName );
}
- InputStream mimeStream = null;
-
- try
+ try (InputStream mimeStream = mimeURL.openStream())
{
- mimeStream = mimeURL.openStream();
load( mimeStream );
}
catch ( IOException e )
{
log.error( "Unable to load mime map " + resourceName + " : " + e.getMessage(), e );
}
- finally
- {
- IOUtils.closeQuietly( mimeStream );
- }
}
public void load( InputStream mimeStream )
// scan all directories in the namespace, and only include those that are known to be projects
List<String> projects = new ArrayList<>();
-
-
File[] files = dir.listFiles( new DirectoryFilter( filter ) );
if ( files != null )
{
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataRepositoryException;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.StringTokenizer;
+//import org.apache.commons.io.IOUtils;
+
public class FileMetadataRepository
implements MetadataRepository
{
}
@Override
- public <T>T obtainAccess( Class<T> aClass )
+ public <T> T obtainAccess( Class<T> aClass )
{
throw new IllegalArgumentException(
"Access using " + aClass + " is not supported on the file metadata storage" );
File directory = new File( getDirectory( artifactMetadata.getRepositoryId() ),
artifactMetadata.getNamespace() + "/" + artifactMetadata.getProject() + "/"
- + baseVersion );
+ + baseVersion
+ );
Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
throws IOException
{
Properties properties = new Properties();
- FileInputStream in = null;
- try
+ try (InputStream in = Files.newInputStream( new File( directory, propertiesKey + ".properties" ).toPath() ))
{
- in = new FileInputStream( new File( directory, propertiesKey + ".properties" ) );
+
properties.load( in );
}
- finally
- {
- IOUtils.closeQuietly( in );
- }
return properties;
}
throws IOException
{
directory.mkdirs();
- FileOutputStream os = new FileOutputStream( new File( directory, propertiesKey + ".properties" ) );
- try
+ try (OutputStream os = Files.newOutputStream( new File( directory, propertiesKey + ".properties" ).toPath() ))
{
properties.store( os, null );
}
- finally
- {
- IOUtils.closeQuietly( os );
- }
}
private static class ArtifactComparator