1 package org.apache.archiva.metadata.repository.file;
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.commons.io.FileUtils;
37 import org.apache.commons.io.IOUtils;
38 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 import java.io.FileInputStream;
44 import java.io.FileNotFoundException;
45 import java.io.FileOutputStream;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.Collection;
50 import java.util.Collections;
51 import java.util.Comparator;
52 import java.util.Date;
53 import java.util.HashMap;
54 import java.util.HashSet;
55 import java.util.LinkedHashSet;
56 import java.util.List;
58 import java.util.Properties;
60 import java.util.StringTokenizer;
63 * @plexus.component role="org.apache.archiva.metadata.repository.MetadataRepository"
65 public class FileMetadataRepository
66 implements MetadataRepository
69 * @plexus.requirement role="org.apache.archiva.metadata.model.MetadataFacetFactory"
71 private Map<String, MetadataFacetFactory> metadataFacetFactories;
76 private ArchivaConfiguration configuration;
78 private static final Logger log = LoggerFactory.getLogger( FileMetadataRepository.class );
80 private static final String PROJECT_METADATA_KEY = "project-metadata";
82 private static final String PROJECT_VERSION_METADATA_KEY = "version-metadata";
84 private static final String NAMESPACE_METADATA_KEY = "namespace-metadata";
86 private static final String METADATA_KEY = "metadata";
88 private File getBaseDirectory( String repoId )
90 // TODO: should be configurable, like the index
91 String basedir = configuration.getConfiguration().getManagedRepositoriesAsMap().get( repoId ).getLocation();
92 File dir = new File( basedir, ".archiva" );
96 private File getDirectory( String repoId )
98 return new File( getBaseDirectory( repoId ), "content" );
101 public void updateProject( String repoId, ProjectMetadata project )
103 updateProject( repoId, project.getNamespace(), project.getId() );
106 private void updateProject( String repoId, String namespace, String id )
108 // TODO: this is a more braindead implementation than we would normally expect, for prototyping purposes
109 updateNamespace( repoId, namespace );
113 File namespaceDirectory = new File( getDirectory( repoId ), namespace );
114 Properties properties = new Properties();
115 properties.setProperty( "namespace", namespace );
116 properties.setProperty( "id", id );
117 writeProperties( properties, new File( namespaceDirectory, id ), PROJECT_METADATA_KEY );
119 catch ( IOException e )
126 public void updateProjectVersion( String repoId, String namespace, String projectId,
127 ProjectVersionMetadata versionMetadata )
129 updateProject( repoId, namespace, projectId );
131 File directory = new File( getDirectory( repoId ),
132 namespace + "/" + projectId + "/" + versionMetadata.getId() );
134 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
135 // remove properties that are not references or artifacts
136 for ( Object key : new ArrayList( properties.keySet() ) )
138 String name = (String) key;
139 if ( !name.contains( ":" ) && !name.equals( "facetIds" ) )
141 properties.remove( name );
144 properties.setProperty( "id", versionMetadata.getId() );
145 setProperty( properties, "name", versionMetadata.getName() );
146 setProperty( properties, "description", versionMetadata.getDescription() );
147 setProperty( properties, "url", versionMetadata.getUrl() );
148 setProperty( properties, "incomplete", String.valueOf( versionMetadata.isIncomplete() ) );
149 if ( versionMetadata.getScm() != null )
151 setProperty( properties, "scm.connection", versionMetadata.getScm().getConnection() );
152 setProperty( properties, "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() );
153 setProperty( properties, "scm.url", versionMetadata.getScm().getUrl() );
155 if ( versionMetadata.getCiManagement() != null )
157 setProperty( properties, "ci.system", versionMetadata.getCiManagement().getSystem() );
158 setProperty( properties, "ci.url", versionMetadata.getCiManagement().getUrl() );
160 if ( versionMetadata.getIssueManagement() != null )
162 setProperty( properties, "issue.system", versionMetadata.getIssueManagement().getSystem() );
163 setProperty( properties, "issue.url", versionMetadata.getIssueManagement().getUrl() );
165 if ( versionMetadata.getOrganization() != null )
167 setProperty( properties, "org.name", versionMetadata.getOrganization().getName() );
168 setProperty( properties, "org.url", versionMetadata.getOrganization().getUrl() );
171 for ( License license : versionMetadata.getLicenses() )
173 setProperty( properties, "license." + i + ".name", license.getName() );
174 setProperty( properties, "license." + i + ".url", license.getUrl() );
178 for ( MailingList mailingList : versionMetadata.getMailingLists() )
180 setProperty( properties, "mailingList." + i + ".archive", mailingList.getMainArchiveUrl() );
181 setProperty( properties, "mailingList." + i + ".name", mailingList.getName() );
182 setProperty( properties, "mailingList." + i + ".post", mailingList.getPostAddress() );
183 setProperty( properties, "mailingList." + i + ".unsubscribe", mailingList.getUnsubscribeAddress() );
184 setProperty( properties, "mailingList." + i + ".subscribe", mailingList.getSubscribeAddress() );
185 setProperty( properties, "mailingList." + i + ".otherArchives", join( mailingList.getOtherArchives() ) );
189 for ( Dependency dependency : versionMetadata.getDependencies() )
191 setProperty( properties, "dependency." + i + ".classifier", dependency.getClassifier() );
192 setProperty( properties, "dependency." + i + ".scope", dependency.getScope() );
193 setProperty( properties, "dependency." + i + ".systemPath", dependency.getSystemPath() );
194 setProperty( properties, "dependency." + i + ".artifactId", dependency.getArtifactId() );
195 setProperty( properties, "dependency." + i + ".groupId", dependency.getGroupId() );
196 setProperty( properties, "dependency." + i + ".version", dependency.getVersion() );
197 setProperty( properties, "dependency." + i + ".type", dependency.getType() );
200 Set<String> facetIds = new LinkedHashSet<String>( versionMetadata.getFacetIds() );
201 facetIds.addAll( Arrays.asList( properties.getProperty( "facetIds", "" ).split( "," ) ) );
202 properties.setProperty( "facetIds", join( facetIds ) );
204 for ( MetadataFacet facet : versionMetadata.getFacetList() )
206 properties.putAll( facet.toProperties() );
211 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
213 catch ( IOException e )
216 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
220 public void updateProjectReference( String repoId, String namespace, String projectId, String projectVersion,
221 ProjectVersionReference reference )
223 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
225 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
226 int i = Integer.valueOf( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
227 setProperty( properties, "ref:lastReferenceNum", Integer.toString( i ) );
228 setProperty( properties, "ref:reference." + i + ".namespace", reference.getNamespace() );
229 setProperty( properties, "ref:reference." + i + ".projectId", reference.getProjectId() );
230 setProperty( properties, "ref:reference." + i + ".projectVersion", reference.getProjectVersion() );
231 setProperty( properties, "ref:reference." + i + ".referenceType", reference.getReferenceType().toString() );
235 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
237 catch ( IOException e )
240 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
244 public void updateNamespace( String repoId, String namespace )
248 File namespaceDirectory = new File( getDirectory( repoId ), namespace );
249 Properties properties = new Properties();
250 properties.setProperty( "namespace", namespace );
251 writeProperties( properties, namespaceDirectory, NAMESPACE_METADATA_KEY );
254 catch ( IOException e )
261 public List<String> getMetadataFacets( String repoId, String facetId )
263 File directory = getMetadataDirectory( repoId, facetId );
264 List<String> facets = new ArrayList<String>();
265 recurse( facets, "", directory );
269 private void recurse( List<String> facets, String prefix, File directory )
271 File[] list = directory.listFiles();
274 for ( File dir : list )
276 if ( dir.isDirectory() )
278 recurse( facets, prefix + "/" + dir.getName(), dir );
280 else if ( dir.getName().equals( METADATA_KEY + ".properties" ) )
282 facets.add( prefix.substring( 1 ) );
288 public MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name )
290 Properties properties;
293 properties = readProperties( new File( getMetadataDirectory( repositoryId, facetId ), name ),
296 catch ( FileNotFoundException e )
300 catch ( IOException e )
303 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
306 MetadataFacet metadataFacet = null;
307 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
308 if ( metadataFacetFactory != null )
310 metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
311 Map<String, String> map = new HashMap<String, String>();
312 for ( Object key : new ArrayList( properties.keySet() ) )
314 String property = (String) key;
315 map.put( property, properties.getProperty( property ) );
317 metadataFacet.fromProperties( map );
319 return metadataFacet;
322 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
324 Properties properties = new Properties();
325 properties.putAll( metadataFacet.toProperties() );
329 File directory = new File( getMetadataDirectory( repositoryId, metadataFacet.getFacetId() ),
330 metadataFacet.getName() );
331 writeProperties( properties, directory, METADATA_KEY );
333 catch ( IOException e )
336 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
340 public void removeMetadataFacets( String repositoryId, String facetId )
344 FileUtils.deleteDirectory( getMetadataDirectory( repositoryId, facetId ) );
346 catch ( IOException e )
349 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
353 public void removeMetadataFacet( String repoId, String facetId, String name )
355 File dir = new File( getMetadataDirectory( repoId, facetId ), name );
358 FileUtils.deleteDirectory( dir );
360 catch ( IOException e )
363 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
367 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date startTime, Date endTime )
369 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
370 // of this information (eg. in Lucene, as before)
372 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
373 for ( String ns : getRootNamespaces( repoId ) )
375 getArtifactsByDateRange( artifacts, repoId, ns, startTime, endTime );
377 Collections.sort( artifacts, new ArtifactComparator() );
381 private void getArtifactsByDateRange( List<ArtifactMetadata> artifacts, String repoId, String ns, Date startTime,
384 for ( String namespace : getNamespaces( repoId, ns ) )
386 getArtifactsByDateRange( artifacts, repoId, ns + "." + namespace, startTime, endTime );
389 for ( String project : getProjects( repoId, ns ) )
391 for ( String version : getProjectVersions( repoId, ns, project ) )
393 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
395 if ( startTime == null || startTime.before( artifact.getWhenGathered() ) )
397 if ( endTime == null || endTime.after( artifact.getWhenGathered() ) )
399 artifacts.add( artifact );
407 public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
408 String projectVersion )
410 Map<String, ArtifactMetadata> artifacts = new HashMap<String, ArtifactMetadata>();
412 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
414 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
416 for ( Map.Entry entry : properties.entrySet() )
418 String name = (String) entry.getKey();
419 StringTokenizer tok = new StringTokenizer( name, ":" );
420 if ( tok.hasMoreTokens() && "artifact".equals( tok.nextToken() ) )
422 String field = tok.nextToken();
423 String id = tok.nextToken();
425 ArtifactMetadata artifact = artifacts.get( id );
426 // TODO: facets (&test)
427 if ( artifact == null )
429 artifact = new ArtifactMetadata();
430 artifact.setRepositoryId( repoId );
431 artifact.setNamespace( namespace );
432 artifact.setProject( projectId );
433 artifact.setVersion( projectVersion );
434 artifact.setId( id );
435 artifacts.put( id, artifact );
438 String value = (String) entry.getValue();
439 if ( "updated".equals( field ) )
441 artifact.setFileLastModified( Long.valueOf( value ) );
443 else if ( "size".equals( field ) )
445 artifact.setSize( Long.valueOf( value ) );
447 else if ( "whenGathered".equals( field ) )
449 artifact.setWhenGathered( new Date( Long.valueOf( value ) ) );
451 else if ( "version".equals( field ) )
453 artifact.setVersion( value );
455 else if ( "md5".equals( field ) )
457 artifact.setMd5( value );
459 else if ( "sha1".equals( field ) )
461 artifact.setSha1( value );
465 return artifacts.values();
468 public Collection<String> getRepositories()
470 return configuration.getConfiguration().getManagedRepositoriesAsMap().keySet();
473 public List<ArtifactMetadata> getArtifactsByChecksum( String repositoryId, String checksum )
475 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
476 // of this information (eg. in Lucene, as before)
477 // alternatively, we could build a referential tree in the content repository, however it would need some levels
478 // of depth to avoid being too broad to be useful (eg. /repository/checksums/a/ab/abcdef1234567)
480 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
481 for ( String ns : getRootNamespaces( repositoryId ) )
483 getArtifactsByChecksum( artifacts, repositoryId, ns, checksum );
488 public void deleteArtifact( String repoId, String namespace, String project, String version, String id )
490 File directory = new File( getDirectory( repoId ), namespace + "/" + project + "/" + version );
492 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
494 properties.remove( "artifact:updated:" + id );
495 properties.remove( "artifact:whenGathered:" + id );
496 properties.remove( "artifact:size:" + id );
497 properties.remove( "artifact:md5:" + id );
498 properties.remove( "artifact:sha1:" + id );
499 properties.remove( "artifact:version:" + id );
503 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
505 catch ( IOException e )
508 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
512 public void deleteRepository( String repoId )
516 FileUtils.deleteDirectory( getDirectory( repoId ) );
518 catch ( IOException e )
521 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
525 private void getArtifactsByChecksum( List<ArtifactMetadata> artifacts, String repositoryId, String ns,
528 for ( String namespace : getNamespaces( repositoryId, ns ) )
530 getArtifactsByChecksum( artifacts, repositoryId, ns + "." + namespace, checksum );
533 for ( String project : getProjects( repositoryId, ns ) )
535 for ( String version : getProjectVersions( repositoryId, ns, project ) )
537 for ( ArtifactMetadata artifact : getArtifacts( repositoryId, ns, project, version ) )
539 if ( checksum.equals( artifact.getMd5() ) || checksum.equals( artifact.getSha1() ) )
541 artifacts.add( artifact );
548 private File getMetadataDirectory( String repoId, String facetId )
550 return new File( getBaseDirectory( repoId ), "facets/" + facetId );
553 private String join( Collection<String> ids )
555 if ( !ids.isEmpty() )
557 StringBuilder s = new StringBuilder();
558 for ( String id : ids )
563 return s.substring( 0, s.length() - 1 );
568 private void setProperty( Properties properties, String name, String value )
572 properties.setProperty( name, value );
576 public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion,
577 ArtifactMetadata artifact )
579 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
581 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
583 properties.setProperty( "artifact:updated:" + artifact.getId(), Long.toString(
584 artifact.getFileLastModified().getTime() ) );
585 properties.setProperty( "artifact:whenGathered:" + artifact.getId(), Long.toString(
586 artifact.getWhenGathered().getTime() ) );
587 properties.setProperty( "artifact:size:" + artifact.getId(), Long.toString( artifact.getSize() ) );
588 if ( artifact.getMd5() != null )
590 properties.setProperty( "artifact:md5:" + artifact.getId(), artifact.getMd5() );
592 if ( artifact.getSha1() != null )
594 properties.setProperty( "artifact:sha1:" + artifact.getId(), artifact.getSha1() );
596 properties.setProperty( "artifact:version:" + artifact.getId(), artifact.getVersion() );
600 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
602 catch ( IOException e )
605 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
609 private Properties readOrCreateProperties( File directory, String propertiesKey )
613 return readProperties( directory, propertiesKey );
615 catch ( FileNotFoundException e )
617 // ignore and return new properties
619 catch ( IOException e )
622 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
624 return new Properties();
627 private Properties readProperties( File directory, String propertiesKey )
630 Properties properties = new Properties();
631 FileInputStream in = null;
634 in = new FileInputStream( new File( directory, propertiesKey + ".properties" ) );
635 properties.load( in );
639 IOUtils.closeQuietly( in );
644 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
646 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
648 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
650 ProjectMetadata project = new ProjectMetadata();
651 project.setNamespace( properties.getProperty( "namespace" ) );
652 project.setId( properties.getProperty( "id" ) );
656 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
657 String projectVersion )
659 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
661 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
662 String id = properties.getProperty( "id" );
663 ProjectVersionMetadata versionMetadata = null;
666 versionMetadata = new ProjectVersionMetadata();
667 versionMetadata.setId( id );
668 versionMetadata.setName( properties.getProperty( "name" ) );
669 versionMetadata.setDescription( properties.getProperty( "description" ) );
670 versionMetadata.setUrl( properties.getProperty( "url" ) );
671 versionMetadata.setIncomplete( Boolean.valueOf( properties.getProperty( "incomplete", "false" ) ) );
673 String scmConnection = properties.getProperty( "scm.connection" );
674 String scmDeveloperConnection = properties.getProperty( "scm.developerConnection" );
675 String scmUrl = properties.getProperty( "scm.url" );
676 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
679 scm.setConnection( scmConnection );
680 scm.setDeveloperConnection( scmDeveloperConnection );
681 scm.setUrl( scmUrl );
682 versionMetadata.setScm( scm );
685 String ciSystem = properties.getProperty( "ci.system" );
686 String ciUrl = properties.getProperty( "ci.url" );
687 if ( ciSystem != null || ciUrl != null )
689 CiManagement ci = new CiManagement();
690 ci.setSystem( ciSystem );
692 versionMetadata.setCiManagement( ci );
695 String issueSystem = properties.getProperty( "issue.system" );
696 String issueUrl = properties.getProperty( "issue.url" );
697 if ( issueSystem != null || issueUrl != null )
699 IssueManagement issueManagement = new IssueManagement();
700 issueManagement.setSystem( issueSystem );
701 issueManagement.setUrl( issueUrl );
702 versionMetadata.setIssueManagement( issueManagement );
705 String orgName = properties.getProperty( "org.name" );
706 String orgUrl = properties.getProperty( "org.url" );
707 if ( orgName != null || orgUrl != null )
709 Organization org = new Organization();
710 org.setName( orgName );
711 org.setUrl( orgUrl );
712 versionMetadata.setOrganization( org );
715 boolean done = false;
719 String licenseName = properties.getProperty( "license." + i + ".name" );
720 String licenseUrl = properties.getProperty( "license." + i + ".url" );
721 if ( licenseName != null || licenseUrl != null )
723 License license = new License();
724 license.setName( licenseName );
725 license.setUrl( licenseUrl );
726 versionMetadata.addLicense( license );
739 String mailingListName = properties.getProperty( "mailingList." + i + ".name" );
740 if ( mailingListName != null )
742 MailingList mailingList = new MailingList();
743 mailingList.setName( mailingListName );
744 mailingList.setMainArchiveUrl( properties.getProperty( "mailingList." + i + ".archive" ) );
745 mailingList.setOtherArchives( Arrays.asList( properties.getProperty(
746 "mailingList." + i + ".otherArchives" ).split( "," ) ) );
747 mailingList.setPostAddress( properties.getProperty( "mailingList." + i + ".post" ) );
748 mailingList.setSubscribeAddress( properties.getProperty( "mailingList." + i + ".subscribe" ) );
749 mailingList.setUnsubscribeAddress( properties.getProperty( "mailingList." + i + ".unsubscribe" ) );
750 versionMetadata.addMailingList( mailingList );
763 String dependencyArtifactId = properties.getProperty( "dependency." + i + ".artifactId" );
764 if ( dependencyArtifactId != null )
766 Dependency dependency = new Dependency();
767 dependency.setArtifactId( dependencyArtifactId );
768 dependency.setGroupId( properties.getProperty( "dependency." + i + ".groupId" ) );
769 dependency.setClassifier( properties.getProperty( "dependency." + i + ".classifier" ) );
770 dependency.setOptional( Boolean.valueOf( properties.getProperty(
771 "dependency." + i + ".optional" ) ) );
772 dependency.setScope( properties.getProperty( "dependency." + i + ".scope" ) );
773 dependency.setSystemPath( properties.getProperty( "dependency." + i + ".systemPath" ) );
774 dependency.setType( properties.getProperty( "dependency." + i + ".type" ) );
775 dependency.setVersion( properties.getProperty( "dependency." + i + ".version" ) );
776 versionMetadata.addDependency( dependency );
785 String facetIds = properties.getProperty( "facetIds", "" );
786 if ( facetIds.length() > 0 )
788 for ( String facetId : facetIds.split( "," ) )
790 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
791 if ( factory == null )
793 log.error( "Attempted to load unknown metadata facet: " + facetId );
797 MetadataFacet facet = factory.createMetadataFacet();
798 Map<String, String> map = new HashMap<String, String>();
799 for ( Object key : new ArrayList( properties.keySet() ) )
801 String property = (String) key;
802 if ( property.startsWith( facet.getFacetId() ) )
804 map.put( property, properties.getProperty( property ) );
807 facet.fromProperties( map );
808 versionMetadata.addFacet( facet );
813 for ( MetadataFacet facet : versionMetadata.getFacetList() )
815 properties.putAll( facet.toProperties() );
818 return versionMetadata;
821 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
822 String projectVersion )
824 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
826 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
828 Set<String> versions = new HashSet<String>();
829 for ( Map.Entry entry : properties.entrySet() )
831 String name = (String) entry.getKey();
832 if ( name.startsWith( "artifact:version:" ) )
834 versions.add( (String) entry.getValue() );
840 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
841 String projectVersion )
843 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
845 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
846 int numberOfRefs = Integer.valueOf( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
848 List<ProjectVersionReference> references = new ArrayList<ProjectVersionReference>();
849 for ( int i = 0; i < numberOfRefs; i++ )
851 ProjectVersionReference reference = new ProjectVersionReference();
852 reference.setProjectId( properties.getProperty( "ref:reference." + i + ".projectId" ) );
853 reference.setNamespace( properties.getProperty( "ref:reference." + i + ".namespace" ) );
854 reference.setProjectVersion( properties.getProperty( "ref:reference." + i + ".projectVersion" ) );
855 reference.setReferenceType( ProjectVersionReference.ReferenceType.valueOf( properties.getProperty(
856 "ref:reference." + i + ".referenceType" ) ) );
857 references.add( reference );
862 public Collection<String> getRootNamespaces( String repoId )
864 return getNamespaces( repoId, null );
867 public Collection<String> getNamespaces( String repoId, String baseNamespace )
869 List<String> allNamespaces = new ArrayList<String>();
870 File directory = getDirectory( repoId );
871 File[] files = directory.listFiles();
874 for ( File namespace : files )
876 if ( new File( namespace, NAMESPACE_METADATA_KEY + ".properties" ).exists() )
878 allNamespaces.add( namespace.getName() );
883 Set<String> namespaces = new LinkedHashSet<String>();
884 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
885 for ( String namespace : allNamespaces )
887 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
889 int i = namespace.indexOf( '.', fromIndex );
892 namespaces.add( namespace.substring( fromIndex, i ) );
896 namespaces.add( namespace.substring( fromIndex ) );
900 return new ArrayList<String>( namespaces );
903 public Collection<String> getProjects( String repoId, String namespace )
905 List<String> projects = new ArrayList<String>();
906 File directory = new File( getDirectory( repoId ), namespace );
907 File[] files = directory.listFiles();
910 for ( File project : files )
912 if ( new File( project, PROJECT_METADATA_KEY + ".properties" ).exists() )
914 projects.add( project.getName() );
921 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
923 List<String> projectVersions = new ArrayList<String>();
924 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
925 File[] files = directory.listFiles();
928 for ( File projectVersion : files )
930 if ( new File( projectVersion, PROJECT_VERSION_METADATA_KEY + ".properties" ).exists() )
932 projectVersions.add( projectVersion.getName() );
936 return projectVersions;
939 private void writeProperties( Properties properties, File directory, String propertiesKey )
943 FileOutputStream os = new FileOutputStream( new File( directory, propertiesKey + ".properties" ) );
946 properties.store( os, null );
950 IOUtils.closeQuietly( os );
954 public void setMetadataFacetFactories( Map<String, MetadataFacetFactory> metadataFacetFactories )
956 this.metadataFacetFactories = metadataFacetFactories;
959 public void setConfiguration( ArchivaConfiguration configuration )
961 this.configuration = configuration;
964 private static class ArtifactComparator
965 implements Comparator<ArtifactMetadata>
967 public int compare( ArtifactMetadata artifact1, ArtifactMetadata artifact2 )
969 if ( artifact1.getWhenGathered() == artifact2.getWhenGathered() )
973 if ( artifact1.getWhenGathered() == null )
977 if ( artifact2.getWhenGathered() == null )
981 return artifact1.getWhenGathered().compareTo( artifact2.getWhenGathered() );