]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1327] hook save()/close()/revert() methods to the repository implementation
authorBrett Porter <brett@apache.org>
Wed, 29 Dec 2010 13:02:59 +0000 (13:02 +0000)
committerBrett Porter <brett@apache.org>
Wed, 29 Dec 2010 13:02:59 +0000 (13:02 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1053630 13f79535-47bb-0310-9956-ffa450edef68

14 files changed:
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java
archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/ArchivaRepositoryScanningTaskExecutorTest.java
archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java
archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java
archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java
archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java
archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySession.java
archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java
archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java
archiva-modules/plugins/metadata-store-jcr/pom.xml
archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java
archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrRepositorySessionFactory.java [new file with mode: 0644]
archiva-modules/plugins/metadata-store-jcr/src/test/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepositoryTest.java
archiva-modules/plugins/metadata-store-jcr/src/test/repository.xml

index 3c9756f885252c0e48c4cba13a4f1c793067f2fd..f100aa191b07bbe1f8154663f0533cd16b4d17ac 100644 (file)
@@ -30,6 +30,7 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 
+// TODO: remove, it does nothing
 public class TestMetadataRepository
     implements MetadataRepository
 {
@@ -160,6 +161,21 @@ public class TestMetadataRepository
         return null;  //To change body of implemented methods use File | Settings | File Templates.
     }
 
+    public void save()
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    public void close()
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    public void revert()
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
+
     public List<ArtifactMetadata> getArtifacts( String repositoryId )
     {
         return null;
index f7b188c348b1900062587a7a651e32489270f742..f5098e4feef95b09ca73798c6934c72c38dcafe1 100644 (file)
@@ -21,6 +21,8 @@ package org.apache.archiva.scheduler.repository;
 
 import org.apache.archiva.metadata.repository.MetadataRepository;
 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
+import org.apache.archiva.metadata.repository.RepositorySessionFactory;
+import org.apache.archiva.metadata.repository.TestRepositorySessionFactory;
 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
@@ -114,6 +116,9 @@ public class ArchivaRepositoryScanningTaskExecutorTest
         testConsumer = (TestConsumer) lookup( KnownRepositoryContentConsumer.class, "test-consumer" );
 
         metadataRepository = mock( MetadataRepository.class );
+
+        TestRepositorySessionFactory factory = (TestRepositorySessionFactory) lookup( RepositorySessionFactory.class );
+        factory.setRepository( metadataRepository );
     }
 
     protected void tearDown()
index f221ee5ee8da20477b6c6d5eef253a8d041fe4ea..96a87e09f3d3b158f82ef95f603f483a3df71194 100644 (file)
@@ -117,6 +117,7 @@ public class NewArtifactsRssFeedProcessorTest
         return artifact;
     }
 
+    // TODO: replace with mockito
     private class MetadataRepositoryMock
         implements MetadataRepository
     {
@@ -215,6 +216,21 @@ public class NewArtifactsRssFeedProcessorTest
             throw new UnsupportedOperationException();
         }
 
+        public void save()
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public void close()
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public void revert()
+        {
+            throw new UnsupportedOperationException();
+        }
+
         public Collection<String> getNamespaces( String arg0, String arg1 )
         {
             throw new UnsupportedOperationException();
index 2dc4a03699d5e121679bc95f12a289bba5846fb7..7d1528ad46fac17c9605b424ca7a3bc763d5b79e 100644 (file)
@@ -200,6 +200,19 @@ public class TestMetadataRepository
         return artifacts;
     }
 
+    public void save()
+    {
+    }
+
+    public void close()
+    {
+    }
+
+    public void revert()
+    {
+        throw new UnsupportedOperationException();
+    }
+
     public List<ArtifactMetadata> getArtifacts( String repositoryId )
     {
         return artifacts;
index 3c9756f885252c0e48c4cba13a4f1c793067f2fd..2c16fc6d133aa523a7738d238e93a0d7a7311743 100644 (file)
@@ -30,6 +30,7 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 
+// TODO: remove - it does nothing
 public class TestMetadataRepository
     implements MetadataRepository
 {
@@ -160,6 +161,21 @@ public class TestMetadataRepository
         return null;  //To change body of implemented methods use File | Settings | File Templates.
     }
 
+    public void save()
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    public void close()
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    public void revert()
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
+
     public List<ArtifactMetadata> getArtifacts( String repositoryId )
     {
         return null;
index f43fbc4fbad3c77bb2824217ce7305ae7ddaf957..440aa1476bc4fb00f61b1a4ed11304912815e91c 100644 (file)
@@ -133,4 +133,12 @@ public interface MetadataRepository
     Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
                                                String projectVersion )
         throws MetadataResolutionException;
+
+    void save()
+        throws MetadataRepositoryException;
+
+    void close();
+
+    void revert()
+        throws MetadataRepositoryException;
 }
