Switching redback to java.nio

This commit is contained in:
Martin Stockhammer 2017-09-21 18:50:40 +02:00
parent e7b05cfc28
commit 56febc67a9
7 changed files with 59 additions and 59 deletions

View File

@ -19,12 +19,12 @@ package org.apache.archiva.redback.configuration;
* under the License.
*/
import org.apache.archiva.redback.components.registry.Registry;
import org.apache.archiva.redback.components.registry.RegistryException;
import org.apache.archiva.redback.components.evaluator.DefaultExpressionEvaluator;
import org.apache.archiva.redback.components.evaluator.EvaluatorException;
import org.apache.archiva.redback.components.evaluator.ExpressionEvaluator;
import org.apache.archiva.redback.components.evaluator.sources.SystemPropertyExpressionSource;
import org.apache.archiva.redback.components.registry.Registry;
import org.apache.archiva.redback.components.registry.RegistryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@ -32,7 +32,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
@ -133,7 +133,7 @@ public class DefaultUserConfiguration
}
log.info( "Attempting to find configuration [{}] (resolved to [{}])", configName, configName );
registry.addConfigurationFromFile( new File( configName ), PREFIX );
registry.addConfigurationFromFile(Paths.get(configName).toFile(), PREFIX );
}
}
}

View File

@ -26,8 +26,8 @@ import org.apache.archiva.redback.rbac.RbacManagerException;
import org.apache.archiva.redback.users.UserManagerException;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
/**
* Data management tool API.
@ -35,22 +35,22 @@ import java.io.IOException;
public interface DataManagementTool
{
void backupRBACDatabase( RBACManager manager, File backupDirectory )
void backupRBACDatabase( RBACManager manager, Path backupDirectory )
throws RbacManagerException, IOException, XMLStreamException;
void backupUserDatabase( UserManager manager, File backupDirectory )
void backupUserDatabase( UserManager manager, Path backupDirectory )
throws IOException, XMLStreamException, UserManagerException;
void backupKeyDatabase( KeyManager manager, File backupDirectory )
void backupKeyDatabase( KeyManager manager, Path backupDirectory )
throws IOException, XMLStreamException;
void restoreRBACDatabase( RBACManager manager, File backupDirectory )
void restoreRBACDatabase( RBACManager manager, Path backupDirectory )
throws IOException, XMLStreamException, RbacManagerException;
void restoreUsersDatabase( UserManager manager, File backupDirectory )
void restoreUsersDatabase( UserManager manager, Path backupDirectory )
throws IOException, XMLStreamException, UserManagerException;
void restoreKeysDatabase( KeyManager manager, File backupDirectory )
void restoreKeysDatabase( KeyManager manager, Path backupDirectory )
throws IOException, XMLStreamException;
void eraseRBACDatabase( RBACManager manager );

View File

@ -23,11 +23,7 @@ import junit.framework.TestCase;
import org.apache.archiva.redback.keys.AuthenticationKey;
import org.apache.archiva.redback.keys.KeyManager;
import org.apache.archiva.redback.keys.KeyManagerException;
import org.apache.archiva.redback.rbac.Permission;
import org.apache.archiva.redback.rbac.RBACManager;
import org.apache.archiva.redback.rbac.RbacManagerException;
import org.apache.archiva.redback.rbac.Role;
import org.apache.archiva.redback.rbac.UserAssignment;
import org.apache.archiva.redback.rbac.*;
import org.apache.archiva.redback.tests.utils.RBACDefaults;
import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.users.UserManager;
@ -45,11 +41,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
@ -67,7 +63,7 @@ public class DataManagementTest
@Inject
private DataManagementTool dataManagementTool;
private File targetDirectory;
private Path targetDirectory;
@Inject
@Named(value = "userManager#jpa")
@ -128,16 +124,16 @@ public class DataManagementTest
dataManagementTool.backupRBACDatabase( manager, targetDirectory );
File backupFile = new File( targetDirectory, "rbac.xml" );
Path backupFile = targetDirectory.resolve( "rbac.xml" );
assertTrue( "Check database exists", backupFile.exists() );
assertTrue( "Check database exists", Files.exists(backupFile) );
StringWriter sw = new StringWriter();
IOUtils.copy( getClass().getResourceAsStream( "/expected-rbac.xml" ), sw );
XMLAssert.assertXMLEqual( new StringReader( sw.toString() ),
new StringReader( FileUtils.readFileToString( backupFile ) ) );
new StringReader( FileUtils.readFileToString( backupFile.toFile() ) ) );
}
@ -167,15 +163,15 @@ public class DataManagementTest
dataManagementTool.backupUserDatabase( manager, targetDirectory );
File backupFile = new File( targetDirectory, "users.xml" );
Path backupFile = targetDirectory.resolve( "users.xml" );
assertTrue( "Check database exists", backupFile.exists() );
assertTrue( "Check database exists", Files.exists(backupFile) );
StringWriter sw = new StringWriter();
IOUtils.copy( getClass().getResourceAsStream( "/expected-users.xml" ), sw );
String actual = FileUtils.readFileToString( backupFile ).trim();
String actual = FileUtils.readFileToString( backupFile.toFile() ).trim();
String expected = sw.toString().trim();
XMLAssert.assertXMLEqual( removeTimestampVariance( expected ), removeTimestampVariance( actual ) );
@ -210,15 +206,15 @@ public class DataManagementTest
dataManagementTool.backupKeyDatabase( manager, targetDirectory );
File backupFile = new File( targetDirectory, "keys.xml" );
Path backupFile = targetDirectory.resolve( "keys.xml" );
assertTrue( "Check database exists", backupFile.exists() );
assertTrue( "Check database exists", Files.exists(backupFile) );
StringWriter sw = new StringWriter();
IOUtils.copy( getClass().getResourceAsStream( "/expected-keys.xml" ), sw );
String actual = FileUtils.readFileToString( backupFile ).trim();
String actual = FileUtils.readFileToString( backupFile.toFile() ).trim();
String expected = sw.toString().trim();
XMLAssert.assertXMLEqual( removeKeyAndTimestampVariance( expected ), removeKeyAndTimestampVariance( actual ) );
@ -244,11 +240,11 @@ public class DataManagementTest
assertEmpty( manager );
File backupFile = new File( targetDirectory, "rbac.xml" );
Path backupFile = targetDirectory.resolve("rbac.xml" );
InputStream is = getClass().getResourceAsStream( "/expected-rbac.xml" );
FileWriter fw = new FileWriter( backupFile );
BufferedWriter fw = Files.newBufferedWriter(backupFile, StandardOpenOption.CREATE_NEW);
IOUtils.copy( is, fw );
@ -331,9 +327,9 @@ public class DataManagementTest
assertEmpty( manager );
File backupFile = new File( targetDirectory, "users.xml" );
Path backupFile = targetDirectory.resolve( "users.xml" );
FileWriter fw = new FileWriter( backupFile );
BufferedWriter fw = Files.newBufferedWriter(backupFile, StandardOpenOption.CREATE_NEW );
IOUtils.copy( getClass().getResourceAsStream( "/expected-users.xml" ), fw );
@ -389,9 +385,9 @@ public class DataManagementTest
assertEmpty( manager );
File backupFile = new File( targetDirectory, "keys.xml" );
Path backupFile = targetDirectory.resolve( "keys.xml" );
FileWriter fw = new FileWriter( backupFile );
BufferedWriter fw = Files.newBufferedWriter( backupFile, StandardOpenOption.CREATE_NEW );
IOUtils.copy( getClass().getResourceAsStream( "/expected-keys.xml" ), fw );
@ -451,12 +447,11 @@ public class DataManagementTest
assertEquals( resource, permission.getResource().getIdentifier() );
}
private static File createBackupDirectory()
{
private static Path createBackupDirectory() throws IOException {
String timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US ).format( new Date() );
File targetDirectory = new File( SystemUtils.getJavaIoTmpDir(), "./target/backups/" + timestamp );
targetDirectory.mkdirs();
Path targetDirectory = Paths.get( SystemUtils.getJavaIoTmpDir().toString(), "./target/backups/" + timestamp );
Files.createDirectories(targetDirectory);
return targetDirectory;
}

View File

@ -40,8 +40,10 @@ import org.springframework.stereotype.Service;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.Properties;
@ -113,15 +115,15 @@ public class AdminAutoCreateCheck
log.info( "{} system props is empty don't use an auto creation admin ", FORCE_ADMIN_FILE_PATH );
return;
}
File file = new File( forceAdminFilePath );
if ( !file.exists() )
Path file = Paths.get( forceAdminFilePath );
if ( !Files.exists(file) )
{
log.warn( "file set in sysprops {} not exists skip admin auto creation", FORCE_ADMIN_FILE_PATH );
return;
}
log.debug( "user {} not found try auto creation", getAdminUid() );
Properties properties = new Properties();
FileInputStream fis = new FileInputStream( file );
InputStream fis = Files.newInputStream(file);
try
{
properties.load( fis );

View File

@ -36,8 +36,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import javax.inject.Named;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* RoleProfileTest:
@ -89,13 +90,13 @@ public class RoleModelProcessorTest
private RedbackRoleModel getModelFromFile( String file )
throws IOException, XMLStreamException
{
File resource = new File( getBasedir() + file );
Path resource = Paths.get( getBasedir() + file );
assertNotNull( resource );
RedbackRoleModelStaxReader modelReader = new RedbackRoleModelStaxReader();
RedbackRoleModel redback = modelReader.read( resource.getAbsolutePath() );
RedbackRoleModel redback = modelReader.read( resource.toAbsolutePath().toString() );
assertNotNull( redback );
return redback;

View File

@ -35,7 +35,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* RoleProfileTest:
@ -77,13 +78,13 @@ public class RoleTemplateProcessorTest
public void testLoading()
throws Exception
{
File resource = new File( getBasedir() + "/src/test/template-tests/redback-1.xml" );
Path resource = Paths.get(getBasedir() + "/src/test/template-tests/redback-1.xml");
assertNotNull( resource );
RedbackRoleModelStaxReader modelReader = new RedbackRoleModelStaxReader();
RedbackRoleModel redback = modelReader.read( resource.getAbsolutePath() );
RedbackRoleModel redback = modelReader.read( resource.toAbsolutePath().toString() );
assertNotNull( redback );

View File

@ -29,7 +29,8 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
/**
@ -71,13 +72,13 @@ public class RoleModelValidatorTest
@Test
public void testGood() throws Exception
{
File resource = new File( getBasedir() + "/src/test/validation-tests/redback-good.xml");
Path resource = Paths.get(getBasedir() + "/src/test/validation-tests/redback-good.xml");
assertNotNull( resource );
RedbackRoleModelStaxReader modelReader = new RedbackRoleModelStaxReader();
RedbackRoleModel redback = modelReader.read( resource.getAbsolutePath() );
RedbackRoleModel redback = modelReader.read( resource.toAbsolutePath().toString() );
assertNotNull( redback );
@ -89,13 +90,13 @@ public class RoleModelValidatorTest
@Test
public void testBad() throws Exception
{
File resource = new File( getBasedir() + "/src/test/validation-tests/redback-bad.xml");
Path resource = Paths.get( getBasedir() + "/src/test/validation-tests/redback-bad.xml");
assertNotNull( resource );
RedbackRoleModelStaxReader modelReader = new RedbackRoleModelStaxReader();
RedbackRoleModel redback = modelReader.read( resource.getAbsolutePath() );
RedbackRoleModel redback = modelReader.read( resource.toAbsolutePath().toString() );
assertNotNull( redback );
@ -120,13 +121,13 @@ public class RoleModelValidatorTest
@Test
public void testCore() throws Exception
{
File resource = new File( getBasedir() + "/src/test/validation-tests/redback-core.xml");
Path resource = Paths.get( getBasedir() + "/src/test/validation-tests/redback-core.xml");
assertNotNull( resource );
RedbackRoleModelStaxReader modelReader = new RedbackRoleModelStaxReader();
RedbackRoleModel redback = modelReader.read( resource.getAbsolutePath() );
RedbackRoleModel redback = modelReader.read( resource.toAbsolutePath().toString() );
assertNotNull( redback );