[MRM-1327] clean up path construction and method sorting

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1051466 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2010-12-21 11:40:08 +00:00
parent 7f610bca75
commit 1ff88d24fe
1 changed files with 100 additions and 104 deletions

View File

@ -69,11 +69,9 @@ import javax.jcr.query.QueryResult;
/** /**
* @plexus.component role="org.apache.archiva.metadata.repository.MetadataRepository" * @plexus.component role="org.apache.archiva.metadata.repository.MetadataRepository"
* @todo path construction should be centralised
* @todo review all methods for alternate implementations (use of queries)
* @todo below: exception handling * @todo below: exception handling
* @todo below: revise storage format for project version metadata * @todo below: revise storage format for project version metadata
* @todo review for unnecessary node addition in r/o methods * @todo revise reference storage
*/ */
public class JcrMetadataRepository public class JcrMetadataRepository
implements MetadataRepository implements MetadataRepository
@ -596,10 +594,7 @@ public class JcrMetadataRepository
try try
{ {
Node root = session.getRootNode(); Node root = session.getRootNode();
String path = String path = getArtifactPath( repositoryId, namespace, projectId, projectVersion, id );
"repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion +
"/" + id;
// TODO: exception if missing?
if ( root.hasNode( path ) ) if ( root.hasNode( path ) )
{ {
root.getNode( path ).remove(); root.getNode( path ).remove();
@ -617,8 +612,7 @@ public class JcrMetadataRepository
try try
{ {
Node root = session.getRootNode(); Node root = session.getRootNode();
String path = "repositories/" + repositoryId; String path = getRepositoryPath( repositoryId );
// TODO: exception if missing?
if ( root.hasNode( path ) ) if ( root.hasNode( path ) )
{ {
root.getNode( path ).remove(); root.getNode( path ).remove();
@ -665,7 +659,7 @@ public class JcrMetadataRepository
Node root = session.getRootNode(); Node root = session.getRootNode();
// basically just checking it exists // basically just checking it exists
String path = "repositories/" + repositoryId + "/content/" + namespace + "/" + projectId; String path = getProjectPath( repositoryId, namespace, projectId );
if ( root.hasNode( path ) ) if ( root.hasNode( path ) )
{ {
metadata = new ProjectMetadata(); metadata = new ProjectMetadata();
@ -692,8 +686,7 @@ public class JcrMetadataRepository
{ {
Node root = session.getRootNode(); Node root = session.getRootNode();
String path = String path = getProjectVersionPath( repositoryId, namespace, projectId, projectVersion );
"repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion;
if ( !root.hasNode( path ) ) if ( !root.hasNode( path ) )
{ {
return null; return null;
@ -866,12 +859,6 @@ public class JcrMetadataRepository
return versionMetadata; return versionMetadata;
} }
private static String getPropertyString( Node node, String name )
throws RepositoryException
{
return node.hasProperty( name ) ? node.getProperty( name ).getString() : null;
}
public Collection<String> getArtifactVersions( String repositoryId, String namespace, String projectId, public Collection<String> getArtifactVersions( String repositoryId, String namespace, String projectId,
String projectVersion ) String projectVersion )
{ {
@ -881,14 +868,10 @@ public class JcrMetadataRepository
{ {
Node root = session.getRootNode(); Node root = session.getRootNode();
Node node = root.getNode( Node node = root.getNode( getProjectVersionPath( repositoryId, namespace, projectId, projectVersion ) );
"repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion );
NodeIterator iterator = node.getNodes(); for ( Node n : JcrUtils.getChildNodes( node ) )
while ( iterator.hasNext() )
{ {
Node n = iterator.nextNode();
versions.add( n.getProperty( "version" ).getString() ); versions.add( n.getProperty( "version" ).getString() );
} }
} }
@ -915,14 +898,11 @@ public class JcrMetadataRepository
{ {
Node root = session.getRootNode(); Node root = session.getRootNode();
String path = String path = getProjectVersionPath( repositoryId, namespace, projectId, projectVersion ) + "/references";
"repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion +
"/references";
if ( root.hasNode( path ) ) if ( root.hasNode( path ) )
{ {
Node node = root.getNode( path ); Node node = root.getNode( path );
// TODO: use query by reference type
NodeIterator i = node.getNodes(); NodeIterator i = node.getNodes();
while ( i.hasNext() ) while ( i.hasNext() )
{ {
@ -966,71 +946,23 @@ public class JcrMetadataRepository
return getNamespaces( repositoryId, null ); return getNamespaces( repositoryId, null );
} }
private Collection<String> getNodeNames( String path )
{
List<String> names = new ArrayList<String>();
try
{
Node root = session.getRootNode();
Node repository = root.getNode( path );
NodeIterator nodes = repository.getNodes();
while ( nodes.hasNext() )
{
Node node = nodes.nextNode();
names.add( node.getName() );
}
}
catch ( PathNotFoundException e )
{
// ignore repo not found for now
// TODO: throw specific exception if repo doesn't exist
}
catch ( RepositoryException e )
{
// TODO
throw new RuntimeException( e );
}
return names;
}
public Collection<String> getNamespaces( String repositoryId, String baseNamespace ) public Collection<String> getNamespaces( String repositoryId, String baseNamespace )
{ {
List<String> namespaces = new ArrayList<String>(); String path = baseNamespace != null
try ? getNamespacePath( repositoryId, baseNamespace )
{ : getRepositoryContentPath( repositoryId );
Node node = getOrAddRepositoryContentNode( repositoryId );
if ( baseNamespace != null )
{
node = getOrAddNodeByPath( node, baseNamespace.replace( '.', '/' ) );
}
for ( Node n : JcrUtils.getChildNodes( node ) ) return getNodeNames( path );
{
namespaces.add( n.getName() );
}
}
catch ( RepositoryException e )
{
// TODO
throw new RuntimeException( e );
}
return namespaces;
} }
public Collection<String> getProjects( String repositoryId, String namespace ) public Collection<String> getProjects( String repositoryId, String namespace )
{ {
// TODO: could be simpler with pathed namespaces, rely on namespace property return getNodeNames( getNamespacePath( repositoryId, namespace ) );
return getNodeNames( "repositories/" + repositoryId + "/content/" + namespace );
} }
public Collection<String> getProjectVersions( String repositoryId, String namespace, String projectId ) public Collection<String> getProjectVersions( String repositoryId, String namespace, String projectId )
{ {
// TODO: could be simpler with pathed namespaces, rely on namespace property return getNodeNames( getProjectPath( repositoryId, namespace, projectId ) );
return getNodeNames( "repositories/" + repositoryId + "/content/" + namespace + "/" + projectId );
} }
public Collection<ArtifactMetadata> getArtifacts( String repositoryId, String namespace, String projectId, public Collection<ArtifactMetadata> getArtifacts( String repositoryId, String namespace, String projectId,
@ -1041,20 +973,15 @@ public class JcrMetadataRepository
try try
{ {
Node root = session.getRootNode(); Node root = session.getRootNode();
String path = String path = getProjectVersionPath( repositoryId, namespace, projectId, projectVersion );
"repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion;
if ( root.hasNode( path ) ) if ( root.hasNode( path ) )
{ {
Node node = root.getNode( path ); Node node = root.getNode( path );
NodeIterator iterator = node.getNodes(); for ( Node n : JcrUtils.getChildNodes( node ) )
while ( iterator.hasNext() )
{ {
Node artifactNode = iterator.nextNode(); artifacts.add( getArtifactFromNode( repositoryId, n ) );
ArtifactMetadata artifact = getArtifactFromNode( repositoryId, artifactNode );
artifacts.add( artifact );
} }
} }
} }
@ -1067,6 +994,34 @@ public class JcrMetadataRepository
return artifacts; return artifacts;
} }
void close()
{
try
{
// TODO: this shouldn't be here! Repository may need a context
session.save();
}
catch ( RepositoryException e )
{
// TODO
throw new RuntimeException( e );
}
session.logout();
}
public void setMetadataFacetFactories( Map<String, MetadataFacetFactory> metadataFacetFactories )
{
this.metadataFacetFactories = metadataFacetFactories;
// TODO: check if actually called by normal injection
// TODO: consider using namespaces for facets instead of the current approach:
// for ( String facetId : metadataFacetFactories.keySet() )
// {
// session.getWorkspace().getNamespaceRegistry().registerNamespace( facetId, facetId );
// }
}
private ArtifactMetadata getArtifactFromNode( String repositoryId, Node artifactNode ) private ArtifactMetadata getArtifactFromNode( String repositoryId, Node artifactNode )
throws RepositoryException throws RepositoryException
{ {
@ -1142,37 +1097,78 @@ public class JcrMetadataRepository
return artifact; return artifact;
} }
void close() private static String getPropertyString( Node node, String name )
throws RepositoryException
{ {
return node.hasProperty( name ) ? node.getProperty( name ).getString() : null;
}
private Collection<String> getNodeNames( String path )
{
List<String> names = new ArrayList<String>();
try try
{ {
// TODO: this shouldn't be here! Repository may need a context Node root = session.getRootNode();
session.save();
Node repository = root.getNode( path );
NodeIterator nodes = repository.getNodes();
while ( nodes.hasNext() )
{
Node node = nodes.nextNode();
names.add( node.getName() );
}
}
catch ( PathNotFoundException e )
{
// ignore repo not found for now
// TODO: throw specific exception if repo doesn't exist
} }
catch ( RepositoryException e ) catch ( RepositoryException e )
{ {
// TODO // TODO
throw new RuntimeException( e ); throw new RuntimeException( e );
} }
session.logout();
return names;
} }
public void setMetadataFacetFactories( Map<String, MetadataFacetFactory> metadataFacetFactories ) private static String getRepositoryPath( String repositoryId )
{ {
this.metadataFacetFactories = metadataFacetFactories; return "repositories/" + repositoryId;
}
// TODO: check if actually called by normal injection private static String getRepositoryContentPath( String repositoryId )
{
// for ( String facetId : metadataFacetFactories.keySet() ) return getRepositoryPath( repositoryId ) + "/content/";
// {
// // TODO: second arg should be a better URL for the namespace
// session.getWorkspace().getNamespaceRegistry().registerNamespace( facetId, facetId );
// }
} }
private static String getFacetPath( String repositoryId, String facetId ) private static String getFacetPath( String repositoryId, String facetId )
{ {
return "repositories/" + repositoryId + "/facets/" + facetId; return getRepositoryPath( repositoryId ) + "/facets/" + facetId;
}
private static String getNamespacePath( String repositoryId, String namespace )
{
return getRepositoryContentPath( repositoryId ) + namespace.replace( '.', '/' );
}
private static String getProjectPath( String repositoryId, String namespace, String projectId )
{
return getNamespacePath( repositoryId, namespace ) + "/" + projectId;
}
private static String getProjectVersionPath( String repositoryId, String namespace, String projectId,
String projectVersion )
{
return getProjectPath( repositoryId, namespace, projectId ) + "/" + projectVersion;
}
private static String getArtifactPath( String repositoryId, String namespace, String projectId,
String projectVersion, String id )
{
return getProjectVersionPath( repositoryId, namespace, projectId, projectVersion ) + "/" + id;
} }
private Node getOrAddNodeByPath( Node baseNode, String name ) private Node getOrAddNodeByPath( Node baseNode, String name )