index 1d9f431695c2d098d32f43cd55f5945432bcb3f5..64ff4077a1e4bd835d7ebb8bffa4b11324a354e2 100644 (file)
@@ -61,14 +61,30 @@ public class RepositorySession
 
     public void save()
     {
-        // FIXME
+        try
+        {
+            repository.save();
+        }
+        catch ( MetadataRepositoryException e )
+        {
+            // FIXME
+            throw new RuntimeException( e );
+        }
 
         dirty = false;
     }
 
     public void revert()
     {
-        // FIXME
+        try
+        {
+            repository.revert();
+        }
+        catch ( MetadataRepositoryException e )
+        {
+            // FIXME
+            throw new RuntimeException( e );
+        }
 
         dirty = false;
     }
@@ -86,7 +102,7 @@ public class RepositorySession
             save();
         }
 
-        // FIXME
+        repository.close();
     }
 
     public void markDirty()
index 9d26b53c11bda1be582784979ca272539f191d84..a75457301270497d90774acba6b966696f0abe7b 100644 (file)
@@ -177,4 +177,19 @@ public class TestMetadataRepository
     {
         return null;  //To change body of implemented methods use File | Settings | File Templates.
     }
+
+    public void save()
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    public void close()
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    public void revert()
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
 }
\ No newline at end of file
index a65506fe981927b2cb9207ce104dfa6848a301da..bab991619f37788d2bc92a41ae760e0c02fe741d 100644 (file)
@@ -33,6 +33,7 @@ import org.apache.archiva.metadata.model.ProjectVersionMetadata;
 import org.apache.archiva.metadata.model.ProjectVersionReference;
 import org.apache.archiva.metadata.model.Scm;
 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.apache.maven.archiva.configuration.ArchivaConfiguration;
@@ -530,6 +531,23 @@ public class FileMetadataRepository
         return artifacts.values();
     }
 
+    public void save()
+        throws MetadataRepositoryException
+    {
+        // it's all instantly persisted
+    }
+
+    public void close()
+    {
+        // nothing additional to close
+    }
+
+    public void revert()
+        throws MetadataRepositoryException
+    {
+        log.warn( "Attempted to revert a session, but the file-based repository storage doesn't support it" );
+    }
+
     private void updateArtifactFacets( ArtifactMetadata artifact, Properties properties )
     {
         String propertyPrefix = "artifact:facet:" + artifact.getId() + ":";
index 7b3003a2bdbd2c349559b7950ee0731881ace72a..4874623e6097694e5fd7f1d521bb21c345ba0c1b 100644 (file)
@@ -48,7 +48,6 @@
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-spring</artifactId>
-      <scope>test</scope>
     </dependency>
     <!-- FIXME: dependency management -->
     <dependency>
index 044a6895cccdf2e0ae08a6fc15c2b7666d84d743..2d3688bb08bc42ba9a608d7eaf3c3c35cdb5ad4d 100644 (file)
@@ -50,7 +50,7 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import javax.jcr.LoginException;
+import javax.jcr.NamespaceRegistry;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.PathNotFoundException;
@@ -67,7 +67,6 @@ import javax.jcr.query.Query;
 import javax.jcr.query.QueryResult;
 
 /**
- * @plexus.component role="org.apache.archiva.metadata.repository.MetadataRepository"
  * @todo below: revise storage format for project version metadata
  * @todo revise reference storage
  */
@@ -76,56 +75,50 @@ public class JcrMetadataRepository
 {
     private static final String JCR_LAST_MODIFIED = "jcr:lastModified";
 
-    private static final String ARTIFACT_NODE_TYPE = "archiva:artifact";
+    static final String ARTIFACT_NODE_TYPE = "archiva:artifact";
 
-    private static final String FACET_NODE_TYPE = "archiva:facet";
+    static final String FACET_NODE_TYPE = "archiva:facet";
 
     private static final String QUERY_ARTIFACTS = "SELECT * FROM [" + ARTIFACT_NODE_TYPE + "]";
 
-    /**
-     * @plexus.requirement role="org.apache.archiva.metadata.model.MetadataFacetFactory"
-     */
-    private Map<String, MetadataFacetFactory> metadataFacetFactories;
+    private final Map<String, MetadataFacetFactory> metadataFacetFactories;
 
     private static final Logger log = LoggerFactory.getLogger( JcrMetadataRepository.class );
 
-    /**
-     * @plexus.requirement
-     */
     private Repository repository;
 
     private Session session;
 
-    public JcrMetadataRepository()
+    public JcrMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories, Repository repository )
+        throws RepositoryException
     {
+        this.metadataFacetFactories = metadataFacetFactories;
+        this.repository = repository;
+
+        session = repository.login( new SimpleCredentials( "username", "password".toCharArray() ) );
     }
 
-    public void login()
+    static void initialize( Session session )
+        throws RepositoryException
     {
-        // FIXME: need to close this at the end - do we need to add it in the API?
-
-        try
-        {
-            // FIXME: shouldn't do this in constructor since it's a singleton
-            session = repository.login( new SimpleCredentials( "username", "password".toCharArray() ) );
+        // TODO: consider using namespaces for facets instead of the current approach:
+        // (if used, check if actually called by normal injection)
+//        for ( String facetId : metadataFacetFactories.keySet() )
+//        {
+//            session.getWorkspace().getNamespaceRegistry().registerNamespace( facetId, facetId );
+//        }
 
-            Workspace workspace = session.getWorkspace();
-            workspace.getNamespaceRegistry().registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
+        Workspace workspace = session.getWorkspace();
+        NamespaceRegistry registry = workspace.getNamespaceRegistry();
 
-            NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
-            registerMixinNodeType( nodeTypeManager, ARTIFACT_NODE_TYPE );
-            registerMixinNodeType( nodeTypeManager, FACET_NODE_TYPE );
-        }
-        catch ( LoginException e )
-        {
-            // FIXME
-            throw new RuntimeException( e );
-        }
-        catch ( RepositoryException e )
+        if ( !Arrays.asList( registry.getPrefixes() ).contains( "archiva" ) )
         {
-            // FIXME
-            throw new RuntimeException( e );
+            registry.registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
         }
+
+        NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
+        registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.ARTIFACT_NODE_TYPE );
+        registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.FACET_NODE_TYPE );
     }
 
     private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String name )
