From: Brett Porter Date: Tue, 28 Aug 2007 06:49:02 +0000 (+0000) Subject: [MRM-462] simplify and synchronize access to repository configuration in the reposito... X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9eaa9af1d6b9f192a9a3026cd96e66731ee2d5c6;p=archiva.git [MRM-462] simplify and synchronize access to repository configuration in the repository servlet git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@570335 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/MRM-462/archiva-web/archiva-webapp/pom.xml b/MRM-462/archiva-web/archiva-webapp/pom.xml index 2e0c3f051..4058fab66 100644 --- a/MRM-462/archiva-web/archiva-webapp/pom.xml +++ b/MRM-462/archiva-web/archiva-webapp/pom.xml @@ -218,6 +218,27 @@ 1.6.2 test + + org.codehaus.plexus.redback + redback-keys-memory + ${redback.version} + test + + + org.codehaus.plexus.redback + redback-rbac-memory + ${redback.version} + test + + + org.codehaus.plexus.redback + redback-users-memory + + 1.0-alpha-2-SNAPSHOT + test + diff --git a/MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/RepositoryServlet.java b/MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/RepositoryServlet.java index 6030dacac..325e24539 100644 --- a/MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/RepositoryServlet.java +++ b/MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/RepositoryServlet.java @@ -46,9 +46,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; import java.util.Map; /** @@ -61,30 +58,17 @@ public class RepositoryServlet extends MultiplexedWebDavServlet implements RegistryListener { - /** - * @plexus.requirement - */ private SecuritySystem securitySystem; - /** - * @plexus.requirement role-hint="basic" - */ private HttpAuthenticator httpAuth; - /** - * @plexus.requirement - */ private AuditLog audit; - /** - * @plexus.requirement - */ private ArchivaConfiguration configuration; - private Map repositoryMap = - new HashMap(); + private Map repositoryMap; - public void initComponents() + public synchronized void initComponents() throws ServletException { super.initComponents(); @@ -96,18 +80,14 @@ public class RepositoryServlet configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName() ); configuration.addChangeListener( this ); - updateRepositoryMap(); + repositoryMap = configuration.getConfiguration().getManagedRepositoriesAsMap(); } - public void initServers( ServletConfig servletConfig ) + public synchronized void initServers( ServletConfig servletConfig ) throws DavServerException { - List repositories = configuration.getConfiguration().getManagedRepositories(); - Iterator itrepos = repositories.iterator(); - while ( itrepos.hasNext() ) + for ( ManagedRepositoryConfiguration repo : repositoryMap.values() ) { - ManagedRepositoryConfiguration repo = (ManagedRepositoryConfiguration) itrepos.next(); - File repoDir = new File( repo.getLocation() ); if ( !repoDir.exists() ) @@ -126,18 +106,18 @@ public class RepositoryServlet } } - public ManagedRepositoryConfiguration getRepository( DavServerRequest request ) + public synchronized ManagedRepositoryConfiguration getRepository( String prefix ) { - // TODO: use sync wrapper instead? - synchronized ( this.repositoryMap ) + if ( repositoryMap == null ) { - return repositoryMap.get( request.getPrefix() ); + repositoryMap = configuration.getConfiguration().getManagedRepositoriesAsMap(); } + return repositoryMap.get( prefix ); } - public String getRepositoryName( DavServerRequest request ) + private String getRepositoryName( DavServerRequest request ) { - ManagedRepositoryConfiguration repoConfig = getRepository( request ); + ManagedRepositoryConfiguration repoConfig = getRepository( request.getPrefix() ); if ( repoConfig == null ) { return "Unknown"; @@ -146,15 +126,6 @@ public class RepositoryServlet return repoConfig.getName(); } - private void updateRepositoryMap() - { - synchronized ( this.repositoryMap ) - { - this.repositoryMap.clear(); - this.repositoryMap.putAll( configuration.getConfiguration().getManagedRepositoriesAsMap() ); - } - } - public boolean isAuthenticated( DavServerRequest davRequest, HttpServletResponse response ) throws ServletException, IOException { @@ -210,8 +181,8 @@ public class RepositoryServlet permission = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD; } - AuthorizationResult authzResult = securitySystem.authorize( securitySession, permission, davRequest - .getPrefix() ); + AuthorizationResult authzResult = + securitySystem.authorize( securitySession, permission, davRequest.getPrefix() ); if ( !authzResult.isAuthorized() ) { @@ -241,29 +212,13 @@ public class RepositoryServlet // nothing to do } - public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + public synchronized void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) { if ( ConfigurationNames.isManagedRepositories( propertyName ) ) { - // Attempt to reduce the number of times we refresh the repository map. - if ( propertyName.endsWith( ".id" ) || propertyName.endsWith( ".url" ) ) + if ( propertyName.endsWith( ".id" ) || propertyName.endsWith( ".location" ) ) { - synchronized ( this.repositoryMap ) - { - updateRepositoryMap(); - - getDavManager().removeAllServers(); - - try - { - initServers( getServletConfig() ); - } - catch ( DavServerException e ) - { - log( "Error restarting WebDAV server after configuration change - service disabled: " + - e.getMessage(), e ); - } - } + repositoryMap = null; } } } diff --git a/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/repository/RepositoryServletTest.java b/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/repository/RepositoryServletTest.java index 5ff9fc094..a5e4164c9 100644 --- a/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/repository/RepositoryServletTest.java +++ b/MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/repository/RepositoryServletTest.java @@ -24,11 +24,17 @@ import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebResponse; import com.meterware.servletunit.ServletRunner; import com.meterware.servletunit.ServletUnitClient; +import org.apache.maven.archiva.configuration.ArchivaConfiguration; +import org.apache.maven.archiva.configuration.Configuration; +import org.apache.maven.archiva.configuration.IndeterminateConfigurationException; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.registry.RegistryException; import org.codehaus.plexus.util.FileUtils; import org.xml.sax.SAXException; +import javax.servlet.ServletException; import java.io.File; import java.io.IOException; @@ -37,16 +43,30 @@ public class RepositoryServletTest { private ServletUnitClient sc; - private String appserverBase; + private static final String REQUEST_PATH = "http://localhost/repository/internal/path/to/artifact.jar"; + + private File repositoryLocation; + + private ArchivaConfiguration configuration; + + private static final String REPOSITORY_ID = "internal"; + + private static final String NEW_REPOSITORY_ID = "new-id"; + + private static final String NEW_REPOSITORY_NAME = "New Repository"; protected void setUp() throws Exception { super.setUp(); - appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath(); + // TODO: purely to quiet logging - shouldn't be needed + String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath(); System.setProperty( "appserver.base", appserverBase ); - System.setProperty( "derby.system.home", appserverBase ); + + configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.ROLE ); + + repositoryLocation = new File( appserverBase, "data/repositories/internal" ); ServletRunner sr = new ServletRunner(); sr.registerServlet( "/repository/*", UnauthenticatedRepositoryServlet.class.getName() ); @@ -57,15 +77,69 @@ public class RepositoryServletTest public void testPutWithMissingParentCollection() throws IOException, SAXException { - File repository = new File( appserverBase, "data/repositories/internal" ); - FileUtils.deleteDirectory( repository ); + FileUtils.deleteDirectory( repositoryLocation ); - WebRequest request = new PutMethodWebRequest( "http://localhost/repository/internal/path/to/artifact.jar", - getClass().getResourceAsStream( "/artifact.jar" ), + WebRequest request = new PutMethodWebRequest( REQUEST_PATH, getClass().getResourceAsStream( "/artifact.jar" ), "application/octet-stream" ); WebResponse response = sc.getResponse( request ); assertNotNull( "No response received", response ); assertEquals( "file contents", "artifact.jar\n", - FileUtils.fileRead( new File( repository, "path/to/artifact.jar" ) ) ); + FileUtils.fileRead( new File( repositoryLocation, "path/to/artifact.jar" ) ) ); + } + + public void testGetRepository() + throws IOException, ServletException + { + RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet(); + assertNotNull( servlet ); + + ManagedRepositoryConfiguration repository = servlet.getRepository( REPOSITORY_ID ); + assertNotNull( repository ); + assertEquals( "Archiva Managed Internal Repository", repository.getName() ); + } + + public void testGetRepositoryAfterDelete() + throws IOException, ServletException, RegistryException, IndeterminateConfigurationException + { + RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet(); + assertNotNull( servlet ); + + Configuration c = configuration.getConfiguration(); + c.removeManagedRepository( c.findManagedRepositoryById( REPOSITORY_ID ) ); + // TODO! it would be better to use a mock configuration and "save" to more accurately reflect the calls made + triggerConfigurationChange( servlet, "managedRepositories.managedRepository(0).id", REPOSITORY_ID ); + + ManagedRepositoryConfiguration repository = servlet.getRepository( REPOSITORY_ID ); + assertNull( repository ); + } + + public void testGetRepositoryAfterAdd() + throws IOException, ServletException, RegistryException, IndeterminateConfigurationException + { + RepositoryServlet servlet = (RepositoryServlet) sc.newInvocation( REQUEST_PATH ).getServlet(); + assertNotNull( servlet ); + + Configuration c = configuration.getConfiguration(); + ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration(); + repo.setId( NEW_REPOSITORY_ID ); + repo.setName( NEW_REPOSITORY_NAME ); + c.addManagedRepository( repo ); + // TODO! it would be better to use a mock configuration and "save" to more accurately reflect the calls made + triggerConfigurationChange( servlet, "managedRepositories.managedRepository(2).id", NEW_REPOSITORY_ID ); + + ManagedRepositoryConfiguration repository = servlet.getRepository( NEW_REPOSITORY_ID ); + assertNotNull( repository ); + assertEquals( NEW_REPOSITORY_NAME, repository.getName() ); + + // check other is still intact + repository = servlet.getRepository( REPOSITORY_ID ); + assertNotNull( repository ); + assertEquals( "Archiva Managed Internal Repository", repository.getName() ); + } + + private void triggerConfigurationChange( RepositoryServlet servlet, String name, String value ) + { + servlet.beforeConfigurationChange( null, name, value ); + servlet.afterConfigurationChange( null, name, value ); } } diff --git a/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/repository/RepositoryServletTest.xml b/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/repository/RepositoryServletTest.xml index 403a063b8..6f2bbc5a4 100644 --- a/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/repository/RepositoryServletTest.xml +++ b/MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/repository/RepositoryServletTest.xml @@ -33,98 +33,177 @@ org.codehaus.plexus.webdav.DavServerManager default org.codehaus.plexus.webdav.DefaultDavServerManager - DefaultDavServerManager proxied + - org.codehaus.plexus.jdo.JdoFactory - users - org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory - - - - + + org.codehaus.plexus.redback.authentication.Authenticator + user-manager + org.codehaus.plexus.redback.authentication.users.UserManagerAuthenticator + + + org.codehaus.plexus.redback.users.UserManager + memory + userManager + + + org.codehaus.plexus.redback.policy.UserSecurityPolicy + securityPolicy + + + - + + org.codehaus.plexus.redback.authentication.Authenticator + keystore + org.codehaus.plexus.redback.authentication.keystore.KeyStoreAuthenticator + + + org.codehaus.plexus.redback.keys.KeyManager + memory + keystore + + + org.codehaus.plexus.redback.users.UserManager + memory + userManager + + + - - org.apache.derby.jdbc.EmbeddedDriver - jdbc:derby:${basedir}/target/appserver-base/database;create=true - sa - + + org.codehaus.plexus.redback.authorization.rbac.evaluator.PermissionEvaluator + default + org.codehaus.plexus.redback.authorization.rbac.evaluator.DefaultPermissionEvaluator + + + + org.codehaus.plexus.redback.users.UserManager + memory + userManager + + + - - + + org.codehaus.plexus.redback.authorization.Authorizer + rbac + org.codehaus.plexus.redback.authorization.rbac.RbacAuthorizer + + + org.codehaus.plexus.redback.rbac.RBACManager + memory + manager + + + org.codehaus.plexus.redback.users.UserManager + memory + userManager + + + org.codehaus.plexus.redback.authorization.rbac.evaluator.PermissionEvaluator + default + evaluator + + + - - + + org.codehaus.plexus.redback.role.RoleManager + default + org.codehaus.plexus.redback.role.DefaultRoleManager + singleton + + + org.codehaus.plexus.redback.role.merger.RoleModelMerger + default + modelMerger + + + org.codehaus.plexus.redback.role.validator.RoleModelValidator + default + modelValidator + + + org.codehaus.plexus.redback.role.processor.RoleModelProcessor + default + modelProcessor + + + org.codehaus.plexus.redback.role.template.RoleTemplateProcessor + default + templateProcessor + + + org.codehaus.plexus.redback.rbac.RBACManager + memory + rbacManager + + + org.codehaus.plexus.PlexusContainer + container + + + - - org.jpox.PersistenceManagerFactoryImpl - - - javax.jdo.PersistenceManagerFactoryClass - org.jpox.PersistenceManagerFactoryImpl - - - org.jpox.autoCreateSchema - true - - - org.jpox.autoStartMechanism - SchemaTable - - - org.jpox.autoStartMechanismMode - Ignored - - - org.jpox.validateTables - false - - - org.jpox.validateConstraints - false - - - org.jpox.transactionIsolation - READ_UNCOMMITTED - - - org.jpox.poid.transactionIsolation - READ_UNCOMMITTED - - - org.jpox.rdbms.dateTimezone - JDK_DEFAULT_TIMEZONE - - - + + org.codehaus.plexus.redback.role.processor.RoleModelProcessor + default + org.codehaus.plexus.redback.role.processor.DefaultRoleModelProcessor + + + org.codehaus.plexus.redback.rbac.RBACManager + memory + rbacManager + + + + org.codehaus.plexus.redback.role.template.RoleTemplateProcessor + default + org.codehaus.plexus.redback.role.template.DefaultRoleTemplateProcessor + + + org.codehaus.plexus.redback.rbac.RBACManager + memory + rbacManager + + +