1 package org.apache.archiva.metadata.repository.jcr;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.CiManagement;
24 import org.apache.archiva.metadata.model.Dependency;
25 import org.apache.archiva.metadata.model.IssueManagement;
26 import org.apache.archiva.metadata.model.License;
27 import org.apache.archiva.metadata.model.MailingList;
28 import org.apache.archiva.metadata.model.MetadataFacet;
29 import org.apache.archiva.metadata.model.MetadataFacetFactory;
30 import org.apache.archiva.metadata.model.Organization;
31 import org.apache.archiva.metadata.model.ProjectMetadata;
32 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
33 import org.apache.archiva.metadata.model.ProjectVersionReference;
34 import org.apache.archiva.metadata.model.Scm;
35 import org.apache.archiva.metadata.repository.MetadataRepository;
36 import org.apache.archiva.metadata.repository.MetadataResolutionException;
37 import org.apache.jackrabbit.commons.JcrUtils;
38 import org.apache.jackrabbit.core.TransientRepository;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Calendar;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.Date;
49 import java.util.HashMap;
50 import java.util.LinkedHashSet;
51 import java.util.List;
54 import javax.jcr.LoginException;
55 import javax.jcr.Node;
56 import javax.jcr.NodeIterator;
57 import javax.jcr.PathNotFoundException;
58 import javax.jcr.Property;
59 import javax.jcr.Repository;
60 import javax.jcr.RepositoryException;
61 import javax.jcr.Session;
62 import javax.jcr.SimpleCredentials;
63 import javax.jcr.ValueFactory;
64 import javax.jcr.Workspace;
65 import javax.jcr.nodetype.NodeTypeManager;
66 import javax.jcr.nodetype.NodeTypeTemplate;
67 import javax.jcr.query.Query;
68 import javax.jcr.query.QueryResult;
71 * @plexus.component role="org.apache.archiva.metadata.repository.MetadataRepository"
72 * @todo path construction should be centralised
73 * @todo review all methods for alternate implementations (use of queries)
74 * @todo below: exception handling
75 * @todo below: revise storage format for project version metadata
76 * @todo review for unnecessary node addition in r/o methods
78 public class JcrMetadataRepository
79 implements MetadataRepository
81 private static final String JCR_LAST_MODIFIED = "jcr:lastModified";
83 private static final String ARTIFACT_NODE_TYPE = "archiva:artifact";
85 private static final String FACET_NODE_TYPE = "archiva:facet";
87 private static final String QUERY_ARTIFACTS = "SELECT * FROM [" + ARTIFACT_NODE_TYPE + "]";
90 * @plexus.requirement role="org.apache.archiva.metadata.model.MetadataFacetFactory"
92 private Map<String, MetadataFacetFactory> metadataFacetFactories;
94 private static final Logger log = LoggerFactory.getLogger( JcrMetadataRepository.class );
96 private static Repository repository;
98 private Session session;
100 public JcrMetadataRepository()
102 // TODO: need to close this at the end - do we need to add it in the API?
106 // TODO: push this in from the test, and make it possible from the webapp
107 if ( repository == null )
109 repository = new TransientRepository( new File( "src/test/repository.xml" ), new File( "target/jcr" ) );
111 // TODO: shouldn't do this in constructor since it's a singleton
112 session = repository.login( new SimpleCredentials( "username", "password".toCharArray() ) );
114 Workspace workspace = session.getWorkspace();
115 workspace.getNamespaceRegistry().registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
117 NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
118 registerMixinNodeType( nodeTypeManager, ARTIFACT_NODE_TYPE );
119 registerMixinNodeType( nodeTypeManager, FACET_NODE_TYPE );
121 catch ( LoginException e )
124 throw new RuntimeException( e );
126 catch ( RepositoryException e )
129 throw new RuntimeException( e );
133 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String name )
134 throws RepositoryException
136 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
137 nodeType.setMixin( true );
138 nodeType.setName( name );
139 nodeTypeManager.registerNodeType( nodeType, false );
142 public void updateProject( String repositoryId, ProjectMetadata project )
144 updateProject( repositoryId, project.getNamespace(), project.getId() );
147 private void updateProject( String repositoryId, String namespace, String projectId )
149 updateNamespace( repositoryId, namespace );
153 getOrAddProjectNode( repositoryId, namespace, projectId );
155 catch ( RepositoryException e )
158 throw new RuntimeException( e );
162 public void updateArtifact( String repositoryId, String namespace, String projectId, String projectVersion,
163 ArtifactMetadata artifactMeta )
165 updateNamespace( repositoryId, namespace );
169 Node node = getOrAddArtifactNode( repositoryId, namespace, projectId, projectVersion,
170 artifactMeta.getId() );
172 Calendar cal = Calendar.getInstance();
173 cal.setTime( artifactMeta.getFileLastModified() );
174 node.setProperty( JCR_LAST_MODIFIED, cal );
176 cal = Calendar.getInstance();
177 cal.setTime( artifactMeta.getWhenGathered() );
178 node.setProperty( "whenGathered", cal );
180 node.setProperty( "size", artifactMeta.getSize() );
181 node.setProperty( "md5", artifactMeta.getMd5() );
182 node.setProperty( "sha1", artifactMeta.getSha1() );
184 node.setProperty( "version", artifactMeta.getVersion() );
186 for ( MetadataFacet facet : artifactMeta.getFacetList() )
188 if ( node.hasNode( facet.getFacetId() ) )
190 node.getNode( facet.getFacetId() ).remove();
193 // recreate, to ensure properties are removed
194 Node n = node.addNode( facet.getFacetId() );
195 n.addMixin( FACET_NODE_TYPE );
197 for ( Map.Entry<String, String> entry : facet.toProperties().entrySet() )
199 n.setProperty( entry.getKey(), entry.getValue() );
202 // TODO: need some context around this so it can be done only when needed
205 catch ( RepositoryException e )
208 throw new RuntimeException( e );
212 public void updateProjectVersion( String repositoryId, String namespace, String projectId,
213 ProjectVersionMetadata versionMetadata )
215 updateProject( repositoryId, namespace, projectId );
219 Node versionNode = getOrAddProjectVersionNode( repositoryId, namespace, projectId,
220 versionMetadata.getId() );
222 versionNode.setProperty( "name", versionMetadata.getName() );
223 versionNode.setProperty( "description", versionMetadata.getDescription() );
224 versionNode.setProperty( "url", versionMetadata.getUrl() );
225 versionNode.setProperty( "incomplete", versionMetadata.isIncomplete() );
227 // TODO: decide how to treat these in the content repo
228 if ( versionMetadata.getScm() != null )
230 versionNode.setProperty( "scm.connection", versionMetadata.getScm().getConnection() );
231 versionNode.setProperty( "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() );
232 versionNode.setProperty( "scm.url", versionMetadata.getScm().getUrl() );
234 if ( versionMetadata.getCiManagement() != null )
236 versionNode.setProperty( "ci.system", versionMetadata.getCiManagement().getSystem() );
237 versionNode.setProperty( "ci.url", versionMetadata.getCiManagement().getUrl() );
239 if ( versionMetadata.getIssueManagement() != null )
241 versionNode.setProperty( "issue.system", versionMetadata.getIssueManagement().getSystem() );
242 versionNode.setProperty( "issue.url", versionMetadata.getIssueManagement().getUrl() );
244 if ( versionMetadata.getOrganization() != null )
246 versionNode.setProperty( "org.name", versionMetadata.getOrganization().getName() );
247 versionNode.setProperty( "org.url", versionMetadata.getOrganization().getUrl() );
250 for ( License license : versionMetadata.getLicenses() )
252 versionNode.setProperty( "license." + i + ".name", license.getName() );
253 versionNode.setProperty( "license." + i + ".url", license.getUrl() );
257 for ( MailingList mailingList : versionMetadata.getMailingLists() )
259 versionNode.setProperty( "mailingList." + i + ".archive", mailingList.getMainArchiveUrl() );
260 versionNode.setProperty( "mailingList." + i + ".name", mailingList.getName() );
261 versionNode.setProperty( "mailingList." + i + ".post", mailingList.getPostAddress() );
262 versionNode.setProperty( "mailingList." + i + ".unsubscribe", mailingList.getUnsubscribeAddress() );
263 versionNode.setProperty( "mailingList." + i + ".subscribe", mailingList.getSubscribeAddress() );
264 versionNode.setProperty( "mailingList." + i + ".otherArchives", join(
265 mailingList.getOtherArchives() ) );
269 for ( Dependency dependency : versionMetadata.getDependencies() )
271 versionNode.setProperty( "dependency." + i + ".classifier", dependency.getClassifier() );
272 versionNode.setProperty( "dependency." + i + ".scope", dependency.getScope() );
273 versionNode.setProperty( "dependency." + i + ".systemPath", dependency.getSystemPath() );
274 versionNode.setProperty( "dependency." + i + ".artifactId", dependency.getArtifactId() );
275 versionNode.setProperty( "dependency." + i + ".groupId", dependency.getGroupId() );
276 versionNode.setProperty( "dependency." + i + ".version", dependency.getVersion() );
277 versionNode.setProperty( "dependency." + i + ".type", dependency.getType() );
281 for ( MetadataFacet facet : versionMetadata.getFacetList() )
283 // recreate, to ensure properties are removed
284 if ( versionNode.hasNode( facet.getFacetId() ) )
286 versionNode.getNode( facet.getFacetId() ).remove();
288 Node n = versionNode.addNode( facet.getFacetId() );
289 n.addMixin( FACET_NODE_TYPE );
291 for ( Map.Entry<String, String> entry : facet.toProperties().entrySet() )
293 n.setProperty( entry.getKey(), entry.getValue() );
297 catch ( RepositoryException e )
300 throw new RuntimeException( e );
304 public void updateProjectReference( String repositoryId, String namespace, String projectId, String projectVersion,
305 ProjectVersionReference reference )
307 // not using weak references, since they still need to exist upfront to be referred to
310 Node node = getOrAddRepositoryContentNode( repositoryId );
311 node = JcrUtils.getOrAddNode( node, namespace );
312 node = JcrUtils.getOrAddNode( node, projectId );
313 node = JcrUtils.getOrAddNode( node, projectVersion );
314 node = JcrUtils.getOrAddNode( node, "references" );
315 node = JcrUtils.getOrAddNode( node, reference.getNamespace() );
316 node = JcrUtils.getOrAddNode( node, reference.getProjectId() );
317 node = JcrUtils.getOrAddNode( node, reference.getProjectVersion() );
318 node.setProperty( "type", reference.getReferenceType().toString() );
320 catch ( RepositoryException e )
323 throw new RuntimeException( e );
327 public void updateNamespace( String repositoryId, String namespace )
331 Node node = getOrAddNamespaceNode( repositoryId, namespace );
332 node.setProperty( "namespace", namespace );
334 catch ( RepositoryException e )
337 throw new RuntimeException( e );
341 public List<String> getMetadataFacets( String repositoryId, String facetId )
343 List<String> facets = new ArrayList<String>();
347 // no need to construct node-by-node here, as we'll find in the next instance, the facet names have / and
348 // are paths themselves
349 Node node = session.getRootNode().getNode( getFacetPath( repositoryId, facetId ) );
351 // TODO: this is a bit awkward. Might be better to review the purpose of this function - why is the list of
353 recurse( facets, "", node );
355 catch ( PathNotFoundException e )
357 // ignored - the facet doesn't exist, so return the empty list
359 catch ( RepositoryException e )
362 throw new RuntimeException( e );
367 private void recurse( List<String> facets, String prefix, Node node )
368 throws RepositoryException
370 for ( Node n : JcrUtils.getChildNodes( node ) )
372 String name = prefix + "/" + n.getName();
375 recurse( facets, name, n );
379 // strip leading / first
380 facets.add( name.substring( 1 ) );
385 public MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name )
387 MetadataFacet metadataFacet = null;
390 Node root = session.getRootNode();
391 Node node = root.getNode( getFacetPath( repositoryId, facetId, name ) );
393 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
394 if ( metadataFacetFactory != null )
396 metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
397 Map<String, String> map = new HashMap<String, String>();
398 for ( Property property : JcrUtils.getProperties( node ) )
400 String p = property.getName();
401 if ( !p.startsWith( "jcr:" ) )
403 map.put( p, property.getString() );
406 metadataFacet.fromProperties( map );
409 catch ( PathNotFoundException e )
411 // ignored - the facet doesn't exist, so return null
413 catch ( RepositoryException e )
416 throw new RuntimeException( e );
418 return metadataFacet;
421 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
425 Node repo = getOrAddRepositoryNode( repositoryId );
426 Node facets = JcrUtils.getOrAddNode( repo, "facets" );
428 String id = metadataFacet.getFacetId();
429 Node facetNode = JcrUtils.getOrAddNode( facets, id );
431 Node node = getOrAddNodeByPath( facetNode, metadataFacet.getName() );
433 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
435 node.setProperty( entry.getKey(), entry.getValue() );
438 catch ( RepositoryException e )
441 throw new RuntimeException( e );
445 public void removeMetadataFacets( String repositoryId, String facetId )
449 Node root = session.getRootNode();
450 String path = getFacetPath( repositoryId, facetId );
451 if ( root.hasNode( path ) )
453 root.getNode( path ).remove();
456 catch ( RepositoryException e )
459 throw new RuntimeException( e );
463 public void removeMetadataFacet( String repositoryId, String facetId, String name )
467 Node root = session.getRootNode();
468 String path = getFacetPath( repositoryId, facetId, name );
469 if ( root.hasNode( path ) )
471 Node node = root.getNode( path );
474 // also remove empty container nodes
475 Node parent = node.getParent();
479 while ( !node.hasNodes() );
482 catch ( RepositoryException e )
485 throw new RuntimeException( e );
489 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date startTime, Date endTime )
491 List<ArtifactMetadata> artifacts;
493 String q = QUERY_ARTIFACTS;
495 String clause = " WHERE";
496 if ( startTime != null )
498 q += clause + " [whenGathered] >= $start";
501 if ( endTime != null )
503 q += clause + " [whenGathered] <= $end";
508 Query query = session.getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
509 ValueFactory valueFactory = session.getValueFactory();
510 if ( startTime != null )
512 query.bindValue( "start", valueFactory.createValue( createCalendar( startTime ) ) );
514 if ( endTime != null )
516 query.bindValue( "end", valueFactory.createValue( createCalendar( endTime ) ) );
518 QueryResult result = query.execute();
520 artifacts = new ArrayList<ArtifactMetadata>();
521 for ( Node n : JcrUtils.getNodes( result ) )
523 artifacts.add( getArtifactFromNode( repoId, n ) );
526 catch ( RepositoryException e )
529 throw new RuntimeException( e );
534 public Collection<String> getRepositories()
536 List<String> repositories;
540 Node root = session.getRootNode();
541 if ( root.hasNode( "repositories" ) )
543 Node node = root.getNode( "repositories" );
545 repositories = new ArrayList<String>();
546 NodeIterator i = node.getNodes();
547 while ( i.hasNext() )
549 Node n = i.nextNode();
550 repositories.add( n.getName() );
555 repositories = Collections.emptyList();
558 catch ( RepositoryException e )
561 throw new RuntimeException( e );
566 public List<ArtifactMetadata> getArtifactsByChecksum( String repositoryId, String checksum )
568 List<ArtifactMetadata> artifacts;
570 String q = QUERY_ARTIFACTS + " WHERE [sha1] = $checksum OR [md5] = $checksum";
574 Query query = session.getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
575 ValueFactory valueFactory = session.getValueFactory();
576 query.bindValue( "checksum", valueFactory.createValue( checksum ) );
577 QueryResult result = query.execute();
579 artifacts = new ArrayList<ArtifactMetadata>();
580 for ( Node n : JcrUtils.getNodes( result ) )
582 artifacts.add( getArtifactFromNode( repositoryId, n ) );
585 catch ( RepositoryException e )
588 throw new RuntimeException( e );
593 public void deleteArtifact( String repositoryId, String namespace, String projectId, String projectVersion,
598 Node root = session.getRootNode();
600 "repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion +
602 // TODO: exception if missing?
603 if ( root.hasNode( path ) )
605 root.getNode( path ).remove();
608 catch ( RepositoryException e )
611 throw new RuntimeException( e );
615 public void deleteRepository( String repositoryId )
619 Node root = session.getRootNode();
620 String path = "repositories/" + repositoryId;
621 // TODO: exception if missing?
622 if ( root.hasNode( path ) )
624 root.getNode( path ).remove();
627 catch ( RepositoryException e )
630 throw new RuntimeException( e );
634 public List<ArtifactMetadata> getArtifacts( String repositoryId )
636 List<ArtifactMetadata> artifacts;
638 String q = QUERY_ARTIFACTS;
642 Query query = session.getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
643 QueryResult result = query.execute();
645 artifacts = new ArrayList<ArtifactMetadata>();
646 for ( Node n : JcrUtils.getNodes( result ) )
648 artifacts.add( getArtifactFromNode( repositoryId, n ) );
651 catch ( RepositoryException e )
654 throw new RuntimeException( e );
659 public ProjectMetadata getProject( String repositoryId, String namespace, String projectId )
661 ProjectMetadata metadata = null;
665 Node root = session.getRootNode();
667 // basically just checking it exists
668 String path = "repositories/" + repositoryId + "/content/" + namespace + "/" + projectId;
669 if ( root.hasNode( path ) )
671 metadata = new ProjectMetadata();
672 metadata.setId( projectId );
673 metadata.setNamespace( namespace );
676 catch ( RepositoryException e )
679 throw new RuntimeException( e );
685 public ProjectVersionMetadata getProjectVersion( String repositoryId, String namespace, String projectId,
686 String projectVersion )
687 throws MetadataResolutionException
689 ProjectVersionMetadata versionMetadata;
693 Node root = session.getRootNode();
696 "repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion;
697 if ( !root.hasNode( path ) )
702 Node node = root.getNode( path );
704 versionMetadata = new ProjectVersionMetadata();
705 versionMetadata.setId( projectVersion );
706 versionMetadata.setName( getPropertyString( node, "name" ) );
707 versionMetadata.setDescription( getPropertyString( node, "description" ) );
708 versionMetadata.setUrl( getPropertyString( node, "url" ) );
709 versionMetadata.setIncomplete( node.hasProperty( "incomplete" ) && node.getProperty(
710 "incomplete" ).getBoolean() );
712 // TODO: decide how to treat these in the content repo
713 String scmConnection = getPropertyString( node, "scm.connection" );
714 String scmDeveloperConnection = getPropertyString( node, "scm.developerConnection" );
715 String scmUrl = getPropertyString( node, "scm.url" );
716 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
719 scm.setConnection( scmConnection );
720 scm.setDeveloperConnection( scmDeveloperConnection );
721 scm.setUrl( scmUrl );
722 versionMetadata.setScm( scm );
725 String ciSystem = getPropertyString( node, "ci.system" );
726 String ciUrl = getPropertyString( node, "ci.url" );
727 if ( ciSystem != null || ciUrl != null )
729 CiManagement ci = new CiManagement();
730 ci.setSystem( ciSystem );
732 versionMetadata.setCiManagement( ci );
735 String issueSystem = getPropertyString( node, "issue.system" );
736 String issueUrl = getPropertyString( node, "issue.url" );
737 if ( issueSystem != null || issueUrl != null )
739 IssueManagement issueManagement = new IssueManagement();
740 issueManagement.setSystem( issueSystem );
741 issueManagement.setUrl( issueUrl );
742 versionMetadata.setIssueManagement( issueManagement );
745 String orgName = getPropertyString( node, "org.name" );
746 String orgUrl = getPropertyString( node, "org.url" );
747 if ( orgName != null || orgUrl != null )
749 Organization org = new Organization();
750 org.setName( orgName );
751 org.setUrl( orgUrl );
752 versionMetadata.setOrganization( org );
755 boolean done = false;
759 String licenseName = getPropertyString( node, "license." + i + ".name" );
760 String licenseUrl = getPropertyString( node, "license." + i + ".url" );
761 if ( licenseName != null || licenseUrl != null )
763 License license = new License();
764 license.setName( licenseName );
765 license.setUrl( licenseUrl );
766 versionMetadata.addLicense( license );
779 String mailingListName = getPropertyString( node, "mailingList." + i + ".name" );
780 if ( mailingListName != null )
782 MailingList mailingList = new MailingList();
783 mailingList.setName( mailingListName );
784 mailingList.setMainArchiveUrl( getPropertyString( node, "mailingList." + i + ".archive" ) );
785 String n = "mailingList." + i + ".otherArchives";
786 if ( node.hasProperty( n ) )
788 mailingList.setOtherArchives( Arrays.asList( getPropertyString( node, n ).split( "," ) ) );
792 mailingList.setOtherArchives( Collections.<String>emptyList() );
794 mailingList.setPostAddress( getPropertyString( node, "mailingList." + i + ".post" ) );
795 mailingList.setSubscribeAddress( getPropertyString( node, "mailingList." + i + ".subscribe" ) );
796 mailingList.setUnsubscribeAddress( getPropertyString( node, "mailingList." + i + ".unsubscribe" ) );
797 versionMetadata.addMailingList( mailingList );
810 String dependencyArtifactId = getPropertyString( node, "dependency." + i + ".artifactId" );
811 if ( dependencyArtifactId != null )
813 Dependency dependency = new Dependency();
814 dependency.setArtifactId( dependencyArtifactId );
815 dependency.setGroupId( getPropertyString( node, "dependency." + i + ".groupId" ) );
816 dependency.setClassifier( getPropertyString( node, "dependency." + i + ".classifier" ) );
817 dependency.setOptional( Boolean.valueOf( getPropertyString( node,
818 "dependency." + i + ".optional" ) ) );
819 dependency.setScope( getPropertyString( node, "dependency." + i + ".scope" ) );
820 dependency.setSystemPath( getPropertyString( node, "dependency." + i + ".systemPath" ) );
821 dependency.setType( getPropertyString( node, "dependency." + i + ".type" ) );
822 dependency.setVersion( getPropertyString( node, "dependency." + i + ".version" ) );
823 versionMetadata.addDependency( dependency );
832 for ( Node n : JcrUtils.getChildNodes( node ) )
834 if ( n.isNodeType( FACET_NODE_TYPE ) )
836 String name = n.getName();
837 MetadataFacetFactory factory = metadataFacetFactories.get( name );
838 if ( factory == null )
840 log.error( "Attempted to load unknown project version metadata facet: " + name );
844 MetadataFacet facet = factory.createMetadataFacet();
845 Map<String, String> map = new HashMap<String, String>();
846 for ( Property property : JcrUtils.getProperties( n ) )
848 String p = property.getName();
849 if ( !p.startsWith( "jcr:" ) )
851 map.put( p, property.getString() );
854 facet.fromProperties( map );
855 versionMetadata.addFacet( facet );
860 catch ( RepositoryException e )
863 throw new RuntimeException( e );
866 return versionMetadata;
869 private static String getPropertyString( Node node, String name )
870 throws RepositoryException
872 return node.hasProperty( name ) ? node.getProperty( name ).getString() : null;
875 public Collection<String> getArtifactVersions( String repositoryId, String namespace, String projectId,
876 String projectVersion )
878 Set<String> versions = new LinkedHashSet<String>();
882 Node root = session.getRootNode();
884 Node node = root.getNode(
885 "repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion );
887 NodeIterator iterator = node.getNodes();
888 while ( iterator.hasNext() )
890 Node n = iterator.nextNode();
892 versions.add( n.getProperty( "version" ).getString() );
895 catch ( PathNotFoundException e )
897 // ignore repo not found for now
898 // TODO: throw specific exception if repo doesn't exist
900 catch ( RepositoryException e )
903 throw new RuntimeException( e );
909 public Collection<ProjectVersionReference> getProjectReferences( String repositoryId, String namespace,
910 String projectId, String projectVersion )
912 List<ProjectVersionReference> references = new ArrayList<ProjectVersionReference>();
916 Node root = session.getRootNode();
919 "repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion +
921 if ( root.hasNode( path ) )
923 Node node = root.getNode( path );
925 // TODO: use query by reference type
926 NodeIterator i = node.getNodes();
927 while ( i.hasNext() )
929 Node ns = i.nextNode();
931 NodeIterator j = ns.getNodes();
933 while ( j.hasNext() )
935 Node project = j.nextNode();
937 NodeIterator k = project.getNodes();
939 while ( k.hasNext() )
941 Node version = k.nextNode();
943 ProjectVersionReference ref = new ProjectVersionReference();
944 ref.setNamespace( ns.getName() );
945 ref.setProjectId( project.getName() );
946 ref.setProjectVersion( version.getName() );
947 String type = version.getProperty( "type" ).getString();
948 ref.setReferenceType( ProjectVersionReference.ReferenceType.valueOf( type ) );
949 references.add( ref );
955 catch ( RepositoryException e )
958 throw new RuntimeException( e );
964 public Collection<String> getRootNamespaces( String repositoryId )
966 return getNamespaces( repositoryId, null );
969 private Collection<String> getNodeNames( String path )
971 List<String> names = new ArrayList<String>();
975 Node root = session.getRootNode();
977 Node repository = root.getNode( path );
979 NodeIterator nodes = repository.getNodes();
980 while ( nodes.hasNext() )
982 Node node = nodes.nextNode();
983 names.add( node.getName() );
986 catch ( PathNotFoundException e )
988 // ignore repo not found for now
989 // TODO: throw specific exception if repo doesn't exist
991 catch ( RepositoryException e )
994 throw new RuntimeException( e );
1000 public Collection<String> getNamespaces( String repositoryId, String baseNamespace )
1002 List<String> namespaces = new ArrayList<String>();
1005 Node node = getOrAddRepositoryContentNode( repositoryId );
1006 if ( baseNamespace != null )
1008 node = getOrAddNodeByPath( node, baseNamespace.replace( '.', '/' ) );
1011 for ( Node n : JcrUtils.getChildNodes( node ) )
1013 namespaces.add( n.getName() );
1016 catch ( RepositoryException e )
1019 throw new RuntimeException( e );
1024 public Collection<String> getProjects( String repositoryId, String namespace )
1026 // TODO: could be simpler with pathed namespaces, rely on namespace property
1027 return getNodeNames( "repositories/" + repositoryId + "/content/" + namespace );
1030 public Collection<String> getProjectVersions( String repositoryId, String namespace, String projectId )
1032 // TODO: could be simpler with pathed namespaces, rely on namespace property
1033 return getNodeNames( "repositories/" + repositoryId + "/content/" + namespace + "/" + projectId );
1036 public Collection<ArtifactMetadata> getArtifacts( String repositoryId, String namespace, String projectId,
1037 String projectVersion )
1039 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
1043 Node root = session.getRootNode();
1045 "repositories/" + repositoryId + "/content/" + namespace + "/" + projectId + "/" + projectVersion;
1047 if ( root.hasNode( path ) )
1049 Node node = root.getNode( path );
1051 NodeIterator iterator = node.getNodes();
1052 while ( iterator.hasNext() )
1054 Node artifactNode = iterator.nextNode();
1056 ArtifactMetadata artifact = getArtifactFromNode( repositoryId, artifactNode );
1057 artifacts.add( artifact );
1061 catch ( RepositoryException e )
1064 throw new RuntimeException( e );
1070 private ArtifactMetadata getArtifactFromNode( String repositoryId, Node artifactNode )
1071 throws RepositoryException
1073 String id = artifactNode.getName();
1075 ArtifactMetadata artifact = new ArtifactMetadata();
1076 artifact.setId( id );
1077 artifact.setRepositoryId( repositoryId );
1079 Node projectVersionNode = artifactNode.getParent();
1080 Node projectNode = projectVersionNode.getParent();
1081 Node namespaceNode = projectNode.getParent();
1083 artifact.setNamespace( namespaceNode.getProperty( "namespace" ).getString() );
1084 artifact.setProject( projectNode.getName() );
1085 artifact.setProjectVersion( projectVersionNode.getName() );
1086 artifact.setVersion( artifactNode.hasProperty( "version" )
1087 ? artifactNode.getProperty( "version" ).getString()
1088 : projectVersionNode.getName() );
1090 if ( artifactNode.hasProperty( JCR_LAST_MODIFIED ) )
1092 artifact.setFileLastModified( artifactNode.getProperty( JCR_LAST_MODIFIED ).getDate().getTimeInMillis() );
1095 if ( artifactNode.hasProperty( "whenGathered" ) )
1097 artifact.setWhenGathered( artifactNode.getProperty( "whenGathered" ).getDate().getTime() );
1100 if ( artifactNode.hasProperty( "size" ) )
1102 artifact.setSize( artifactNode.getProperty( "size" ).getLong() );
1105 if ( artifactNode.hasProperty( "md5" ) )
1107 artifact.setMd5( artifactNode.getProperty( "md5" ).getString() );
1110 if ( artifactNode.hasProperty( "sha1" ) )
1112 artifact.setSha1( artifactNode.getProperty( "sha1" ).getString() );
1115 for ( Node n : JcrUtils.getChildNodes( artifactNode ) )
1117 if ( n.isNodeType( FACET_NODE_TYPE ) )
1119 String name = n.getName();
1120 MetadataFacetFactory factory = metadataFacetFactories.get( name );
1121 if ( factory == null )
1123 log.error( "Attempted to load unknown project version metadata facet: " + name );
1127 MetadataFacet facet = factory.createMetadataFacet();
1128 Map<String, String> map = new HashMap<String, String>();
1129 for ( Property p : JcrUtils.getProperties( n ) )
1131 String property = p.getName();
1132 if ( !property.startsWith( "jcr:" ) )
1134 map.put( property, p.getString() );
1137 facet.fromProperties( map );
1138 artifact.addFacet( facet );
1149 // TODO: this shouldn't be here! Repository may need a context
1152 catch ( RepositoryException e )
1155 throw new RuntimeException( e );
1160 public void setMetadataFacetFactories( Map<String, MetadataFacetFactory> metadataFacetFactories )
1162 this.metadataFacetFactories = metadataFacetFactories;
1164 // TODO: check if actually called by normal injection
1166 // for ( String facetId : metadataFacetFactories.keySet() )
1168 // // TODO: second arg should be a better URL for the namespace
1169 // session.getWorkspace().getNamespaceRegistry().registerNamespace( facetId, facetId );
1173 private static String getFacetPath( String repositoryId, String facetId )
1175 return "repositories/" + repositoryId + "/facets/" + facetId;
1178 private Node getOrAddNodeByPath( Node baseNode, String name )
1179 throws RepositoryException
1181 Node node = baseNode;
1182 for ( String n : name.split( "/" ) )
1184 node = JcrUtils.getOrAddNode( node, n );
1189 private static String getFacetPath( String repositoryId, String facetId, String name )
1191 return getFacetPath( repositoryId, facetId ) + "/" + name;
1194 private Node getOrAddRepositoryNode( String repositoryId )
1195 throws RepositoryException
1197 Node root = session.getRootNode();
1198 Node node = JcrUtils.getOrAddNode( root, "repositories" );
1199 node = JcrUtils.getOrAddNode( node, repositoryId );
1203 private Node getOrAddRepositoryContentNode( String repositoryId )
1204 throws RepositoryException
1206 Node node = getOrAddRepositoryNode( repositoryId );
1207 return JcrUtils.getOrAddNode( node, "content" );
1210 private Node getOrAddNamespaceNode( String repositoryId, String namespace )
1211 throws RepositoryException
1213 Node repo = getOrAddRepositoryContentNode( repositoryId );
1214 return getOrAddNodeByPath( repo, namespace.replace( '.', '/' ) );
1217 private Node getOrAddProjectNode( String repositoryId, String namespace, String projectId )
1218 throws RepositoryException
1220 Node namespaceNode = getOrAddNamespaceNode( repositoryId, namespace );
1221 return JcrUtils.getOrAddNode( namespaceNode, projectId );
1224 private Node getOrAddProjectVersionNode( String repositoryId, String namespace, String projectId,
1225 String projectVersion )
1226 throws RepositoryException
1228 Node projectNode = getOrAddProjectNode( repositoryId, namespace, projectId );
1229 return JcrUtils.getOrAddNode( projectNode, projectVersion );
1232 private Node getOrAddArtifactNode( String repositoryId, String namespace, String projectId, String projectVersion,
1234 throws RepositoryException
1236 Node versionNode = getOrAddProjectVersionNode( repositoryId, namespace, projectId, projectVersion );
1237 Node node = JcrUtils.getOrAddNode( versionNode, id );
1238 node.addMixin( ARTIFACT_NODE_TYPE );
1242 private static Calendar createCalendar( Date time )
1244 Calendar cal = Calendar.getInstance();
1245 cal.setTime( time );
1249 private String join( Collection<String> ids )
1251 if ( ids != null && !ids.isEmpty() )
1253 StringBuilder s = new StringBuilder();
1254 for ( String id : ids )
1259 return s.substring( 0, s.length() - 1 );
1264 public Session getJcrSession()