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.MetadataRepositoryException;
37 import org.apache.archiva.metadata.repository.MetadataResolutionException;
38 import org.apache.commons.lang.StringUtils;
39 import org.apache.jackrabbit.commons.JcrUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import javax.jcr.NamespaceRegistry;
44 import javax.jcr.Node;
45 import javax.jcr.NodeIterator;
46 import javax.jcr.PathNotFoundException;
47 import javax.jcr.Property;
48 import javax.jcr.Repository;
49 import javax.jcr.RepositoryException;
50 import javax.jcr.Session;
51 import javax.jcr.SimpleCredentials;
52 import javax.jcr.ValueFactory;
53 import javax.jcr.Workspace;
54 import javax.jcr.nodetype.NodeTypeManager;
55 import javax.jcr.nodetype.NodeTypeTemplate;
56 import javax.jcr.query.Query;
57 import javax.jcr.query.QueryResult;
58 import java.util.ArrayList;
59 import java.util.Arrays;
60 import java.util.Calendar;
61 import java.util.Collection;
62 import java.util.Collections;
63 import java.util.Date;
64 import java.util.HashMap;
65 import java.util.Iterator;
66 import java.util.LinkedHashSet;
67 import java.util.List;
72 * TODO below: revise storage format for project version metadata
73 * TODO revise reference storage
75 public class JcrMetadataRepository
76 implements MetadataRepository
79 private static final String JCR_LAST_MODIFIED = "jcr:lastModified";
81 static final String NAMESPACE_NODE_TYPE = "archiva:namespace";
83 static final String PROJECT_NODE_TYPE = "archiva:project";
85 static final String PROJECT_VERSION_NODE_TYPE = "archiva:projectVersion";
87 static final String ARTIFACT_NODE_TYPE = "archiva:artifact";
89 static final String FACET_NODE_TYPE = "archiva:facet";
91 private static final String DEPENDENCY_NODE_TYPE = "archiva:dependency";
93 private final Map<String, MetadataFacetFactory> metadataFacetFactories;
95 private Logger log = LoggerFactory.getLogger( JcrMetadataRepository.class );
97 private Repository repository;
99 private Session jcrSession;
101 public JcrMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories, Repository repository )
102 throws RepositoryException
104 this.metadataFacetFactories = metadataFacetFactories;
105 this.repository = repository;
109 static void initialize( Session session )
110 throws RepositoryException
113 // TODO: consider using namespaces for facets instead of the current approach:
114 // (if used, check if actually called by normal injection)
115 // for ( String facetId : metadataFacetFactories.keySet() )
117 // session.getWorkspace().getNamespaceRegistry().registerNamespace( facetId, facetId );
120 Workspace workspace = session.getWorkspace();
121 NamespaceRegistry registry = workspace.getNamespaceRegistry();
123 if ( !Arrays.asList( registry.getPrefixes() ).contains( "archiva" ) )
125 registry.registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
128 NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
129 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.NAMESPACE_NODE_TYPE );
130 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.PROJECT_NODE_TYPE );
131 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.PROJECT_VERSION_NODE_TYPE );
132 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.ARTIFACT_NODE_TYPE );
133 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.FACET_NODE_TYPE );
134 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.DEPENDENCY_NODE_TYPE );
138 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String name )
139 throws RepositoryException
141 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
142 nodeType.setMixin( true );
143 nodeType.setName( name );
145 // for now just don't re-create - but in future if we change the definition, make sure to remove first as an
147 if ( !nodeTypeManager.hasNodeType( name ) )
149 nodeTypeManager.registerNodeType( nodeType, false );
153 public void updateProject( String repositoryId, ProjectMetadata project )
154 throws MetadataRepositoryException
156 updateProject( repositoryId, project.getNamespace(), project.getId() );
159 private void updateProject( String repositoryId, String namespace, String projectId )
160 throws MetadataRepositoryException
162 updateNamespace( repositoryId, namespace );
166 getOrAddProjectNode( repositoryId, namespace, projectId );
168 catch ( RepositoryException e )
170 throw new MetadataRepositoryException( e.getMessage(), e );
174 public void updateArtifact( String repositoryId, String namespace, String projectId, String projectVersion,
175 ArtifactMetadata artifactMeta )
176 throws MetadataRepositoryException
178 updateNamespace( repositoryId, namespace );
183 getOrAddArtifactNode( repositoryId, namespace, projectId, projectVersion, artifactMeta.getId() );
185 Calendar cal = Calendar.getInstance();
186 cal.setTime( artifactMeta.getFileLastModified() );
187 node.setProperty( JCR_LAST_MODIFIED, cal );
189 cal = Calendar.getInstance();
190 cal.setTime( artifactMeta.getWhenGathered() );
191 node.setProperty( "whenGathered", cal );
193 node.setProperty( "size", artifactMeta.getSize() );
194 node.setProperty( "md5", artifactMeta.getMd5() );
195 node.setProperty( "sha1", artifactMeta.getSha1() );
197 node.setProperty( "version", artifactMeta.getVersion() );
199 // iterate over available facets to update/add/remove from the artifactMetadata
200 for ( String facetId : metadataFacetFactories.keySet() )
202 MetadataFacet metadataFacet = artifactMeta.getFacet( facetId );
203 if ( metadataFacet == null )
207 if ( node.hasNode( facetId ) )
209 node.getNode( facetId ).remove();
211 if ( metadataFacet != null )
213 // recreate, to ensure properties are removed
214 Node n = node.addNode( facetId );
215 n.addMixin( FACET_NODE_TYPE );
217 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
219 n.setProperty( entry.getKey(), entry.getValue() );
224 catch ( RepositoryException e )
226 throw new MetadataRepositoryException( e.getMessage(), e );
230 public void updateProjectVersion( String repositoryId, String namespace, String projectId,
231 ProjectVersionMetadata versionMetadata )
232 throws MetadataRepositoryException
234 updateProject( repositoryId, namespace, projectId );
239 getOrAddProjectVersionNode( repositoryId, namespace, projectId, versionMetadata.getId() );
241 versionNode.setProperty( "name", versionMetadata.getName() );
242 versionNode.setProperty( "description", versionMetadata.getDescription() );
243 versionNode.setProperty( "url", versionMetadata.getUrl() );
244 versionNode.setProperty( "incomplete", versionMetadata.isIncomplete() );
246 // FIXME: decide how to treat these in the content repo
247 if ( versionMetadata.getScm() != null )
249 versionNode.setProperty( "scm.connection", versionMetadata.getScm().getConnection() );
250 versionNode.setProperty( "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() );
251 versionNode.setProperty( "scm.url", versionMetadata.getScm().getUrl() );
253 if ( versionMetadata.getCiManagement() != null )
255 versionNode.setProperty( "ci.system", versionMetadata.getCiManagement().getSystem() );
256 versionNode.setProperty( "ci.url", versionMetadata.getCiManagement().getUrl() );
258 if ( versionMetadata.getIssueManagement() != null )
260 versionNode.setProperty( "issue.system", versionMetadata.getIssueManagement().getSystem() );
261 versionNode.setProperty( "issue.url", versionMetadata.getIssueManagement().getUrl() );
263 if ( versionMetadata.getOrganization() != null )
265 versionNode.setProperty( "org.name", versionMetadata.getOrganization().getName() );
266 versionNode.setProperty( "org.url", versionMetadata.getOrganization().getUrl() );
269 for ( License license : versionMetadata.getLicenses() )
271 versionNode.setProperty( "license." + i + ".name", license.getName() );
272 versionNode.setProperty( "license." + i + ".url", license.getUrl() );
276 for ( MailingList mailingList : versionMetadata.getMailingLists() )
278 versionNode.setProperty( "mailingList." + i + ".archive", mailingList.getMainArchiveUrl() );
279 versionNode.setProperty( "mailingList." + i + ".name", mailingList.getName() );
280 versionNode.setProperty( "mailingList." + i + ".post", mailingList.getPostAddress() );
281 versionNode.setProperty( "mailingList." + i + ".unsubscribe", mailingList.getUnsubscribeAddress() );
282 versionNode.setProperty( "mailingList." + i + ".subscribe", mailingList.getSubscribeAddress() );
283 versionNode.setProperty( "mailingList." + i + ".otherArchives",
284 join( mailingList.getOtherArchives() ) );
288 if ( !versionMetadata.getDependencies().isEmpty() )
290 Node dependenciesNode = JcrUtils.getOrAddNode( versionNode, "dependencies" );
292 for ( Dependency dependency : versionMetadata.getDependencies() )
294 // Note that we deliberately don't alter the namespace path - not enough dependencies for
295 // number of nodes at a given depth to be an issue. Similarly, we don't add subnodes for each
296 // component of the ID as that creates extra depth and causes a great cost in space and memory
298 // FIXME: change group ID to namespace
299 // FIXME: change to artifact's ID - this is constructed by the Maven 2 format for now.
300 // This won't support types where the extension doesn't match the type.
301 // (see also Maven2RepositoryStorage#readProjectVersionMetadata construction of POM)
303 dependency.getGroupId() + ";" + dependency.getArtifactId() + "-" + dependency.getVersion();
304 if ( dependency.getClassifier() != null )
306 id += "-" + dependency.getClassifier();
308 id += "." + dependency.getType();
310 Node n = JcrUtils.getOrAddNode( dependenciesNode, id );
311 n.addMixin( DEPENDENCY_NODE_TYPE );
313 // FIXME: remove temp code just to make it keep working
314 n.setProperty( "groupId", dependency.getGroupId() );
315 n.setProperty( "artifactId", dependency.getArtifactId() );
316 n.setProperty( "version", dependency.getVersion() );
317 n.setProperty( "type", dependency.getType() );
318 n.setProperty( "classifier", dependency.getClassifier() );
319 n.setProperty( "scope", dependency.getScope() );
320 n.setProperty( "systemPath", dependency.getSystemPath() );
321 n.setProperty( "optional", dependency.isOptional() );
323 // node has no native content at this time, just facets
324 // no need to list a type as it's implied by the path. Parents are Maven specific.
326 // FIXME: add scope, systemPath, type, version, classifier & maven2 specific IDs as a facet
327 // (should also have been added to the Dependency)
329 // TODO: add a property that is a weak reference to the originating artifact, creating it if
330 // necessary (without adding the archiva:artifact mixin so that it doesn't get listed as an
331 // artifact, which gives a different meaning to "incomplete" which is a known local project
332 // that doesn't have metadata yet but has artifacts). (Though we may want to give it the
333 // artifact mixin and another property to identify all non-local artifacts for the closure
338 for ( MetadataFacet facet : versionMetadata.getFacetList() )
340 // recreate, to ensure properties are removed
341 if ( versionNode.hasNode( facet.getFacetId() ) )
343 versionNode.getNode( facet.getFacetId() ).remove();
345 Node n = versionNode.addNode( facet.getFacetId() );
346 n.addMixin( FACET_NODE_TYPE );
348 for ( Map.Entry<String, String> entry : facet.toProperties().entrySet() )
350 n.setProperty( entry.getKey(), entry.getValue() );
354 catch ( RepositoryException e )
356 throw new MetadataRepositoryException( e.getMessage(), e );
360 public void updateNamespace( String repositoryId, String namespace )
361 throws MetadataRepositoryException
365 Node node = getOrAddNamespaceNode( repositoryId, namespace );
366 node.setProperty( "namespace", namespace );
368 catch ( RepositoryException e )
370 throw new MetadataRepositoryException( e.getMessage(), e );
374 public void removeProject( String repositoryId, String namespace, String projectId )
375 throws MetadataRepositoryException
379 Node root = getJcrSession().getRootNode();
380 String namespacePath = getNamespacePath( repositoryId, namespace );
382 if ( root.hasNode( namespacePath ) )
384 Iterator<Node> nodeIterator = JcrUtils.getChildNodes( root.getNode( namespacePath ) ).iterator();
385 while ( nodeIterator.hasNext() )
387 Node node = nodeIterator.next();
388 if ( node.isNodeType( PROJECT_NODE_TYPE ) && projectId.equals( node.getName() ) )
396 catch ( RepositoryException e )
398 throw new MetadataRepositoryException( e.getMessage(), e );
404 public boolean hasMetadataFacet( String repositoryId, String facetId )
405 throws MetadataRepositoryException
409 Node node = getJcrSession().getRootNode().getNode( getFacetPath( repositoryId, facetId ) );
410 return node.getNodes().hasNext();
412 catch ( PathNotFoundException e )
414 // ignored - the facet doesn't exist, so return false
417 catch ( RepositoryException e )
419 throw new MetadataRepositoryException( e.getMessage(), e );
423 public List<String> getMetadataFacets( String repositoryId, String facetId )
424 throws MetadataRepositoryException
426 List<String> facets = new ArrayList<String>();
430 // no need to construct node-by-node here, as we'll find in the next instance, the facet names have / and
431 // are paths themselves
432 Node node = getJcrSession().getRootNode().getNode( getFacetPath( repositoryId, facetId ) );
434 // TODO: this is a bit awkward. Might be better to review the purpose of this function - why is the list of
436 recurse( facets, "", node );
438 catch ( PathNotFoundException e )
440 // ignored - the facet doesn't exist, so return the empty list
442 catch ( RepositoryException e )
444 throw new MetadataRepositoryException( e.getMessage(), e );
449 private void recurse( List<String> facets, String prefix, Node node )
450 throws RepositoryException
452 for ( Node n : JcrUtils.getChildNodes( node ) )
454 String name = prefix + "/" + n.getName();
457 recurse( facets, name, n );
461 // strip leading / first
462 facets.add( name.substring( 1 ) );
467 public MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name )
468 throws MetadataRepositoryException
470 MetadataFacet metadataFacet = null;
473 Node root = getJcrSession().getRootNode();
474 Node node = root.getNode( getFacetPath( repositoryId, facetId, name ) );
476 if ( metadataFacetFactories == null )
478 return metadataFacet;
481 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
482 if ( metadataFacetFactory != null )
484 metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
485 Map<String, String> map = new HashMap<String, String>();
486 for ( Property property : JcrUtils.getProperties( node ) )
488 String p = property.getName();
489 if ( !p.startsWith( "jcr:" ) )
491 map.put( p, property.getString() );
494 metadataFacet.fromProperties( map );
497 catch ( PathNotFoundException e )
499 // ignored - the facet doesn't exist, so return null
501 catch ( RepositoryException e )
503 throw new MetadataRepositoryException( e.getMessage(), e );
505 return metadataFacet;
508 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
509 throws MetadataRepositoryException
513 Node repo = getOrAddRepositoryNode( repositoryId );
514 Node facets = JcrUtils.getOrAddNode( repo, "facets" );
516 String id = metadataFacet.getFacetId();
517 Node facetNode = JcrUtils.getOrAddNode( facets, id );
519 Node node = getOrAddNodeByPath( facetNode, metadataFacet.getName() );
521 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
523 node.setProperty( entry.getKey(), entry.getValue() );
526 catch ( RepositoryException e )
528 throw new MetadataRepositoryException( e.getMessage(), e );
532 public void removeNamespace( String repositoryId, String projectId )
533 throws MetadataRepositoryException
537 Node root = getJcrSession().getRootNode();
538 String path = getNamespacePath( repositoryId, projectId );
539 if ( root.hasNode( path ) )
541 Node node = root.getNode( path );
542 if ( node.isNodeType( NAMESPACE_NODE_TYPE ) )
548 catch ( RepositoryException e )
550 throw new MetadataRepositoryException( e.getMessage(), e );
554 public void removeMetadataFacets( String repositoryId, String facetId )
555 throws MetadataRepositoryException
559 Node root = getJcrSession().getRootNode();
560 String path = getFacetPath( repositoryId, facetId );
561 if ( root.hasNode( path ) )
563 root.getNode( path ).remove();
566 catch ( RepositoryException e )
568 throw new MetadataRepositoryException( e.getMessage(), e );
572 public void removeMetadataFacet( String repositoryId, String facetId, String name )
573 throws MetadataRepositoryException
577 Node root = getJcrSession().getRootNode();
578 String path = getFacetPath( repositoryId, facetId, name );
579 if ( root.hasNode( path ) )
581 Node node = root.getNode( path );
584 // also remove empty container nodes
585 Node parent = node.getParent();
589 while ( !node.hasNodes() );
592 catch ( RepositoryException e )
594 throw new MetadataRepositoryException( e.getMessage(), e );
598 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date startTime, Date endTime )
599 throws MetadataRepositoryException
601 List<ArtifactMetadata> artifacts;
603 String q = getArtifactQuery( repoId );
605 if ( startTime != null )
607 q += " AND [whenGathered] >= $start";
609 if ( endTime != null )
611 q += " AND [whenGathered] <= $end";
616 Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
617 ValueFactory valueFactory = getJcrSession().getValueFactory();
618 if ( startTime != null )
620 query.bindValue( "start", valueFactory.createValue( createCalendar( startTime ) ) );
622 if ( endTime != null )
624 query.bindValue( "end", valueFactory.createValue( createCalendar( endTime ) ) );
626 QueryResult result = query.execute();
628 artifacts = new ArrayList<ArtifactMetadata>();
629 for ( Node n : JcrUtils.getNodes( result ) )
631 artifacts.add( getArtifactFromNode( repoId, n ) );
634 catch ( RepositoryException e )
636 throw new MetadataRepositoryException( e.getMessage(), e );
641 public Collection<String> getRepositories()
642 throws MetadataRepositoryException
644 List<String> repositories;
648 Node root = getJcrSession().getRootNode();
649 if ( root.hasNode( "repositories" ) )
651 Node node = root.getNode( "repositories" );
653 repositories = new ArrayList<String>();
654 NodeIterator i = node.getNodes();
655 while ( i.hasNext() )
657 Node n = i.nextNode();
658 repositories.add( n.getName() );
663 repositories = Collections.emptyList();
666 catch ( RepositoryException e )
668 throw new MetadataRepositoryException( e.getMessage(), e );
673 public List<ArtifactMetadata> getArtifactsByChecksum( String repositoryId, String checksum )
674 throws MetadataRepositoryException
676 List<ArtifactMetadata> artifacts;
678 String q = getArtifactQuery( repositoryId ) + " AND ([sha1] = $checksum OR [md5] = $checksum)";
682 Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
683 ValueFactory valueFactory = getJcrSession().getValueFactory();
684 query.bindValue( "checksum", valueFactory.createValue( checksum ) );
685 QueryResult result = query.execute();
687 artifacts = new ArrayList<ArtifactMetadata>();
688 for ( Node n : JcrUtils.getNodes( result ) )
690 artifacts.add( getArtifactFromNode( repositoryId, n ) );
693 catch ( RepositoryException e )
695 throw new MetadataRepositoryException( e.getMessage(), e );
701 public void removeRepository( String repositoryId )
702 throws MetadataRepositoryException
706 Node root = getJcrSession().getRootNode();
707 String path = getRepositoryPath( repositoryId );
708 if ( root.hasNode( path ) )
710 root.getNode( path ).remove();
713 catch ( RepositoryException e )
715 throw new MetadataRepositoryException( e.getMessage(), e );
719 public List<ArtifactMetadata> getArtifacts( String repositoryId )
720 throws MetadataRepositoryException
722 List<ArtifactMetadata> artifacts;
724 String q = getArtifactQuery( repositoryId );
728 Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
729 QueryResult result = query.execute();
731 artifacts = new ArrayList<ArtifactMetadata>();
732 for ( Node n : JcrUtils.getNodes( result ) )
734 if ( n.isNodeType( ARTIFACT_NODE_TYPE ) )
736 artifacts.add( getArtifactFromNode( repositoryId, n ) );
740 catch ( RepositoryException e )
742 throw new MetadataRepositoryException( e.getMessage(), e );
747 private static String getArtifactQuery( String repositoryId )
749 return "SELECT * FROM [" + ARTIFACT_NODE_TYPE + "] AS artifact WHERE ISDESCENDANTNODE(artifact,'/" +
750 getRepositoryContentPath( repositoryId ) + "')";
753 public ProjectMetadata getProject( String repositoryId, String namespace, String projectId )
754 throws MetadataResolutionException
756 ProjectMetadata metadata = null;
760 Node root = getJcrSession().getRootNode();
762 // basically just checking it exists
763 String path = getProjectPath( repositoryId, namespace, projectId );
764 if ( root.hasNode( path ) )
766 metadata = new ProjectMetadata();
767 metadata.setId( projectId );
768 metadata.setNamespace( namespace );
771 catch ( RepositoryException e )
773 throw new MetadataResolutionException( e.getMessage(), e );
779 public ProjectVersionMetadata getProjectVersion( String repositoryId, String namespace, String projectId,
780 String projectVersion )
781 throws MetadataResolutionException
783 ProjectVersionMetadata versionMetadata;
787 Node root = getJcrSession().getRootNode();
789 String path = getProjectVersionPath( repositoryId, namespace, projectId, projectVersion );
790 if ( !root.hasNode( path ) )
795 Node node = root.getNode( path );
797 versionMetadata = new ProjectVersionMetadata();
798 versionMetadata.setId( projectVersion );
799 versionMetadata.setName( getPropertyString( node, "name" ) );
800 versionMetadata.setDescription( getPropertyString( node, "description" ) );
801 versionMetadata.setUrl( getPropertyString( node, "url" ) );
802 versionMetadata.setIncomplete(
803 node.hasProperty( "incomplete" ) && node.getProperty( "incomplete" ).getBoolean() );
805 // FIXME: decide how to treat these in the content repo
806 String scmConnection = getPropertyString( node, "scm.connection" );
807 String scmDeveloperConnection = getPropertyString( node, "scm.developerConnection" );
808 String scmUrl = getPropertyString( node, "scm.url" );
809 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
812 scm.setConnection( scmConnection );
813 scm.setDeveloperConnection( scmDeveloperConnection );
814 scm.setUrl( scmUrl );
815 versionMetadata.setScm( scm );
818 String ciSystem = getPropertyString( node, "ci.system" );
819 String ciUrl = getPropertyString( node, "ci.url" );
820 if ( ciSystem != null || ciUrl != null )
822 CiManagement ci = new CiManagement();
823 ci.setSystem( ciSystem );
825 versionMetadata.setCiManagement( ci );
828 String issueSystem = getPropertyString( node, "issue.system" );
829 String issueUrl = getPropertyString( node, "issue.url" );
830 if ( issueSystem != null || issueUrl != null )
832 IssueManagement issueManagement = new IssueManagement();
833 issueManagement.setSystem( issueSystem );
834 issueManagement.setUrl( issueUrl );
835 versionMetadata.setIssueManagement( issueManagement );
838 String orgName = getPropertyString( node, "org.name" );
839 String orgUrl = getPropertyString( node, "org.url" );
840 if ( orgName != null || orgUrl != null )
842 Organization org = new Organization();
843 org.setName( orgName );
844 org.setUrl( orgUrl );
845 versionMetadata.setOrganization( org );
848 boolean done = false;
852 String licenseName = getPropertyString( node, "license." + i + ".name" );
853 String licenseUrl = getPropertyString( node, "license." + i + ".url" );
854 if ( licenseName != null || licenseUrl != null )
856 License license = new License();
857 license.setName( licenseName );
858 license.setUrl( licenseUrl );
859 versionMetadata.addLicense( license );
872 String mailingListName = getPropertyString( node, "mailingList." + i + ".name" );
873 if ( mailingListName != null )
875 MailingList mailingList = new MailingList();
876 mailingList.setName( mailingListName );
877 mailingList.setMainArchiveUrl( getPropertyString( node, "mailingList." + i + ".archive" ) );
878 String n = "mailingList." + i + ".otherArchives";
879 if ( node.hasProperty( n ) )
881 mailingList.setOtherArchives( Arrays.asList( getPropertyString( node, n ).split( "," ) ) );
885 mailingList.setOtherArchives( Collections.<String>emptyList() );
887 mailingList.setPostAddress( getPropertyString( node, "mailingList." + i + ".post" ) );
888 mailingList.setSubscribeAddress( getPropertyString( node, "mailingList." + i + ".subscribe" ) );
889 mailingList.setUnsubscribeAddress( getPropertyString( node, "mailingList." + i + ".unsubscribe" ) );
890 versionMetadata.addMailingList( mailingList );
899 if ( node.hasNode( "dependencies" ) )
901 Node dependenciesNode = node.getNode( "dependencies" );
902 for ( Node n : JcrUtils.getChildNodes( dependenciesNode ) )
904 if ( n.isNodeType( DEPENDENCY_NODE_TYPE ) )
906 Dependency dependency = new Dependency();
907 // FIXME: correct these properties
908 dependency.setArtifactId( getPropertyString( n, "artifactId" ) );
909 dependency.setGroupId( getPropertyString( n, "groupId" ) );
910 dependency.setClassifier( getPropertyString( n, "classifier" ) );
911 dependency.setOptional( Boolean.valueOf( getPropertyString( n, "optional" ) ) );
912 dependency.setScope( getPropertyString( n, "scope" ) );
913 dependency.setSystemPath( getPropertyString( n, "systemPath" ) );
914 dependency.setType( getPropertyString( n, "type" ) );
915 dependency.setVersion( getPropertyString( n, "version" ) );
916 versionMetadata.addDependency( dependency );
921 for ( Node n : JcrUtils.getChildNodes( node ) )
923 if ( n.isNodeType( FACET_NODE_TYPE ) )
925 String name = n.getName();
926 MetadataFacetFactory factory = metadataFacetFactories.get( name );
927 if ( factory == null )
929 log.error( "Attempted to load unknown project version metadata facet: {}", name );
933 MetadataFacet facet = factory.createMetadataFacet();
934 Map<String, String> map = new HashMap<String, String>();
935 for ( Property property : JcrUtils.getProperties( n ) )
937 String p = property.getName();
938 if ( !p.startsWith( "jcr:" ) )
940 map.put( p, property.getString() );
943 facet.fromProperties( map );
944 versionMetadata.addFacet( facet );
949 catch ( RepositoryException e )
951 throw new MetadataResolutionException( e.getMessage(), e );
954 return versionMetadata;
957 public Collection<String> getArtifactVersions( String repositoryId, String namespace, String projectId,
958 String projectVersion )
959 throws MetadataResolutionException
961 Set<String> versions = new LinkedHashSet<String>();
965 Node root = getJcrSession().getRootNode();
967 Node node = root.getNode( getProjectVersionPath( repositoryId, namespace, projectId, projectVersion ) );
969 for ( Node n : JcrUtils.getChildNodes( node ) )
971 versions.add( n.getProperty( "version" ).getString() );
974 catch ( PathNotFoundException e )
976 // ignore repo not found for now
978 catch ( RepositoryException e )
980 throw new MetadataResolutionException( e.getMessage(), e );
986 public Collection<ProjectVersionReference> getProjectReferences( String repositoryId, String namespace,
987 String projectId, String projectVersion )
988 throws MetadataResolutionException
990 List<ProjectVersionReference> references = new ArrayList<ProjectVersionReference>();
992 // TODO: bind variables instead
993 String q = "SELECT * FROM [archiva:dependency] WHERE ISDESCENDANTNODE([/repositories/" + repositoryId +
994 "/content]) AND [groupId]='" + namespace + "' AND [artifactId]='" + projectId + "'";
995 if ( projectVersion != null )
997 q += " AND [version]='" + projectVersion + "'";
1001 Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
1002 QueryResult result = query.execute();
1004 for ( Node n : JcrUtils.getNodes( result ) )
1006 n = n.getParent(); // dependencies grouping element
1008 n = n.getParent(); // project version
1009 String usedByProjectVersion = n.getName();
1011 n = n.getParent(); // project
1012 String usedByProject = n.getName();
1014 n = n.getParent(); // namespace
1015 String usedByNamespace = n.getProperty( "namespace" ).getString();
1017 ProjectVersionReference ref = new ProjectVersionReference();
1018 ref.setNamespace( usedByNamespace );
1019 ref.setProjectId( usedByProject );
1020 ref.setProjectVersion( usedByProjectVersion );
1021 ref.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
1022 references.add( ref );
1025 catch ( RepositoryException e )
1027 throw new MetadataResolutionException( e.getMessage(), e );
1033 public Collection<String> getRootNamespaces( String repositoryId )
1034 throws MetadataResolutionException
1036 return getNamespaces( repositoryId, null );
1039 public Collection<String> getNamespaces( String repositoryId, String baseNamespace )
1040 throws MetadataResolutionException
1042 String path = baseNamespace != null
1043 ? getNamespacePath( repositoryId, baseNamespace )
1044 : getRepositoryContentPath( repositoryId );
1046 return getNodeNames( path, NAMESPACE_NODE_TYPE );
1049 public Collection<String> getProjects( String repositoryId, String namespace )
1050 throws MetadataResolutionException
1052 return getNodeNames( getNamespacePath( repositoryId, namespace ), PROJECT_NODE_TYPE );
1055 public Collection<String> getProjectVersions( String repositoryId, String namespace, String projectId )
1056 throws MetadataResolutionException
1058 return getNodeNames( getProjectPath( repositoryId, namespace, projectId ), PROJECT_VERSION_NODE_TYPE );
1061 public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
1062 throws MetadataRepositoryException
1065 String repositoryId = artifactMetadata.getRepositoryId();
1069 Node root = getJcrSession().getRootNode();
1071 getProjectVersionPath( repositoryId, artifactMetadata.getNamespace(), artifactMetadata.getProject(),
1074 if ( root.hasNode( path ) )
1076 Node node = root.getNode( path );
1078 for ( Node n : JcrUtils.getChildNodes( node ) )
1080 if ( n.isNodeType( ARTIFACT_NODE_TYPE ) )
1082 if ( n.hasProperty( "version" ) )
1084 String version = n.getProperty( "version" ).getString();
1085 if ( StringUtils.equals( version, artifactMetadata.getVersion() ) )
1095 catch ( RepositoryException e )
1097 throw new MetadataRepositoryException( e.getMessage(), e );
1104 public void removeProjectVersion( String repoId, String namespace, String projectId, String projectVersion )
1105 throws MetadataRepositoryException
1110 String path = getProjectPath( repoId, namespace, projectId );
1111 Node root = getJcrSession().getRootNode();
1113 Node nodeAtPath = root.getNode( path );
1115 for ( Node node : JcrUtils.getChildNodes( nodeAtPath ) )
1117 if ( node.isNodeType( PROJECT_VERSION_NODE_TYPE ) && StringUtils.equals( projectVersion,
1124 catch ( RepositoryException e )
1126 throw new MetadataRepositoryException( e.getMessage(), e );
1130 public void removeArtifact( String repositoryId, String namespace, String projectId, String projectVersion,
1132 throws MetadataRepositoryException
1136 Node root = getJcrSession().getRootNode();
1137 String path = getArtifactPath( repositoryId, namespace, projectId, projectVersion, id );
1138 if ( root.hasNode( path ) )
1140 root.getNode( path ).remove();
1145 path = getProjectPath( repositoryId, namespace, projectId );
1147 Node nodeAtPath = root.getNode( path );
1149 for ( Node node : JcrUtils.getChildNodes( nodeAtPath ) )
1151 if ( node.isNodeType( PROJECT_VERSION_NODE_TYPE ) && StringUtils.equals( node.getName(),
1158 catch ( RepositoryException e )
1160 throw new MetadataRepositoryException( e.getMessage(), e );
1164 public void removeArtifact( String repositoryId, String namespace, String project, String projectVersion,
1165 MetadataFacet metadataFacet )
1166 throws MetadataRepositoryException
1170 Node root = getJcrSession().getRootNode();
1171 String path = getProjectVersionPath( repositoryId, namespace, project, projectVersion );
1173 if ( root.hasNode( path ) )
1175 Node node = root.getNode( path );
1177 for ( Node n : JcrUtils.getChildNodes( node ) )
1179 if ( n.isNodeType( ARTIFACT_NODE_TYPE ) )
1181 ArtifactMetadata artifactMetadata = getArtifactFromNode( repositoryId, n );
1182 log.debug( "artifactMetadata: {}", artifactMetadata );
1183 MetadataFacet metadataFacetToRemove = artifactMetadata.getFacet( metadataFacet.getFacetId() );
1184 if ( metadataFacetToRemove != null && metadataFacet.equals( metadataFacetToRemove ) )
1192 catch ( RepositoryException e )
1194 throw new MetadataRepositoryException( e.getMessage(), e );
1198 public Collection<ArtifactMetadata> getArtifacts( String repositoryId, String namespace, String projectId,
1199 String projectVersion )
1200 throws MetadataResolutionException
1202 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
1206 Node root = getJcrSession().getRootNode();
1207 String path = getProjectVersionPath( repositoryId, namespace, projectId, projectVersion );
1209 if ( root.hasNode( path ) )
1211 Node node = root.getNode( path );
1213 for ( Node n : JcrUtils.getChildNodes( node ) )
1215 if ( n.isNodeType( ARTIFACT_NODE_TYPE ) )
1217 artifacts.add( getArtifactFromNode( repositoryId, n ) );
1222 catch ( RepositoryException e )
1224 throw new MetadataResolutionException( e.getMessage(), e );
1234 getJcrSession().save();
1236 catch ( RepositoryException e )
1238 throw new RuntimeException( e.getMessage(), e );
1242 public void revert()
1246 getJcrSession().refresh( false );
1248 catch ( RepositoryException e )
1250 throw new RuntimeException( e.getMessage(), e );
1254 public boolean canObtainAccess( Class<?> aClass )
1256 return aClass == Session.class;
1259 public <T>T obtainAccess( Class<T> aClass )
1260 throws MetadataRepositoryException
1262 if ( aClass == Session.class )
1266 return (T) getJcrSession();
1268 catch ( RepositoryException e )
1270 log.error( e.getMessage(), e );
1271 throw new MetadataRepositoryException( e.getMessage(), e );
1274 throw new IllegalArgumentException(
1275 "Access using " + aClass + " is not supported on the JCR metadata storage" );
1279 throws MetadataRepositoryException
1283 if ( getJcrSession().isLive() )
1285 getJcrSession().logout();
1288 catch ( RepositoryException e )
1290 log.error( e.getMessage(), e );
1291 throw new MetadataRepositoryException( e.getMessage(), e );
1295 private ArtifactMetadata getArtifactFromNode( String repositoryId, Node artifactNode )
1296 throws RepositoryException
1298 String id = artifactNode.getName();
1300 ArtifactMetadata artifact = new ArtifactMetadata();
1301 artifact.setId( id );
1302 artifact.setRepositoryId( repositoryId );
1304 Node projectVersionNode = artifactNode.getParent();
1305 Node projectNode = projectVersionNode.getParent();
1306 Node namespaceNode = projectNode.getParent();
1308 artifact.setNamespace( namespaceNode.getProperty( "namespace" ).getString() );
1309 artifact.setProject( projectNode.getName() );
1310 artifact.setProjectVersion( projectVersionNode.getName() );
1311 artifact.setVersion( artifactNode.hasProperty( "version" )
1312 ? artifactNode.getProperty( "version" ).getString()
1313 : projectVersionNode.getName() );
1315 if ( artifactNode.hasProperty( JCR_LAST_MODIFIED ) )
1317 artifact.setFileLastModified( artifactNode.getProperty( JCR_LAST_MODIFIED ).getDate().getTimeInMillis() );
1320 if ( artifactNode.hasProperty( "whenGathered" ) )
1322 artifact.setWhenGathered( artifactNode.getProperty( "whenGathered" ).getDate().getTime() );
1325 if ( artifactNode.hasProperty( "size" ) )
1327 artifact.setSize( artifactNode.getProperty( "size" ).getLong() );
1330 if ( artifactNode.hasProperty( "md5" ) )
1332 artifact.setMd5( artifactNode.getProperty( "md5" ).getString() );
1335 if ( artifactNode.hasProperty( "sha1" ) )
1337 artifact.setSha1( artifactNode.getProperty( "sha1" ).getString() );
1340 for ( Node n : JcrUtils.getChildNodes( artifactNode ) )
1342 if ( n.isNodeType( FACET_NODE_TYPE ) )
1344 String name = n.getName();
1345 MetadataFacetFactory factory = metadataFacetFactories.get( name );
1346 if ( factory == null )
1348 log.error( "Attempted to load unknown project version metadata facet: " + name );
1352 MetadataFacet facet = factory.createMetadataFacet();
1353 Map<String, String> map = new HashMap<String, String>();
1354 for ( Property p : JcrUtils.getProperties( n ) )
1356 String property = p.getName();
1357 if ( !property.startsWith( "jcr:" ) )
1359 map.put( property, p.getString() );
1362 facet.fromProperties( map );
1363 artifact.addFacet( facet );
1370 private static String getPropertyString( Node node, String name )
1371 throws RepositoryException
1373 return node.hasProperty( name ) ? node.getProperty( name ).getString() : null;
1376 private Collection<String> getNodeNames( String path, String nodeType )
1377 throws MetadataResolutionException
1379 List<String> names = new ArrayList<String>();
1383 Node root = getJcrSession().getRootNode();
1385 Node nodeAtPath = root.getNode( path );
1387 for ( Node node : JcrUtils.getChildNodes( nodeAtPath ) )
1389 if ( node.isNodeType( nodeType ) )
1391 names.add( node.getName() );
1395 catch ( PathNotFoundException e )
1397 // ignore repo not found for now
1399 catch ( RepositoryException e )
1401 throw new MetadataResolutionException( e.getMessage(), e );
1407 private static String getRepositoryPath( String repositoryId )
1409 return "repositories/" + repositoryId;
1412 private static String getRepositoryContentPath( String repositoryId )
1414 return getRepositoryPath( repositoryId ) + "/content/";
1417 private static String getFacetPath( String repositoryId, String facetId )
1419 return getRepositoryPath( repositoryId ) + "/facets/" + facetId;
1422 private static String getNamespacePath( String repositoryId, String namespace )
1424 return getRepositoryContentPath( repositoryId ) + namespace.replace( '.', '/' );
1427 private static String getProjectPath( String repositoryId, String namespace, String projectId )
1429 return getNamespacePath( repositoryId, namespace ) + "/" + projectId;
1432 private static String getProjectVersionPath( String repositoryId, String namespace, String projectId,
1433 String projectVersion )
1435 return getProjectPath( repositoryId, namespace, projectId ) + "/" + projectVersion;
1438 private static String getArtifactPath( String repositoryId, String namespace, String projectId,
1439 String projectVersion, String id )
1441 return getProjectVersionPath( repositoryId, namespace, projectId, projectVersion ) + "/" + id;
1444 private Node getOrAddNodeByPath( Node baseNode, String name )
1445 throws RepositoryException
1447 return getOrAddNodeByPath( baseNode, name, null );
1450 private Node getOrAddNodeByPath( Node baseNode, String name, String nodeType )
1451 throws RepositoryException
1453 Node node = baseNode;
1454 for ( String n : name.split( "/" ) )
1456 node = JcrUtils.getOrAddNode( node, n );
1457 if ( nodeType != null )
1459 node.addMixin( nodeType );
1465 private static String getFacetPath( String repositoryId, String facetId, String name )
1467 return getFacetPath( repositoryId, facetId ) + "/" + name;
1470 private Node getOrAddRepositoryNode( String repositoryId )
1471 throws RepositoryException
1473 Node root = getJcrSession().getRootNode();
1474 Node node = JcrUtils.getOrAddNode( root, "repositories" );
1475 node = JcrUtils.getOrAddNode( node, repositoryId );
1479 private Node getOrAddRepositoryContentNode( String repositoryId )
1480 throws RepositoryException
1482 Node node = getOrAddRepositoryNode( repositoryId );
1483 return JcrUtils.getOrAddNode( node, "content" );
1486 private Node getOrAddNamespaceNode( String repositoryId, String namespace )
1487 throws RepositoryException
1489 Node repo = getOrAddRepositoryContentNode( repositoryId );
1490 return getOrAddNodeByPath( repo, namespace.replace( '.', '/' ), NAMESPACE_NODE_TYPE );
1493 private Node getOrAddProjectNode( String repositoryId, String namespace, String projectId )
1494 throws RepositoryException
1496 Node namespaceNode = getOrAddNamespaceNode( repositoryId, namespace );
1497 Node node = JcrUtils.getOrAddNode( namespaceNode, projectId );
1498 node.addMixin( PROJECT_NODE_TYPE );
1502 private Node getOrAddProjectVersionNode( String repositoryId, String namespace, String projectId,
1503 String projectVersion )
1504 throws RepositoryException
1506 Node projectNode = getOrAddProjectNode( repositoryId, namespace, projectId );
1507 Node node = JcrUtils.getOrAddNode( projectNode, projectVersion );
1508 node.addMixin( PROJECT_VERSION_NODE_TYPE );
1512 private Node getOrAddArtifactNode( String repositoryId, String namespace, String projectId, String projectVersion,
1514 throws RepositoryException
1516 Node versionNode = getOrAddProjectVersionNode( repositoryId, namespace, projectId, projectVersion );
1517 Node node = JcrUtils.getOrAddNode( versionNode, id );
1518 node.addMixin( ARTIFACT_NODE_TYPE );
1522 private static Calendar createCalendar( Date time )
1524 Calendar cal = Calendar.getInstance();
1525 cal.setTime( time );
1529 private String join( Collection<String> ids )
1531 if ( ids != null && !ids.isEmpty() )
1533 StringBuilder s = new StringBuilder();
1534 for ( String id : ids )
1539 return s.substring( 0, s.length() - 1 );
1544 public Session getJcrSession()
1545 throws RepositoryException
1547 if ( this.jcrSession == null || !this.jcrSession.isLive() )
1549 jcrSession = repository.login( new SimpleCredentials( "admin", "admin".toCharArray() ) );
1551 return this.jcrSession;