From: Brett Porter Date: Wed, 29 Dec 2010 13:02:59 +0000 (+0000) Subject: [MRM-1327] hook save()/close()/revert() methods to the repository implementation X-Git-Tag: archiva-1.4-M1~850 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6ff91471395b21d91fd553faa6103e9adf55a92d;p=archiva.git [MRM-1327] hook save()/close()/revert() methods to the repository implementation git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1053630 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java index 3c9756f88..f100aa191 100644 --- a/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java +++ b/archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java @@ -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 getArtifacts( String repositoryId ) { return null; diff --git a/archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/ArchivaRepositoryScanningTaskExecutorTest.java b/archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/ArchivaRepositoryScanningTaskExecutorTest.java index f7b188c34..f5098e4fe 100644 --- a/archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/ArchivaRepositoryScanningTaskExecutorTest.java +++ b/archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/ArchivaRepositoryScanningTaskExecutorTest.java @@ -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() diff --git a/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java b/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java index f221ee5ee..96a87e09f 100644 --- a/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java +++ b/archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java @@ -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 getNamespaces( String arg0, String arg1 ) { throw new UnsupportedOperationException(); diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java index 2dc4a0369..7d1528ad4 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/metadata/repository/memory/TestMetadataRepository.java @@ -200,6 +200,19 @@ public class TestMetadataRepository return artifacts; } + public void save() + { + } + + public void close() + { + } + + public void revert() + { + throw new UnsupportedOperationException(); + } + public List getArtifacts( String repositoryId ) { return artifacts; diff --git a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java index 3c9756f88..2c16fc6d1 100644 --- a/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java +++ b/archiva-modules/archiva-web/archiva-webdav/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java @@ -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 getArtifacts( String repositoryId ) { return null; diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java index f43fbc4fb..440aa1476 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java @@ -133,4 +133,12 @@ public interface MetadataRepository Collection getArtifacts( String repoId, String namespace, String projectId, String projectVersion ) throws MetadataResolutionException; + + void save() + throws MetadataRepositoryException; + + void close(); + + void revert() + throws MetadataRepositoryException; } diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySession.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySession.java index 1d9f43169..64ff4077a 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySession.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/RepositorySession.java @@ -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() diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java index 9d26b53c1..a75457301 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java @@ -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 diff --git a/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java b/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java index a65506fe9..bab991619 100644 --- a/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java +++ b/archiva-modules/plugins/metadata-repository-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java @@ -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() + ":"; diff --git a/archiva-modules/plugins/metadata-store-jcr/pom.xml b/archiva-modules/plugins/metadata-store-jcr/pom.xml index 7b3003a2b..4874623e6 100644 --- a/archiva-modules/plugins/metadata-store-jcr/pom.xml +++ b/archiva-modules/plugins/metadata-store-jcr/pom.xml @@ -48,7 +48,6 @@ org.codehaus.plexus plexus-spring - test diff --git a/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java b/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java index 044a6895c..2d3688bb0 100644 --- a/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java +++ b/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java @@ -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 metadataFacetFactories; + private final Map metadataFacetFactories; private static final Logger log = LoggerFactory.getLogger( JcrMetadataRepository.class ); - /** - * @plexus.requirement - */ private Repository repository; private Session session; - public JcrMetadataRepository() + public JcrMetadataRepository( Map 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 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 index 000000000..56331a3ad --- /dev/null +++ b/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrRepositorySessionFactory.java @@ -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 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(); + } + } + } +} diff --git a/archiva-modules/plugins/metadata-store-jcr/src/test/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepositoryTest.java b/archiva-modules/plugins/metadata-store-jcr/src/test/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepositoryTest.java index e62d56a19..fbe1d5032 100644 --- a/archiva-modules/plugins/metadata-store-jcr/src/test/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepositoryTest.java +++ b/archiva-modules/plugins/metadata-store-jcr/src/test/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepositoryTest.java @@ -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 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 ) { diff --git a/archiva-modules/plugins/metadata-store-jcr/src/test/repository.xml b/archiva-modules/plugins/metadata-store-jcr/src/test/repository.xml index 3bd956ab5..625570e92 100644 --- a/archiva-modules/plugins/metadata-store-jcr/src/test/repository.xml +++ b/archiva-modules/plugins/metadata-store-jcr/src/test/repository.xml @@ -32,8 +32,6 @@ - -