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( ArtifactMetadata artifactMetadata, String baseVersion )
601 throws MetadataRepositoryException
603 // FIXME implement this
604 throw new UnsupportedOperationException();
607 public void removeArtifact( String repoId, String namespace, String project, String version, String id )
610 File directory = new File( getDirectory( repoId ), namespace + "/" + project + "/" + version );
612 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
614 properties.remove( "artifact:updated:" + id );
615 properties.remove( "artifact:whenGathered:" + id );
616 properties.remove( "artifact:size:" + id );
617 properties.remove( "artifact:md5:" + id );
618 properties.remove( "artifact:sha1:" + id );
619 properties.remove( "artifact:version:" + id );
620 properties.remove( "artifact:facetIds:" + id );
622 String prefix = "artifact:facet:" + id + ":";
623 for ( Object key : new ArrayList<Object>( properties.keySet() ) )
625 String property = (String) key;
626 if ( property.startsWith( prefix ) )
628 properties.remove( property );
635 FileUtils.deleteDirectory( directory );
637 //writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
639 catch ( IOException e )
642 log.error( e.getMessage(), e );
647 * FIXME implements this !!!!
648 * @param repositoryId
651 * @param projectVersion
653 * @param metadataFacet will remove artifacts which have this {@link MetadataFacet} using equals
654 * @throws MetadataRepositoryException
656 public void removeArtifact( String repositoryId, String namespace, String project, String projectVersion,
657 MetadataFacet metadataFacet )
658 throws MetadataRepositoryException
660 throw new UnsupportedOperationException("not implemented");
663 public void removeRepository( String repoId )
665 File dir = getDirectory( repoId );
666 if ( !FileUtils.deleteQuietly( dir ) )
668 log.error( "Cannot delete repository {}", dir );
672 private void getArtifactsByChecksum( List<ArtifactMetadata> artifacts, String repositoryId, String ns,
675 for ( String namespace : getNamespaces( repositoryId, ns ) )
677 getArtifactsByChecksum( artifacts, repositoryId, ns + "." + namespace, checksum );
680 for ( String project : getProjects( repositoryId, ns ) )
682 for ( String version : getProjectVersions( repositoryId, ns, project ) )
684 for ( ArtifactMetadata artifact : getArtifacts( repositoryId, ns, project, version ) )
686 if ( checksum.equals( artifact.getMd5() ) || checksum.equals( artifact.getSha1() ) )
688 artifacts.add( artifact );
695 private File getMetadataDirectory( String repoId, String facetId )
697 return new File( getBaseDirectory( repoId ), "facets/" + facetId );
700 private String join( Collection<String> ids )
702 if ( ids != null && !ids.isEmpty() )
704 StringBuilder s = new StringBuilder();
705 for ( String id : ids )
710 return s.substring( 0, s.length() - 1 );
715 private void setProperty( Properties properties, String name, String value )
719 properties.setProperty( name, value );
723 public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion,
724 ArtifactMetadata artifact )
726 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
727 metadata.setId( projectVersion );
728 updateProjectVersion( repoId, namespace, projectId, metadata );
730 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
732 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
734 clearMetadataFacetProperties( artifact.getFacetList(), properties, "artifact:facet:" + artifact.getId() + ":" );
736 String id = artifact.getId();
737 properties.setProperty( "artifact:updated:" + id, Long.toString( artifact.getFileLastModified().getTime() ) );
738 properties.setProperty( "artifact:whenGathered:" + id, Long.toString( artifact.getWhenGathered().getTime() ) );
739 properties.setProperty( "artifact:size:" + id, Long.toString( artifact.getSize() ) );
740 if ( artifact.getMd5() != null )
742 properties.setProperty( "artifact:md5:" + id, artifact.getMd5() );
744 if ( artifact.getSha1() != null )
746 properties.setProperty( "artifact:sha1:" + id, artifact.getSha1() );
748 properties.setProperty( "artifact:version:" + id, artifact.getVersion() );
750 Set<String> facetIds = new LinkedHashSet<String>( artifact.getFacetIds() );
751 String property = "artifact:facetIds:" + id;
752 facetIds.addAll( Arrays.asList( properties.getProperty( property, "" ).split( "," ) ) );
753 properties.setProperty( property, join( facetIds ) );
755 updateArtifactFacets( artifact, properties );
759 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
761 catch ( IOException e )
764 log.error( e.getMessage(), e );
768 private Properties readOrCreateProperties( File directory, String propertiesKey )
772 return readProperties( directory, propertiesKey );
774 catch ( FileNotFoundException e )
776 // ignore and return new properties
778 catch ( IOException e )
781 log.error( e.getMessage(), e );
783 return new Properties();
786 private Properties readProperties( File directory, String propertiesKey )
789 Properties properties = new Properties();
790 FileInputStream in = null;
793 in = new FileInputStream( new File( directory, propertiesKey + ".properties" ) );
794 properties.load( in );
798 IOUtils.closeQuietly( in );
803 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
805 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
807 Properties properties = readOrCreateProperties( directory, PROJECT_METADATA_KEY );
809 ProjectMetadata project = null;
811 String id = properties.getProperty( "id" );
814 project = new ProjectMetadata();
815 project.setNamespace( properties.getProperty( "namespace" ) );
822 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
823 String projectVersion )
825 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
827 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
828 String id = properties.getProperty( "id" );
829 ProjectVersionMetadata versionMetadata = null;
832 versionMetadata = new ProjectVersionMetadata();
833 versionMetadata.setId( id );
834 versionMetadata.setName( properties.getProperty( "name" ) );
835 versionMetadata.setDescription( properties.getProperty( "description" ) );
836 versionMetadata.setUrl( properties.getProperty( "url" ) );
837 versionMetadata.setIncomplete( Boolean.valueOf( properties.getProperty( "incomplete", "false" ) ) );
839 String scmConnection = properties.getProperty( "scm.connection" );
840 String scmDeveloperConnection = properties.getProperty( "scm.developerConnection" );
841 String scmUrl = properties.getProperty( "scm.url" );
842 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
845 scm.setConnection( scmConnection );
846 scm.setDeveloperConnection( scmDeveloperConnection );
847 scm.setUrl( scmUrl );
848 versionMetadata.setScm( scm );
851 String ciSystem = properties.getProperty( "ci.system" );
852 String ciUrl = properties.getProperty( "ci.url" );
853 if ( ciSystem != null || ciUrl != null )
855 CiManagement ci = new CiManagement();
856 ci.setSystem( ciSystem );
858 versionMetadata.setCiManagement( ci );
861 String issueSystem = properties.getProperty( "issue.system" );
862 String issueUrl = properties.getProperty( "issue.url" );
863 if ( issueSystem != null || issueUrl != null )
865 IssueManagement issueManagement = new IssueManagement();
866 issueManagement.setSystem( issueSystem );
867 issueManagement.setUrl( issueUrl );
868 versionMetadata.setIssueManagement( issueManagement );
871 String orgName = properties.getProperty( "org.name" );
872 String orgUrl = properties.getProperty( "org.url" );
873 if ( orgName != null || orgUrl != null )
875 Organization org = new Organization();
876 org.setName( orgName );
877 org.setUrl( orgUrl );
878 versionMetadata.setOrganization( org );
881 boolean done = false;
885 String licenseName = properties.getProperty( "license." + i + ".name" );
886 String licenseUrl = properties.getProperty( "license." + i + ".url" );
887 if ( licenseName != null || licenseUrl != null )
889 License license = new License();
890 license.setName( licenseName );
891 license.setUrl( licenseUrl );
892 versionMetadata.addLicense( license );
905 String mailingListName = properties.getProperty( "mailingList." + i + ".name" );
906 if ( mailingListName != null )
908 MailingList mailingList = new MailingList();
909 mailingList.setName( mailingListName );
910 mailingList.setMainArchiveUrl( properties.getProperty( "mailingList." + i + ".archive" ) );
911 String p = properties.getProperty( "mailingList." + i + ".otherArchives" );
912 if ( p != null && p.length() > 0 )
914 mailingList.setOtherArchives( Arrays.asList( p.split( "," ) ) );
918 mailingList.setOtherArchives( Collections.<String>emptyList() );
920 mailingList.setPostAddress( properties.getProperty( "mailingList." + i + ".post" ) );
921 mailingList.setSubscribeAddress( properties.getProperty( "mailingList." + i + ".subscribe" ) );
922 mailingList.setUnsubscribeAddress( properties.getProperty( "mailingList." + i + ".unsubscribe" ) );
923 versionMetadata.addMailingList( mailingList );
936 String dependencyArtifactId = properties.getProperty( "dependency." + i + ".artifactId" );
937 if ( dependencyArtifactId != null )
939 Dependency dependency = new Dependency();
940 dependency.setArtifactId( dependencyArtifactId );
941 dependency.setGroupId( properties.getProperty( "dependency." + i + ".groupId" ) );
942 dependency.setClassifier( properties.getProperty( "dependency." + i + ".classifier" ) );
943 dependency.setOptional(
944 Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) );
945 dependency.setScope( properties.getProperty( "dependency." + i + ".scope" ) );
946 dependency.setSystemPath( properties.getProperty( "dependency." + i + ".systemPath" ) );
947 dependency.setType( properties.getProperty( "dependency." + i + ".type" ) );
948 dependency.setVersion( properties.getProperty( "dependency." + i + ".version" ) );
949 dependency.setOptional(
950 Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) );
951 versionMetadata.addDependency( dependency );
960 String facetIds = properties.getProperty( "facetIds", "" );
961 if ( facetIds.length() > 0 )
963 for ( String facetId : facetIds.split( "," ) )
965 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
966 if ( factory == null )
968 log.error( "Attempted to load unknown project version metadata facet: {}", facetId );
972 MetadataFacet facet = factory.createMetadataFacet();
973 Map<String, String> map = new HashMap<String, String>();
974 for ( Object key : new ArrayList( properties.keySet() ) )
976 String property = (String) key;
977 if ( property.startsWith( facet.getFacetId() ) )
979 map.put( property.substring( facet.getFacetId().length() + 1 ),
980 properties.getProperty( property ) );
983 facet.fromProperties( map );
984 versionMetadata.addFacet( facet );
989 updateProjectVersionFacets( versionMetadata, properties );
991 return versionMetadata;
994 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
995 String projectVersion )
997 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
999 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
1001 Set<String> versions = new HashSet<String>();
1002 for ( Map.Entry entry : properties.entrySet() )
1004 String name = (String) entry.getKey();
1005 if ( name.startsWith( "artifact:version:" ) )
1007 versions.add( (String) entry.getValue() );
1013 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1014 String projectVersion )
1016 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
1018 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
1019 int numberOfRefs = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
1021 List<ProjectVersionReference> references = new ArrayList<ProjectVersionReference>();
1022 for ( int i = 0; i < numberOfRefs; i++ )
1024 ProjectVersionReference reference = new ProjectVersionReference();
1025 reference.setProjectId( properties.getProperty( "ref:reference." + i + ".projectId" ) );
1026 reference.setNamespace( properties.getProperty( "ref:reference." + i + ".namespace" ) );
1027 reference.setProjectVersion( properties.getProperty( "ref:reference." + i + ".projectVersion" ) );
1028 reference.setReferenceType( ProjectVersionReference.ReferenceType.valueOf(
1029 properties.getProperty( "ref:reference." + i + ".referenceType" ) ) );
1030 references.add( reference );
1035 public Collection<String> getRootNamespaces( String repoId )
1037 return getNamespaces( repoId, null );
1040 public Collection<String> getNamespaces( String repoId, String baseNamespace )
1042 List<String> allNamespaces = new ArrayList<String>();
1043 File directory = getDirectory( repoId );
1044 File[] files = directory.listFiles();
1045 if ( files != null )
1047 for ( File namespace : files )
1049 if ( new File( namespace, NAMESPACE_METADATA_KEY + ".properties" ).exists() )
1051 allNamespaces.add( namespace.getName() );
1056 Set<String> namespaces = new LinkedHashSet<String>();
1057 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
1058 for ( String namespace : allNamespaces )
1060 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
1062 int i = namespace.indexOf( '.', fromIndex );
1065 namespaces.add( namespace.substring( fromIndex, i ) );
1069 namespaces.add( namespace.substring( fromIndex ) );
1073 return new ArrayList<String>( namespaces );
1076 public Collection<String> getProjects( String repoId, String namespace )
1078 List<String> projects = new ArrayList<String>();
1079 File directory = new File( getDirectory( repoId ), namespace );
1080 File[] files = directory.listFiles();
1081 if ( files != null )
1083 for ( File project : files )
1085 if ( new File( project, PROJECT_METADATA_KEY + ".properties" ).exists() )
1087 projects.add( project.getName() );
1094 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
1096 List<String> projectVersions = new ArrayList<String>();
1097 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
1098 File[] files = directory.listFiles();
1099 if ( files != null )
1101 for ( File projectVersion : files )
1103 if ( new File( projectVersion, PROJECT_VERSION_METADATA_KEY + ".properties" ).exists() )
1105 projectVersions.add( projectVersion.getName() );
1109 return projectVersions;
1112 private void writeProperties( Properties properties, File directory, String propertiesKey )
1116 FileOutputStream os = new FileOutputStream( new File( directory, propertiesKey + ".properties" ) );
1119 properties.store( os, null );
1123 IOUtils.closeQuietly( os );
1127 private static class ArtifactComparator
1128 implements Comparator<ArtifactMetadata>
1130 public int compare( ArtifactMetadata artifact1, ArtifactMetadata artifact2 )
1132 if ( artifact1.getWhenGathered() == artifact2.getWhenGathered() )
1136 if ( artifact1.getWhenGathered() == null )
1140 if ( artifact2.getWhenGathered() == null )
1144 return artifact1.getWhenGathered().compareTo( artifact2.getWhenGathered() );
1148 public List<ArtifactMetadata> getArtifacts( String repoId )
1150 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
1151 for ( String ns : getRootNamespaces( repoId ) )
1153 getArtifacts( artifacts, repoId, ns );
1158 private void getArtifacts( List<ArtifactMetadata> artifacts, String repoId, String ns )
1160 for ( String namespace : getNamespaces( repoId, ns ) )
1162 getArtifacts( artifacts, repoId, ns + "." + namespace );
1165 for ( String project : getProjects( repoId, ns ) )
1167 for ( String version : getProjectVersions( repoId, ns, project ) )
1169 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
1171 artifacts.add( artifact );