@@ -134,7 +127,13 @@ public class JcrMetadataRepository
         NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
         nodeType.setMixin( true );
         nodeType.setName( name );
-        nodeTypeManager.registerNodeType( nodeType, false );
+
+        // for now just don't re-create - but in future if we change the definition, make sure to remove first as an
+        // upgrade path
+        if ( !nodeTypeManager.hasNodeType( name ) )
+        {
+            nodeTypeManager.registerNodeType( nodeType, false );
+        }
     }
 
     public void updateProject( String repositoryId, ProjectMetadata project )
@@ -997,31 +996,35 @@ public class JcrMetadataRepository
         return artifacts;
     }
 
-    void close()
+    public void save()
+        throws MetadataRepositoryException
     {
         try
         {
-            // FIXME: this shouldn't be here! Repository may need a context
             session.save();
         }
         catch ( RepositoryException e )
         {
-            // FIXME
-            throw new RuntimeException( e );
+            throw new MetadataRepositoryException( e.getMessage(), e );
         }
-        session.logout();
     }
 
-    public void setMetadataFacetFactories( Map<String, MetadataFacetFactory> metadataFacetFactories )
+    public void revert()
+        throws MetadataRepositoryException
     {
-        this.metadataFacetFactories = metadataFacetFactories;
+        try
+        {
+            session.refresh( false );
+        }
+        catch ( RepositoryException e )
+        {
+            throw new MetadataRepositoryException( e.getMessage(), e );
+        }
+    }
 
-        // TODO: consider using namespaces for facets instead of the current approach:
-        // (if used, check if actually called by normal injection)
-//        for ( String facetId : metadataFacetFactories.keySet() )
-//        {
-//            session.getWorkspace().getNamespaceRegistry().registerNamespace( facetId, facetId );
-//        }
+    public void close()
+    {
+        session.logout();
     }
 
     private ArtifactMetadata getArtifactFromNode( String repositoryId, Node artifactNode )
