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 return new File( basedir, ".archiva" );
95 private File getDirectory( String repoId )
97 return new File( getBaseDirectory( repoId ), "content" );
100 public void updateProject( String repoId, ProjectMetadata project )
102 updateProject( repoId, project.getNamespace(), project.getId() );
105 private void updateProject( String repoId, String namespace, String id )
107 // TODO: this is a more braindead implementation than we would normally expect, for prototyping purposes
108 updateNamespace( repoId, namespace );
112 File namespaceDirectory = new File( getDirectory( repoId ), namespace );
113 Properties properties = new Properties();
114 properties.setProperty( "namespace", namespace );
115 properties.setProperty( "id", id );
116 writeProperties( properties, new File( namespaceDirectory, id ), PROJECT_METADATA_KEY );
118 catch ( IOException e )
121 log.error( e.getMessage(), e );
125 public void updateProjectVersion( String repoId, String namespace, String projectId,
126 ProjectVersionMetadata versionMetadata )
128 updateProject( repoId, namespace, projectId );
131 new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + versionMetadata.getId() );
133 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
134 // remove properties that are not references or artifacts
135 for ( Object key : new ArrayList<Object>( properties.keySet() ) )
137 String name = (String) key;
138 if ( !name.contains( ":" ) && !name.equals( "facetIds" ) )
140 properties.remove( name );
143 // clear the facet contents so old properties are no longer written
144 clearMetadataFacetProperties( versionMetadata.getFacetList(), properties, "" );
146 properties.setProperty( "id", versionMetadata.getId() );
147 setProperty( properties, "name", versionMetadata.getName() );
148 setProperty( properties, "description", versionMetadata.getDescription() );
149 setProperty( properties, "url", versionMetadata.getUrl() );
150 setProperty( properties, "incomplete", String.valueOf( versionMetadata.isIncomplete() ) );
151 if ( versionMetadata.getScm() != null )
153 setProperty( properties, "scm.connection", versionMetadata.getScm().getConnection() );
154 setProperty( properties, "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() );
155 setProperty( properties, "scm.url", versionMetadata.getScm().getUrl() );
157 if ( versionMetadata.getCiManagement() != null )
159 setProperty( properties, "ci.system", versionMetadata.getCiManagement().getSystem() );
160 setProperty( properties, "ci.url", versionMetadata.getCiManagement().getUrl() );
162 if ( versionMetadata.getIssueManagement() != null )
164 setProperty( properties, "issue.system", versionMetadata.getIssueManagement().getSystem() );
165 setProperty( properties, "issue.url", versionMetadata.getIssueManagement().getUrl() );
167 if ( versionMetadata.getOrganization() != null )
169 setProperty( properties, "org.name", versionMetadata.getOrganization().getName() );
170 setProperty( properties, "org.url", versionMetadata.getOrganization().getUrl() );
173 for ( License license : versionMetadata.getLicenses() )
175 setProperty( properties, "license." + i + ".name", license.getName() );
176 setProperty( properties, "license." + i + ".url", license.getUrl() );
180 for ( MailingList mailingList : versionMetadata.getMailingLists() )
182 setProperty( properties, "mailingList." + i + ".archive", mailingList.getMainArchiveUrl() );
183 setProperty( properties, "mailingList." + i + ".name", mailingList.getName() );
184 setProperty( properties, "mailingList." + i + ".post", mailingList.getPostAddress() );
185 setProperty( properties, "mailingList." + i + ".unsubscribe", mailingList.getUnsubscribeAddress() );
186 setProperty( properties, "mailingList." + i + ".subscribe", mailingList.getSubscribeAddress() );
187 setProperty( properties, "mailingList." + i + ".otherArchives", join( mailingList.getOtherArchives() ) );
191 ProjectVersionReference reference = new ProjectVersionReference();
192 reference.setNamespace( namespace );
193 reference.setProjectId( projectId );
194 reference.setProjectVersion( versionMetadata.getId() );
195 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
196 for ( Dependency dependency : versionMetadata.getDependencies() )
198 setProperty( properties, "dependency." + i + ".classifier", dependency.getClassifier() );
199 setProperty( properties, "dependency." + i + ".scope", dependency.getScope() );
200 setProperty( properties, "dependency." + i + ".systemPath", dependency.getSystemPath() );
201 setProperty( properties, "dependency." + i + ".artifactId", dependency.getArtifactId() );
202 setProperty( properties, "dependency." + i + ".groupId", dependency.getGroupId() );
203 setProperty( properties, "dependency." + i + ".version", dependency.getVersion() );
204 setProperty( properties, "dependency." + i + ".type", dependency.getType() );
205 setProperty( properties, "dependency." + i + ".optional", String.valueOf( dependency.isOptional() ) );
207 updateProjectReference( repoId, dependency.getGroupId(), dependency.getArtifactId(),
208 dependency.getVersion(), reference );
212 Set<String> facetIds = new LinkedHashSet<String>( versionMetadata.getFacetIds() );
213 facetIds.addAll( Arrays.asList( properties.getProperty( "facetIds", "" ).split( "," ) ) );
214 properties.setProperty( "facetIds", join( facetIds ) );
216 updateProjectVersionFacets( versionMetadata, properties );
220 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
222 catch ( IOException e )
225 log.error( e.getMessage(), e );
229 private void updateProjectVersionFacets( ProjectVersionMetadata versionMetadata, Properties properties )
231 for ( MetadataFacet facet : versionMetadata.getFacetList() )
233 for ( Map.Entry<String, String> entry : facet.toProperties().entrySet() )
235 properties.setProperty( facet.getFacetId() + ":" + entry.getKey(), entry.getValue() );
240 private static void clearMetadataFacetProperties( Collection<MetadataFacet> facetList, Properties properties,
243 List<Object> propsToRemove = new ArrayList<Object>();
244 for ( MetadataFacet facet : facetList )
246 for ( Object key : properties.keySet() )
248 String keyString = (String) key;
249 if ( keyString.startsWith( prefix + facet.getFacetId() + ":" ) )
251 propsToRemove.add( key );
256 for ( Object key : propsToRemove )
258 properties.remove( key );
262 private void updateProjectReference( String repoId, String namespace, String projectId, String projectVersion,
263 ProjectVersionReference reference )
265 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
267 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
268 int i = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
269 setProperty( properties, "ref:lastReferenceNum", Integer.toString( i ) );
270 setProperty( properties, "ref:reference." + i + ".namespace", reference.getNamespace() );
271 setProperty( properties, "ref:reference." + i + ".projectId", reference.getProjectId() );
272 setProperty( properties, "ref:reference." + i + ".projectVersion", reference.getProjectVersion() );
273 setProperty( properties, "ref:reference." + i + ".referenceType", reference.getReferenceType().toString() );
277 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
279 catch ( IOException e )
282 log.error( e.getMessage(), e );
286 public void updateNamespace( String repoId, String namespace )
290 File namespaceDirectory = new File( getDirectory( repoId ), namespace );
291 Properties properties = new Properties();
292 properties.setProperty( "namespace", namespace );
293 writeProperties( properties, namespaceDirectory, NAMESPACE_METADATA_KEY );
296 catch ( IOException e )
299 log.error( e.getMessage(), e );
303 public List<String> getMetadataFacets( String repoId, String facetId )
305 File directory = getMetadataDirectory( repoId, facetId );
306 List<String> facets = new ArrayList<String>();
307 recurse( facets, "", directory );
311 public boolean hasMetadataFacet( String repositoryId, String facetId )
312 throws MetadataRepositoryException
314 // TODO could be improved a bit
315 return !getMetadataFacets( repositoryId, facetId ).isEmpty();
318 private void recurse( List<String> facets, String prefix, File directory )
320 File[] list = directory.listFiles();
323 for ( File dir : list )
325 if ( dir.isDirectory() )
327 recurse( facets, prefix + "/" + dir.getName(), dir );
329 else if ( dir.getName().equals( METADATA_KEY + ".properties" ) )
331 facets.add( prefix.substring( 1 ) );
337 public MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name )
339 Properties properties;
343 readProperties( new File( getMetadataDirectory( repositoryId, facetId ), name ), METADATA_KEY );
345 catch ( FileNotFoundException e )
349 catch ( IOException e )
352 log.error( e.getMessage(), e );
355 MetadataFacet metadataFacet = null;
356 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
357 if ( metadataFacetFactory != null )
359 metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
360 Map<String, String> map = new HashMap<String, String>();
361 for ( Object key : new ArrayList( properties.keySet() ) )
363 String property = (String) key;
364 map.put( property, properties.getProperty( property ) );
366 metadataFacet.fromProperties( map );
368 return metadataFacet;
371 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
373 Properties properties = new Properties();
374 properties.putAll( metadataFacet.toProperties() );
379 new File( getMetadataDirectory( repositoryId, metadataFacet.getFacetId() ), metadataFacet.getName() );
380 writeProperties( properties, directory, METADATA_KEY );
382 catch ( IOException e )
385 log.error( e.getMessage(), e );
389 public void removeMetadataFacets( String repositoryId, String facetId )
391 File dir = getMetadataDirectory( repositoryId, facetId );
392 if ( !FileUtils.deleteQuietly( dir ) )
394 log.error( "Cannot delete the metadata repository {}", dir );
398 public void removeMetadataFacet( String repoId, String facetId, String name )
400 File dir = new File( getMetadataDirectory( repoId, facetId ), name );
401 if ( !FileUtils.deleteQuietly( dir ) )
403 log.error( "Cannot delete the metadata repository {}", dir );
407 public List<ArtifactMetadata> getArtifactsByDateRange( String repoId, Date startTime, Date endTime )
409 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
410 // of this information (eg. in Lucene, as before)
412 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
413 for ( String ns : getRootNamespaces( repoId ) )
415 getArtifactsByDateRange( artifacts, repoId, ns, startTime, endTime );
417 Collections.sort( artifacts, new ArtifactComparator() );
421 private void getArtifactsByDateRange( List<ArtifactMetadata> artifacts, String repoId, String ns, Date startTime,
424 for ( String namespace : getNamespaces( repoId, ns ) )
426 getArtifactsByDateRange( artifacts, repoId, ns + "." + namespace, startTime, endTime );
429 for ( String project : getProjects( repoId, ns ) )
431 for ( String version : getProjectVersions( repoId, ns, project ) )
433 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
435 if ( startTime == null || startTime.before( artifact.getWhenGathered() ) )
437 if ( endTime == null || endTime.after( artifact.getWhenGathered() ) )
439 artifacts.add( artifact );
447 public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
448 String projectVersion )
450 Map<String, ArtifactMetadata> artifacts = new HashMap<String, ArtifactMetadata>();
452 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
454 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
456 for ( Map.Entry entry : properties.entrySet() )
458 String name = (String) entry.getKey();
459 StringTokenizer tok = new StringTokenizer( name, ":" );
460 if ( tok.hasMoreTokens() && "artifact".equals( tok.nextToken() ) )
462 String field = tok.nextToken();
463 String id = tok.nextToken();
465 ArtifactMetadata artifact = artifacts.get( id );
466 if ( artifact == null )
468 artifact = new ArtifactMetadata();
469 artifact.setRepositoryId( repoId );
470 artifact.setNamespace( namespace );
471 artifact.setProject( projectId );
472 artifact.setProjectVersion( projectVersion );
473 artifact.setVersion( projectVersion );
474 artifact.setId( id );
475 artifacts.put( id, artifact );
478 String value = (String) entry.getValue();
479 if ( "updated".equals( field ) )
481 artifact.setFileLastModified( Long.parseLong( value ) );
483 else if ( "size".equals( field ) )
485 artifact.setSize( Long.valueOf( value ) );
487 else if ( "whenGathered".equals( field ) )
489 artifact.setWhenGathered( new Date( Long.parseLong( value ) ) );
491 else if ( "version".equals( field ) )
493 artifact.setVersion( value );
495 else if ( "md5".equals( field ) )
497 artifact.setMd5( value );
499 else if ( "sha1".equals( field ) )
501 artifact.setSha1( value );
503 else if ( "facetIds".equals( field ) )
505 if ( value.length() > 0 )
507 String propertyPrefix = "artifact:facet:" + id + ":";
508 for ( String facetId : value.split( "," ) )
510 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
511 if ( factory == null )
513 log.error( "Attempted to load unknown artifact metadata facet: " + facetId );
517 MetadataFacet facet = factory.createMetadataFacet();
518 String prefix = propertyPrefix + facet.getFacetId();
519 Map<String, String> map = new HashMap<String, String>();
520 for ( Object key : new ArrayList( properties.keySet() ) )
522 String property = (String) key;
523 if ( property.startsWith( prefix ) )
525 map.put( property.substring( prefix.length() + 1 ),
526 properties.getProperty( property ) );
529 facet.fromProperties( map );
530 artifact.addFacet( facet );
535 updateArtifactFacets( artifact, properties );
539 return artifacts.values();
544 // it's all instantly persisted
549 // nothing additional to close
554 log.warn( "Attempted to revert a session, but the file-based repository storage doesn't support it" );
557 public boolean canObtainAccess( Class<?> aClass )
562 public Object obtainAccess( Class<?> aClass )
564 throw new IllegalArgumentException(
565 "Access using " + aClass + " is not supported on the file metadata storage" );
568 private void updateArtifactFacets( ArtifactMetadata artifact, Properties properties )
570 String propertyPrefix = "artifact:facet:" + artifact.getId() + ":";
571 for ( MetadataFacet facet : artifact.getFacetList() )
573 for ( Map.Entry<String, String> e : facet.toProperties().entrySet() )
575 String key = propertyPrefix + facet.getFacetId() + ":" + e.getKey();
576 properties.setProperty( key, e.getValue() );
581 public Collection<String> getRepositories()
583 List<String> repositories = new ArrayList<String>();
584 for ( ManagedRepositoryConfiguration managedRepositoryConfiguration : configuration.getConfiguration().getManagedRepositories() )
586 repositories.add( managedRepositoryConfiguration.getId() );
591 public List<ArtifactMetadata> getArtifactsByChecksum( String repositoryId, String checksum )
593 // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index
594 // of this information (eg. in Lucene, as before)
595 // alternatively, we could build a referential tree in the content repository, however it would need some levels
596 // of depth to avoid being too broad to be useful (eg. /repository/checksums/a/ab/abcdef1234567)
598 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
599 for ( String ns : getRootNamespaces( repositoryId ) )
601 getArtifactsByChecksum( artifacts, repositoryId, ns, checksum );
606 public void removeNamespace( String repositoryId, String project )
607 throws MetadataRepositoryException
611 File namespaceDirectory = new File( getDirectory( repositoryId ), project );
612 FileUtils.deleteDirectory( namespaceDirectory );
613 //Properties properties = new Properties();
614 //properties.setProperty( "namespace", namespace );
615 //writeProperties( properties, namespaceDirectory, NAMESPACE_METADATA_KEY );
618 catch ( IOException e )
620 throw new MetadataRepositoryException( e.getMessage(), e );
624 public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
625 throws MetadataRepositoryException
628 File directory = new File( getDirectory( artifactMetadata.getRepositoryId() ),
629 artifactMetadata.getNamespace() + "/" + artifactMetadata.getProject() + "/"
632 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
634 String id = artifactMetadata.getId();
636 properties.remove( "artifact:updated:" + id );
637 properties.remove( "artifact:whenGathered:" + id );
638 properties.remove( "artifact:size:" + id );
639 properties.remove( "artifact:md5:" + id );
640 properties.remove( "artifact:sha1:" + id );
641 properties.remove( "artifact:version:" + id );
642 properties.remove( "artifact:facetIds:" + id );
644 String prefix = "artifact:facet:" + id + ":";
645 for ( Object key : new ArrayList<Object>( properties.keySet() ) )
647 String property = (String) key;
648 if ( property.startsWith( prefix ) )
650 properties.remove( property );
656 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
658 catch ( IOException e )
661 log.error( e.getMessage(), e );
666 public void removeArtifact( String repoId, String namespace, String project, String version, String id )
669 File directory = new File( getDirectory( repoId ), namespace + "/" + project + "/" + version );
671 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
673 properties.remove( "artifact:updated:" + id );
674 properties.remove( "artifact:whenGathered:" + id );
675 properties.remove( "artifact:size:" + id );
676 properties.remove( "artifact:md5:" + id );
677 properties.remove( "artifact:sha1:" + id );
678 properties.remove( "artifact:version:" + id );
679 properties.remove( "artifact:facetIds:" + id );
681 String prefix = "artifact:facet:" + id + ":";
682 for ( Object key : new ArrayList<Object>( properties.keySet() ) )
684 String property = (String) key;
685 if ( property.startsWith( prefix ) )
687 properties.remove( property );
694 FileUtils.deleteDirectory( directory );
696 //writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
698 catch ( IOException e )
701 log.error( e.getMessage(), e );
706 * FIXME implements this !!!!
708 * @param repositoryId
711 * @param projectVersion
712 * @param metadataFacet will remove artifacts which have this {@link MetadataFacet} using equals
713 * @throws MetadataRepositoryException
715 public void removeArtifact( String repositoryId, String namespace, String project, String projectVersion,
716 MetadataFacet metadataFacet )
717 throws MetadataRepositoryException
719 throw new UnsupportedOperationException( "not implemented" );
722 public void removeRepository( String repoId )
724 File dir = getDirectory( repoId );
725 if ( !FileUtils.deleteQuietly( dir ) )
727 log.error( "Cannot delete repository {}", dir );
731 private void getArtifactsByChecksum( List<ArtifactMetadata> artifacts, String repositoryId, String ns,
734 for ( String namespace : getNamespaces( repositoryId, ns ) )
736 getArtifactsByChecksum( artifacts, repositoryId, ns + "." + namespace, checksum );
739 for ( String project : getProjects( repositoryId, ns ) )
741 for ( String version : getProjectVersions( repositoryId, ns, project ) )
743 for ( ArtifactMetadata artifact : getArtifacts( repositoryId, ns, project, version ) )
745 if ( checksum.equals( artifact.getMd5() ) || checksum.equals( artifact.getSha1() ) )
747 artifacts.add( artifact );
754 private File getMetadataDirectory( String repoId, String facetId )
756 return new File( getBaseDirectory( repoId ), "facets/" + facetId );
759 private String join( Collection<String> ids )
761 if ( ids != null && !ids.isEmpty() )
763 StringBuilder s = new StringBuilder();
764 for ( String id : ids )
769 return s.substring( 0, s.length() - 1 );
774 private void setProperty( Properties properties, String name, String value )
778 properties.setProperty( name, value );
782 public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion,
783 ArtifactMetadata artifact )
785 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
786 metadata.setId( projectVersion );
787 updateProjectVersion( repoId, namespace, projectId, metadata );
789 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
791 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
793 clearMetadataFacetProperties( artifact.getFacetList(), properties, "artifact:facet:" + artifact.getId() + ":" );
795 String id = artifact.getId();
796 properties.setProperty( "artifact:updated:" + id, Long.toString( artifact.getFileLastModified().getTime() ) );
797 properties.setProperty( "artifact:whenGathered:" + id, Long.toString( artifact.getWhenGathered().getTime() ) );
798 properties.setProperty( "artifact:size:" + id, Long.toString( artifact.getSize() ) );
799 if ( artifact.getMd5() != null )
801 properties.setProperty( "artifact:md5:" + id, artifact.getMd5() );
803 if ( artifact.getSha1() != null )
805 properties.setProperty( "artifact:sha1:" + id, artifact.getSha1() );
807 properties.setProperty( "artifact:version:" + id, artifact.getVersion() );
809 Set<String> facetIds = new LinkedHashSet<String>( artifact.getFacetIds() );
810 String property = "artifact:facetIds:" + id;
811 facetIds.addAll( Arrays.asList( properties.getProperty( property, "" ).split( "," ) ) );
812 properties.setProperty( property, join( facetIds ) );
814 updateArtifactFacets( artifact, properties );
818 writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY );
820 catch ( IOException e )
823 log.error( e.getMessage(), e );
827 private Properties readOrCreateProperties( File directory, String propertiesKey )
831 return readProperties( directory, propertiesKey );
833 catch ( FileNotFoundException e )
835 // ignore and return new properties
837 catch ( IOException e )
840 log.error( e.getMessage(), e );
842 return new Properties();
845 private Properties readProperties( File directory, String propertiesKey )
848 Properties properties = new Properties();
849 FileInputStream in = null;
852 in = new FileInputStream( new File( directory, propertiesKey + ".properties" ) );
853 properties.load( in );
857 IOUtils.closeQuietly( in );
862 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
864 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
866 Properties properties = readOrCreateProperties( directory, PROJECT_METADATA_KEY );
868 ProjectMetadata project = null;
870 String id = properties.getProperty( "id" );
873 project = new ProjectMetadata();
874 project.setNamespace( properties.getProperty( "namespace" ) );
881 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
882 String projectVersion )
884 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
886 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
887 String id = properties.getProperty( "id" );
888 ProjectVersionMetadata versionMetadata = null;
891 versionMetadata = new ProjectVersionMetadata();
892 versionMetadata.setId( id );
893 versionMetadata.setName( properties.getProperty( "name" ) );
894 versionMetadata.setDescription( properties.getProperty( "description" ) );
895 versionMetadata.setUrl( properties.getProperty( "url" ) );
896 versionMetadata.setIncomplete( Boolean.valueOf( properties.getProperty( "incomplete", "false" ) ) );
898 String scmConnection = properties.getProperty( "scm.connection" );
899 String scmDeveloperConnection = properties.getProperty( "scm.developerConnection" );
900 String scmUrl = properties.getProperty( "scm.url" );
901 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
904 scm.setConnection( scmConnection );
905 scm.setDeveloperConnection( scmDeveloperConnection );
906 scm.setUrl( scmUrl );
907 versionMetadata.setScm( scm );
910 String ciSystem = properties.getProperty( "ci.system" );
911 String ciUrl = properties.getProperty( "ci.url" );
912 if ( ciSystem != null || ciUrl != null )
914 CiManagement ci = new CiManagement();
915 ci.setSystem( ciSystem );
917 versionMetadata.setCiManagement( ci );
920 String issueSystem = properties.getProperty( "issue.system" );
921 String issueUrl = properties.getProperty( "issue.url" );
922 if ( issueSystem != null || issueUrl != null )
924 IssueManagement issueManagement = new IssueManagement();
925 issueManagement.setSystem( issueSystem );
926 issueManagement.setUrl( issueUrl );
927 versionMetadata.setIssueManagement( issueManagement );
930 String orgName = properties.getProperty( "org.name" );
931 String orgUrl = properties.getProperty( "org.url" );
932 if ( orgName != null || orgUrl != null )
934 Organization org = new Organization();
935 org.setName( orgName );
936 org.setUrl( orgUrl );
937 versionMetadata.setOrganization( org );
940 boolean done = false;
944 String licenseName = properties.getProperty( "license." + i + ".name" );
945 String licenseUrl = properties.getProperty( "license." + i + ".url" );
946 if ( licenseName != null || licenseUrl != null )
948 License license = new License();
949 license.setName( licenseName );
950 license.setUrl( licenseUrl );
951 versionMetadata.addLicense( license );
964 String mailingListName = properties.getProperty( "mailingList." + i + ".name" );
965 if ( mailingListName != null )
967 MailingList mailingList = new MailingList();
968 mailingList.setName( mailingListName );
969 mailingList.setMainArchiveUrl( properties.getProperty( "mailingList." + i + ".archive" ) );
970 String p = properties.getProperty( "mailingList." + i + ".otherArchives" );
971 if ( p != null && p.length() > 0 )
973 mailingList.setOtherArchives( Arrays.asList( p.split( "," ) ) );
977 mailingList.setOtherArchives( Collections.<String>emptyList() );
979 mailingList.setPostAddress( properties.getProperty( "mailingList." + i + ".post" ) );
980 mailingList.setSubscribeAddress( properties.getProperty( "mailingList." + i + ".subscribe" ) );
981 mailingList.setUnsubscribeAddress( properties.getProperty( "mailingList." + i + ".unsubscribe" ) );
982 versionMetadata.addMailingList( mailingList );
995 String dependencyArtifactId = properties.getProperty( "dependency." + i + ".artifactId" );
996 if ( dependencyArtifactId != null )
998 Dependency dependency = new Dependency();
999 dependency.setArtifactId( dependencyArtifactId );
1000 dependency.setGroupId( properties.getProperty( "dependency." + i + ".groupId" ) );
1001 dependency.setClassifier( properties.getProperty( "dependency." + i + ".classifier" ) );
1002 dependency.setOptional(
1003 Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) );
1004 dependency.setScope( properties.getProperty( "dependency." + i + ".scope" ) );
1005 dependency.setSystemPath( properties.getProperty( "dependency." + i + ".systemPath" ) );
1006 dependency.setType( properties.getProperty( "dependency." + i + ".type" ) );
1007 dependency.setVersion( properties.getProperty( "dependency." + i + ".version" ) );
1008 dependency.setOptional(
1009 Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) );
1010 versionMetadata.addDependency( dependency );
1019 String facetIds = properties.getProperty( "facetIds", "" );
1020 if ( facetIds.length() > 0 )
1022 for ( String facetId : facetIds.split( "," ) )
1024 MetadataFacetFactory factory = metadataFacetFactories.get( facetId );
1025 if ( factory == null )
1027 log.error( "Attempted to load unknown project version metadata facet: {}", facetId );
1031 MetadataFacet facet = factory.createMetadataFacet();
1032 Map<String, String> map = new HashMap<String, String>();
1033 for ( Object key : new ArrayList( properties.keySet() ) )
1035 String property = (String) key;
1036 if ( property.startsWith( facet.getFacetId() ) )
1038 map.put( property.substring( facet.getFacetId().length() + 1 ),
1039 properties.getProperty( property ) );
1042 facet.fromProperties( map );
1043 versionMetadata.addFacet( facet );
1048 updateProjectVersionFacets( versionMetadata, properties );
1050 return versionMetadata;
1053 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
1054 String projectVersion )
1056 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
1058 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
1060 Set<String> versions = new HashSet<String>();
1061 for ( Map.Entry entry : properties.entrySet() )
1063 String name = (String) entry.getKey();
1064 if ( name.startsWith( "artifact:version:" ) )
1066 versions.add( (String) entry.getValue() );
1072 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1073 String projectVersion )
1075 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
1077 Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY );
1078 int numberOfRefs = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1;
1080 List<ProjectVersionReference> references = new ArrayList<ProjectVersionReference>();
1081 for ( int i = 0; i < numberOfRefs; i++ )
1083 ProjectVersionReference reference = new ProjectVersionReference();
1084 reference.setProjectId( properties.getProperty( "ref:reference." + i + ".projectId" ) );
1085 reference.setNamespace( properties.getProperty( "ref:reference." + i + ".namespace" ) );
1086 reference.setProjectVersion( properties.getProperty( "ref:reference." + i + ".projectVersion" ) );
1087 reference.setReferenceType( ProjectVersionReference.ReferenceType.valueOf(
1088 properties.getProperty( "ref:reference." + i + ".referenceType" ) ) );
1089 references.add( reference );
1094 public Collection<String> getRootNamespaces( String repoId )
1096 return getNamespaces( repoId, null );
1099 public Collection<String> getNamespaces( String repoId, String baseNamespace )
1101 List<String> allNamespaces = new ArrayList<String>();
1102 File directory = getDirectory( repoId );
1103 File[] files = directory.listFiles();
1104 if ( files != null )
1106 for ( File namespace : files )
1108 if ( new File( namespace, NAMESPACE_METADATA_KEY + ".properties" ).exists() )
1110 allNamespaces.add( namespace.getName() );
1115 Set<String> namespaces = new LinkedHashSet<String>();
1116 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
1117 for ( String namespace : allNamespaces )
1119 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
1121 int i = namespace.indexOf( '.', fromIndex );
1124 namespaces.add( namespace.substring( fromIndex, i ) );
1128 namespaces.add( namespace.substring( fromIndex ) );
1132 return new ArrayList<String>( namespaces );
1135 public Collection<String> getProjects( String repoId, String namespace )
1137 List<String> projects = new ArrayList<String>();
1138 File directory = new File( getDirectory( repoId ), namespace );
1139 File[] files = directory.listFiles();
1140 if ( files != null )
1142 for ( File project : files )
1144 if ( new File( project, PROJECT_METADATA_KEY + ".properties" ).exists() )
1146 projects.add( project.getName() );
1153 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
1155 List<String> projectVersions = new ArrayList<String>();
1156 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId );
1157 File[] files = directory.listFiles();
1158 if ( files != null )
1160 for ( File projectVersion : files )
1162 if ( new File( projectVersion, PROJECT_VERSION_METADATA_KEY + ".properties" ).exists() )
1164 projectVersions.add( projectVersion.getName() );
1168 return projectVersions;
1171 public void removeProject( String repositoryId, String namespace, String projectId )
1172 throws MetadataRepositoryException
1174 File directory = new File( getDirectory( repositoryId ), namespace + "/" + projectId );
1177 if ( directory.exists() )
1179 FileUtils.deleteDirectory( directory );
1182 catch ( IOException e )
1184 throw new MetadataRepositoryException( e.getMessage(), e );
1188 public void removeProjectVersion( String repoId, String namespace, String projectId, String projectVersion )
1189 throws MetadataRepositoryException
1191 File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion );
1192 if ( directory.exists() )
1196 FileUtils.deleteDirectory( directory );
1198 catch ( IOException e )
1200 throw new MetadataRepositoryException( e.getMessage(), e );
1205 private void writeProperties( Properties properties, File directory, String propertiesKey )
1209 FileOutputStream os = new FileOutputStream( new File( directory, propertiesKey + ".properties" ) );
1212 properties.store( os, null );
1216 IOUtils.closeQuietly( os );
1220 private static class ArtifactComparator
1221 implements Comparator<ArtifactMetadata>
1223 public int compare( ArtifactMetadata artifact1, ArtifactMetadata artifact2 )
1225 if ( artifact1.getWhenGathered() == artifact2.getWhenGathered() )
1229 if ( artifact1.getWhenGathered() == null )
1233 if ( artifact2.getWhenGathered() == null )
1237 return artifact1.getWhenGathered().compareTo( artifact2.getWhenGathered() );
1241 public List<ArtifactMetadata> getArtifacts( String repoId )
1243 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
1244 for ( String ns : getRootNamespaces( repoId ) )
1246 getArtifacts( artifacts, repoId, ns );
1251 private void getArtifacts( List<ArtifactMetadata> artifacts, String repoId, String ns )
1253 for ( String namespace : getNamespaces( repoId, ns ) )
1255 getArtifacts( artifacts, repoId, ns + "." + namespace );
1258 for ( String project : getProjects( repoId, ns ) )
1260 for ( String version : getProjectVersions( repoId, ns, project ) )
1262 for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) )
1264 artifacts.add( artifact );