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.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
24 import org.apache.archiva.metadata.model.ArtifactMetadata;
25 import org.apache.archiva.metadata.model.CiManagement;
26 import org.apache.archiva.metadata.model.Dependency;
27 import org.apache.archiva.metadata.model.IssueManagement;
28 import org.apache.archiva.metadata.model.License;
29 import org.apache.archiva.metadata.model.MailingList;
30 import org.apache.archiva.metadata.model.MetadataFacet;
31 import org.apache.archiva.metadata.model.MetadataFacetFactory;
32 import org.apache.archiva.metadata.model.Organization;
33 import org.apache.archiva.metadata.model.ProjectMetadata;
34 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
35 import org.apache.archiva.metadata.model.ProjectVersionReference;
36 import org.apache.archiva.metadata.model.Scm;
37 import org.apache.archiva.metadata.repository.MetadataRepository;
38 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
39 import org.apache.commons.io.FileUtils;
40 import org.apache.commons.io.IOUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 import java.io.FileInputStream;
46 import java.io.FileNotFoundException;
47 import java.io.FileOutputStream;
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.Comparator;
54 import java.util.Date;
55 import java.util.HashMap;
56 import java.util.HashSet;
57 import java.util.LinkedHashSet;
58 import java.util.List;
60 import java.util.Properties;
62 import java.util.StringTokenizer;
64 public class FileMetadataRepository
65 implements MetadataRepository
67 private final Map<String, MetadataFacetFactory> metadataFacetFactories;
69 private final ArchivaConfiguration configuration;
71 private Logger log = LoggerFactory.getLogger( FileMetadataRepository.class );
73 private static final String PROJECT_METADATA_KEY = "project-metadata";
75 private static final String PROJECT_VERSION_METADATA_KEY = "version-metadata";
77 private static final String NAMESPACE_METADATA_KEY = "namespace-metadata";
79 private static final String METADATA_KEY = "metadata";
81 public FileMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories,
82 ArchivaConfiguration configuration )
84 this.metadataFacetFactories = metadataFacetFactories;
85 this.configuration = configuration;
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 )
122 log.error( e.getMessage(), e );
126 public void updateProjectVersion( String repoId, String namespace, String projectId,
127 ProjectVersionMetadata versionMetadata )
129 updateProject( repoId, namespace, projectId );
132 new File( getDirectory( repoId ), 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<Object>( 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.getFacetList(), 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 ProjectVersionReference reference = new ProjectVersionReference();
193 reference.setNamespace( namespace );
194 reference.setProjectId( projectId );
195 reference.setProjectVersion( versionMetadata.getId() );
196 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
197 for ( Dependency dependency : versionMetadata.getDependencies() )
199 setProperty( properties, "dependency." + i + ".classifier", dependency.getClassifier() );
200 setProperty( properties, "dependency." + i + ".scope", dependency.getScope() );
201 setProperty( properties, "dependency." + i + ".systemPath", dependency.getSystemPath() );
202 setProperty( properties, "dependency." + i + ".artifactId", dependency.getArtifactId() );
203 setProperty( properties, "dependency." + i + ".groupId", dependency.getGroupId() );
204 setProperty( properties, "dependency." + i + ".version", dependency.getVersion() );
205 setProperty( properties, "dependency." + i + ".type", dependency.getType() );
206 setProperty( properties, "dependency." + i + ".optional", String.valueOf( dependency.isOptional() ) );
208 updateProjectReference( repoId, dependency.getGroupId(), dependency.getArtifactId(),
209 dependency.getVersion(), reference );
213 Set<String> facetIds = new LinkedHashSet<String>( versionMetadata.getFacetIds() );
214 facetIds.addAll( Arrays.asList( properties.getProperty( "facetIds", "" ).split( "," ) ) );
215 properties.setProperty( "facetIds", join( facetIds ) );
217 updateProjectVersionFacets( versionMetadata, properties );
221 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
223 catch ( IOException e )
226 log.error( e.getMessage(), e );
230 private void updateProjectVersionFacets( ProjectVersionMetadata versionMetadata, Properties properties )
232 for ( MetadataFacet facet : versionMetadata.getFacetList() )
234 for ( Map.Entry<String, String> entry : facet.toProperties().entrySet() )
236 properties.setProperty( facet.getFacetId() + ":" + entry.getKey(), entry.getValue() );
241 private static void clearMetadataFacetProperties( Collection<MetadataFacet> facetList, Properties properties,
244 List<Object> propsToRemove = new ArrayList<Object>();
245 for ( MetadataFacet facet : facetList )
247 for ( Object key : properties.keySet() )
249 String keyString = (String) key;
250 if ( keyString.startsWith( prefix + facet.getFacetId() + ":" ) )
252 propsToRemove.add( key );
257 for ( Object key : propsToRemove )
259 properties.remove( key );
263 private void updateProjectReference( String repoId, String namespace, String projectId, String projectVersion,
264 ProjectVersionReference reference )
266 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
268 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
269 int i = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
270 setProperty( properties, "ref:lastReferenceNum", Integer.toString( i ) );
271 setProperty( properties, "ref:reference." + i + ".namespace", reference.getNamespace() );
272 setProperty( properties, "ref:reference." + i + ".projectId", reference.getProjectId() );
273 setProperty( properties, "ref:reference." + i + ".projectVersion", reference.getProjectVersion() );
274 setProperty( properties, "ref:reference." + i + ".referenceType", reference.getReferenceType().toString() );
278 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
280 catch ( IOException e )
283 log.error( e.getMessage(), e );
287 public void updateNamespace( String repoId, String namespace )
291 File namespaceDirectory = new File( getDirectory( repoId ), namespace );
292 Properties properties = new Properties();
293 properties.setProperty( "namespace", namespace );
294 writeProperties( properties, namespaceDirectory, NAMESPACE_METADATA_KEY );
297 catch ( IOException e )
300 log.error( e.getMessage(), e );
304 public List<String> getMetadataFacets( String repoId, String facetId )
306 File directory = getMetadataDirectory( repoId, facetId );
307 List<String> facets = new ArrayList<String>();
308 recurse( facets, "", directory );
312 private void recurse( List<String> facets, String prefix, File directory )
314 File[] list = directory.listFiles();
317 for ( File dir : list )
319 if ( dir.isDirectory() )
321 recurse( facets, prefix + "/" + dir.getName(), dir );
323 else if ( dir.getName().equals( METADATA_KEY + ".properties" ) )
325 facets.add( prefix.substring( 1 ) );
331 public MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name )
333 Properties properties;
337 readProperties( new File( getMetadataDirectory( repositoryId, facetId ), name ), METADATA_KEY );
339 catch ( FileNotFoundException e )
343 catch ( IOException e )
346 log.error( e.getMessage(), e );
349 MetadataFacet metadataFacet = null;
350 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
351 if ( metadataFacetFactory != null )
353 metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
354 Map<String, String> map = new HashMap<String, String>();
355 for ( Object key : new ArrayList( properties.keySet() ) )
357 String property = (String) key;
358 map.put( property, properties.getProperty( property ) );
360 metadataFacet.fromProperties( map );
362 return metadataFacet;
365 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
367 Properties properties = new Properties();
368 properties.putAll( metadataFacet.toProperties() );
373 new File( getMetadataDirectory( repositoryId, metadataFacet.getFacetId() ), metadataFacet.getName() );
374 writeProperties( properties, directory, METADATA_KEY );
376 catch ( IOException e )
379 log.error( e.getMessage(), e );
383 public void removeMetadataFacets( String repositoryId, String facetId )
385 File dir = getMetadataDirectory( repositoryId, facetId );
386 if ( !FileUtils.deleteQuietly( dir ) )
388 log.error( "Cannot delete the metadata repository {}", dir );
392 public void removeMetadataFacet( String repoId, String facetId, String name )
394 File dir = new File( getMetadataDirectory( repoId, facetId ), name );
395 if ( !FileUtils.deleteQuietly( dir ) )
397 log.error( "Cannot delete the metadata repository {}", dir );
401 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date startTime, Date endTime )
403 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
404 // of this information (eg. in Lucene, as before)
406 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
407 for ( String ns : getRootNamespaces( repoId ) )
409 getArtifactsByDateRange( artifacts, repoId, ns, startTime, endTime );
411 Collections.sort( artifacts, new ArtifactComparator() );
415 private void getArtifactsByDateRange( List<ArtifactMetadata> artifacts, String repoId, String ns, Date startTime,
418 for ( String namespace : getNamespaces( repoId, ns ) )
420 getArtifactsByDateRange( artifacts, repoId, ns + "." + namespace, startTime, endTime );
423 for ( String project : getProjects( repoId, ns ) )
425 for ( String version : getProjectVersions( repoId, ns, project ) )
427 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
429 if ( startTime == null || startTime.before( artifact.getWhenGathered() ) )
431 if ( endTime == null || endTime.after( artifact.getWhenGathered() ) )
433 artifacts.add( artifact );
441 public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
442 String projectVersion )
444 Map<String, ArtifactMetadata> artifacts = new HashMap<String, ArtifactMetadata>();
446 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
448 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
450 for ( Map.Entry entry : properties.entrySet() )
452 String name = (String) entry.getKey();
453 StringTokenizer tok = new StringTokenizer( name, ":" );
454 if ( tok.hasMoreTokens() && "artifact".equals( tok.nextToken() ) )
456 String field = tok.nextToken();
457 String id = tok.nextToken();
459 ArtifactMetadata artifact = artifacts.get( id );
460 if ( artifact == null )
462 artifact = new ArtifactMetadata();
463 artifact.setRepositoryId( repoId );
464 artifact.setNamespace( namespace );
465 artifact.setProject( projectId );
466 artifact.setProjectVersion( projectVersion );
467 artifact.setVersion( projectVersion );
468 artifact.setId( id );
469 artifacts.put( id, artifact );
472 String value = (String) entry.getValue();
473 if ( "updated".equals( field ) )
475 artifact.setFileLastModified( Long.parseLong( value ) );
477 else if ( "size".equals( field ) )
479 artifact.setSize( Long.valueOf( value ) );
481 else if ( "whenGathered".equals( field ) )
483 artifact.setWhenGathered( new Date( Long.parseLong( value ) ) );
485 else if ( "version".equals( field ) )
487 artifact.setVersion( value );
489 else if ( "md5".equals( field ) )
491 artifact.setMd5( value );
493 else if ( "sha1".equals( field ) )
495 artifact.setSha1( value );
497 else if ( "facetIds".equals( field ) )
499 if ( value.length() > 0 )
501 String propertyPrefix = "artifact:facet:" + id + ":";
502 for ( String facetId : value.split( "," ) )
504 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
505 if ( factory == null )
507 log.error( "Attempted to load unknown artifact metadata facet: " + facetId );
511 MetadataFacet facet = factory.createMetadataFacet();
512 String prefix = propertyPrefix + facet.getFacetId();
513 Map<String, String> map = new HashMap<String, String>();
514 for ( Object key : new ArrayList( properties.keySet() ) )
516 String property = (String) key;
517 if ( property.startsWith( prefix ) )
519 map.put( property.substring( prefix.length() + 1 ),
520 properties.getProperty( property ) );
523 facet.fromProperties( map );
524 artifact.addFacet( facet );
529 updateArtifactFacets( artifact, properties );
533 return artifacts.values();
538 // it's all instantly persisted
543 // nothing additional to close
548 log.warn( "Attempted to revert a session, but the file-based repository storage doesn't support it" );
551 public boolean canObtainAccess( Class<?> aClass )
556 public Object obtainAccess( Class<?> aClass )
558 throw new IllegalArgumentException(
559 "Access using " + aClass + " is not supported on the file metadata storage" );
562 private void updateArtifactFacets( ArtifactMetadata artifact, Properties properties )
564 String propertyPrefix = "artifact:facet:" + artifact.getId() + ":";
565 for ( MetadataFacet facet : artifact.getFacetList() )
567 for ( Map.Entry<String, String> e : facet.toProperties().entrySet() )
569 String key = propertyPrefix + facet.getFacetId() + ":" + e.getKey();
570 properties.setProperty( key, e.getValue() );
575 public Collection<String> getRepositories()
577 List<String> repositories = new ArrayList<String>();
578 for ( ManagedRepositoryConfiguration managedRepositoryConfiguration : configuration.getConfiguration().getManagedRepositories() )
580 repositories.add( managedRepositoryConfiguration.getId() );
585 public List<ArtifactMetadata> getArtifactsByChecksum( String repositoryId, String checksum )
587 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
588 // of this information (eg. in Lucene, as before)
589 // alternatively, we could build a referential tree in the content repository, however it would need some levels
590 // of depth to avoid being too broad to be useful (eg. /repository/checksums/a/ab/abcdef1234567)
592 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
593 for ( String ns : getRootNamespaces( repositoryId ) )
595 getArtifactsByChecksum( artifacts, repositoryId, ns, checksum );
600 public void removeArtifact( String repoId, String namespace, String project, String version, String id )
603 File directory = new File( getDirectory( repoId ), namespace + "/" + project + "/" + version );
605 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
607 properties.remove( "artifact:updated:" + id );
608 properties.remove( "artifact:whenGathered:" + id );
609 properties.remove( "artifact:size:" + id );
610 properties.remove( "artifact:md5:" + id );
611 properties.remove( "artifact:sha1:" + id );
612 properties.remove( "artifact:version:" + id );
613 properties.remove( "artifact:facetIds:" + id );
615 String prefix = "artifact:facet:" + id + ":";
616 for ( Object key : new ArrayList<Object>( properties.keySet() ) )
618 String property = (String) key;
619 if ( property.startsWith( prefix ) )
621 properties.remove( property );
628 FileUtils.deleteDirectory( directory );
630 //writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
632 catch ( IOException e )
635 log.error( e.getMessage(), e );
640 * FIXME implements this !!!!
641 * @param repositoryId
644 * @param projectVersion
646 * @param metadataFacet will remove artifacts which have this {@link MetadataFacet} using equals
647 * @throws MetadataRepositoryException
649 public void removeArtifact( String repositoryId, String namespace, String project, String projectVersion,
650 MetadataFacet metadataFacet )
651 throws MetadataRepositoryException
653 throw new UnsupportedOperationException("not implemented");
656 public void removeRepository( String repoId )
658 File dir = getDirectory( repoId );
659 if ( !FileUtils.deleteQuietly( dir ) )
661 log.error( "Cannot delete repository {}", dir );
665 private void getArtifactsByChecksum( List<ArtifactMetadata> artifacts, String repositoryId, String ns,
668 for ( String namespace : getNamespaces( repositoryId, ns ) )
670 getArtifactsByChecksum( artifacts, repositoryId, ns + "." + namespace, checksum );
673 for ( String project : getProjects( repositoryId, ns ) )
675 for ( String version : getProjectVersions( repositoryId, ns, project ) )
677 for ( ArtifactMetadata artifact : getArtifacts( repositoryId, ns, project, version ) )
679 if ( checksum.equals( artifact.getMd5() ) || checksum.equals( artifact.getSha1() ) )
681 artifacts.add( artifact );
688 private File getMetadataDirectory( String repoId, String facetId )
690 return new File( getBaseDirectory( repoId ), "facets/" + facetId );
693 private String join( Collection<String> ids )
695 if ( ids != null && !ids.isEmpty() )
697 StringBuilder s = new StringBuilder();
698 for ( String id : ids )
703 return s.substring( 0, s.length() - 1 );
708 private void setProperty( Properties properties, String name, String value )
712 properties.setProperty( name, value );
716 public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion,
717 ArtifactMetadata artifact )
719 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
720 metadata.setId( projectVersion );
721 updateProjectVersion( repoId, namespace, projectId, metadata );
723 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
725 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
727 clearMetadataFacetProperties( artifact.getFacetList(), properties, "artifact:facet:" + artifact.getId() + ":" );
729 String id = artifact.getId();
730 properties.setProperty( "artifact:updated:" + id, Long.toString( artifact.getFileLastModified().getTime() ) );
731 properties.setProperty( "artifact:whenGathered:" + id, Long.toString( artifact.getWhenGathered().getTime() ) );
732 properties.setProperty( "artifact:size:" + id, Long.toString( artifact.getSize() ) );
733 if ( artifact.getMd5() != null )
735 properties.setProperty( "artifact:md5:" + id, artifact.getMd5() );
737 if ( artifact.getSha1() != null )
739 properties.setProperty( "artifact:sha1:" + id, artifact.getSha1() );
741 properties.setProperty( "artifact:version:" + id, artifact.getVersion() );
743 Set<String> facetIds = new LinkedHashSet<String>( artifact.getFacetIds() );
744 String property = "artifact:facetIds:" + id;
745 facetIds.addAll( Arrays.asList( properties.getProperty( property, "" ).split( "," ) ) );
746 properties.setProperty( property, join( facetIds ) );
748 updateArtifactFacets( artifact, properties );
752 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
754 catch ( IOException e )
757 log.error( e.getMessage(), e );
761 private Properties readOrCreateProperties( File directory, String propertiesKey )
765 return readProperties( directory, propertiesKey );
767 catch ( FileNotFoundException e )
769 // ignore and return new properties
771 catch ( IOException e )
774 log.error( e.getMessage(), e );
776 return new Properties();
779 private Properties readProperties( File directory, String propertiesKey )
782 Properties properties = new Properties();
783 FileInputStream in = null;
786 in = new FileInputStream( new File( directory, propertiesKey + ".properties" ) );
787 properties.load( in );
791 IOUtils.closeQuietly( in );
796 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
798 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
800 Properties properties = readOrCreateProperties( directory, PROJECT_METADATA_KEY );
802 ProjectMetadata project = null;
804 String id = properties.getProperty( "id" );
807 project = new ProjectMetadata();
808 project.setNamespace( properties.getProperty( "namespace" ) );
815 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
816 String projectVersion )
818 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
820 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
821 String id = properties.getProperty( "id" );
822 ProjectVersionMetadata versionMetadata = null;
825 versionMetadata = new ProjectVersionMetadata();
826 versionMetadata.setId( id );
827 versionMetadata.setName( properties.getProperty( "name" ) );
828 versionMetadata.setDescription( properties.getProperty( "description" ) );
829 versionMetadata.setUrl( properties.getProperty( "url" ) );
830 versionMetadata.setIncomplete( Boolean.valueOf( properties.getProperty( "incomplete", "false" ) ) );
832 String scmConnection = properties.getProperty( "scm.connection" );
833 String scmDeveloperConnection = properties.getProperty( "scm.developerConnection" );
834 String scmUrl = properties.getProperty( "scm.url" );
835 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
838 scm.setConnection( scmConnection );
839 scm.setDeveloperConnection( scmDeveloperConnection );
840 scm.setUrl( scmUrl );
841 versionMetadata.setScm( scm );
844 String ciSystem = properties.getProperty( "ci.system" );
845 String ciUrl = properties.getProperty( "ci.url" );
846 if ( ciSystem != null || ciUrl != null )
848 CiManagement ci = new CiManagement();
849 ci.setSystem( ciSystem );
851 versionMetadata.setCiManagement( ci );
854 String issueSystem = properties.getProperty( "issue.system" );
855 String issueUrl = properties.getProperty( "issue.url" );
856 if ( issueSystem != null || issueUrl != null )
858 IssueManagement issueManagement = new IssueManagement();
859 issueManagement.setSystem( issueSystem );
860 issueManagement.setUrl( issueUrl );
861 versionMetadata.setIssueManagement( issueManagement );
864 String orgName = properties.getProperty( "org.name" );
865 String orgUrl = properties.getProperty( "org.url" );
866 if ( orgName != null || orgUrl != null )
868 Organization org = new Organization();
869 org.setName( orgName );
870 org.setUrl( orgUrl );
871 versionMetadata.setOrganization( org );
874 boolean done = false;
878 String licenseName = properties.getProperty( "license." + i + ".name" );
879 String licenseUrl = properties.getProperty( "license." + i + ".url" );
880 if ( licenseName != null || licenseUrl != null )
882 License license = new License();
883 license.setName( licenseName );
884 license.setUrl( licenseUrl );
885 versionMetadata.addLicense( license );
898 String mailingListName = properties.getProperty( "mailingList." + i + ".name" );
899 if ( mailingListName != null )
901 MailingList mailingList = new MailingList();
902 mailingList.setName( mailingListName );
903 mailingList.setMainArchiveUrl( properties.getProperty( "mailingList." + i + ".archive" ) );
904 String p = properties.getProperty( "mailingList." + i + ".otherArchives" );
905 if ( p != null && p.length() > 0 )
907 mailingList.setOtherArchives( Arrays.asList( p.split( "," ) ) );
911 mailingList.setOtherArchives( Collections.<String>emptyList() );
913 mailingList.setPostAddress( properties.getProperty( "mailingList." + i + ".post" ) );
914 mailingList.setSubscribeAddress( properties.getProperty( "mailingList." + i + ".subscribe" ) );
915 mailingList.setUnsubscribeAddress( properties.getProperty( "mailingList." + i + ".unsubscribe" ) );
916 versionMetadata.addMailingList( mailingList );
929 String dependencyArtifactId = properties.getProperty( "dependency." + i + ".artifactId" );
930 if ( dependencyArtifactId != null )
932 Dependency dependency = new Dependency();
933 dependency.setArtifactId( dependencyArtifactId );
934 dependency.setGroupId( properties.getProperty( "dependency." + i + ".groupId" ) );
935 dependency.setClassifier( properties.getProperty( "dependency." + i + ".classifier" ) );
936 dependency.setOptional(
937 Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) );
938 dependency.setScope( properties.getProperty( "dependency." + i + ".scope" ) );
939 dependency.setSystemPath( properties.getProperty( "dependency." + i + ".systemPath" ) );
940 dependency.setType( properties.getProperty( "dependency." + i + ".type" ) );
941 dependency.setVersion( properties.getProperty( "dependency." + i + ".version" ) );
942 dependency.setOptional(
943 Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) );
944 versionMetadata.addDependency( dependency );
953 String facetIds = properties.getProperty( "facetIds", "" );
954 if ( facetIds.length() > 0 )
956 for ( String facetId : facetIds.split( "," ) )
958 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
959 if ( factory == null )
961 log.error( "Attempted to load unknown project version metadata facet: {}", facetId );
965 MetadataFacet facet = factory.createMetadataFacet();
966 Map<String, String> map = new HashMap<String, String>();
967 for ( Object key : new ArrayList( properties.keySet() ) )
969 String property = (String) key;
970 if ( property.startsWith( facet.getFacetId() ) )
972 map.put( property.substring( facet.getFacetId().length() + 1 ),
973 properties.getProperty( property ) );
976 facet.fromProperties( map );
977 versionMetadata.addFacet( facet );
982 updateProjectVersionFacets( versionMetadata, properties );
984 return versionMetadata;
987 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
988 String projectVersion )
990 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
992 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
994 Set<String> versions = new HashSet<String>();
995 for ( Map.Entry entry : properties.entrySet() )
997 String name = (String) entry.getKey();
998 if ( name.startsWith( "artifact:version:" ) )
1000 versions.add( (String) entry.getValue() );
1006 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1007 String projectVersion )
1009 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
1011 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
1012 int numberOfRefs = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
1014 List<ProjectVersionReference> references = new ArrayList<ProjectVersionReference>();
1015 for ( int i = 0; i < numberOfRefs; i++ )
1017 ProjectVersionReference reference = new ProjectVersionReference();
1018 reference.setProjectId( properties.getProperty( "ref:reference." + i + ".projectId" ) );
1019 reference.setNamespace( properties.getProperty( "ref:reference." + i + ".namespace" ) );
1020 reference.setProjectVersion( properties.getProperty( "ref:reference." + i + ".projectVersion" ) );
1021 reference.setReferenceType( ProjectVersionReference.ReferenceType.valueOf(
1022 properties.getProperty( "ref:reference." + i + ".referenceType" ) ) );
1023 references.add( reference );
1028 public Collection<String> getRootNamespaces( String repoId )
1030 return getNamespaces( repoId, null );
1033 public Collection<String> getNamespaces( String repoId, String baseNamespace )
1035 List<String> allNamespaces = new ArrayList<String>();
1036 File directory = getDirectory( repoId );
1037 File[] files = directory.listFiles();
1038 if ( files != null )
1040 for ( File namespace : files )
1042 if ( new File( namespace, NAMESPACE_METADATA_KEY + ".properties" ).exists() )
1044 allNamespaces.add( namespace.getName() );
1049 Set<String> namespaces = new LinkedHashSet<String>();
1050 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
1051 for ( String namespace : allNamespaces )
1053 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
1055 int i = namespace.indexOf( '.', fromIndex );
1058 namespaces.add( namespace.substring( fromIndex, i ) );
1062 namespaces.add( namespace.substring( fromIndex ) );
1066 return new ArrayList<String>( namespaces );
1069 public Collection<String> getProjects( String repoId, String namespace )
1071 List<String> projects = new ArrayList<String>();
1072 File directory = new File( getDirectory( repoId ), namespace );
1073 File[] files = directory.listFiles();
1074 if ( files != null )
1076 for ( File project : files )
1078 if ( new File( project, PROJECT_METADATA_KEY + ".properties" ).exists() )
1080 projects.add( project.getName() );
1087 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
1089 List<String> projectVersions = new ArrayList<String>();
1090 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
1091 File[] files = directory.listFiles();
1092 if ( files != null )
1094 for ( File projectVersion : files )
1096 if ( new File( projectVersion, PROJECT_VERSION_METADATA_KEY + ".properties" ).exists() )
1098 projectVersions.add( projectVersion.getName() );
1102 return projectVersions;
1105 private void writeProperties( Properties properties, File directory, String propertiesKey )
1109 FileOutputStream os = new FileOutputStream( new File( directory, propertiesKey + ".properties" ) );
1112 properties.store( os, null );
1116 IOUtils.closeQuietly( os );
1120 private static class ArtifactComparator
1121 implements Comparator<ArtifactMetadata>
1123 public int compare( ArtifactMetadata artifact1, ArtifactMetadata artifact2 )
1125 if ( artifact1.getWhenGathered() == artifact2.getWhenGathered() )
1129 if ( artifact1.getWhenGathered() == null )
1133 if ( artifact2.getWhenGathered() == null )
1137 return artifact1.getWhenGathered().compareTo( artifact2.getWhenGathered() );
1141 public List<ArtifactMetadata> getArtifacts( String repoId )
1143 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
1144 for ( String ns : getRootNamespaces( repoId ) )
1146 getArtifacts( artifacts, repoId, ns );
1151 private void getArtifacts( List<ArtifactMetadata> artifacts, String repoId, String ns )
1153 for ( String namespace : getNamespaces( repoId, ns ) )
1155 getArtifacts( artifacts, repoId, ns + "." + namespace );
1158 for ( String project : getProjects( repoId, ns ) )
1160 for ( String version : getProjectVersions( repoId, ns, project ) )
1162 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
1164 artifacts.add( artifact );