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.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 import java.io.FileNotFoundException;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.OutputStream;
48 import java.nio.file.Files;
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 //import org.apache.commons.io.IOUtils;
66 public class FileMetadataRepository
67 implements MetadataRepository
69 private final Map<String, MetadataFacetFactory> metadataFacetFactories;
71 private final ArchivaConfiguration configuration;
73 private Logger log = LoggerFactory.getLogger( FileMetadataRepository.class );
75 private static final String PROJECT_METADATA_KEY = "project-metadata";
77 private static final String PROJECT_VERSION_METADATA_KEY = "version-metadata";
79 private static final String NAMESPACE_METADATA_KEY = "namespace-metadata";
81 private static final String METADATA_KEY = "metadata";
83 public FileMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories,
84 ArchivaConfiguration configuration )
86 this.metadataFacetFactories = metadataFacetFactories;
87 this.configuration = configuration;
90 private File getBaseDirectory( String repoId )
92 // TODO: should be configurable, like the index
93 String basedir = configuration.getConfiguration().getManagedRepositoriesAsMap().get( repoId ).getLocation();
94 return new File( basedir, ".archiva" );
97 private File getDirectory( String repoId )
99 return new File( getBaseDirectory( repoId ), "content" );
103 public void updateProject( String repoId, ProjectMetadata project )
105 updateProject( repoId, project.getNamespace(), project.getId() );
108 private void updateProject( String repoId, String namespace, String id )
110 // TODO: this is a more braindead implementation than we would normally expect, for prototyping purposes
111 updateNamespace( repoId, namespace );
115 File namespaceDirectory = new File( getDirectory( repoId ), namespace );
116 Properties properties = new Properties();
117 properties.setProperty( "namespace", namespace );
118 properties.setProperty( "id", id );
119 writeProperties( properties, new File( namespaceDirectory, id ), PROJECT_METADATA_KEY );
121 catch ( IOException e )
124 log.error( e.getMessage(), e );
129 public void updateProjectVersion( String repoId, String namespace, String projectId,
130 ProjectVersionMetadata versionMetadata )
132 updateProject( repoId, namespace, projectId );
135 new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + versionMetadata.getId() );
137 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
138 // remove properties that are not references or artifacts
139 for ( Object key : new ArrayList( properties.keySet() ) )
141 String name = (String) key;
142 if ( !name.contains( ":" ) && !name.equals( "facetIds" ) )
144 properties.remove( name );
147 // clear the facet contents so old properties are no longer written
148 clearMetadataFacetProperties( versionMetadata.getFacetList(), properties, "" );
150 properties.setProperty( "id", versionMetadata.getId() );
151 setProperty( properties, "name", versionMetadata.getName() );
152 setProperty( properties, "description", versionMetadata.getDescription() );
153 setProperty( properties, "url", versionMetadata.getUrl() );
154 setProperty( properties, "incomplete", String.valueOf( versionMetadata.isIncomplete() ) );
155 if ( versionMetadata.getScm() != null )
157 setProperty( properties, "scm.connection", versionMetadata.getScm().getConnection() );
158 setProperty( properties, "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() );
159 setProperty( properties, "scm.url", versionMetadata.getScm().getUrl() );
161 if ( versionMetadata.getCiManagement() != null )
163 setProperty( properties, "ci.system", versionMetadata.getCiManagement().getSystem() );
164 setProperty( properties, "ci.url", versionMetadata.getCiManagement().getUrl() );
166 if ( versionMetadata.getIssueManagement() != null )
168 setProperty( properties, "issue.system", versionMetadata.getIssueManagement().getSystem() );
169 setProperty( properties, "issue.url", versionMetadata.getIssueManagement().getUrl() );
171 if ( versionMetadata.getOrganization() != null )
173 setProperty( properties, "org.name", versionMetadata.getOrganization().getName() );
174 setProperty( properties, "org.url", versionMetadata.getOrganization().getUrl() );
177 for ( License license : versionMetadata.getLicenses() )
179 setProperty( properties, "license." + i + ".name", license.getName() );
180 setProperty( properties, "license." + i + ".url", license.getUrl() );
184 for ( MailingList mailingList : versionMetadata.getMailingLists() )
186 setProperty( properties, "mailingList." + i + ".archive", mailingList.getMainArchiveUrl() );
187 setProperty( properties, "mailingList." + i + ".name", mailingList.getName() );
188 setProperty( properties, "mailingList." + i + ".post", mailingList.getPostAddress() );
189 setProperty( properties, "mailingList." + i + ".unsubscribe", mailingList.getUnsubscribeAddress() );
190 setProperty( properties, "mailingList." + i + ".subscribe", mailingList.getSubscribeAddress() );
191 setProperty( properties, "mailingList." + i + ".otherArchives", join( mailingList.getOtherArchives() ) );
195 ProjectVersionReference reference = new ProjectVersionReference();
196 reference.setNamespace( namespace );
197 reference.setProjectId( projectId );
198 reference.setProjectVersion( versionMetadata.getId() );
199 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
200 for ( Dependency dependency : versionMetadata.getDependencies() )
202 setProperty( properties, "dependency." + i + ".classifier", dependency.getClassifier() );
203 setProperty( properties, "dependency." + i + ".scope", dependency.getScope() );
204 setProperty( properties, "dependency." + i + ".systemPath", dependency.getSystemPath() );
205 setProperty( properties, "dependency." + i + ".artifactId", dependency.getArtifactId() );
206 setProperty( properties, "dependency." + i + ".groupId", dependency.getGroupId() );
207 setProperty( properties, "dependency." + i + ".version", dependency.getVersion() );
208 setProperty( properties, "dependency." + i + ".type", dependency.getType() );
209 setProperty( properties, "dependency." + i + ".optional", String.valueOf( dependency.isOptional() ) );
211 updateProjectReference( repoId, dependency.getGroupId(), dependency.getArtifactId(),
212 dependency.getVersion(), reference );
216 Set<String> facetIds = new LinkedHashSet<String>( versionMetadata.getFacetIds() );
217 facetIds.addAll( Arrays.asList( properties.getProperty( "facetIds", "" ).split( "," ) ) );
218 properties.setProperty( "facetIds", join( facetIds ) );
220 updateProjectVersionFacets( versionMetadata, properties );
224 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
226 catch ( IOException e )
229 log.error( e.getMessage(), e );
233 private void updateProjectVersionFacets( ProjectVersionMetadata versionMetadata, Properties properties )
235 for ( MetadataFacet facet : versionMetadata.getFacetList() )
237 for ( Map.Entry<String, String> entry : facet.toProperties().entrySet() )
239 properties.setProperty( facet.getFacetId() + ":" + entry.getKey(), entry.getValue() );
244 private static void clearMetadataFacetProperties( Collection<MetadataFacet> facetList, Properties properties,
247 List<Object> propsToRemove = new ArrayList<>();
248 for ( MetadataFacet facet : facetList )
250 for ( Object key : new ArrayList( properties.keySet() ) )
252 String keyString = (String) key;
253 if ( keyString.startsWith( prefix + facet.getFacetId() + ":" ) )
255 propsToRemove.add( key );
260 for ( Object key : propsToRemove )
262 properties.remove( key );
266 private void updateProjectReference( String repoId, String namespace, String projectId, String projectVersion,
267 ProjectVersionReference reference )
269 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
271 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
272 int i = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
273 setProperty( properties, "ref:lastReferenceNum", Integer.toString( i ) );
274 setProperty( properties, "ref:reference." + i + ".namespace", reference.getNamespace() );
275 setProperty( properties, "ref:reference." + i + ".projectId", reference.getProjectId() );
276 setProperty( properties, "ref:reference." + i + ".projectVersion", reference.getProjectVersion() );
277 setProperty( properties, "ref:reference." + i + ".referenceType", reference.getReferenceType().toString() );
281 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
283 catch ( IOException e )
286 log.error( e.getMessage(), e );
291 public void updateNamespace( String repoId, String namespace )
295 File namespaceDirectory = new File( getDirectory( repoId ), namespace );
296 Properties properties = new Properties();
297 properties.setProperty( "namespace", namespace );
298 writeProperties( properties, namespaceDirectory, NAMESPACE_METADATA_KEY );
301 catch ( IOException e )
304 log.error( e.getMessage(), e );
309 public List<String> getMetadataFacets( String repoId, String facetId )
311 File directory = getMetadataDirectory( repoId, facetId );
312 List<String> facets = new ArrayList<>();
313 recurse( facets, "", directory );
318 public boolean hasMetadataFacet( String repositoryId, String facetId )
319 throws MetadataRepositoryException
321 // TODO could be improved a bit
322 return !getMetadataFacets( repositoryId, facetId ).isEmpty();
325 private void recurse( List<String> facets, String prefix, File directory )
327 File[] list = directory.listFiles();
330 for ( File dir : list )
332 if ( dir.isDirectory() )
334 recurse( facets, prefix + "/" + dir.getName(), dir );
336 else if ( dir.getName().equals( METADATA_KEY + ".properties" ) )
338 facets.add( prefix.substring( 1 ) );
345 public MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name )
347 Properties properties;
351 readProperties( new File( getMetadataDirectory( repositoryId, facetId ), name ), METADATA_KEY );
353 catch ( FileNotFoundException e )
357 catch ( IOException e )
360 log.error( e.getMessage(), e );
363 MetadataFacet metadataFacet = null;
364 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
365 if ( metadataFacetFactory != null )
367 metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
368 Map<String, String> map = new HashMap<>();
369 for ( Object key : new ArrayList( properties.keySet() ) )
371 String property = (String) key;
372 map.put( property, properties.getProperty( property ) );
374 metadataFacet.fromProperties( map );
376 return metadataFacet;
380 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
382 Properties properties = new Properties();
383 properties.putAll( metadataFacet.toProperties() );
388 new File( getMetadataDirectory( repositoryId, metadataFacet.getFacetId() ), metadataFacet.getName() );
389 writeProperties( properties, directory, METADATA_KEY );
391 catch ( IOException e )
394 log.error( e.getMessage(), e );
399 public void removeMetadataFacets( String repositoryId, String facetId )
401 File dir = getMetadataDirectory( repositoryId, facetId );
402 if ( !FileUtils.deleteQuietly( dir ) )
404 log.error( "Cannot delete the metadata repository {}", dir );
409 public void removeMetadataFacet( String repoId, String facetId, String name )
411 File dir = new File( getMetadataDirectory( repoId, facetId ), name );
412 if ( !FileUtils.deleteQuietly( dir ) )
414 log.error( "Cannot delete the metadata repository {}", dir );
419 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date startTime, Date endTime )
421 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
422 // of this information (eg. in Lucene, as before)
424 List<ArtifactMetadata> artifacts = new ArrayList<>();
425 for ( String ns : getRootNamespaces( repoId ) )
427 getArtifactsByDateRange( artifacts, repoId, ns, startTime, endTime );
429 Collections.sort( artifacts, new ArtifactComparator() );
433 private void getArtifactsByDateRange( List<ArtifactMetadata> artifacts, String repoId, String ns, Date startTime,
436 for ( String namespace : getNamespaces( repoId, ns ) )
438 getArtifactsByDateRange( artifacts, repoId, ns + "." + namespace, startTime, endTime );
441 for ( String project : getProjects( repoId, ns ) )
443 for ( String version : getProjectVersions( repoId, ns, project ) )
445 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
447 if ( startTime == null || startTime.before( artifact.getWhenGathered() ) )
449 if ( endTime == null || endTime.after( artifact.getWhenGathered() ) )
451 artifacts.add( artifact );
460 public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
461 String projectVersion )
463 Map<String, ArtifactMetadata> artifacts = new HashMap<>();
465 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
467 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
469 for ( Map.Entry entry : properties.entrySet() )
471 String name = (String) entry.getKey();
472 StringTokenizer tok = new StringTokenizer( name, ":" );
473 if ( tok.hasMoreTokens() && "artifact".equals( tok.nextToken() ) )
475 String field = tok.nextToken();
476 String id = tok.nextToken();
478 ArtifactMetadata artifact = artifacts.get( id );
479 if ( artifact == null )
481 artifact = new ArtifactMetadata();
482 artifact.setRepositoryId( repoId );
483 artifact.setNamespace( namespace );
484 artifact.setProject( projectId );
485 artifact.setProjectVersion( projectVersion );
486 artifact.setVersion( projectVersion );
487 artifact.setId( id );
488 artifacts.put( id, artifact );
491 String value = (String) entry.getValue();
492 if ( "updated".equals( field ) )
494 artifact.setFileLastModified( Long.parseLong( value ) );
496 else if ( "size".equals( field ) )
498 artifact.setSize( Long.valueOf( value ) );
500 else if ( "whenGathered".equals( field ) )
502 artifact.setWhenGathered( new Date( Long.parseLong( value ) ) );
504 else if ( "version".equals( field ) )
506 artifact.setVersion( value );
508 else if ( "md5".equals( field ) )
510 artifact.setMd5( value );
512 else if ( "sha1".equals( field ) )
514 artifact.setSha1( value );
516 else if ( "facetIds".equals( field ) )
518 if ( value.length() > 0 )
520 String propertyPrefix = "artifact:facet:" + id + ":";
521 for ( String facetId : value.split( "," ) )
523 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
524 if ( factory == null )
526 log.error( "Attempted to load unknown artifact metadata facet: " + facetId );
530 MetadataFacet facet = factory.createMetadataFacet();
531 String prefix = propertyPrefix + facet.getFacetId();
532 Map<String, String> map = new HashMap<>();
533 for ( Object key : new ArrayList( properties.keySet() ) )
535 String property = (String) key;
536 if ( property.startsWith( prefix ) )
538 map.put( property.substring( prefix.length() + 1 ),
539 properties.getProperty( property ) );
542 facet.fromProperties( map );
543 artifact.addFacet( facet );
548 updateArtifactFacets( artifact, properties );
552 return artifacts.values();
558 // it's all instantly persisted
564 // nothing additional to close
570 log.warn( "Attempted to revert a session, but the file-based repository storage doesn't support it" );
574 public boolean canObtainAccess( Class<?> aClass )
580 public <T> T obtainAccess( Class<T> aClass )
582 throw new IllegalArgumentException(
583 "Access using " + aClass + " is not supported on the file metadata storage" );
586 private void updateArtifactFacets( ArtifactMetadata artifact, Properties properties )
588 String propertyPrefix = "artifact:facet:" + artifact.getId() + ":";
589 for ( MetadataFacet facet : artifact.getFacetList() )
591 for ( Map.Entry<String, String> e : facet.toProperties().entrySet() )
593 String key = propertyPrefix + facet.getFacetId() + ":" + e.getKey();
594 properties.setProperty( key, e.getValue() );
600 public Collection<String> getRepositories()
602 List<String> repositories = new ArrayList<>();
603 for ( ManagedRepositoryConfiguration managedRepositoryConfiguration : configuration.getConfiguration().getManagedRepositories() )
605 repositories.add( managedRepositoryConfiguration.getId() );
611 public List<ArtifactMetadata> getArtifactsByChecksum( String repositoryId, String checksum )
613 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
614 // of this information (eg. in Lucene, as before)
615 // alternatively, we could build a referential tree in the content repository, however it would need some levels
616 // of depth to avoid being too broad to be useful (eg. /repository/checksums/a/ab/abcdef1234567)
618 List<ArtifactMetadata> artifacts = new ArrayList<>();
619 for ( String ns : getRootNamespaces( repositoryId ) )
621 getArtifactsByChecksum( artifacts, repositoryId, ns, checksum );
627 public void removeNamespace( String repositoryId, String project )
628 throws MetadataRepositoryException
632 File namespaceDirectory = new File( getDirectory( repositoryId ), project );
633 FileUtils.deleteDirectory( namespaceDirectory );
634 //Properties properties = new Properties();
635 //properties.setProperty( "namespace", namespace );
636 //writeProperties( properties, namespaceDirectory, NAMESPACE_METADATA_KEY );
639 catch ( IOException e )
641 throw new MetadataRepositoryException( e.getMessage(), e );
646 public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
647 throws MetadataRepositoryException
650 File directory = new File( getDirectory( artifactMetadata.getRepositoryId() ),
651 artifactMetadata.getNamespace() + "/" + artifactMetadata.getProject() + "/"
655 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
657 String id = artifactMetadata.getId();
659 properties.remove( "artifact:updated:" + id );
660 properties.remove( "artifact:whenGathered:" + id );
661 properties.remove( "artifact:size:" + id );
662 properties.remove( "artifact:md5:" + id );
663 properties.remove( "artifact:sha1:" + id );
664 properties.remove( "artifact:version:" + id );
665 properties.remove( "artifact:facetIds:" + id );
667 String prefix = "artifact:facet:" + id + ":";
668 for ( Object key : new ArrayList( properties.keySet() ) )
670 String property = (String) key;
671 if ( property.startsWith( prefix ) )
673 properties.remove( property );
679 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
681 catch ( IOException e )
684 log.error( e.getMessage(), e );
690 public void removeArtifact( String repoId, String namespace, String project, String version, String id )
693 File directory = new File( getDirectory( repoId ), namespace + "/" + project + "/" + version );
695 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
697 properties.remove( "artifact:updated:" + id );
698 properties.remove( "artifact:whenGathered:" + id );
699 properties.remove( "artifact:size:" + id );
700 properties.remove( "artifact:md5:" + id );
701 properties.remove( "artifact:sha1:" + id );
702 properties.remove( "artifact:version:" + id );
703 properties.remove( "artifact:facetIds:" + id );
705 String prefix = "artifact:facet:" + id + ":";
706 for ( Object key : new ArrayList( properties.keySet() ) )
708 String property = (String) key;
709 if ( property.startsWith( prefix ) )
711 properties.remove( property );
718 FileUtils.deleteDirectory( directory );
720 //writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
722 catch ( IOException e )
725 log.error( e.getMessage(), e );
730 * FIXME implements this !!!!
732 * @param repositoryId
735 * @param projectVersion
736 * @param metadataFacet will remove artifacts which have this {@link MetadataFacet} using equals
737 * @throws MetadataRepositoryException
740 public void removeArtifact( String repositoryId, String namespace, String project, String projectVersion,
741 MetadataFacet metadataFacet )
742 throws MetadataRepositoryException
744 throw new UnsupportedOperationException( "not implemented" );
748 public void removeRepository( String repoId )
750 File dir = getDirectory( repoId );
751 if ( !FileUtils.deleteQuietly( dir ) )
753 log.error( "Cannot delete repository {}", dir );
757 private void getArtifactsByChecksum( List<ArtifactMetadata> artifacts, String repositoryId, String ns,
760 for ( String namespace : getNamespaces( repositoryId, ns ) )
762 getArtifactsByChecksum( artifacts, repositoryId, ns + "." + namespace, checksum );
765 for ( String project : getProjects( repositoryId, ns ) )
767 for ( String version : getProjectVersions( repositoryId, ns, project ) )
769 for ( ArtifactMetadata artifact : getArtifacts( repositoryId, ns, project, version ) )
771 if ( checksum.equals( artifact.getMd5() ) || checksum.equals( artifact.getSha1() ) )
773 artifacts.add( artifact );
780 private File getMetadataDirectory( String repoId, String facetId )
782 return new File( getBaseDirectory( repoId ), "facets/" + facetId );
785 private String join( Collection<String> ids )
787 if ( ids != null && !ids.isEmpty() )
789 StringBuilder s = new StringBuilder();
790 for ( String id : ids )
795 return s.substring( 0, s.length() - 1 );
800 private void setProperty( Properties properties, String name, String value )
804 properties.setProperty( name, value );
809 public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion,
810 ArtifactMetadata artifact )
812 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
813 metadata.setId( projectVersion );
814 updateProjectVersion( repoId, namespace, projectId, metadata );
816 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
818 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
820 clearMetadataFacetProperties( artifact.getFacetList(), properties, "artifact:facet:" + artifact.getId() + ":" );
822 String id = artifact.getId();
823 properties.setProperty( "artifact:updated:" + id, Long.toString( artifact.getFileLastModified().getTime() ) );
824 properties.setProperty( "artifact:whenGathered:" + id, Long.toString( artifact.getWhenGathered().getTime() ) );
825 properties.setProperty( "artifact:size:" + id, Long.toString( artifact.getSize() ) );
826 if ( artifact.getMd5() != null )
828 properties.setProperty( "artifact:md5:" + id, artifact.getMd5() );
830 if ( artifact.getSha1() != null )
832 properties.setProperty( "artifact:sha1:" + id, artifact.getSha1() );
834 properties.setProperty( "artifact:version:" + id, artifact.getVersion() );
836 Set<String> facetIds = new LinkedHashSet<String>( artifact.getFacetIds() );
837 String property = "artifact:facetIds:" + id;
838 facetIds.addAll( Arrays.asList( properties.getProperty( property, "" ).split( "," ) ) );
839 properties.setProperty( property, join( facetIds ) );
841 updateArtifactFacets( artifact, properties );
845 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
847 catch ( IOException e )
850 log.error( e.getMessage(), e );
854 private Properties readOrCreateProperties( File directory, String propertiesKey )
858 return readProperties( directory, propertiesKey );
860 catch ( FileNotFoundException e )
862 // ignore and return new properties
864 catch ( IOException e )
867 log.error( e.getMessage(), e );
869 return new Properties();
872 private Properties readProperties( File directory, String propertiesKey )
875 Properties properties = new Properties();
876 try (InputStream in = Files.newInputStream( new File( directory, propertiesKey + ".properties" ).toPath() ))
879 properties.load( in );
885 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
887 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
889 Properties properties = readOrCreateProperties( directory, PROJECT_METADATA_KEY );
891 ProjectMetadata project = null;
893 String id = properties.getProperty( "id" );
896 project = new ProjectMetadata();
897 project.setNamespace( properties.getProperty( "namespace" ) );
905 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
906 String projectVersion )
908 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
910 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
911 String id = properties.getProperty( "id" );
912 ProjectVersionMetadata versionMetadata = null;
915 versionMetadata = new ProjectVersionMetadata();
916 versionMetadata.setId( id );
917 versionMetadata.setName( properties.getProperty( "name" ) );
918 versionMetadata.setDescription( properties.getProperty( "description" ) );
919 versionMetadata.setUrl( properties.getProperty( "url" ) );
920 versionMetadata.setIncomplete( Boolean.valueOf( properties.getProperty( "incomplete", "false" ) ) );
922 String scmConnection = properties.getProperty( "scm.connection" );
923 String scmDeveloperConnection = properties.getProperty( "scm.developerConnection" );
924 String scmUrl = properties.getProperty( "scm.url" );
925 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
928 scm.setConnection( scmConnection );
929 scm.setDeveloperConnection( scmDeveloperConnection );
930 scm.setUrl( scmUrl );
931 versionMetadata.setScm( scm );
934 String ciSystem = properties.getProperty( "ci.system" );
935 String ciUrl = properties.getProperty( "ci.url" );
936 if ( ciSystem != null || ciUrl != null )
938 CiManagement ci = new CiManagement();
939 ci.setSystem( ciSystem );
941 versionMetadata.setCiManagement( ci );
944 String issueSystem = properties.getProperty( "issue.system" );
945 String issueUrl = properties.getProperty( "issue.url" );
946 if ( issueSystem != null || issueUrl != null )
948 IssueManagement issueManagement = new IssueManagement();
949 issueManagement.setSystem( issueSystem );
950 issueManagement.setUrl( issueUrl );
951 versionMetadata.setIssueManagement( issueManagement );
954 String orgName = properties.getProperty( "org.name" );
955 String orgUrl = properties.getProperty( "org.url" );
956 if ( orgName != null || orgUrl != null )
958 Organization org = new Organization();
959 org.setName( orgName );
960 org.setUrl( orgUrl );
961 versionMetadata.setOrganization( org );
964 boolean done = false;
968 String licenseName = properties.getProperty( "license." + i + ".name" );
969 String licenseUrl = properties.getProperty( "license." + i + ".url" );
970 if ( licenseName != null || licenseUrl != null )
972 License license = new License();
973 license.setName( licenseName );
974 license.setUrl( licenseUrl );
975 versionMetadata.addLicense( license );
988 String mailingListName = properties.getProperty( "mailingList." + i + ".name" );
989 if ( mailingListName != null )
991 MailingList mailingList = new MailingList();
992 mailingList.setName( mailingListName );
993 mailingList.setMainArchiveUrl( properties.getProperty( "mailingList." + i + ".archive" ) );
994 String p = properties.getProperty( "mailingList." + i + ".otherArchives" );
995 if ( p != null && p.length() > 0 )
997 mailingList.setOtherArchives( Arrays.asList( p.split( "," ) ) );
1001 mailingList.setOtherArchives( Collections.<String>emptyList() );
1003 mailingList.setPostAddress( properties.getProperty( "mailingList." + i + ".post" ) );
1004 mailingList.setSubscribeAddress( properties.getProperty( "mailingList." + i + ".subscribe" ) );
1005 mailingList.setUnsubscribeAddress( properties.getProperty( "mailingList." + i + ".unsubscribe" ) );
1006 versionMetadata.addMailingList( mailingList );
1019 String dependencyArtifactId = properties.getProperty( "dependency." + i + ".artifactId" );
1020 if ( dependencyArtifactId != null )
1022 Dependency dependency = new Dependency();
1023 dependency.setArtifactId( dependencyArtifactId );
1024 dependency.setGroupId( properties.getProperty( "dependency." + i + ".groupId" ) );
1025 dependency.setClassifier( properties.getProperty( "dependency." + i + ".classifier" ) );
1026 dependency.setOptional(
1027 Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) );
1028 dependency.setScope( properties.getProperty( "dependency." + i + ".scope" ) );
1029 dependency.setSystemPath( properties.getProperty( "dependency." + i + ".systemPath" ) );
1030 dependency.setType( properties.getProperty( "dependency." + i + ".type" ) );
1031 dependency.setVersion( properties.getProperty( "dependency." + i + ".version" ) );
1032 dependency.setOptional(
1033 Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) );
1034 versionMetadata.addDependency( dependency );
1043 String facetIds = properties.getProperty( "facetIds", "" );
1044 if ( facetIds.length() > 0 )
1046 for ( String facetId : facetIds.split( "," ) )
1048 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
1049 if ( factory == null )
1051 log.error( "Attempted to load unknown project version metadata facet: {}", facetId );
1055 MetadataFacet facet = factory.createMetadataFacet();
1056 Map<String, String> map = new HashMap<>();
1057 for ( Object key : new ArrayList( properties.keySet() ) )
1059 String property = (String) key;
1060 if ( property.startsWith( facet.getFacetId() ) )
1062 map.put( property.substring( facet.getFacetId().length() + 1 ),
1063 properties.getProperty( property ) );
1066 facet.fromProperties( map );
1067 versionMetadata.addFacet( facet );
1072 updateProjectVersionFacets( versionMetadata, properties );
1074 return versionMetadata;
1078 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
1079 String projectVersion )
1081 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
1083 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
1085 Set<String> versions = new HashSet<String>();
1086 for ( Map.Entry entry : properties.entrySet() )
1088 String name = (String) entry.getKey();
1089 if ( name.startsWith( "artifact:version:" ) )
1091 versions.add( (String) entry.getValue() );
1098 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1099 String projectVersion )
1101 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
1103 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
1104 int numberOfRefs = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
1106 List<ProjectVersionReference> references = new ArrayList<>();
1107 for ( int i = 0; i < numberOfRefs; i++ )
1109 ProjectVersionReference reference = new ProjectVersionReference();
1110 reference.setProjectId( properties.getProperty( "ref:reference." + i + ".projectId" ) );
1111 reference.setNamespace( properties.getProperty( "ref:reference." + i + ".namespace" ) );
1112 reference.setProjectVersion( properties.getProperty( "ref:reference." + i + ".projectVersion" ) );
1113 reference.setReferenceType( ProjectVersionReference.ReferenceType.valueOf(
1114 properties.getProperty( "ref:reference." + i + ".referenceType" ) ) );
1115 references.add( reference );
1121 public Collection<String> getRootNamespaces( String repoId )
1123 return getNamespaces( repoId, null );
1127 public Collection<String> getNamespaces( String repoId, String baseNamespace )
1129 List<String> allNamespaces = new ArrayList<>();
1130 File directory = getDirectory( repoId );
1131 File[] files = directory.listFiles();
1132 if ( files != null )
1134 for ( File namespace : files )
1136 if ( new File( namespace, NAMESPACE_METADATA_KEY + ".properties" ).exists() )
1138 allNamespaces.add( namespace.getName() );
1143 Set<String> namespaces = new LinkedHashSet<>();
1144 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
1145 for ( String namespace : allNamespaces )
1147 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
1149 int i = namespace.indexOf( '.', fromIndex );
1152 namespaces.add( namespace.substring( fromIndex, i ) );
1156 namespaces.add( namespace.substring( fromIndex ) );
1160 return new ArrayList<>( namespaces );
1164 public Collection<String> getProjects( String repoId, String namespace )
1166 List<String> projects = new ArrayList<>();
1167 File directory = new File( getDirectory( repoId ), namespace );
1168 File[] files = directory.listFiles();
1169 if ( files != null )
1171 for ( File project : files )
1173 if ( new File( project, PROJECT_METADATA_KEY + ".properties" ).exists() )
1175 projects.add( project.getName() );
1183 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
1185 List<String> projectVersions = new ArrayList<>();
1186 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
1187 File[] files = directory.listFiles();
1188 if ( files != null )
1190 for ( File projectVersion : files )
1192 if ( new File( projectVersion, PROJECT_VERSION_METADATA_KEY + ".properties" ).exists() )
1194 projectVersions.add( projectVersion.getName() );
1198 return projectVersions;
1202 public void removeProject( String repositoryId, String namespace, String projectId )
1203 throws MetadataRepositoryException
1205 File directory = new File( getDirectory( repositoryId ), namespace + "/" + projectId );
1208 if ( directory.exists() )
1210 FileUtils.deleteDirectory( directory );
1213 catch ( IOException e )
1215 throw new MetadataRepositoryException( e.getMessage(), e );
1220 public void removeProjectVersion( String repoId, String namespace, String projectId, String projectVersion )
1221 throws MetadataRepositoryException
1223 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
1224 if ( directory.exists() )
1228 FileUtils.deleteDirectory( directory );
1230 catch ( IOException e )
1232 throw new MetadataRepositoryException( e.getMessage(), e );
1237 private void writeProperties( Properties properties, File directory, String propertiesKey )
1241 try (OutputStream os = Files.newOutputStream( new File( directory, propertiesKey + ".properties" ).toPath() ))
1243 properties.store( os, null );
1247 private static class ArtifactComparator
1248 implements Comparator<ArtifactMetadata>
1251 public int compare( ArtifactMetadata artifact1, ArtifactMetadata artifact2 )
1253 if ( artifact1.getWhenGathered() == artifact2.getWhenGathered() )
1257 if ( artifact1.getWhenGathered() == null )
1261 if ( artifact2.getWhenGathered() == null )
1265 return artifact1.getWhenGathered().compareTo( artifact2.getWhenGathered() );
1270 public List<ArtifactMetadata> getArtifacts( String repoId )
1272 List<ArtifactMetadata> artifacts = new ArrayList<>();
1273 for ( String ns : getRootNamespaces( repoId ) )
1275 getArtifacts( artifacts, repoId, ns );
1280 private void getArtifacts( List<ArtifactMetadata> artifacts, String repoId, String ns )
1282 for ( String namespace : getNamespaces( repoId, ns ) )
1284 getArtifacts( artifacts, repoId, ns + "." + namespace );
1287 for ( String project : getProjects( repoId, ns ) )
1289 for ( String version : getProjectVersions( repoId, ns, project ) )
1291 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
1293 artifacts.add( artifact );