diff --git a/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrRepositorySessionFactory.java b/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrRepositorySessionFactory.java
new file mode 100644 (file)
index 0000000..56331a3
--- /dev/null
@@ -0,0 +1,96 @@
+package org.apache.archiva.metadata.repository.jcr;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.metadata.model.MetadataFacetFactory;
+import org.apache.archiva.metadata.repository.MetadataRepository;
+import org.apache.archiva.metadata.repository.MetadataResolver;
+import org.apache.archiva.metadata.repository.RepositorySession;
+import org.apache.archiva.metadata.repository.RepositorySessionFactory;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
+
+import java.util.Map;
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+
+/**
+ * @plexus.component role="org.apache.archiva.metadata.repository.RepositorySessionFactory" role-hint="jcr"
+ */
+public class JcrRepositorySessionFactory
+    implements RepositorySessionFactory, Initializable
+{
+    /**
+     * @plexus.requirement role="org.apache.archiva.metadata.model.MetadataFacetFactory"
+     */
+    private Map<String, MetadataFacetFactory> metadataFacetFactories;
+
+    /**
+     * @plexus.requirement
+     */
+    private Repository repository;
+
+    /**
+     * @plexus.requirement
+     */
+    private MetadataResolver metadataResolver;
+
+    public RepositorySession createSession()
+    {
+        try
+        {
+            // FIXME: is this the right separation? or should a JCR session object contain the JCR session information?
+            //  such a change might allow us to avoid creating two objects for each request. It would also clear up
+            //  the ambiguities in the API where session & repository are the inverse of JCR; and the resolver is
+            //  retrieved from the session but must have it passed in. These should be reviewed before finalising the
+            //  API.
+            MetadataRepository metadataRepository = new JcrMetadataRepository( metadataFacetFactories, repository );
+
+            return new RepositorySession( metadataRepository, metadataResolver );
+        }
+        catch ( RepositoryException e )
+        {
+            // FIXME: a custom exception requires refactoring for callers to handle it
+            throw new RuntimeException( e );
+        }
+    }
+
+    public void initialize()
+        throws InitializationException
+    {
+        JcrMetadataRepository metadataRepository = null;
+        try
+        {
+            metadataRepository = new JcrMetadataRepository( metadataFacetFactories, repository );
+            JcrMetadataRepository.initialize( metadataRepository.getJcrSession() );
+        }
+        catch ( RepositoryException e )
+        {
+            throw new InitializationException( e.getMessage(), e );
+        }
+        finally
+        {
+            if ( metadataRepository != null )
+            {
+                metadataRepository.close();
+            }
+        }
+    }
+}
index e62d56a1974058dddb6d15116972a51eb7590c57..fbe1d5032064da70a08bfbe50d64740387286fa9 100644 (file)
@@ -21,11 +21,12 @@ package org.apache.archiva.metadata.repository.jcr;
 
 import org.apache.archiva.metadata.model.MetadataFacetFactory;
 import org.apache.archiva.metadata.repository.AbstractMetadataRepositoryTest;
-import org.apache.archiva.metadata.repository.MetadataRepository;
 import org.apache.commons.io.FileUtils;
 
 import java.util.Map;
+import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 
 public class JcrMetadataRepositoryTest
     extends AbstractMetadataRepositoryTest
@@ -41,14 +42,19 @@ public class JcrMetadataRepositoryTest
 
         Map<String, MetadataFacetFactory> factories = createTestMetadataFacetFactories();
 
-        jcrMetadataRepository = (JcrMetadataRepository) lookup( MetadataRepository.class );
-        jcrMetadataRepository.setMetadataFacetFactories( factories );
-        jcrMetadataRepository.login();
+        // TODO: probably don't need to use Spring for this
+        Repository repository = (Repository) lookup( Repository.class );
+        jcrMetadataRepository = new JcrMetadataRepository( factories, repository );
 
-        // removing content is faster than deleting and re-copying the files from target/jcr
         try
         {
-            jcrMetadataRepository.getJcrSession().getRootNode().getNode( "repositories" ).remove();
+            Session session = jcrMetadataRepository.getJcrSession();
+
+            // set up namespaces, etc.
+            JcrMetadataRepository.initialize( session );
+
+            // removing content is faster than deleting and re-copying the files from target/jcr
+            session.getRootNode().getNode( "repositories" ).remove();
         }
         catch ( RepositoryException e )
         {
index 3bd956ab51decc023b72802de1c45d0b9e3aea45..625570e927330a8cd7279a1fc7c529c92a86a099 100644 (file)
@@ -32,8 +32,6 @@
   </Security>
   <Workspaces rootPath="${rep.home}/workspaces" defaultWorkspace="default"/>
   <Workspace name="${wsp.name}">
-    <!--<FileSystem class="org.apache.jackrabbit.core.fs.mem.MemoryFileSystem"/>-->
-    <!--<PersistenceManager class="org.apache.jackrabbit.core.persistence.mem.InMemPersistenceManager"/>-->
     <FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem">
       <param name="path" value="${wsp.home}"/>
     </FileSystem>