mirror of https://github.com/apache/archiva.git
[MRM-1470] Ability to add a repository relative to Archiva's appserver.base without knowing it's filesystem location
* applied patch Submitted by: Maria Catherine Tan git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1097776 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6566bcaa5e
commit
d72ee0c68b
|
@ -20,11 +20,13 @@ package org.apache.maven.archiva.web.action.admin.repositories;
|
|||
*/
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.maven.archiva.configuration.Configuration;
|
||||
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
|
||||
import org.apache.maven.archiva.security.ArchivaRoleConstants;
|
||||
import org.codehaus.plexus.redback.role.RoleManager;
|
||||
import org.codehaus.plexus.redback.role.RoleManagerException;
|
||||
import org.codehaus.plexus.registry.Registry;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
@ -44,6 +46,13 @@ public abstract class AbstractManagedRepositoriesAction
|
|||
*/
|
||||
protected RoleManager roleManager;
|
||||
|
||||
/**
|
||||
* Plexus registry to read the configuration from.
|
||||
*
|
||||
* @plexus.requirement role-hint="commons-configuration"
|
||||
*/
|
||||
private Registry registry;
|
||||
|
||||
public static final String CONFIRM = "confirm";
|
||||
|
||||
public RoleManager getRoleManager()
|
||||
|
@ -56,6 +65,11 @@ public abstract class AbstractManagedRepositoriesAction
|
|||
this.roleManager = roleManager;
|
||||
}
|
||||
|
||||
public void setRegistry( Registry registry )
|
||||
{
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
|
||||
throws IOException
|
||||
{
|
||||
|
@ -129,4 +143,13 @@ public abstract class AbstractManagedRepositoriesAction
|
|||
|
||||
log.debug( "removed user roles associated with repository " + repoId );
|
||||
}
|
||||
|
||||
protected String removeExpressions( String directory )
|
||||
{
|
||||
String value = StringUtils.replace( directory, "${appserver.base}", registry.getString( "appserver.base",
|
||||
"${appserver.base}" ) );
|
||||
value = StringUtils.replace( value, "${appserver.home}", registry.getString( "appserver.home",
|
||||
"${appserver.home}" ) );
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,6 +74,8 @@ public class AddManagedRepositoryAction
|
|||
|
||||
public String commit()
|
||||
{
|
||||
repository.setLocation( removeExpressions( repository.getLocation() ) );
|
||||
|
||||
File location = new File( repository.getLocation() );
|
||||
if( location.exists() )
|
||||
{
|
||||
|
|
|
@ -100,6 +100,8 @@ public class EditManagedRepositoryAction
|
|||
boolean resetStats = false;
|
||||
|
||||
// check if the location was changed
|
||||
repository.setLocation( removeExpressions( repository.getLocation() ) );
|
||||
|
||||
if ( !StringUtils.equalsIgnoreCase( existingConfig.getLocation().trim(), repository.getLocation().trim() ) )
|
||||
{
|
||||
resetStats = true;
|
||||
|
|
|
@ -60,6 +60,7 @@
|
|||
<constructor-arg ref="repositoryStatisticsManager"/>
|
||||
<constructor-arg ref="repositoryMerger#maven2"/>
|
||||
<constructor-arg ref="auditListener#logging"/>
|
||||
<constructor-arg ref="registry#commons-configuration"/>
|
||||
|
||||
</bean>
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
|
|||
import org.apache.maven.archiva.security.ArchivaRoleConstants;
|
||||
import org.apache.maven.archiva.web.action.AbstractActionTestCase;
|
||||
import org.codehaus.plexus.redback.role.RoleManager;
|
||||
import org.codehaus.plexus.registry.Registry;
|
||||
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
|
||||
import org.codehaus.redback.integration.interceptor.SecureActionBundle;
|
||||
import org.codehaus.redback.integration.interceptor.SecureActionException;
|
||||
|
@ -51,6 +52,10 @@ public class AddManagedRepositoryActionTest
|
|||
|
||||
private MockControl archivaConfigurationControl;
|
||||
|
||||
private Registry registry;
|
||||
|
||||
private MockControl registryControl;
|
||||
|
||||
private ArchivaConfiguration archivaConfiguration;
|
||||
|
||||
private static final String REPO_ID = "repo-ident";
|
||||
|
@ -72,6 +77,11 @@ public class AddManagedRepositoryActionTest
|
|||
roleManagerControl = MockControl.createControl( RoleManager.class );
|
||||
roleManager = (RoleManager) roleManagerControl.getMock();
|
||||
action.setRoleManager( roleManager );
|
||||
|
||||
registryControl = MockControl.createControl( Registry.class );
|
||||
registry = (Registry) registryControl.getMock();
|
||||
action.setRegistry( registry );
|
||||
|
||||
location = getTestFile( "target/test/location" );
|
||||
}
|
||||
|
||||
|
@ -131,6 +141,13 @@ public class AddManagedRepositoryActionTest
|
|||
|
||||
roleManagerControl.replay();
|
||||
|
||||
registry.getString( "appserver.base", "${appserver.base}" );
|
||||
registryControl.setReturnValue( "target/test" );
|
||||
registry.getString( "appserver.home", "${appserver.home}" );
|
||||
registryControl.setReturnValue( "target/test" );
|
||||
|
||||
registryControl.replay();
|
||||
|
||||
Configuration configuration = new Configuration();
|
||||
archivaConfiguration.getConfiguration();
|
||||
archivaConfigurationControl.setReturnValue( configuration );
|
||||
|
@ -148,9 +165,11 @@ public class AddManagedRepositoryActionTest
|
|||
assertEquals( Action.SUCCESS, status );
|
||||
assertTrue( location.exists() );
|
||||
assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
|
||||
assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
|
||||
|
||||
roleManagerControl.verify();
|
||||
archivaConfigurationControl.verify();
|
||||
registryControl.verify();
|
||||
}
|
||||
|
||||
|
||||
|
@ -162,6 +181,13 @@ public class AddManagedRepositoryActionTest
|
|||
location.mkdirs();
|
||||
}
|
||||
|
||||
registry.getString( "appserver.base", "${appserver.base}" );
|
||||
registryControl.setReturnValue( "target/test" );
|
||||
registry.getString( "appserver.home", "${appserver.home}" );
|
||||
registryControl.setReturnValue( "target/test" );
|
||||
|
||||
registryControl.replay();
|
||||
|
||||
action.prepare();
|
||||
ManagedRepositoryConfiguration repository = action.getRepository();
|
||||
populateRepository( repository );
|
||||
|
@ -169,13 +195,15 @@ public class AddManagedRepositoryActionTest
|
|||
assertTrue( location.exists() );
|
||||
String status = action.commit();
|
||||
assertEquals( AddManagedRepositoryAction.CONFIRM, status );
|
||||
assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
|
||||
registryControl.verify();
|
||||
}
|
||||
|
||||
private void populateRepository( ManagedRepositoryConfiguration repository )
|
||||
{
|
||||
repository.setId( REPO_ID );
|
||||
repository.setName( "repo name" );
|
||||
repository.setLocation( location.getAbsolutePath() );
|
||||
repository.setLocation( "${appserver.base}/location" );
|
||||
repository.setLayout( "default" );
|
||||
repository.setRefreshCronExpression( "* 0/5 * * * ?" );
|
||||
repository.setDaysOlder( 31 );
|
||||
|
|
|
@ -32,6 +32,7 @@ import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
|
|||
import org.apache.maven.archiva.security.ArchivaRoleConstants;
|
||||
import org.apache.maven.archiva.web.action.AbstractActionTestCase;
|
||||
import org.codehaus.plexus.redback.role.RoleManager;
|
||||
import org.codehaus.plexus.registry.Registry;
|
||||
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
|
||||
import org.codehaus.redback.integration.interceptor.SecureActionBundle;
|
||||
import org.codehaus.redback.integration.interceptor.SecureActionException;
|
||||
|
@ -62,6 +63,10 @@ public class EditManagedRepositoryActionTest
|
|||
|
||||
private ArchivaConfiguration archivaConfiguration;
|
||||
|
||||
private Registry registry;
|
||||
|
||||
private MockControl registryControl;
|
||||
|
||||
private static final String REPO_ID = "repo-ident";
|
||||
|
||||
private File location;
|
||||
|
@ -82,6 +87,11 @@ public class EditManagedRepositoryActionTest
|
|||
roleManagerControl = MockControl.createControl( RoleManager.class );
|
||||
roleManager = (RoleManager) roleManagerControl.getMock();
|
||||
action.setRoleManager( roleManager );
|
||||
|
||||
registryControl = MockControl.createControl( Registry.class );
|
||||
registry = (Registry) registryControl.getMock();
|
||||
action.setRegistry( registry );
|
||||
|
||||
location = getTestFile( "target/test/location" );
|
||||
|
||||
metadataRepository = mock( MetadataRepository.class );
|
||||
|
@ -124,7 +134,10 @@ public class EditManagedRepositoryActionTest
|
|||
assertEquals( REPO_ID, action.getRepoid() );
|
||||
ManagedRepositoryConfiguration repository = action.getRepository();
|
||||
assertNotNull( repository );
|
||||
assertRepositoryEquals( repository, createRepository() );
|
||||
|
||||
ManagedRepositoryConfiguration newRepository = createRepository();
|
||||
assertRepositoryEquals( repository, newRepository );
|
||||
assertEquals( repository.getLocation(), newRepository.getLocation() );
|
||||
|
||||
String status = action.input();
|
||||
assertEquals( Action.INPUT, status );
|
||||
|
@ -154,6 +167,13 @@ public class EditManagedRepositoryActionTest
|
|||
|
||||
roleManagerControl.replay();
|
||||
|
||||
registry.getString( "appserver.base", "${appserver.base}" );
|
||||
registryControl.setReturnValue( "target/test" );
|
||||
registry.getString( "appserver.home", "${appserver.home}" );
|
||||
registryControl.setReturnValue( "target/test" );
|
||||
|
||||
registryControl.replay();
|
||||
|
||||
Configuration configuration = createConfigurationForEditing( createRepository() );
|
||||
archivaConfiguration.getConfiguration();
|
||||
archivaConfigurationControl.setReturnValue( configuration );
|
||||
|
@ -191,10 +211,12 @@ public class EditManagedRepositoryActionTest
|
|||
newRepository.setName( "new repo name" );
|
||||
assertRepositoryEquals( repository, newRepository );
|
||||
assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
|
||||
assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
|
||||
|
||||
roleManagerControl.verify();
|
||||
archivaConfigurationControl.verify();
|
||||
repositoryStatisticsManagerControl.verify();
|
||||
registryControl.verify();
|
||||
}
|
||||
|
||||
public void testEditRepositoryLocationChanged()
|
||||
|
@ -219,6 +241,13 @@ public class EditManagedRepositoryActionTest
|
|||
|
||||
roleManagerControl.replay();
|
||||
|
||||
registry.getString( "appserver.base", "${appserver.base}" );
|
||||
registryControl.setReturnValue( "target/test" );
|
||||
registry.getString( "appserver.home", "${appserver.home}" );
|
||||
registryControl.setReturnValue( "target/test" );
|
||||
|
||||
registryControl.replay();
|
||||
|
||||
Configuration configuration = createConfigurationForEditing( createRepository() );
|
||||
archivaConfiguration.getConfiguration();
|
||||
archivaConfigurationControl.setReturnValue( configuration );
|
||||
|
@ -250,15 +279,17 @@ public class EditManagedRepositoryActionTest
|
|||
populateRepository( repository );
|
||||
File testFile = getTestFile( "target/test/location/new" );
|
||||
FileUtils.deleteDirectory( testFile );
|
||||
repository.setLocation( testFile.getCanonicalPath() );
|
||||
repository.setLocation( "${appserver.base}/location/new" );
|
||||
action.setRepository( repository );
|
||||
String status = action.commit();
|
||||
assertEquals( Action.SUCCESS, status );
|
||||
assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
|
||||
assertEquals( testFile.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
|
||||
|
||||
roleManagerControl.verify();
|
||||
archivaConfigurationControl.verify();
|
||||
repositoryStatisticsManagerControl.verify();
|
||||
registryControl.verify();
|
||||
}
|
||||
|
||||
private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
|
||||
|
@ -268,7 +299,6 @@ public class EditManagedRepositoryActionTest
|
|||
assertEquals( expectedRepository.getId(), actualRepository.getId() );
|
||||
assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
|
||||
assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
|
||||
assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
|
||||
assertEquals( expectedRepository.getName(), actualRepository.getName() );
|
||||
assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
|
||||
assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
|
||||
|
@ -310,7 +340,7 @@ public class EditManagedRepositoryActionTest
|
|||
{
|
||||
repository.setId( REPO_ID );
|
||||
repository.setName( "repo name" );
|
||||
repository.setLocation( location.getCanonicalPath() );
|
||||
repository.setLocation( "${appserver.base}/location" );
|
||||
repository.setLayout( "default" );
|
||||
repository.setRefreshCronExpression( "* 0/5 * * * ?" );
|
||||
repository.setDaysOlder( 31 );
|
||||
|
@ -326,7 +356,7 @@ public class EditManagedRepositoryActionTest
|
|||
{
|
||||
repository.setId( REPO_ID + "-stage" );
|
||||
repository.setName( "repo name" );
|
||||
repository.setLocation( location.getCanonicalPath() );
|
||||
repository.setLocation( "${appserver.base}/location" );
|
||||
repository.setLayout( "default" );
|
||||
repository.setRefreshCronExpression( "* 0/5 * * * ?" );
|
||||
repository.setDaysOlder( 31 );
|
||||
|
|
|
@ -53,6 +53,7 @@ import org.apache.maven.archiva.repository.ManagedRepositoryContent;
|
|||
import org.apache.maven.archiva.repository.RepositoryContentFactory;
|
||||
import org.apache.maven.archiva.repository.RepositoryException;
|
||||
import org.apache.maven.archiva.repository.RepositoryNotFoundException;
|
||||
import org.codehaus.plexus.registry.Registry;
|
||||
import org.codehaus.plexus.registry.RegistryException;
|
||||
import org.codehaus.plexus.scheduler.CronExpressionValidator;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -96,13 +97,16 @@ public class AdministrationServiceImpl
|
|||
|
||||
private RepositorySessionFactory repositorySessionFactory;
|
||||
|
||||
private Registry registry;
|
||||
|
||||
public AdministrationServiceImpl( ArchivaConfiguration archivaConfig, RepositoryContentConsumers repoConsumersUtil,
|
||||
RepositoryContentFactory repoFactory,
|
||||
RepositorySessionFactory repositorySessionFactory,
|
||||
RepositoryArchivaTaskScheduler repositoryTaskScheduler,
|
||||
Collection<RepositoryListener> listeners,
|
||||
RepositoryStatisticsManager repositoryStatisticsManager,
|
||||
RepositoryMerger repositoryMerger, AuditListener auditListener )
|
||||
RepositoryMerger repositoryMerger, AuditListener auditListener,
|
||||
Registry registry )
|
||||
{
|
||||
this.archivaConfiguration = archivaConfig;
|
||||
this.repoConsumersUtil = repoConsumersUtil;
|
||||
|
@ -113,6 +117,7 @@ public class AdministrationServiceImpl
|
|||
this.repositoryStatisticsManager = repositoryStatisticsManager;
|
||||
this.repositoryMerger = repositoryMerger;
|
||||
this.auditListener = auditListener;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -389,7 +394,7 @@ public class AdministrationServiceImpl
|
|||
repository.setReleases( releasesIncluded );
|
||||
repository.setSnapshots( snapshotsIncluded );
|
||||
repository.setName( name );
|
||||
repository.setLocation( location );
|
||||
repository.setLocation( removeExpressions( location ) );
|
||||
repository.setLayout( layout );
|
||||
repository.setRefreshCronExpression( cronExpression );
|
||||
|
||||
|
@ -730,4 +735,13 @@ public class AdministrationServiceImpl
|
|||
stagingRepository.setSnapshots( repository.isSnapshots() );
|
||||
return stagingRepository;
|
||||
}
|
||||
|
||||
private String removeExpressions( String directory )
|
||||
{
|
||||
String value = StringUtils.replace( directory, "${appserver.base}", registry.getString( "appserver.base",
|
||||
"${appserver.base}" ) );
|
||||
value = StringUtils.replace( value, "${appserver.home}", registry.getString( "appserver.home",
|
||||
"${appserver.home}" ) );
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryConte
|
|||
import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
|
||||
import org.apache.maven.archiva.repository.content.PathParser;
|
||||
import org.apache.maven.archiva.repository.layout.LayoutException;
|
||||
import org.codehaus.plexus.registry.Registry;
|
||||
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
|
||||
import org.easymock.MockControl;
|
||||
import org.easymock.classextension.MockClassControl;
|
||||
|
@ -132,6 +133,10 @@ public class AdministrationServiceImplTest
|
|||
|
||||
private AuditListener auditListener;
|
||||
|
||||
private MockControl registryControl;
|
||||
|
||||
private Registry registry;
|
||||
|
||||
private static final String STAGE = "-stage";
|
||||
|
||||
protected void setUp()
|
||||
|
@ -185,10 +190,13 @@ public class AdministrationServiceImplTest
|
|||
auditListenerControl = MockControl.createControl( AuditListener.class );
|
||||
auditListener = (AuditListener) auditListenerControl.getMock();
|
||||
|
||||
registryControl = MockControl.createControl( Registry.class );
|
||||
registry = (Registry) registryControl.getMock();
|
||||
|
||||
service = new AdministrationServiceImpl( archivaConfig, repoConsumersUtil, repositoryFactory,
|
||||
repositorySessionFactory, repositoryTaskScheduler,
|
||||
Collections.singletonList( listener ), repositoryStatisticsManager,
|
||||
repositoryMerger, auditListener );
|
||||
repositoryMerger, auditListener, registry );
|
||||
}
|
||||
|
||||
/* Tests for repository consumers */
|
||||
|
@ -897,6 +905,7 @@ public class AdministrationServiceImplTest
|
|||
String name = projId + " Releases";
|
||||
String releaseLocation = "target/test-repository/" + projId + ".releases";
|
||||
String stageLocation = releaseLocation + "-stage";
|
||||
String appserverBase = "target";
|
||||
|
||||
ManagedRepositoryConfiguration managedRepo = createManagedRepo( "repo1", "default", "repo", true, false );
|
||||
RemoteRepositoryConfiguration remoteRepo = createRemoteRepository( "central", "Central Repository", "default",
|
||||
|
@ -917,6 +926,8 @@ public class AdministrationServiceImplTest
|
|||
configControl.expectAndReturn( config.getManagedRepositoriesAsMap(), managedRepoMap );
|
||||
configControl.expectAndReturn( config.getRemoteRepositoriesAsMap(), remoteRepoMap );
|
||||
configControl.expectAndReturn( config.getRepositoryGroupsAsMap(), repoGroupMap );
|
||||
registryControl.expectAndReturn( registry.getString( "appserver.base", "${appserver.base}" ), appserverBase );
|
||||
registryControl.expectAndReturn( registry.getString( "appserver.home", "${appserver.home}" ), appserverBase );
|
||||
config.addManagedRepository( managedRepo );
|
||||
configControl.setMatcher( MockControl.ALWAYS_MATCHER );
|
||||
configControl.setVoidCallable();
|
||||
|
@ -928,15 +939,18 @@ public class AdministrationServiceImplTest
|
|||
|
||||
archivaConfigControl.replay();
|
||||
configControl.replay();
|
||||
registryControl.replay();
|
||||
assertFalse( new File( releaseLocation ).isDirectory() );
|
||||
assertFalse( new File( stageLocation ).isDirectory() );
|
||||
boolean success = service.addManagedRepository( repoId, layout, name, releaseLocation, true, true, false, true,
|
||||
boolean success = service.addManagedRepository( repoId, layout, name, "${appserver.base}/test-repository/" + projId + ".releases", true, true, false, true,
|
||||
"0 15 3 * * ? *" );
|
||||
assertTrue( success );
|
||||
assertTrue( new File( releaseLocation ).isDirectory() );
|
||||
assertTrue( new File( stageLocation ).isDirectory() );
|
||||
new File( releaseLocation ).delete();
|
||||
new File( stageLocation ).delete();
|
||||
|
||||
registryControl.verify();
|
||||
}
|
||||
|
||||
/* private methods */
|
||||
|
|
Loading…
Reference in New Issue