]> source.dussan.org Git - archiva.git/commitdiff
[MRM-462] simplify and synchronize access to repository configuration in the reposito...
authorBrett Porter <brett@apache.org>
Tue, 28 Aug 2007 06:49:02 +0000 (06:49 +0000)
committerBrett Porter <brett@apache.org>
Tue, 28 Aug 2007 06:49:02 +0000 (06:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@570335 13f79535-47bb-0310-9956-ffa450edef68

MRM-462/archiva-web/archiva-webapp/pom.xml
MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/repository/RepositoryServlet.java
MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/repository/RepositoryServletTest.java
MRM-462/archiva-web/archiva-webapp/src/test/resources/org/apache/maven/archiva/web/repository/RepositoryServletTest.xml

index 2e0c3f05133918a3148b6c0e63d27177d22990fe..4058fab667d2d55817555d22224b79fddb20138a 100644 (file)
       <version>1.6.2</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus.redback</groupId>
+      <artifactId>redback-keys-memory</artifactId>
+      <version>${redback.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus.redback</groupId>
+      <artifactId>redback-rbac-memory</artifactId>
+      <version>${redback.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus.redback</groupId>
+      <artifactId>redback-users-memory</artifactId>
+      <!-- TODO! when online next
+            <version>${redback.version}</version>
+      -->
+      <version>1.0-alpha-2-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
index 6030dacacd0b5224808bcb66aea897fc66e80a77..325e245393245a5723ca58e3901c063f4f85c657 100644 (file)
@@ -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<String, ManagedRepositoryConfiguration> repositoryMap =
-        new HashMap<String, ManagedRepositoryConfiguration>();
+    private Map<String, ManagedRepositoryConfiguration> 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;
             }
         }
     }
index 5ff9fc09432497c34bdeacbeeff2edc9492825c5..a5e4164c98f6bf23269841c2bf075c7f54c41d70 100644 (file)
@@ -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 );
     }
 }
index 403a063b82430391b88a0de557f013d8ad5f9bb1..6f2bbc5a48a779810ba32a83d5c41d789c42d68b 100644 (file)
       <role>org.codehaus.plexus.webdav.DavServerManager</role>
       <role-hint>default</role-hint>
       <implementation>org.codehaus.plexus.webdav.DefaultDavServerManager</implementation>
-      <description>DefaultDavServerManager</description>
       <configuration>
         <provider-hint>proxied</provider-hint>
       </configuration>
     </component>
 
+    <!-- TODO! shouldn't need so many components just to use in-memory - is flaky since these are auto-generated -->
     <component>
-      <role>org.codehaus.plexus.jdo.JdoFactory</role>
-      <role-hint>users</role-hint>
-      <implementation>org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory</implementation>
-      <configuration>
-
-        <!-- HSQLDB Configuration -->
-        <!--
-          NOTE: NO NOT USE THIS CONFIGURATION FOR A PRODUCTION SYSTEM.
-          HSQLDB keeps all data in memory at all times.
+      <role>org.codehaus.plexus.redback.system.SecuritySystem</role>
+      <role-hint>default</role-hint>
+      <implementation>org.codehaus.plexus.redback.system.DefaultSecuritySystem</implementation>
+      <requirements>
+        <requirement>
+          <role>org.codehaus.plexus.redback.authentication.AuthenticationManager</role>
+          <field-name>authnManager</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.authorization.Authorizer</role>
+          <role-hint>rbac</role-hint>
+          <field-name>authorizer</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.users.UserManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>userManager</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.keys.KeyManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>keyManager</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.policy.UserSecurityPolicy</role>
+          <field-name>policy</field-name>
+        </requirement>
+      </requirements>
+    </component>
 
-          NOTE: JPOX 1.1.1 won't create the tables on start
-          http://www.jpox.org/servlet/jira/browse/CORE-2946
-            -->
+    <component>
+      <role>org.codehaus.plexus.redback.authentication.Authenticator</role>
+      <role-hint>user-manager</role-hint>
+      <implementation>org.codehaus.plexus.redback.authentication.users.UserManagerAuthenticator</implementation>
+      <requirements>
+        <requirement>
+          <role>org.codehaus.plexus.redback.users.UserManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>userManager</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.policy.UserSecurityPolicy</role>
+          <field-name>securityPolicy</field-name>
+        </requirement>
+      </requirements>
+    </component>
 
-        <!-- 
-      <driverName>org.hsqldb.jdbcDriver</driverName>
-      <url>jdbc:hsqldb:mem:test</url>
-      <userName>sa</userName>
-      <password></password>
-        -->
+    <component>
+      <role>org.codehaus.plexus.redback.authentication.Authenticator</role>
+      <role-hint>keystore</role-hint>
+      <implementation>org.codehaus.plexus.redback.authentication.keystore.KeyStoreAuthenticator</implementation>
+      <requirements>
+        <requirement>
+          <role>org.codehaus.plexus.redback.keys.KeyManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>keystore</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.users.UserManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>userManager</field-name>
+        </requirement>
+      </requirements>
+    </component>
 
