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 // clear the facet contents so old properties are no longer written
145 clearMetadataFacetProperties( versionMetadata, properties );
147 properties.setProperty( "id", versionMetadata.getId() );
148 setProperty( properties, "name", versionMetadata.getName() );
149 setProperty( properties, "description", versionMetadata.getDescription() );
150 setProperty( properties, "url", versionMetadata.getUrl() );
151 setProperty( properties, "incomplete", String.valueOf( versionMetadata.isIncomplete() ) );
152 if ( versionMetadata.getScm() != null )
154 setProperty( properties, "scm.connection", versionMetadata.getScm().getConnection() );
155 setProperty( properties, "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() );
156 setProperty( properties, "scm.url", versionMetadata.getScm().getUrl() );
158 if ( versionMetadata.getCiManagement() != null )
160 setProperty( properties, "ci.system", versionMetadata.getCiManagement().getSystem() );
161 setProperty( properties, "ci.url", versionMetadata.getCiManagement().getUrl() );
163 if ( versionMetadata.getIssueManagement() != null )
165 setProperty( properties, "issue.system", versionMetadata.getIssueManagement().getSystem() );
166 setProperty( properties, "issue.url", versionMetadata.getIssueManagement().getUrl() );
168 if ( versionMetadata.getOrganization() != null )
170 setProperty( properties, "org.name", versionMetadata.getOrganization().getName() );
171 setProperty( properties, "org.url", versionMetadata.getOrganization().getUrl() );
174 for ( License license : versionMetadata.getLicenses() )
176 setProperty( properties, "license." + i + ".name", license.getName() );
177 setProperty( properties, "license." + i + ".url", license.getUrl() );
181 for ( MailingList mailingList : versionMetadata.getMailingLists() )
183 setProperty( properties, "mailingList." + i + ".archive", mailingList.getMainArchiveUrl() );
184 setProperty( properties, "mailingList." + i + ".name", mailingList.getName() );
185 setProperty( properties, "mailingList." + i + ".post", mailingList.getPostAddress() );
186 setProperty( properties, "mailingList." + i + ".unsubscribe", mailingList.getUnsubscribeAddress() );
187 setProperty( properties, "mailingList." + i + ".subscribe", mailingList.getSubscribeAddress() );
188 setProperty( properties, "mailingList." + i + ".otherArchives", join( mailingList.getOtherArchives() ) );
192 for ( Dependency dependency : versionMetadata.getDependencies() )
194 setProperty( properties, "dependency." + i + ".classifier", dependency.getClassifier() );
195 setProperty( properties, "dependency." + i + ".scope", dependency.getScope() );
196 setProperty( properties, "dependency." + i + ".systemPath", dependency.getSystemPath() );
197 setProperty( properties, "dependency." + i + ".artifactId", dependency.getArtifactId() );
198 setProperty( properties, "dependency." + i + ".groupId", dependency.getGroupId() );
199 setProperty( properties, "dependency." + i + ".version", dependency.getVersion() );
200 setProperty( properties, "dependency." + i + ".type", dependency.getType() );
203 Set<String> facetIds = new LinkedHashSet<String>( versionMetadata.getFacetIds() );
204 facetIds.addAll( Arrays.asList( properties.getProperty( "facetIds", "" ).split( "," ) ) );
205 properties.setProperty( "facetIds", join( facetIds ) );
207 updateProjectVersionFacets( versionMetadata, properties );
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 private void updateProjectVersionFacets( ProjectVersionMetadata versionMetadata, Properties properties )
222 for ( MetadataFacet facet : versionMetadata.getFacetList() )
224 for ( Map.Entry<String,String> entry : facet.toProperties().entrySet() )
226 properties.setProperty( facet.getFacetId() + ":" + entry.getKey(), entry.getValue() );
231 private void clearMetadataFacetProperties( ProjectVersionMetadata versionMetadata, Properties properties )
233 List<Object> propsToRemove = new ArrayList<Object>();
234 for ( MetadataFacet facet : versionMetadata.getFacetList() )
236 for ( Object key : properties.keySet() )
238 String keyString = ( String ) key;
239 if( keyString.startsWith( facet.getFacetId() + ":" ) )
241 propsToRemove.add( key );
246 for( Object key : propsToRemove )
248 properties.remove( key );
252 public void updateProjectReference( String repoId, String namespace, String projectId, String projectVersion,
253 ProjectVersionReference reference )
255 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
257 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
258 int i = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
259 setProperty( properties, "ref:lastReferenceNum", Integer.toString( i ) );
260 setProperty( properties, "ref:reference." + i + ".namespace", reference.getNamespace() );
261 setProperty( properties, "ref:reference." + i + ".projectId", reference.getProjectId() );
262 setProperty( properties, "ref:reference." + i + ".projectVersion", reference.getProjectVersion() );
263 setProperty( properties, "ref:reference." + i + ".referenceType", reference.getReferenceType().toString() );
267 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
269 catch ( IOException e )
272 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
276 public void updateNamespace( String repoId, String namespace )
280 File namespaceDirectory = new File( getDirectory( repoId ), namespace );
281 Properties properties = new Properties();
282 properties.setProperty( "namespace", namespace );
283 writeProperties( properties, namespaceDirectory, NAMESPACE_METADATA_KEY );
286 catch ( IOException e )
293 public List<String> getMetadataFacets( String repoId, String facetId )
295 File directory = getMetadataDirectory( repoId, facetId );
296 List<String> facets = new ArrayList<String>();
297 recurse( facets, "", directory );
301 private void recurse( List<String> facets, String prefix, File directory )
303 File[] list = directory.listFiles();
306 for ( File dir : list )
308 if ( dir.isDirectory() )
310 recurse( facets, prefix + "/" + dir.getName(), dir );
312 else if ( dir.getName().equals( METADATA_KEY + ".properties" ) )
314 facets.add( prefix.substring( 1 ) );
320 public MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name )
322 Properties properties;
325 properties = readProperties( new File( getMetadataDirectory( repositoryId, facetId ), name ),
328 catch ( FileNotFoundException e )
332 catch ( IOException e )
335 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
338 MetadataFacet metadataFacet = null;
339 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
340 if ( metadataFacetFactory != null )
342 metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
343 Map<String, String> map = new HashMap<String, String>();
344 for ( Object key : new ArrayList( properties.keySet() ) )
346 String property = (String) key;
347 map.put( property, properties.getProperty( property ) );
349 metadataFacet.fromProperties( map );
351 return metadataFacet;
354 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
356 Properties properties = new Properties();
357 properties.putAll( metadataFacet.toProperties() );
361 File directory = new File( getMetadataDirectory( repositoryId, metadataFacet.getFacetId() ),
362 metadataFacet.getName() );
363 writeProperties( properties, directory, METADATA_KEY );
365 catch ( IOException e )
368 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
372 public void removeMetadataFacets( String repositoryId, String facetId )
376 FileUtils.deleteDirectory( getMetadataDirectory( repositoryId, facetId ) );
378 catch ( IOException e )
381 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
385 public void removeMetadataFacet( String repoId, String facetId, String name )
387 File dir = new File( getMetadataDirectory( repoId, facetId ), name );
390 FileUtils.deleteDirectory( dir );
392 catch ( IOException e )
395 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
399 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date startTime, Date endTime )
401 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
402 // of this information (eg. in Lucene, as before)
404 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
405 for ( String ns : getRootNamespaces( repoId ) )
407 getArtifactsByDateRange( artifacts, repoId, ns, startTime, endTime );
409 Collections.sort( artifacts, new ArtifactComparator() );
413 private void getArtifactsByDateRange( List<ArtifactMetadata> artifacts, String repoId, String ns, Date startTime,
416 for ( String namespace : getNamespaces( repoId, ns ) )
418 getArtifactsByDateRange( artifacts, repoId, ns + "." + namespace, startTime, endTime );
421 for ( String project : getProjects( repoId, ns ) )
423 for ( String version : getProjectVersions( repoId, ns, project ) )
425 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
427 if ( startTime == null || startTime.before( artifact.getWhenGathered() ) )
429 if ( endTime == null || endTime.after( artifact.getWhenGathered() ) )
431 artifacts.add( artifact );
439 public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
440 String projectVersion )
442 Map<String, ArtifactMetadata> artifacts = new HashMap<String, ArtifactMetadata>();
444 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
446 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
448 for ( Map.Entry entry : properties.entrySet() )
450 String name = (String) entry.getKey();
451 StringTokenizer tok = new StringTokenizer( name, ":" );
452 if ( tok.hasMoreTokens() && "artifact".equals( tok.nextToken() ) )
454 String field = tok.nextToken();
455 String id = tok.nextToken();
457 ArtifactMetadata artifact = artifacts.get( id );
458 if ( artifact == null )
460 artifact = new ArtifactMetadata();
461 artifact.setRepositoryId( repoId );
462 artifact.setNamespace( namespace );
463 artifact.setProject( projectId );
464 artifact.setProjectVersion( projectVersion );
465 artifact.setVersion( projectVersion );
466 artifact.setId( id );
467 artifacts.put( id, artifact );
470 String value = (String) entry.getValue();
471 if ( "updated".equals( field ) )
473 artifact.setFileLastModified( Long.parseLong( value ) );
475 else if ( "size".equals( field ) )
477 artifact.setSize( Long.valueOf( value ) );
479 else if ( "whenGathered".equals( field ) )
481 artifact.setWhenGathered( new Date( Long.parseLong( value ) ) );
483 else if ( "version".equals( field ) )
485 artifact.setVersion( value );
487 else if ( "md5".equals( field ) )
489 artifact.setMd5( value );
491 else if ( "sha1".equals( field ) )
493 artifact.setSha1( value );
495 else if ( "facetIds".equals( field ) )
497 if ( value.length() > 0 )
499 String propertyPrefix = "artifact:facet:" + id + ":";
500 for ( String facetId : value.split( "," ) )
502 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
503 if ( factory == null )
505 log.error( "Attempted to load unknown artifact metadata facet: " + facetId );
509 MetadataFacet facet = factory.createMetadataFacet();
510 String prefix = propertyPrefix + facet.getFacetId();
511 Map<String, String> map = new HashMap<String, String>();
512 for ( Object key : new ArrayList( properties.keySet() ) )
514 String property = (String) key;
515 if ( property.startsWith( prefix ) )
517 map.put( property.substring( prefix.length() + 1 ), properties.getProperty(
521 facet.fromProperties( map );
522 artifact.addFacet( facet );
527 updateArtifactFacets( artifact, properties );
531 return artifacts.values();
534 private void updateArtifactFacets( ArtifactMetadata artifact, Properties properties )
536 String propertyPrefix = "artifact:facet:" + artifact.getId() + ":";
537 for ( MetadataFacet facet : artifact.getFacetList() )
539 for ( Map.Entry<String, String> e : facet.toProperties().entrySet() )
541 String key = propertyPrefix + facet.getFacetId() + ":" + e.getKey();
542 properties.setProperty( key, e.getValue() );
547 public Collection<String> getRepositories()
549 return configuration.getConfiguration().getManagedRepositoriesAsMap().keySet();
552 public List<ArtifactMetadata> getArtifactsByChecksum( String repositoryId, String checksum )
554 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
555 // of this information (eg. in Lucene, as before)
556 // alternatively, we could build a referential tree in the content repository, however it would need some levels
557 // of depth to avoid being too broad to be useful (eg. /repository/checksums/a/ab/abcdef1234567)
559 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
560 for ( String ns : getRootNamespaces( repositoryId ) )
562 getArtifactsByChecksum( artifacts, repositoryId, ns, checksum );
567 public void deleteArtifact( String repoId, String namespace, String project, String version, String id )
569 File directory = new File( getDirectory( repoId ), namespace + "/" + project + "/" + version );
571 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
573 properties.remove( "artifact:updated:" + id );
574 properties.remove( "artifact:whenGathered:" + id );
575 properties.remove( "artifact:size:" + id );
576 properties.remove( "artifact:md5:" + id );
577 properties.remove( "artifact:sha1:" + id );
578 properties.remove( "artifact:version:" + id );
579 properties.remove( "artifact:facetIds:" + id );
581 String prefix = "artifact:facet:" + id + ":";
582 for ( Object key : new ArrayList( properties.keySet() ) )
584 String property = (String) key;
585 if ( property.startsWith( prefix ) )
587 properties.remove( property );
593 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
595 catch ( IOException e )
598 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
602 public void deleteRepository( String repoId )
606 FileUtils.deleteDirectory( getDirectory( repoId ) );
608 catch ( IOException e )
611 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
615 private void getArtifactsByChecksum( List<ArtifactMetadata> artifacts, String repositoryId, String ns,
618 for ( String namespace : getNamespaces( repositoryId, ns ) )
620 getArtifactsByChecksum( artifacts, repositoryId, ns + "." + namespace, checksum );
623 for ( String project : getProjects( repositoryId, ns ) )
625 for ( String version : getProjectVersions( repositoryId, ns, project ) )
627 for ( ArtifactMetadata artifact : getArtifacts( repositoryId, ns, project, version ) )
629 if ( checksum.equals( artifact.getMd5() ) || checksum.equals( artifact.getSha1() ) )
631 artifacts.add( artifact );
638 private File getMetadataDirectory( String repoId, String facetId )
640 return new File( getBaseDirectory( repoId ), "facets/" + facetId );
643 private String join( Collection<String> ids )
645 if ( ids != null && !ids.isEmpty() )
647 StringBuilder s = new StringBuilder();
648 for ( String id : ids )
653 return s.substring( 0, s.length() - 1 );
658 private void setProperty( Properties properties, String name, String value )
662 properties.setProperty( name, value );
666 public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion,
667 ArtifactMetadata artifact )
669 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
671 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
673 String id = artifact.getId();
674 properties.setProperty( "artifact:updated:" + id, Long.toString( artifact.getFileLastModified().getTime() ) );
675 properties.setProperty( "artifact:whenGathered:" + id, Long.toString( artifact.getWhenGathered().getTime() ) );
676 properties.setProperty( "artifact:size:" + id, Long.toString( artifact.getSize() ) );
677 if ( artifact.getMd5() != null )
679 properties.setProperty( "artifact:md5:" + id, artifact.getMd5() );
681 if ( artifact.getSha1() != null )
683 properties.setProperty( "artifact:sha1:" + id, artifact.getSha1() );
685 properties.setProperty( "artifact:version:" + id, artifact.getVersion() );
687 Set<String> facetIds = new LinkedHashSet<String>( artifact.getFacetIds() );
688 String property = "artifact:facetIds:" + id;
689 facetIds.addAll( Arrays.asList( properties.getProperty( property, "" ).split( "," ) ) );
690 properties.setProperty( property, join( facetIds ) );
692 updateArtifactFacets( artifact, properties );
696 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
698 catch ( IOException e )
701 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
705 private Properties readOrCreateProperties( File directory, String propertiesKey )
709 return readProperties( directory, propertiesKey );
711 catch ( FileNotFoundException e )
713 // ignore and return new properties
715 catch ( IOException e )
718 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
720 return new Properties();
723 private Properties readProperties( File directory, String propertiesKey )
726 Properties properties = new Properties();
727 FileInputStream in = null;
730 in = new FileInputStream( new File( directory, propertiesKey + ".properties" ) );
731 properties.load( in );
735 IOUtils.closeQuietly( in );
740 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
742 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
744 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
746 ProjectMetadata project = new ProjectMetadata();
747 project.setNamespace( properties.getProperty( "namespace" ) );
748 project.setId( properties.getProperty( "id" ) );
752 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
753 String projectVersion )
755 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
757 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
758 String id = properties.getProperty( "id" );
759 ProjectVersionMetadata versionMetadata = null;
762 versionMetadata = new ProjectVersionMetadata();
763 versionMetadata.setId( id );
764 versionMetadata.setName( properties.getProperty( "name" ) );
765 versionMetadata.setDescription( properties.getProperty( "description" ) );
766 versionMetadata.setUrl( properties.getProperty( "url" ) );
767 versionMetadata.setIncomplete( Boolean.valueOf( properties.getProperty( "incomplete", "false" ) ) );
769 String scmConnection = properties.getProperty( "scm.connection" );
770 String scmDeveloperConnection = properties.getProperty( "scm.developerConnection" );
771 String scmUrl = properties.getProperty( "scm.url" );
772 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
775 scm.setConnection( scmConnection );
776 scm.setDeveloperConnection( scmDeveloperConnection );
777 scm.setUrl( scmUrl );
778 versionMetadata.setScm( scm );
781 String ciSystem = properties.getProperty( "ci.system" );
782 String ciUrl = properties.getProperty( "ci.url" );
783 if ( ciSystem != null || ciUrl != null )
785 CiManagement ci = new CiManagement();
786 ci.setSystem( ciSystem );
788 versionMetadata.setCiManagement( ci );
791 String issueSystem = properties.getProperty( "issue.system" );
792 String issueUrl = properties.getProperty( "issue.url" );
793 if ( issueSystem != null || issueUrl != null )
795 IssueManagement issueManagement = new IssueManagement();
796 issueManagement.setSystem( issueSystem );
797 issueManagement.setUrl( issueUrl );
798 versionMetadata.setIssueManagement( issueManagement );
801 String orgName = properties.getProperty( "org.name" );
802 String orgUrl = properties.getProperty( "org.url" );
803 if ( orgName != null || orgUrl != null )
805 Organization org = new Organization();
806 org.setName( orgName );
807 org.setUrl( orgUrl );
808 versionMetadata.setOrganization( org );
811 boolean done = false;
815 String licenseName = properties.getProperty( "license." + i + ".name" );
816 String licenseUrl = properties.getProperty( "license." + i + ".url" );
817 if ( licenseName != null || licenseUrl != null )
819 License license = new License();
820 license.setName( licenseName );
821 license.setUrl( licenseUrl );
822 versionMetadata.addLicense( license );
835 String mailingListName = properties.getProperty( "mailingList." + i + ".name" );
836 if ( mailingListName != null )
838 MailingList mailingList = new MailingList();
839 mailingList.setName( mailingListName );
840 mailingList.setMainArchiveUrl( properties.getProperty( "mailingList." + i + ".archive" ) );
841 mailingList.setOtherArchives( Arrays.asList( properties.getProperty(
842 "mailingList." + i + ".otherArchives" ).split( "," ) ) );
843 mailingList.setPostAddress( properties.getProperty( "mailingList." + i + ".post" ) );
844 mailingList.setSubscribeAddress( properties.getProperty( "mailingList." + i + ".subscribe" ) );
845 mailingList.setUnsubscribeAddress( properties.getProperty( "mailingList." + i + ".unsubscribe" ) );
846 versionMetadata.addMailingList( mailingList );
859 String dependencyArtifactId = properties.getProperty( "dependency." + i + ".artifactId" );
860 if ( dependencyArtifactId != null )
862 Dependency dependency = new Dependency();
863 dependency.setArtifactId( dependencyArtifactId );
864 dependency.setGroupId( properties.getProperty( "dependency." + i + ".groupId" ) );
865 dependency.setClassifier( properties.getProperty( "dependency." + i + ".classifier" ) );
866 dependency.setOptional( Boolean.valueOf( properties.getProperty(
867 "dependency." + i + ".optional" ) ) );
868 dependency.setScope( properties.getProperty( "dependency." + i + ".scope" ) );
869 dependency.setSystemPath( properties.getProperty( "dependency." + i + ".systemPath" ) );
870 dependency.setType( properties.getProperty( "dependency." + i + ".type" ) );
871 dependency.setVersion( properties.getProperty( "dependency." + i + ".version" ) );
872 versionMetadata.addDependency( dependency );
881 String facetIds = properties.getProperty( "facetIds", "" );
882 if ( facetIds.length() > 0 )
884 for ( String facetId : facetIds.split( "," ) )
886 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
887 if ( factory == null )
889 log.error( "Attempted to load unknown project version metadata facet: " + facetId );
893 MetadataFacet facet = factory.createMetadataFacet();
894 Map<String, String> map = new HashMap<String, String>();
895 for ( Object key : new ArrayList( properties.keySet() ) )
897 String property = (String) key;
898 if ( property.startsWith( facet.getFacetId() ) )
900 map.put( property.substring( facet.getFacetId().length() + 1 ), properties.getProperty(
904 facet.fromProperties( map );
905 versionMetadata.addFacet( facet );
910 updateProjectVersionFacets( versionMetadata, properties );
912 return versionMetadata;
915 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
916 String projectVersion )
918 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
920 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
922 Set<String> versions = new HashSet<String>();
923 for ( Map.Entry entry : properties.entrySet() )
925 String name = (String) entry.getKey();
926 if ( name.startsWith( "artifact:version:" ) )
928 versions.add( (String) entry.getValue() );
934 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
935 String projectVersion )
937 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
939 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
940 int numberOfRefs = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
942 List<ProjectVersionReference> references = new ArrayList<ProjectVersionReference>();
943 for ( int i = 0; i < numberOfRefs; i++ )
945 ProjectVersionReference reference = new ProjectVersionReference();
946 reference.setProjectId( properties.getProperty( "ref:reference." + i + ".projectId" ) );
947 reference.setNamespace( properties.getProperty( "ref:reference." + i + ".namespace" ) );
948 reference.setProjectVersion( properties.getProperty( "ref:reference." + i + ".projectVersion" ) );
949 reference.setReferenceType( ProjectVersionReference.ReferenceType.valueOf( properties.getProperty(
950 "ref:reference." + i + ".referenceType" ) ) );
951 references.add( reference );
956 public Collection<String> getRootNamespaces( String repoId )
958 return getNamespaces( repoId, null );
961 public Collection<String> getNamespaces( String repoId, String baseNamespace )
963 List<String> allNamespaces = new ArrayList<String>();
964 File directory = getDirectory( repoId );
965 File[] files = directory.listFiles();
968 for ( File namespace : files )
970 if ( new File( namespace, NAMESPACE_METADATA_KEY + ".properties" ).exists() )
972 allNamespaces.add( namespace.getName() );
977 Set<String> namespaces = new LinkedHashSet<String>();
978 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
979 for ( String namespace : allNamespaces )
981 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
983 int i = namespace.indexOf( '.', fromIndex );
986 namespaces.add( namespace.substring( fromIndex, i ) );
990 namespaces.add( namespace.substring( fromIndex ) );
994 return new ArrayList<String>( namespaces );
997 public Collection<String> getProjects( String repoId, String namespace )
999 List<String> projects = new ArrayList<String>();
1000 File directory = new File( getDirectory( repoId ), namespace );
1001 File[] files = directory.listFiles();
1002 if ( files != null )
1004 for ( File project : files )
1006 if ( new File( project, PROJECT_METADATA_KEY + ".properties" ).exists() )
1008 projects.add( project.getName() );
1015 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
1017 List<String> projectVersions = new ArrayList<String>();
1018 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
1019 File[] files = directory.listFiles();
1020 if ( files != null )
1022 for ( File projectVersion : files )
1024 if ( new File( projectVersion, PROJECT_VERSION_METADATA_KEY + ".properties" ).exists() )
1026 projectVersions.add( projectVersion.getName() );
1030 return projectVersions;
1033 private void writeProperties( Properties properties, File directory, String propertiesKey )
1037 FileOutputStream os = new FileOutputStream( new File( directory, propertiesKey + ".properties" ) );
1040 properties.store( os, null );
1044 IOUtils.closeQuietly( os );
1048 public void setMetadataFacetFactories( Map<String, MetadataFacetFactory> metadataFacetFactories )
1050 this.metadataFacetFactories = metadataFacetFactories;
1053 public void setConfiguration( ArchivaConfiguration configuration )
1055 this.configuration = configuration;
1058 private static class ArtifactComparator
1059 implements Comparator<ArtifactMetadata>
1061 public int compare( ArtifactMetadata artifact1, ArtifactMetadata artifact2 )
1063 if ( artifact1.getWhenGathered() == artifact2.getWhenGathered() )
1067 if ( artifact1.getWhenGathered() == null )
1071 if ( artifact2.getWhenGathered() == null )
1075 return artifact1.getWhenGathered().compareTo( artifact2.getWhenGathered() );
1079 public List<ArtifactMetadata> getArtifacts( String repoId )
1082 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
1083 for ( String ns : getRootNamespaces( repoId ) )
1085 getArtifacts( artifacts, repoId, ns );
1090 private void getArtifacts( List<ArtifactMetadata> artifacts, String repoId, String ns )
1092 for ( String namespace : getNamespaces( repoId, ns ) )
1094 getArtifacts( artifacts, repoId, ns + "." + namespace );
1097 for ( String project : getProjects( repoId, ns ) )
1099 for ( String version : getProjectVersions( repoId, ns, project ) )
1101 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
1104 artifacts.add( artifact );