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.model.maven2.MavenArtifactFacet;
36 import org.apache.archiva.metadata.repository.MetadataRepository;
37 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
38 import org.apache.archiva.metadata.repository.MetadataResolutionException;
39 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatistics;
40 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsProvider;
41 import org.apache.commons.lang.StringUtils;
42 import org.apache.jackrabbit.commons.JcrUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.google.common.collect.ImmutableMap;
48 import javax.jcr.InvalidItemStateException;
49 import javax.jcr.NamespaceRegistry;
50 import javax.jcr.Node;
51 import javax.jcr.NodeIterator;
52 import javax.jcr.PathNotFoundException;
53 import javax.jcr.Property;
54 import javax.jcr.Repository;
55 import javax.jcr.RepositoryException;
56 import javax.jcr.Session;
57 import javax.jcr.SimpleCredentials;
58 import javax.jcr.ValueFactory;
59 import javax.jcr.Workspace;
60 import javax.jcr.nodetype.NodeTypeManager;
61 import javax.jcr.nodetype.NodeTypeTemplate;
62 import javax.jcr.query.Query;
63 import javax.jcr.query.QueryManager;
64 import javax.jcr.query.QueryResult;
65 import javax.jcr.query.Row;
66 import javax.jcr.query.RowIterator;
68 import java.util.ArrayList;
69 import java.util.Arrays;
70 import java.util.Calendar;
71 import java.util.Collection;
72 import java.util.Collections;
73 import java.util.Date;
74 import java.util.HashMap;
75 import java.util.Iterator;
76 import java.util.LinkedHashSet;
77 import java.util.List;
79 import java.util.Map.Entry;
83 * TODO below: revise storage format for project version metadata
84 * TODO revise reference storage
86 public class JcrMetadataRepository
87 implements MetadataRepository,RepositoryStatisticsProvider
90 private static final String JCR_LAST_MODIFIED = "jcr:lastModified";
92 static final String NAMESPACE_NODE_TYPE = "archiva:namespace";
94 static final String PROJECT_NODE_TYPE = "archiva:project";
96 static final String PROJECT_VERSION_NODE_TYPE = "archiva:projectVersion";
98 static final String ARTIFACT_NODE_TYPE = "archiva:artifact";
100 static final String FACET_NODE_TYPE = "archiva:facet";
102 private static final String DEPENDENCY_NODE_TYPE = "archiva:dependency";
104 private final Map<String, MetadataFacetFactory> metadataFacetFactories;
106 private Logger log = LoggerFactory.getLogger( JcrMetadataRepository.class );
108 private Repository repository;
110 private Session jcrSession;
112 public JcrMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories, Repository repository )
113 throws RepositoryException
115 this.metadataFacetFactories = metadataFacetFactories;
116 this.repository = repository;
120 public static void initialize( Session session )
121 throws RepositoryException
124 // TODO: consider using namespaces for facets instead of the current approach:
125 // (if used, check if actually called by normal injection)
126 // for ( String facetId : metadataFacetFactories.keySet() )
128 // session.getWorkspace().getNamespaceRegistry().registerNamespace( facetId, facetId );
130 Workspace workspace = session.getWorkspace();
131 NamespaceRegistry registry = workspace.getNamespaceRegistry();
133 if ( !Arrays.asList( registry.getPrefixes() ).contains( "archiva" ) )
135 registry.registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
138 NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
139 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.NAMESPACE_NODE_TYPE );
140 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.PROJECT_NODE_TYPE );
141 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.PROJECT_VERSION_NODE_TYPE );
142 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.ARTIFACT_NODE_TYPE );
143 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.FACET_NODE_TYPE );
144 registerMixinNodeType( nodeTypeManager, JcrMetadataRepository.DEPENDENCY_NODE_TYPE );
148 private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String name )
149 throws RepositoryException
151 NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
152 nodeType.setMixin( true );
153 nodeType.setName( name );
155 // for now just don't re-create - but in future if we change the definition, make sure to remove first as an
157 if ( !nodeTypeManager.hasNodeType( name ) )
159 nodeTypeManager.registerNodeType( nodeType, false );
164 public void updateProject( String repositoryId, ProjectMetadata project )
165 throws MetadataRepositoryException
167 updateProject( repositoryId, project.getNamespace(), project.getId() );
170 private void updateProject( String repositoryId, String namespace, String projectId )
171 throws MetadataRepositoryException
173 updateNamespace( repositoryId, namespace );
177 getOrAddProjectNode( repositoryId, namespace, projectId );
179 catch ( RepositoryException e )
181 throw new MetadataRepositoryException( e.getMessage(), e );
186 public void updateArtifact( String repositoryId, String namespace, String projectId, String projectVersion,
187 ArtifactMetadata artifactMeta )
188 throws MetadataRepositoryException
190 updateNamespace( repositoryId, namespace );
195 getOrAddArtifactNode( repositoryId, namespace, projectId, projectVersion, artifactMeta.getId() );
197 Calendar cal = Calendar.getInstance();
198 cal.setTime( artifactMeta.getFileLastModified() );
199 node.setProperty( JCR_LAST_MODIFIED, cal );
201 cal = Calendar.getInstance();
202 cal.setTime( artifactMeta.getWhenGathered() );
203 node.setProperty( "whenGathered", cal );
205 node.setProperty( "size", artifactMeta.getSize() );
206 node.setProperty( "md5", artifactMeta.getMd5() );
207 node.setProperty( "sha1", artifactMeta.getSha1() );
209 node.setProperty( "version", artifactMeta.getVersion() );
211 // iterate over available facets to update/add/remove from the artifactMetadata
212 for ( String facetId : metadataFacetFactories.keySet() )
214 MetadataFacet metadataFacet = artifactMeta.getFacet( facetId );
215 if ( metadataFacet == null )
219 if ( node.hasNode( facetId ) )
221 node.getNode( facetId ).remove();
223 if ( metadataFacet != null )
225 // recreate, to ensure properties are removed
226 Node n = node.addNode( facetId );
227 n.addMixin( FACET_NODE_TYPE );
229 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
231 n.setProperty( entry.getKey(), entry.getValue() );
236 catch ( RepositoryException e )
238 throw new MetadataRepositoryException( e.getMessage(), e );
243 public void updateProjectVersion( String repositoryId, String namespace, String projectId,
244 ProjectVersionMetadata versionMetadata )
245 throws MetadataRepositoryException
247 updateProject( repositoryId, namespace, projectId );
252 getOrAddProjectVersionNode( repositoryId, namespace, projectId, versionMetadata.getId() );
254 versionNode.setProperty( "name", versionMetadata.getName() );
255 versionNode.setProperty( "description", versionMetadata.getDescription() );
256 versionNode.setProperty( "url", versionMetadata.getUrl() );
257 versionNode.setProperty( "incomplete", versionMetadata.isIncomplete() );
259 // FIXME: decide how to treat these in the content repo
260 if ( versionMetadata.getScm() != null )
262 versionNode.setProperty( "scm.connection", versionMetadata.getScm().getConnection() );
263 versionNode.setProperty( "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() );
264 versionNode.setProperty( "scm.url", versionMetadata.getScm().getUrl() );
266 if ( versionMetadata.getCiManagement() != null )
268 versionNode.setProperty( "ci.system", versionMetadata.getCiManagement().getSystem() );
269 versionNode.setProperty( "ci.url", versionMetadata.getCiManagement().getUrl() );
271 if ( versionMetadata.getIssueManagement() != null )
273 versionNode.setProperty( "issue.system", versionMetadata.getIssueManagement().getSystem() );
274 versionNode.setProperty( "issue.url", versionMetadata.getIssueManagement().getUrl() );
276 if ( versionMetadata.getOrganization() != null )
278 versionNode.setProperty( "org.name", versionMetadata.getOrganization().getName() );
279 versionNode.setProperty( "org.url", versionMetadata.getOrganization().getUrl() );
282 for ( License license : versionMetadata.getLicenses() )
284 versionNode.setProperty( "license." + i + ".name", license.getName() );
285 versionNode.setProperty( "license." + i + ".url", license.getUrl() );
289 for ( MailingList mailingList : versionMetadata.getMailingLists() )
291 versionNode.setProperty( "mailingList." + i + ".archive", mailingList.getMainArchiveUrl() );
292 versionNode.setProperty( "mailingList." + i + ".name", mailingList.getName() );
293 versionNode.setProperty( "mailingList." + i + ".post", mailingList.getPostAddress() );
294 versionNode.setProperty( "mailingList." + i + ".unsubscribe", mailingList.getUnsubscribeAddress() );
295 versionNode.setProperty( "mailingList." + i + ".subscribe", mailingList.getSubscribeAddress() );
296 versionNode.setProperty( "mailingList." + i + ".otherArchives",
297 join( mailingList.getOtherArchives() ) );
301 if ( !versionMetadata.getDependencies().isEmpty() )
303 Node dependenciesNode = JcrUtils.getOrAddNode( versionNode, "dependencies" );
305 for ( Dependency dependency : versionMetadata.getDependencies() )
307 // Note that we deliberately don't alter the namespace path - not enough dependencies for
308 // number of nodes at a given depth to be an issue. Similarly, we don't add subnodes for each
309 // component of the ID as that creates extra depth and causes a great cost in space and memory
311 // FIXME: change group ID to namespace
312 // FIXME: change to artifact's ID - this is constructed by the Maven 2 format for now.
313 // This won't support types where the extension doesn't match the type.
314 // (see also Maven2RepositoryStorage#readProjectVersionMetadata construction of POM)
316 dependency.getGroupId() + ";" + dependency.getArtifactId() + "-" + dependency.getVersion();
317 if ( dependency.getClassifier() != null )
319 id += "-" + dependency.getClassifier();
321 id += "." + dependency.getType();
323 Node n = JcrUtils.getOrAddNode( dependenciesNode, id );
324 n.addMixin( DEPENDENCY_NODE_TYPE );
326 // FIXME: remove temp code just to make it keep working
327 n.setProperty( "groupId", dependency.getGroupId() );
328 n.setProperty( "artifactId", dependency.getArtifactId() );
329 n.setProperty( "version", dependency.getVersion() );
330 n.setProperty( "type", dependency.getType() );
331 n.setProperty( "classifier", dependency.getClassifier() );
332 n.setProperty( "scope", dependency.getScope() );
333 n.setProperty( "systemPath", dependency.getSystemPath() );
334 n.setProperty( "optional", dependency.isOptional() );
336 // node has no native content at this time, just facets
337 // no need to list a type as it's implied by the path. Parents are Maven specific.
339 // FIXME: add scope, systemPath, type, version, classifier & maven2 specific IDs as a facet
340 // (should also have been added to the Dependency)
342 // TODO: add a property that is a weak reference to the originating artifact, creating it if
343 // necessary (without adding the archiva:artifact mixin so that it doesn't get listed as an
344 // artifact, which gives a different meaning to "incomplete" which is a known local project
345 // that doesn't have metadata yet but has artifacts). (Though we may want to give it the
346 // artifact mixin and another property to identify all non-local artifacts for the closure
351 for ( MetadataFacet facet : versionMetadata.getFacetList() )
353 // recreate, to ensure properties are removed
354 if ( versionNode.hasNode( facet.getFacetId() ) )
356 versionNode.getNode( facet.getFacetId() ).remove();
358 Node n = versionNode.addNode( facet.getFacetId() );
359 n.addMixin( FACET_NODE_TYPE );
361 for ( Map.Entry<String, String> entry : facet.toProperties().entrySet() )
363 n.setProperty( entry.getKey(), entry.getValue() );
367 catch ( RepositoryException e )
369 throw new MetadataRepositoryException( e.getMessage(), e );
374 public void updateNamespace( String repositoryId, String namespace )
375 throws MetadataRepositoryException
379 Node node = getOrAddNamespaceNode( repositoryId, namespace );
380 node.setProperty( "namespace", namespace );
382 catch ( RepositoryException e )
384 throw new MetadataRepositoryException( e.getMessage(), e );
389 public void removeProject( String repositoryId, String namespace, String projectId )
390 throws MetadataRepositoryException
394 Node root = getJcrSession().getRootNode();
395 String namespacePath = getNamespacePath( repositoryId, namespace );
397 if ( root.hasNode( namespacePath ) )
399 Iterator<Node> nodeIterator = JcrUtils.getChildNodes( root.getNode( namespacePath ) ).iterator();
400 while ( nodeIterator.hasNext() )
402 Node node = nodeIterator.next();
403 if ( node.isNodeType( PROJECT_NODE_TYPE ) && projectId.equals( node.getName() ) )
411 catch ( RepositoryException e )
413 throw new MetadataRepositoryException( e.getMessage(), e );
420 public boolean hasMetadataFacet( String repositoryId, String facetId )
421 throws MetadataRepositoryException
425 Node node = getJcrSession().getRootNode().getNode( getFacetPath( repositoryId, facetId ) );
426 return node.getNodes().hasNext();
428 catch ( PathNotFoundException e )
430 // ignored - the facet doesn't exist, so return false
433 catch ( RepositoryException e )
435 throw new MetadataRepositoryException( e.getMessage(), e );
440 public List<String> getMetadataFacets( String repositoryId, String facetId )
441 throws MetadataRepositoryException
443 List<String> facets = new ArrayList<>();
447 // no need to construct node-by-node here, as we'll find in the next instance, the facet names have / and
448 // are paths themselves
449 Node node = getJcrSession().getRootNode().getNode( getFacetPath( repositoryId, facetId ) );
451 // TODO: this is a bit awkward. Might be better to review the purpose of this function - why is the list of
453 recurse( facets, "", node );
455 catch ( PathNotFoundException e )
457 // ignored - the facet doesn't exist, so return the empty list
459 catch ( RepositoryException e )
461 throw new MetadataRepositoryException( e.getMessage(), e );
466 private void recurse( List<String> facets, String prefix, Node node )
467 throws RepositoryException
469 for ( Node n : JcrUtils.getChildNodes( node ) )
471 String name = prefix + "/" + n.getName();
474 recurse( facets, name, n );
478 // strip leading / first
479 facets.add( name.substring( 1 ) );
485 public MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name )
486 throws MetadataRepositoryException
488 MetadataFacet metadataFacet = null;
491 Node root = getJcrSession().getRootNode();
492 Node node = root.getNode( getFacetPath( repositoryId, facetId, name ) );
494 if ( metadataFacetFactories == null )
496 return metadataFacet;
499 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
500 if ( metadataFacetFactory != null )
502 metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
503 Map<String, String> map = new HashMap<>();
504 for ( Property property : JcrUtils.getProperties( node ) )
506 String p = property.getName();
507 if ( !p.startsWith( "jcr:" ) )
509 map.put( p, property.getString() );
512 metadataFacet.fromProperties( map );
515 catch ( PathNotFoundException e )
517 // ignored - the facet doesn't exist, so return null
519 catch ( RepositoryException e )
521 throw new MetadataRepositoryException( e.getMessage(), e );
523 return metadataFacet;
527 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
528 throws MetadataRepositoryException
532 Node repo = getOrAddRepositoryNode( repositoryId );
533 Node facets = JcrUtils.getOrAddNode( repo, "facets" );
535 String id = metadataFacet.getFacetId();
536 Node facetNode = JcrUtils.getOrAddNode( facets, id );
538 Node node = getOrAddNodeByPath( facetNode, metadataFacet.getName() );
540 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
542 node.setProperty( entry.getKey(), entry.getValue() );
545 catch ( RepositoryException e )
547 throw new MetadataRepositoryException( e.getMessage(), e );
552 public void removeNamespace( String repositoryId, String projectId )
553 throws MetadataRepositoryException
557 Node root = getJcrSession().getRootNode();
558 String path = getNamespacePath( repositoryId, projectId );
559 if ( root.hasNode( path ) )
561 Node node = root.getNode( path );
562 if ( node.isNodeType( NAMESPACE_NODE_TYPE ) )
568 catch ( RepositoryException e )
570 throw new MetadataRepositoryException( e.getMessage(), e );
575 public void removeMetadataFacets( String repositoryId, String facetId )
576 throws MetadataRepositoryException
580 Node root = getJcrSession().getRootNode();
581 String path = getFacetPath( repositoryId, facetId );
582 if ( root.hasNode( path ) )
584 root.getNode( path ).remove();
587 catch ( RepositoryException e )
589 throw new MetadataRepositoryException( e.getMessage(), e );
594 public void removeMetadataFacet( String repositoryId, String facetId, String name )
595 throws MetadataRepositoryException
599 Node root = getJcrSession().getRootNode();
600 String path = getFacetPath( repositoryId, facetId, name );
601 if ( root.hasNode( path ) )
603 Node node = root.getNode( path );
606 // also remove empty container nodes
607 Node parent = node.getParent();
611 while ( !node.hasNodes() );
614 catch ( RepositoryException e )
616 throw new MetadataRepositoryException( e.getMessage(), e );
621 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date startTime, Date endTime )
622 throws MetadataRepositoryException
624 List<ArtifactMetadata> artifacts;
626 String q = getArtifactQuery( repoId );
628 if ( startTime != null )
630 q += " AND [whenGathered] >= $start";
632 if ( endTime != null )
634 q += " AND [whenGathered] <= $end";
639 Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
640 ValueFactory valueFactory = getJcrSession().getValueFactory();
641 if ( startTime != null )
643 query.bindValue( "start", valueFactory.createValue( createCalendar( startTime ) ) );
645 if ( endTime != null )
647 query.bindValue( "end", valueFactory.createValue( createCalendar( endTime ) ) );
649 QueryResult result = query.execute();
651 artifacts = new ArrayList<>();
652 for ( Node n : JcrUtils.getNodes( result ) )
654 artifacts.add( getArtifactFromNode( repoId, n ) );
657 catch ( RepositoryException e )
659 throw new MetadataRepositoryException( e.getMessage(), e );
665 public Collection<String> getRepositories()
666 throws MetadataRepositoryException
668 List<String> repositories;
672 Node root = getJcrSession().getRootNode();
673 if ( root.hasNode( "repositories" ) )
675 Node node = root.getNode( "repositories" );
677 repositories = new ArrayList<>();
678 NodeIterator i = node.getNodes();
679 while ( i.hasNext() )
681 Node n = i.nextNode();
682 repositories.add( n.getName() );
687 repositories = Collections.emptyList();
690 catch ( RepositoryException e )
692 throw new MetadataRepositoryException( e.getMessage(), e );
698 public List<ArtifactMetadata> getArtifactsByChecksum( String repositoryId, String checksum )
699 throws MetadataRepositoryException
701 List<ArtifactMetadata> artifacts;
703 String q = getArtifactQuery( repositoryId ) + " AND ([sha1] = $checksum OR [md5] = $checksum)";
707 Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
708 ValueFactory valueFactory = getJcrSession().getValueFactory();
709 query.bindValue( "checksum", valueFactory.createValue( checksum ) );
710 QueryResult result = query.execute();
712 artifacts = new ArrayList<>();
713 for ( Node n : JcrUtils.getNodes( result ) )
715 artifacts.add( getArtifactFromNode( repositoryId, n ) );
718 catch ( RepositoryException e )
720 throw new MetadataRepositoryException( e.getMessage(), e );
725 private List<ArtifactMetadata> runJcrQuery( String repositoryId, String q, Map<String, String> bindings )
726 throws MetadataRepositoryException
728 List<ArtifactMetadata> artifacts;
729 if ( repositoryId != null )
731 q += " AND ISDESCENDANTNODE(artifact,'/" + getRepositoryContentPath( repositoryId ) + "')";
734 log.info( "Running JCR Query: {}", q );
738 Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
739 ValueFactory valueFactory = getJcrSession().getValueFactory();
740 for ( Entry<String, String> entry : bindings.entrySet() )
742 query.bindValue( entry.getKey(), valueFactory.createValue( entry.getValue() ) );
744 long start = Calendar.getInstance().getTimeInMillis();
745 QueryResult result = query.execute();
746 long end = Calendar.getInstance().getTimeInMillis();
747 log.info( "JCR Query ran in {} milliseconds: {}", end - start , q );
749 artifacts = new ArrayList<>();
750 RowIterator rows = result.getRows();
751 while ( rows.hasNext() )
753 Row row = rows.nextRow();
754 Node node = row.getNode( "artifact" );
755 artifacts.add( getArtifactFromNode( repositoryId, node ) );
758 catch ( RepositoryException e )
760 throw new MetadataRepositoryException( e.getMessage(), e );
766 public List<ArtifactMetadata> getArtifactsByProjectVersionMetadata( String key, String value, String repositoryId )
767 throws MetadataRepositoryException
770 "SELECT * FROM [" + PROJECT_VERSION_NODE_TYPE + "] AS projectVersion INNER JOIN [" + ARTIFACT_NODE_TYPE
771 + "] AS artifact ON ISCHILDNODE(artifact, projectVersion) INNER JOIN [" + FACET_NODE_TYPE
772 + "] AS facet ON ISCHILDNODE(facet, projectVersion) WHERE ([facet].[" + key + "] = $value)";
774 return runJcrQuery( repositoryId, q, ImmutableMap.of( "value", value ) );
779 public List<ArtifactMetadata> getArtifactsByMetadata( String key, String value, String repositoryId )
780 throws MetadataRepositoryException
783 "SELECT * FROM [" + ARTIFACT_NODE_TYPE + "] AS artifact INNER JOIN [" + FACET_NODE_TYPE
784 + "] AS facet ON ISCHILDNODE(facet, artifact) WHERE ([facet].[" + key + "] = $value)";
786 return runJcrQuery( repositoryId, q, ImmutableMap.of( "value", value ) );
791 public List<ArtifactMetadata> getArtifactsByProperty( String key, String value, String repositoryId )
792 throws MetadataRepositoryException
795 "SELECT * FROM [" + PROJECT_VERSION_NODE_TYPE + "] AS projectVersion INNER JOIN [" + ARTIFACT_NODE_TYPE
796 + "] AS artifact ON ISCHILDNODE(artifact, projectVersion) WHERE ([projectVersion].[" + key
799 return runJcrQuery( repositoryId, q, ImmutableMap.of( "value", value ) );
804 public void removeRepository( String repositoryId )
805 throws MetadataRepositoryException
809 Node root = getJcrSession().getRootNode();
810 String path = getRepositoryPath( repositoryId );
811 if ( root.hasNode( path ) )
813 root.getNode( path ).remove();
816 catch ( RepositoryException e )
818 throw new MetadataRepositoryException( e.getMessage(), e );
823 public List<ArtifactMetadata> getArtifacts( String repositoryId )
824 throws MetadataRepositoryException
826 List<ArtifactMetadata> artifacts;
828 String q = getArtifactQuery( repositoryId );
832 Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
833 QueryResult result = query.execute();
835 artifacts = new ArrayList<>();
836 for ( Node n : JcrUtils.getNodes( result ) )
838 if ( n.isNodeType( ARTIFACT_NODE_TYPE ) )
840 artifacts.add( getArtifactFromNode( repositoryId, n ) );
844 catch ( RepositoryException e )
846 throw new MetadataRepositoryException( e.getMessage(), e );
851 private static String getArtifactQuery( String repositoryId )
853 return "SELECT * FROM [" + ARTIFACT_NODE_TYPE + "] AS artifact WHERE ISDESCENDANTNODE(artifact,'/" +
854 getRepositoryContentPath( repositoryId ) + "')";
858 public ProjectMetadata getProject( String repositoryId, String namespace, String projectId )
859 throws MetadataResolutionException
861 ProjectMetadata metadata = null;
865 Node root = getJcrSession().getRootNode();
867 // basically just checking it exists
868 String path = getProjectPath( repositoryId, namespace, projectId );
869 if ( root.hasNode( path ) )
871 metadata = new ProjectMetadata();
872 metadata.setId( projectId );
873 metadata.setNamespace( namespace );
876 catch ( RepositoryException e )
878 throw new MetadataResolutionException( e.getMessage(), e );
885 public ProjectVersionMetadata getProjectVersion( String repositoryId, String namespace, String projectId,
886 String projectVersion )
887 throws MetadataResolutionException
889 ProjectVersionMetadata versionMetadata;
893 Node root = getJcrSession().getRootNode();
895 String path = getProjectVersionPath( repositoryId, namespace, projectId, projectVersion );
896 if ( !root.hasNode( path ) )
901 Node node = root.getNode( path );
903 versionMetadata = new ProjectVersionMetadata();
904 versionMetadata.setId( projectVersion );
905 versionMetadata.setName( getPropertyString( node, "name" ) );
906 versionMetadata.setDescription( getPropertyString( node, "description" ) );
907 versionMetadata.setUrl( getPropertyString( node, "url" ) );
908 versionMetadata.setIncomplete(
909 node.hasProperty( "incomplete" ) && node.getProperty( "incomplete" ).getBoolean() );
911 // FIXME: decide how to treat these in the content repo
912 String scmConnection = getPropertyString( node, "scm.connection" );
913 String scmDeveloperConnection = getPropertyString( node, "scm.developerConnection" );
914 String scmUrl = getPropertyString( node, "scm.url" );
915 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
918 scm.setConnection( scmConnection );
919 scm.setDeveloperConnection( scmDeveloperConnection );
920 scm.setUrl( scmUrl );
921 versionMetadata.setScm( scm );
924 String ciSystem = getPropertyString( node, "ci.system" );
925 String ciUrl = getPropertyString( node, "ci.url" );
926 if ( ciSystem != null || ciUrl != null )
928 CiManagement ci = new CiManagement();
929 ci.setSystem( ciSystem );
931 versionMetadata.setCiManagement( ci );
934 String issueSystem = getPropertyString( node, "issue.system" );
935 String issueUrl = getPropertyString( node, "issue.url" );
936 if ( issueSystem != null || issueUrl != null )
938 IssueManagement issueManagement = new IssueManagement();
939 issueManagement.setSystem( issueSystem );
940 issueManagement.setUrl( issueUrl );
941 versionMetadata.setIssueManagement( issueManagement );
944 String orgName = getPropertyString( node, "org.name" );
945 String orgUrl = getPropertyString( node, "org.url" );
946 if ( orgName != null || orgUrl != null )
948 Organization org = new Organization();
949 org.setName( orgName );
950 org.setUrl( orgUrl );
951 versionMetadata.setOrganization( org );
954 boolean done = false;
958 String licenseName = getPropertyString( node, "license." + i + ".name" );
959 String licenseUrl = getPropertyString( node, "license." + i + ".url" );
960 if ( licenseName != null || licenseUrl != null )
962 License license = new License();
963 license.setName( licenseName );
964 license.setUrl( licenseUrl );
965 versionMetadata.addLicense( license );
978 String mailingListName = getPropertyString( node, "mailingList." + i + ".name" );
979 if ( mailingListName != null )
981 MailingList mailingList = new MailingList();
982 mailingList.setName( mailingListName );
983 mailingList.setMainArchiveUrl( getPropertyString( node, "mailingList." + i + ".archive" ) );
984 String n = "mailingList." + i + ".otherArchives";
985 if ( node.hasProperty( n ) )
987 mailingList.setOtherArchives( Arrays.asList( getPropertyString( node, n ).split( "," ) ) );
991 mailingList.setOtherArchives( Collections.<String>emptyList() );
993 mailingList.setPostAddress( getPropertyString( node, "mailingList." + i + ".post" ) );
994 mailingList.setSubscribeAddress( getPropertyString( node, "mailingList." + i + ".subscribe" ) );
995 mailingList.setUnsubscribeAddress( getPropertyString( node, "mailingList." + i + ".unsubscribe" ) );
996 versionMetadata.addMailingList( mailingList );
1005 if ( node.hasNode( "dependencies" ) )
1007 Node dependenciesNode = node.getNode( "dependencies" );
1008 for ( Node n : JcrUtils.getChildNodes( dependenciesNode ) )
1010 if ( n.isNodeType( DEPENDENCY_NODE_TYPE ) )
1012 Dependency dependency = new Dependency();
1013 // FIXME: correct these properties
1014 dependency.setArtifactId( getPropertyString( n, "artifactId" ) );
1015 dependency.setGroupId( getPropertyString( n, "groupId" ) );
1016 dependency.setClassifier( getPropertyString( n, "classifier" ) );
1017 dependency.setOptional( Boolean.valueOf( getPropertyString( n, "optional" ) ) );
1018 dependency.setScope( getPropertyString( n, "scope" ) );
1019 dependency.setSystemPath( getPropertyString( n, "systemPath" ) );
1020 dependency.setType( getPropertyString( n, "type" ) );
1021 dependency.setVersion( getPropertyString( n, "version" ) );
1022 versionMetadata.addDependency( dependency );
1027 for ( Node n : JcrUtils.getChildNodes( node ) )
1029 if ( n.isNodeType( FACET_NODE_TYPE ) )
1031 String name = n.getName();
1032 MetadataFacetFactory factory = metadataFacetFactories.get( name );
1033 if ( factory == null )
1035 log.error( "Attempted to load unknown project version metadata facet: {}", name );
1039 MetadataFacet facet = factory.createMetadataFacet();
1040 Map<String, String> map = new HashMap<>();
1041 for ( Property property : JcrUtils.getProperties( n ) )
1043 String p = property.getName();
1044 if ( !p.startsWith( "jcr:" ) )
1046 map.put( p, property.getString() );
1049 facet.fromProperties( map );
1050 versionMetadata.addFacet( facet );
1055 catch ( RepositoryException e )
1057 throw new MetadataResolutionException( e.getMessage(), e );
1060 return versionMetadata;
1064 public Collection<String> getArtifactVersions( String repositoryId, String namespace, String projectId,
1065 String projectVersion )
1066 throws MetadataResolutionException
1068 Set<String> versions = new LinkedHashSet<String>();
1072 Node root = getJcrSession().getRootNode();
1074 Node node = root.getNode( getProjectVersionPath( repositoryId, namespace, projectId, projectVersion ) );
1076 for ( Node n : JcrUtils.getChildNodes( node ) )
1078 versions.add( n.getProperty( "version" ).getString() );
1081 catch ( PathNotFoundException e )
1083 // ignore repo not found for now
1085 catch ( RepositoryException e )
1087 throw new MetadataResolutionException( e.getMessage(), e );
1094 public Collection<ProjectVersionReference> getProjectReferences( String repositoryId, String namespace,
1095 String projectId, String projectVersion )
1096 throws MetadataResolutionException
1099 List<ProjectVersionReference> references = new ArrayList<>();
1101 // TODO: bind variables instead
1102 String q = "SELECT * FROM [archiva:dependency] WHERE ISDESCENDANTNODE([/repositories/" + repositoryId +
1103 "/content]) AND [groupId]='" + namespace + "' AND [artifactId]='" + projectId + "'";
1104 if ( projectVersion != null )
1106 q += " AND [version]='" + projectVersion + "'";
1110 Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 );
1111 QueryResult result = query.execute();
1113 for ( Node n : JcrUtils.getNodes( result ) )
1115 n = n.getParent(); // dependencies grouping element
1117 n = n.getParent(); // project version
1118 String usedByProjectVersion = n.getName();
1120 n = n.getParent(); // project
1121 String usedByProject = n.getName();
1123 n = n.getParent(); // namespace
1124 String usedByNamespace = n.getProperty( "namespace" ).getString();
1126 ProjectVersionReference ref = new ProjectVersionReference();
1127 ref.setNamespace( usedByNamespace );
1128 ref.setProjectId( usedByProject );
1129 ref.setProjectVersion( usedByProjectVersion );
1130 ref.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
1131 references.add( ref );
1134 catch ( RepositoryException e )
1136 throw new MetadataResolutionException( e.getMessage(), e );
1143 public Collection<String> getRootNamespaces( String repositoryId )
1144 throws MetadataResolutionException
1146 return getNamespaces( repositoryId, null );
1150 public Collection<String> getNamespaces( String repositoryId, String baseNamespace )
1151 throws MetadataResolutionException
1153 String path = baseNamespace != null
1154 ? getNamespacePath( repositoryId, baseNamespace )
1155 : getRepositoryContentPath( repositoryId );
1157 return getNodeNames( path, NAMESPACE_NODE_TYPE );
1161 public Collection<String> getProjects( String repositoryId, String namespace )
1162 throws MetadataResolutionException
1164 return getNodeNames( getNamespacePath( repositoryId, namespace ), PROJECT_NODE_TYPE );
1168 public Collection<String> getProjectVersions( String repositoryId, String namespace, String projectId )
1169 throws MetadataResolutionException
1171 return getNodeNames( getProjectPath( repositoryId, namespace, projectId ), PROJECT_VERSION_NODE_TYPE );
1175 public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
1176 throws MetadataRepositoryException
1179 String repositoryId = artifactMetadata.getRepositoryId();
1183 Node root = getJcrSession().getRootNode();
1185 getProjectVersionPath( repositoryId, artifactMetadata.getNamespace(), artifactMetadata.getProject(),
1188 if ( root.hasNode( path ) )
1190 Node node = root.getNode( path );
1192 for ( Node n : JcrUtils.getChildNodes( node ) )
1194 if ( n.isNodeType( ARTIFACT_NODE_TYPE ) )
1196 if ( n.hasProperty( "version" ) )
1198 String version = n.getProperty( "version" ).getString();
1199 if ( StringUtils.equals( version, artifactMetadata.getVersion() ) )
1209 catch ( RepositoryException e )
1211 throw new MetadataRepositoryException( e.getMessage(), e );
1219 public void removeProjectVersion( String repoId, String namespace, String projectId, String projectVersion )
1220 throws MetadataRepositoryException
1225 String path = getProjectPath( repoId, namespace, projectId );
1226 Node root = getJcrSession().getRootNode();
1228 Node nodeAtPath = root.getNode( path );
1230 for ( Node node : JcrUtils.getChildNodes( nodeAtPath ) )
1232 if ( node.isNodeType( PROJECT_VERSION_NODE_TYPE ) && StringUtils.equals( projectVersion,
1239 catch ( RepositoryException e )
1241 throw new MetadataRepositoryException( e.getMessage(), e );
1246 public void removeArtifact( String repositoryId, String namespace, String projectId, String projectVersion,
1248 throws MetadataRepositoryException
1252 Node root = getJcrSession().getRootNode();
1253 String path = getArtifactPath( repositoryId, namespace, projectId, projectVersion, id );
1254 if ( root.hasNode( path ) )
1256 root.getNode( path ).remove();
1261 path = getProjectPath( repositoryId, namespace, projectId );
1263 Node nodeAtPath = root.getNode( path );
1265 for ( Node node : JcrUtils.getChildNodes( nodeAtPath ) )
1267 if ( node.isNodeType( PROJECT_VERSION_NODE_TYPE ) //
1268 && StringUtils.equals( node.getName(), projectVersion ) )
1274 catch ( RepositoryException e )
1276 throw new MetadataRepositoryException( e.getMessage(), e );
1281 public void removeArtifact( String repositoryId, String namespace, String project, String projectVersion,
1282 MetadataFacet metadataFacet )
1283 throws MetadataRepositoryException
1287 Node root = getJcrSession().getRootNode();
1288 String path = getProjectVersionPath( repositoryId, namespace, project, projectVersion );
1290 if ( root.hasNode( path ) )
1292 Node node = root.getNode( path );
1294 for ( Node n : JcrUtils.getChildNodes( node ) )
1296 if ( n.isNodeType( ARTIFACT_NODE_TYPE ) )
1298 ArtifactMetadata artifactMetadata = getArtifactFromNode( repositoryId, n );
1299 log.debug( "artifactMetadata: {}", artifactMetadata );
1300 MetadataFacet metadataFacetToRemove = artifactMetadata.getFacet( metadataFacet.getFacetId() );
1301 if ( metadataFacetToRemove != null && metadataFacet.equals( metadataFacetToRemove ) )
1309 catch ( RepositoryException e )
1311 throw new MetadataRepositoryException( e.getMessage(), e );
1316 public Collection<ArtifactMetadata> getArtifacts( String repositoryId, String namespace, String projectId,
1317 String projectVersion )
1318 throws MetadataResolutionException
1320 List<ArtifactMetadata> artifacts = new ArrayList<>();
1324 Node root = getJcrSession().getRootNode();
1325 String path = getProjectVersionPath( repositoryId, namespace, projectId, projectVersion );
1327 if ( root.hasNode( path ) )
1329 Node node = root.getNode( path );
1331 for ( Node n : JcrUtils.getChildNodes( node ) )
1333 if ( n.isNodeType( ARTIFACT_NODE_TYPE ) )
1335 artifacts.add( getArtifactFromNode( repositoryId, n ) );
1340 catch ( RepositoryException e )
1342 throw new MetadataResolutionException( e.getMessage(), e );
1353 getJcrSession().save();
1354 } catch ( InvalidItemStateException e ) {
1355 // olamy this might happen when deleting a repo while is under scanning
1356 log.warn( "skip InvalidItemStateException:" + e.getMessage(), e );
1358 catch ( RepositoryException e )
1360 throw new RuntimeException( e.getMessage(), e );
1365 public void revert()
1369 getJcrSession().refresh( false );
1371 catch ( RepositoryException e )
1373 throw new RuntimeException( e.getMessage(), e );
1378 public boolean canObtainAccess( Class<?> aClass )
1380 return aClass == Session.class;
1384 public <T>T obtainAccess( Class<T> aClass )
1385 throws MetadataRepositoryException
1387 if ( aClass == Session.class )
1391 return (T) getJcrSession();
1393 catch ( RepositoryException e )
1395 log.error( e.getMessage(), e );
1396 throw new MetadataRepositoryException( e.getMessage(), e );
1399 throw new IllegalArgumentException(
1400 "Access using " + aClass + " is not supported on the JCR metadata storage" );
1405 throws MetadataRepositoryException
1407 if ( jcrSession != null && jcrSession.isLive() )
1409 jcrSession.logout();
1415 * Exact is ignored as we can't do exact search in any property, we need a key
1418 public List<ArtifactMetadata> searchArtifacts( String text, String repositoryId, boolean exact )
1419 throws MetadataRepositoryException
1421 return searchArtifacts( null, text, repositoryId, exact );
1425 public List<ArtifactMetadata> searchArtifacts( String key, String text, String repositoryId, boolean exact )
1426 throws MetadataRepositoryException
1428 // we can't do exact search in any property (*), we need a key
1429 boolean e = exact && key != null;
1430 String theKey = key == null ? "*" : "[" + key + "]";
1431 String projectVersionCondition =
1432 e ? "(projectVersion." + theKey + " = $value)" : "contains([projectVersion]." + theKey + ", $value)";
1433 String facetCondition = e ? "(facet." + theKey + " = $value)" : "contains([facet]." + theKey + ", $value)";
1435 "SELECT * FROM [" + PROJECT_VERSION_NODE_TYPE + "] AS projectVersion LEFT OUTER JOIN ["
1436 + ARTIFACT_NODE_TYPE + "] AS artifact ON ISCHILDNODE(artifact, projectVersion) LEFT OUTER JOIN ["
1437 + FACET_NODE_TYPE + "] AS facet ON ISCHILDNODE(facet, projectVersion) WHERE ("
1438 + projectVersionCondition + " OR " + facetCondition + ")";
1440 return runJcrQuery( repositoryId, q, ImmutableMap.of( "value", text ) );
1443 private ArtifactMetadata getArtifactFromNode( String repositoryId, Node artifactNode )
1444 throws RepositoryException
1446 String id = artifactNode.getName();
1448 ArtifactMetadata artifact = new ArtifactMetadata();
1449 artifact.setId( id );
1450 artifact.setRepositoryId( repositoryId == null ? artifactNode.getAncestor(2).getName() : repositoryId );
1452 Node projectVersionNode = artifactNode.getParent();
1453 Node projectNode = projectVersionNode.getParent();
1454 Node namespaceNode = projectNode.getParent();
1456 artifact.setNamespace( namespaceNode.getProperty( "namespace" ).getString() );
1457 artifact.setProject( projectNode.getName() );
1458 artifact.setProjectVersion( projectVersionNode.getName() );
1459 artifact.setVersion( artifactNode.hasProperty( "version" )
1460 ? artifactNode.getProperty( "version" ).getString()
1461 : projectVersionNode.getName() );
1463 if ( artifactNode.hasProperty( JCR_LAST_MODIFIED ) )
1465 artifact.setFileLastModified( artifactNode.getProperty( JCR_LAST_MODIFIED ).getDate().getTimeInMillis() );
1468 if ( artifactNode.hasProperty( "whenGathered" ) )
1470 artifact.setWhenGathered( artifactNode.getProperty( "whenGathered" ).getDate().getTime() );
1473 if ( artifactNode.hasProperty( "size" ) )
1475 artifact.setSize( artifactNode.getProperty( "size" ).getLong() );
1478 if ( artifactNode.hasProperty( "md5" ) )
1480 artifact.setMd5( artifactNode.getProperty( "md5" ).getString() );
1483 if ( artifactNode.hasProperty( "sha1" ) )
1485 artifact.setSha1( artifactNode.getProperty( "sha1" ).getString() );
1488 for ( Node n : JcrUtils.getChildNodes( artifactNode ) )
1490 if ( n.isNodeType( FACET_NODE_TYPE ) )
1492 String name = n.getName();
1493 MetadataFacetFactory factory = metadataFacetFactories.get( name );
1494 if ( factory == null )
1496 log.error( "Attempted to load unknown project version metadata facet: " + name );
1500 MetadataFacet facet = factory.createMetadataFacet();
1501 Map<String, String> map = new HashMap<>();
1502 for ( Property p : JcrUtils.getProperties( n ) )
1504 String property = p.getName();
1505 if ( !property.startsWith( "jcr:" ) )
1507 map.put( property, p.getString() );
1510 facet.fromProperties( map );
1511 artifact.addFacet( facet );
1518 private static String getPropertyString( Node node, String name )
1519 throws RepositoryException
1521 return node.hasProperty( name ) ? node.getProperty( name ).getString() : null;
1524 private Collection<String> getNodeNames( String path, String nodeType )
1525 throws MetadataResolutionException
1527 List<String> names = new ArrayList<>();
1531 Node root = getJcrSession().getRootNode();
1533 Node nodeAtPath = root.getNode( path );
1535 for ( Node node : JcrUtils.getChildNodes( nodeAtPath ) )
1537 if ( node.isNodeType( nodeType ) )
1539 names.add( node.getName() );
1543 catch ( PathNotFoundException e )
1545 // ignore repo not found for now
1547 catch ( RepositoryException e )
1549 throw new MetadataResolutionException( e.getMessage(), e );
1555 private static String getRepositoryPath( String repositoryId )
1557 return "repositories/" + repositoryId;
1560 private static String getRepositoryContentPath( String repositoryId )
1562 return getRepositoryPath( repositoryId ) + "/content/";
1565 private static String getFacetPath( String repositoryId, String facetId )
1567 return getRepositoryPath( repositoryId ) + "/facets/" + facetId;
1570 private static String getNamespacePath( String repositoryId, String namespace )
1572 return getRepositoryContentPath( repositoryId ) + namespace.replace( '.', '/' );
1575 private static String getProjectPath( String repositoryId, String namespace, String projectId )
1577 return getNamespacePath( repositoryId, namespace ) + "/" + projectId;
1580 private static String getProjectVersionPath( String repositoryId, String namespace, String projectId,
1581 String projectVersion )
1583 return getProjectPath( repositoryId, namespace, projectId ) + "/" + projectVersion;
1586 private static String getArtifactPath( String repositoryId, String namespace, String projectId,
1587 String projectVersion, String id )
1589 return getProjectVersionPath( repositoryId, namespace, projectId, projectVersion ) + "/" + id;
1592 private Node getOrAddNodeByPath( Node baseNode, String name )
1593 throws RepositoryException
1595 return getOrAddNodeByPath( baseNode, name, null );
1598 private Node getOrAddNodeByPath( Node baseNode, String name, String nodeType )
1599 throws RepositoryException
1601 Node node = baseNode;
1602 for ( String n : name.split( "/" ) )
1604 node = JcrUtils.getOrAddNode( node, n );
1605 if ( nodeType != null )
1607 node.addMixin( nodeType );
1613 private static String getFacetPath( String repositoryId, String facetId, String name )
1615 return getFacetPath( repositoryId, facetId ) + "/" + name;
1618 private Node getOrAddRepositoryNode( String repositoryId )
1619 throws RepositoryException
1621 Node root = getJcrSession().getRootNode();
1622 Node node = JcrUtils.getOrAddNode( root, "repositories" );
1623 node = JcrUtils.getOrAddNode( node, repositoryId );
1627 private Node getOrAddRepositoryContentNode( String repositoryId )
1628 throws RepositoryException
1630 Node node = getOrAddRepositoryNode( repositoryId );
1631 return JcrUtils.getOrAddNode( node, "content" );
1634 private Node getOrAddNamespaceNode( String repositoryId, String namespace )
1635 throws RepositoryException
1637 Node repo = getOrAddRepositoryContentNode( repositoryId );
1638 return getOrAddNodeByPath( repo, namespace.replace( '.', '/' ), NAMESPACE_NODE_TYPE );
1641 private Node getOrAddProjectNode( String repositoryId, String namespace, String projectId )
1642 throws RepositoryException
1644 Node namespaceNode = getOrAddNamespaceNode( repositoryId, namespace );
1645 Node node = JcrUtils.getOrAddNode( namespaceNode, projectId );
1646 node.addMixin( PROJECT_NODE_TYPE );
1650 private Node getOrAddProjectVersionNode( String repositoryId, String namespace, String projectId,
1651 String projectVersion )
1652 throws RepositoryException
1654 Node projectNode = getOrAddProjectNode( repositoryId, namespace, projectId );
1655 Node node = JcrUtils.getOrAddNode( projectNode, projectVersion );
1656 node.addMixin( PROJECT_VERSION_NODE_TYPE );
1660 private Node getOrAddArtifactNode( String repositoryId, String namespace, String projectId, String projectVersion,
1662 throws RepositoryException
1664 Node versionNode = getOrAddProjectVersionNode( repositoryId, namespace, projectId, projectVersion );
1665 Node node = JcrUtils.getOrAddNode( versionNode, id );
1666 node.addMixin( ARTIFACT_NODE_TYPE );
1670 private static Calendar createCalendar( Date time )
1672 Calendar cal = Calendar.getInstance();
1673 cal.setTime( time );
1677 private String join( Collection<String> ids )
1679 if ( ids != null && !ids.isEmpty() )
1681 StringBuilder s = new StringBuilder();
1682 for ( String id : ids )
1687 return s.substring( 0, s.length() - 1 );
1692 public Session getJcrSession()
1693 throws RepositoryException
1695 if ( this.jcrSession == null || !this.jcrSession.isLive() )
1697 jcrSession = repository.login( new SimpleCredentials( "admin", "admin".toCharArray() ) );
1699 return this.jcrSession;
1703 public void populateStatistics( MetadataRepository repository, String repositoryId,
1704 RepositoryStatistics repositoryStatistics )
1705 throws MetadataRepositoryException
1707 if (!(repository instanceof JcrMetadataRepository)) {
1708 throw new MetadataRepositoryException( "The statistics population is only possible for JcrMetdataRepository implementations" );
1710 Session session = (Session) repository.obtainAccess( Session.class );
1711 // TODO: these may be best as running totals, maintained by observations on the properties in JCR
1715 QueryManager queryManager = session.getWorkspace().getQueryManager();
1717 // TODO: JCR-SQL2 query will not complete on a large repo in Jackrabbit 2.2.0 - see JCR-2835
1718 // Using the JCR-SQL2 variants gives
1719 // "org.apache.lucene.search.BooleanQuery$TooManyClauses: maxClauseCount is set to 1024"
1720 // String whereClause = "WHERE ISDESCENDANTNODE([/repositories/" + repositoryId + "/content])";
1721 // Query query = queryManager.createQuery( "SELECT size FROM [archiva:artifact] " + whereClause,
1722 // Query.JCR_SQL2 );
1723 String whereClause = "WHERE jcr:path LIKE '/repositories/" + repositoryId + "/content/%'";
1724 Query query = queryManager.createQuery( "SELECT size FROM archiva:artifact " + whereClause, Query.SQL );
1726 QueryResult queryResult = query.execute();
1728 Map<String, Integer> totalByType = new HashMap<>();
1729 long totalSize = 0, totalArtifacts = 0;
1730 for ( Row row : JcrUtils.getRows( queryResult ) )
1732 Node n = row.getNode();
1733 totalSize += row.getValue( "size" ).getLong();
1736 if ( n.hasNode( MavenArtifactFacet.FACET_ID ) )
1738 Node facetNode = n.getNode( MavenArtifactFacet.FACET_ID );
1739 type = facetNode.getProperty( "type" ).getString();
1745 Integer prev = totalByType.get( type );
1746 totalByType.put( type, prev != null ? prev + 1 : 1 );
1751 repositoryStatistics.setTotalArtifactCount( totalArtifacts );
1752 repositoryStatistics.setTotalArtifactFileSize( totalSize );
1753 for ( Map.Entry<String, Integer> entry : totalByType.entrySet() )
1755 System.out.println("Setting count for type: "+entry.getKey()+" = "+entry.getValue());
1756 repositoryStatistics.setTotalCountForType( entry.getKey(), entry.getValue() );
1759 // The query ordering is a trick to ensure that the size is correct, otherwise due to lazy init it will be -1
1760 // query = queryManager.createQuery( "SELECT * FROM [archiva:project] " + whereClause, Query.JCR_SQL2 );
1761 query = queryManager.createQuery( "SELECT * FROM archiva:project " + whereClause + " ORDER BY jcr:score",
1763 repositoryStatistics.setTotalProjectCount( query.execute().getRows().getSize() );
1765 // query = queryManager.createQuery(
1766 // "SELECT * FROM [archiva:namespace] " + whereClause + " AND namespace IS NOT NULL", Query.JCR_SQL2 );
1767 query = queryManager.createQuery(
1768 "SELECT * FROM archiva:namespace " + whereClause + " AND namespace IS NOT NULL ORDER BY jcr:score",
1770 repositoryStatistics.setTotalGroupCount( query.execute().getRows().getSize() );
1772 catch ( RepositoryException e )
1774 throw new MetadataRepositoryException( e.getMessage(), e );