-        <!-- Apache Derby Configuration -->
-        <driverName>org.apache.derby.jdbc.EmbeddedDriver</driverName>
-        <url>jdbc:derby:${basedir}/target/appserver-base/database;create=true</url>
-        <userName>sa</userName>
-        <password></password>
+    <component>
+      <role>org.codehaus.plexus.redback.authorization.rbac.evaluator.PermissionEvaluator</role>
+      <role-hint>default</role-hint>
+      <implementation>org.codehaus.plexus.redback.authorization.rbac.evaluator.DefaultPermissionEvaluator
+      </implementation>
+      <requirements>
+        <requirement>
+          <role>org.codehaus.plexus.redback.users.UserManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>userManager</field-name>
+        </requirement>
+      </requirements>
+    </component>
 
-        <!-- MySql Configuration -->
-        <!-- 
-        <driverName>com.mysql.jdbc.Driver</driverName>
-        <url>jdbc:mysql://localhost/archiva</url>
-        <userName>archiva</userName>
-        <password>archiva</password>
-          -->
+    <component>
+      <role>org.codehaus.plexus.redback.authorization.Authorizer</role>
+      <role-hint>rbac</role-hint>
+      <implementation>org.codehaus.plexus.redback.authorization.rbac.RbacAuthorizer</implementation>
+      <requirements>
+        <requirement>
+          <role>org.codehaus.plexus.redback.rbac.RBACManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>manager</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.users.UserManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>userManager</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.authorization.rbac.evaluator.PermissionEvaluator</role>
+          <role-hint>default</role-hint>
+          <field-name>evaluator</field-name>
+        </requirement>
+      </requirements>
+    </component>
 
-        <!-- Postgresql Configuration -->
-        <!--
-        <driverName>org.postgresql.Driver</driverName>
-        <url>jdbc:postgresql://localhost/continuum</url>
-        <userName>username</userName>
-        <password></password>
-        -->
+    <component>
+      <role>org.codehaus.plexus.redback.role.RoleManager</role>
+      <role-hint>default</role-hint>
+      <implementation>org.codehaus.plexus.redback.role.DefaultRoleManager</implementation>
+      <instantiation-strategy>singleton</instantiation-strategy>
+      <requirements>
+        <requirement>
+          <role>org.codehaus.plexus.redback.role.merger.RoleModelMerger</role>
+          <role-hint>default</role-hint>
+          <field-name>modelMerger</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.role.validator.RoleModelValidator</role>
+          <role-hint>default</role-hint>
+          <field-name>modelValidator</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.role.processor.RoleModelProcessor</role>
+          <role-hint>default</role-hint>
+          <field-name>modelProcessor</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.role.template.RoleTemplateProcessor</role>
+          <role-hint>default</role-hint>
+          <field-name>templateProcessor</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.redback.rbac.RBACManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>rbacManager</field-name>
+        </requirement>
+        <requirement>
+          <role>org.codehaus.plexus.PlexusContainer</role>
+          <field-name>container</field-name>
+        </requirement>
+      </requirements>
+    </component>
 
-        <!-- JPOX and JDO configuration -->
-        <persistenceManagerFactoryClass>org.jpox.PersistenceManagerFactoryImpl</persistenceManagerFactoryClass>
-        <otherProperties>
-          <property>
-            <name>javax.jdo.PersistenceManagerFactoryClass</name>
-            <value>org.jpox.PersistenceManagerFactoryImpl</value>
-          </property>
-          <property>
-            <name>org.jpox.autoCreateSchema</name>
-            <value>true</value>
-          </property>
-          <property>
-            <name>org.jpox.autoStartMechanism</name>
-            <value>SchemaTable</value>
-          </property>
-          <property>
-            <name>org.jpox.autoStartMechanismMode</name>
-            <value>Ignored</value>
-          </property>
-          <property>
-            <name>org.jpox.validateTables</name>
-            <value>false</value>
-          </property>
-          <property>
-            <name>org.jpox.validateConstraints</name>
-            <value>false</value>
-          </property>
-          <property>
-            <name>org.jpox.transactionIsolation</name>
-            <value>READ_UNCOMMITTED</value>
-          </property>
-          <property>
-            <name>org.jpox.poid.transactionIsolation</name>
-            <value>READ_UNCOMMITTED</value>
-          </property>
-          <property>
-            <name>org.jpox.rdbms.dateTimezone</name>
-            <value>JDK_DEFAULT_TIMEZONE</value>
-          </property>
-        </otherProperties>
-      </configuration>
+    <component>
+      <role>org.codehaus.plexus.redback.role.processor.RoleModelProcessor</role>
+      <role-hint>default</role-hint>
+      <implementation>org.codehaus.plexus.redback.role.processor.DefaultRoleModelProcessor</implementation>
+      <requirements>
+        <requirement>
+          <role>org.codehaus.plexus.redback.rbac.RBACManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>rbacManager</field-name>
+        </requirement>
+      </requirements>
     </component>
 
+    <component>
+      <role>org.codehaus.plexus.redback.role.template.RoleTemplateProcessor</role>
+      <role-hint>default</role-hint>
+      <implementation>org.codehaus.plexus.redback.role.template.DefaultRoleTemplateProcessor</implementation>
+      <requirements>
+        <requirement>
+          <role>org.codehaus.plexus.redback.rbac.RBACManager</role>
+          <role-hint>memory</role-hint>
+          <field-name>rbacManager</field-name>
+        </requirement>
+      </requirements>
+    </component>
   </components>
 </plexus>