From b195fa3946c13513760d2028996e329c81a4bc87 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 7 Oct 2015 12:05:01 +1100 Subject: [PATCH] more fix --- .../archiva-consumer-archetype/pom.xml | 6 + .../file/FileMetadataRepository.java | 1147 +++++++++-------- 2 files changed, 647 insertions(+), 506 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/pom.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/pom.xml index 1f9a9f714..2e86b3287 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/pom.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/pom.xml @@ -86,6 +86,12 @@ test + + org.apache.archiva + metadata-store-file + test + + diff --git a/archiva-modules/plugins/metadata-store-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java b/archiva-modules/plugins/metadata-store-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java index cca21bcd4..2da1a5b6f 100644 --- a/archiva-modules/plugins/metadata-store-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java +++ b/archiva-modules/plugins/metadata-store-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java @@ -36,6 +36,7 @@ import org.apache.archiva.metadata.model.ProjectVersionReference; import org.apache.archiva.metadata.model.Scm; import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.MetadataRepositoryException; +import org.apache.archiva.metadata.repository.MetadataResolutionException; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,8 +63,6 @@ import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; -//import org.apache.commons.io.IOUtils; - public class FileMetadataRepository implements MetadataRepository { @@ -89,13 +88,21 @@ public class FileMetadataRepository } private File getBaseDirectory( String repoId ) + throws IOException { // TODO: should be configurable, like the index - String basedir = configuration.getConfiguration().getManagedRepositoriesAsMap().get( repoId ).getLocation(); + ManagedRepositoryConfiguration managedRepositoryConfiguration = + configuration.getConfiguration().getManagedRepositoriesAsMap().get( repoId ); + if ( managedRepositoryConfiguration == null ) + { + return Files.createTempDirectory( repoId ).toFile(); + } + String basedir = managedRepositoryConfiguration.getLocation(); return new File( basedir, ".archiva" ); } private File getDirectory( String repoId ) + throws IOException { return new File( getBaseDirectory( repoId ), "content" ); } @@ -130,98 +137,100 @@ public class FileMetadataRepository public void updateProjectVersion( String repoId, String namespace, String projectId, ProjectVersionMetadata versionMetadata ) { - updateProject( repoId, namespace, projectId ); - - File directory = - new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + versionMetadata.getId() ); - Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - // remove properties that are not references or artifacts - for ( Object key : new ArrayList( properties.keySet() ) ) + try { - String name = (String) key; - if ( !name.contains( ":" ) && !name.equals( "facetIds" ) ) - { - properties.remove( name ); - } + updateProject( repoId, namespace, projectId ); - // clear the facet contents so old properties are no longer written - clearMetadataFacetProperties( versionMetadata.getFacetList(), properties, "" ); - } - properties.setProperty( "id", versionMetadata.getId() ); - setProperty( properties, "name", versionMetadata.getName() ); - setProperty( properties, "description", versionMetadata.getDescription() ); - setProperty( properties, "url", versionMetadata.getUrl() ); - setProperty( properties, "incomplete", String.valueOf( versionMetadata.isIncomplete() ) ); - if ( versionMetadata.getScm() != null ) - { - setProperty( properties, "scm.connection", versionMetadata.getScm().getConnection() ); - setProperty( properties, "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() ); - setProperty( properties, "scm.url", versionMetadata.getScm().getUrl() ); - } - if ( versionMetadata.getCiManagement() != null ) - { - setProperty( properties, "ci.system", versionMetadata.getCiManagement().getSystem() ); - setProperty( properties, "ci.url", versionMetadata.getCiManagement().getUrl() ); - } - if ( versionMetadata.getIssueManagement() != null ) - { - setProperty( properties, "issue.system", versionMetadata.getIssueManagement().getSystem() ); - setProperty( properties, "issue.url", versionMetadata.getIssueManagement().getUrl() ); - } - if ( versionMetadata.getOrganization() != null ) - { - setProperty( properties, "org.name", versionMetadata.getOrganization().getName() ); - setProperty( properties, "org.url", versionMetadata.getOrganization().getUrl() ); - } - int i = 0; - for ( License license : versionMetadata.getLicenses() ) - { - setProperty( properties, "license." + i + ".name", license.getName() ); - setProperty( properties, "license." + i + ".url", license.getUrl() ); - i++; - } - i = 0; - for ( MailingList mailingList : versionMetadata.getMailingLists() ) - { - setProperty( properties, "mailingList." + i + ".archive", mailingList.getMainArchiveUrl() ); - setProperty( properties, "mailingList." + i + ".name", mailingList.getName() ); - setProperty( properties, "mailingList." + i + ".post", mailingList.getPostAddress() ); - setProperty( properties, "mailingList." + i + ".unsubscribe", mailingList.getUnsubscribeAddress() ); - setProperty( properties, "mailingList." + i + ".subscribe", mailingList.getSubscribeAddress() ); - setProperty( properties, "mailingList." + i + ".otherArchives", join( mailingList.getOtherArchives() ) ); - i++; - } - i = 0; - ProjectVersionReference reference = new ProjectVersionReference(); - reference.setNamespace( namespace ); - reference.setProjectId( projectId ); - reference.setProjectVersion( versionMetadata.getId() ); - reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY ); - for ( Dependency dependency : versionMetadata.getDependencies() ) - { - setProperty( properties, "dependency." + i + ".classifier", dependency.getClassifier() ); - setProperty( properties, "dependency." + i + ".scope", dependency.getScope() ); - setProperty( properties, "dependency." + i + ".systemPath", dependency.getSystemPath() ); - setProperty( properties, "dependency." + i + ".artifactId", dependency.getArtifactId() ); - setProperty( properties, "dependency." + i + ".groupId", dependency.getGroupId() ); - setProperty( properties, "dependency." + i + ".version", dependency.getVersion() ); - setProperty( properties, "dependency." + i + ".type", dependency.getType() ); - setProperty( properties, "dependency." + i + ".optional", String.valueOf( dependency.isOptional() ) ); + File directory = + new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + versionMetadata.getId() ); - updateProjectReference( repoId, dependency.getGroupId(), dependency.getArtifactId(), - dependency.getVersion(), reference ); + Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); + // remove properties that are not references or artifacts + for ( Object key : new ArrayList( properties.keySet() ) ) + { + String name = (String) key; + if ( !name.contains( ":" ) && !name.equals( "facetIds" ) ) + { + properties.remove( name ); + } - i++; - } - Set facetIds = new LinkedHashSet( versionMetadata.getFacetIds() ); - facetIds.addAll( Arrays.asList( properties.getProperty( "facetIds", "" ).split( "," ) ) ); - properties.setProperty( "facetIds", join( facetIds ) ); + // clear the facet contents so old properties are no longer written + clearMetadataFacetProperties( versionMetadata.getFacetList(), properties, "" ); + } + properties.setProperty( "id", versionMetadata.getId() ); + setProperty( properties, "name", versionMetadata.getName() ); + setProperty( properties, "description", versionMetadata.getDescription() ); + setProperty( properties, "url", versionMetadata.getUrl() ); + setProperty( properties, "incomplete", String.valueOf( versionMetadata.isIncomplete() ) ); + if ( versionMetadata.getScm() != null ) + { + setProperty( properties, "scm.connection", versionMetadata.getScm().getConnection() ); + setProperty( properties, "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() ); + setProperty( properties, "scm.url", versionMetadata.getScm().getUrl() ); + } + if ( versionMetadata.getCiManagement() != null ) + { + setProperty( properties, "ci.system", versionMetadata.getCiManagement().getSystem() ); + setProperty( properties, "ci.url", versionMetadata.getCiManagement().getUrl() ); + } + if ( versionMetadata.getIssueManagement() != null ) + { + setProperty( properties, "issue.system", versionMetadata.getIssueManagement().getSystem() ); + setProperty( properties, "issue.url", versionMetadata.getIssueManagement().getUrl() ); + } + if ( versionMetadata.getOrganization() != null ) + { + setProperty( properties, "org.name", versionMetadata.getOrganization().getName() ); + setProperty( properties, "org.url", versionMetadata.getOrganization().getUrl() ); + } + int i = 0; + for ( License license : versionMetadata.getLicenses() ) + { + setProperty( properties, "license." + i + ".name", license.getName() ); + setProperty( properties, "license." + i + ".url", license.getUrl() ); + i++; + } + i = 0; + for ( MailingList mailingList : versionMetadata.getMailingLists() ) + { + setProperty( properties, "mailingList." + i + ".archive", mailingList.getMainArchiveUrl() ); + setProperty( properties, "mailingList." + i + ".name", mailingList.getName() ); + setProperty( properties, "mailingList." + i + ".post", mailingList.getPostAddress() ); + setProperty( properties, "mailingList." + i + ".unsubscribe", mailingList.getUnsubscribeAddress() ); + setProperty( properties, "mailingList." + i + ".subscribe", mailingList.getSubscribeAddress() ); + setProperty( properties, "mailingList." + i + ".otherArchives", + join( mailingList.getOtherArchives() ) ); + i++; + } + i = 0; + ProjectVersionReference reference = new ProjectVersionReference(); + reference.setNamespace( namespace ); + reference.setProjectId( projectId ); + reference.setProjectVersion( versionMetadata.getId() ); + reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY ); + for ( Dependency dependency : versionMetadata.getDependencies() ) + { + setProperty( properties, "dependency." + i + ".classifier", dependency.getClassifier() ); + setProperty( properties, "dependency." + i + ".scope", dependency.getScope() ); + setProperty( properties, "dependency." + i + ".systemPath", dependency.getSystemPath() ); + setProperty( properties, "dependency." + i + ".artifactId", dependency.getArtifactId() ); + setProperty( properties, "dependency." + i + ".groupId", dependency.getGroupId() ); + setProperty( properties, "dependency." + i + ".version", dependency.getVersion() ); + setProperty( properties, "dependency." + i + ".type", dependency.getType() ); + setProperty( properties, "dependency." + i + ".optional", String.valueOf( dependency.isOptional() ) ); + + updateProjectReference( repoId, dependency.getGroupId(), dependency.getArtifactId(), + dependency.getVersion(), reference ); - updateProjectVersionFacets( versionMetadata, properties ); + i++; + } + Set facetIds = new LinkedHashSet( versionMetadata.getFacetIds() ); + facetIds.addAll( Arrays.asList( properties.getProperty( "facetIds", "" ).split( "," ) ) ); + properties.setProperty( "facetIds", join( facetIds ) ); + + updateProjectVersionFacets( versionMetadata, properties ); - try - { writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY ); } catch ( IOException e ) @@ -267,18 +276,18 @@ public class FileMetadataRepository private void updateProjectReference( String repoId, String namespace, String projectId, String projectVersion, ProjectVersionReference reference ) { - File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); - - Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - int i = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1; - setProperty( properties, "ref:lastReferenceNum", Integer.toString( i ) ); - setProperty( properties, "ref:reference." + i + ".namespace", reference.getNamespace() ); - setProperty( properties, "ref:reference." + i + ".projectId", reference.getProjectId() ); - setProperty( properties, "ref:reference." + i + ".projectVersion", reference.getProjectVersion() ); - setProperty( properties, "ref:reference." + i + ".referenceType", reference.getReferenceType().toString() ); - try { + File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); + + Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); + int i = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1; + setProperty( properties, "ref:lastReferenceNum", Integer.toString( i ) ); + setProperty( properties, "ref:reference." + i + ".namespace", reference.getNamespace() ); + setProperty( properties, "ref:reference." + i + ".projectId", reference.getProjectId() ); + setProperty( properties, "ref:reference." + i + ".projectVersion", reference.getProjectVersion() ); + setProperty( properties, "ref:reference." + i + ".referenceType", reference.getReferenceType().toString() ); + writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY ); } catch ( IOException e ) @@ -308,11 +317,19 @@ public class FileMetadataRepository @Override public List getMetadataFacets( String repoId, String facetId ) + throws MetadataRepositoryException { - File directory = getMetadataDirectory( repoId, facetId ); - List facets = new ArrayList<>(); - recurse( facets, "", directory ); - return facets; + try + { + File directory = getMetadataDirectory( repoId, facetId ); + List facets = new ArrayList<>(); + recurse( facets, "", directory ); + return facets; + } + catch ( IOException e ) + { + throw new MetadataRepositoryException( e.getMessage(), e ); + } } @Override @@ -398,26 +415,43 @@ public class FileMetadataRepository @Override public void removeMetadataFacets( String repositoryId, String facetId ) + throws MetadataRepositoryException { - File dir = getMetadataDirectory( repositoryId, facetId ); - if ( !FileUtils.deleteQuietly( dir ) ) + try { - log.error( "Cannot delete the metadata repository {}", dir ); + File dir = getMetadataDirectory( repositoryId, facetId ); + if ( !Files.deleteIfExists( dir.toPath() ) ) + { + log.error( "Cannot delete the metadata repository {}", dir ); + } + } + catch ( IOException e ) + { + throw new MetadataRepositoryException( e.getMessage(), e ); } } @Override public void removeMetadataFacet( String repoId, String facetId, String name ) + throws MetadataRepositoryException { - File dir = new File( getMetadataDirectory( repoId, facetId ), name ); - if ( !FileUtils.deleteQuietly( dir ) ) + try { - log.error( "Cannot delete the metadata repository {}", dir ); + File dir = new File( getMetadataDirectory( repoId, facetId ), name ); + if ( !Files.deleteIfExists( dir.toPath() ) ) + { + log.error( "Cannot delete the metadata repository {}", dir ); + } + } + catch ( IOException e ) + { + throw new MetadataRepositoryException( e.getMessage(), e ); } } @Override public List getArtifactsByDateRange( String repoId, Date startTime, Date endTime ) + throws MetadataRepositoryException { // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index // of this information (eg. in Lucene, as before) @@ -433,124 +467,140 @@ public class FileMetadataRepository private void getArtifactsByDateRange( List artifacts, String repoId, String ns, Date startTime, Date endTime ) + throws MetadataRepositoryException { - for ( String namespace : getNamespaces( repoId, ns ) ) + try { - getArtifactsByDateRange( artifacts, repoId, ns + "." + namespace, startTime, endTime ); - } + for ( String namespace : getNamespaces( repoId, ns ) ) + { + getArtifactsByDateRange( artifacts, repoId, ns + "." + namespace, startTime, endTime ); + } - for ( String project : getProjects( repoId, ns ) ) - { - for ( String version : getProjectVersions( repoId, ns, project ) ) + for ( String project : getProjects( repoId, ns ) ) { - for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) ) + for ( String version : getProjectVersions( repoId, ns, project ) ) { - if ( startTime == null || startTime.before( artifact.getWhenGathered() ) ) + for ( ArtifactMetadata artifact : getArtifacts( repoId, ns, project, version ) ) { - if ( endTime == null || endTime.after( artifact.getWhenGathered() ) ) + if ( startTime == null || startTime.before( artifact.getWhenGathered() ) ) { - artifacts.add( artifact ); + if ( endTime == null || endTime.after( artifact.getWhenGathered() ) ) + { + artifacts.add( artifact ); + } } } } } } + catch ( MetadataResolutionException e ) + { + throw new MetadataRepositoryException( e.getMessage(), e ); + } } @Override public Collection getArtifacts( String repoId, String namespace, String projectId, String projectVersion ) + throws MetadataResolutionException { - Map artifacts = new HashMap<>(); + try + { + Map artifacts = new HashMap<>(); - File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); + File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); - Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); + Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - for ( Map.Entry entry : properties.entrySet() ) - { - String name = (String) entry.getKey(); - StringTokenizer tok = new StringTokenizer( name, ":" ); - if ( tok.hasMoreTokens() && "artifact".equals( tok.nextToken() ) ) + for ( Map.Entry entry : properties.entrySet() ) { - String field = tok.nextToken(); - String id = tok.nextToken(); - - ArtifactMetadata artifact = artifacts.get( id ); - if ( artifact == null ) + String name = (String) entry.getKey(); + StringTokenizer tok = new StringTokenizer( name, ":" ); + if ( tok.hasMoreTokens() && "artifact".equals( tok.nextToken() ) ) { - artifact = new ArtifactMetadata(); - artifact.setRepositoryId( repoId ); - artifact.setNamespace( namespace ); - artifact.setProject( projectId ); - artifact.setProjectVersion( projectVersion ); - artifact.setVersion( projectVersion ); - artifact.setId( id ); - artifacts.put( id, artifact ); - } + String field = tok.nextToken(); + String id = tok.nextToken(); - String value = (String) entry.getValue(); - if ( "updated".equals( field ) ) - { - artifact.setFileLastModified( Long.parseLong( value ) ); - } - else if ( "size".equals( field ) ) - { - artifact.setSize( Long.valueOf( value ) ); - } - else if ( "whenGathered".equals( field ) ) - { - artifact.setWhenGathered( new Date( Long.parseLong( value ) ) ); - } - else if ( "version".equals( field ) ) - { - artifact.setVersion( value ); - } - else if ( "md5".equals( field ) ) - { - artifact.setMd5( value ); - } - else if ( "sha1".equals( field ) ) - { - artifact.setSha1( value ); - } - else if ( "facetIds".equals( field ) ) - { - if ( value.length() > 0 ) + ArtifactMetadata artifact = artifacts.get( id ); + if ( artifact == null ) + { + artifact = new ArtifactMetadata(); + artifact.setRepositoryId( repoId ); + artifact.setNamespace( namespace ); + artifact.setProject( projectId ); + artifact.setProjectVersion( projectVersion ); + artifact.setVersion( projectVersion ); + artifact.setId( id ); + artifacts.put( id, artifact ); + } + + String value = (String) entry.getValue(); + if ( "updated".equals( field ) ) + { + artifact.setFileLastModified( Long.parseLong( value ) ); + } + else if ( "size".equals( field ) ) + { + artifact.setSize( Long.valueOf( value ) ); + } + else if ( "whenGathered".equals( field ) ) + { + artifact.setWhenGathered( new Date( Long.parseLong( value ) ) ); + } + else if ( "version".equals( field ) ) + { + artifact.setVersion( value ); + } + else if ( "md5".equals( field ) ) + { + artifact.setMd5( value ); + } + else if ( "sha1".equals( field ) ) + { + artifact.setSha1( value ); + } + else if ( "facetIds".equals( field ) ) { - String propertyPrefix = "artifact:facet:" + id + ":"; - for ( String facetId : value.split( "," ) ) + if ( value.length() > 0 ) { - MetadataFacetFactory factory = metadataFacetFactories.get( facetId ); - if ( factory == null ) + String propertyPrefix = "artifact:facet:" + id + ":"; + for ( String facetId : value.split( "," ) ) { - log.error( "Attempted to load unknown artifact metadata facet: " + facetId ); - } - else - { - MetadataFacet facet = factory.createMetadataFacet(); - String prefix = propertyPrefix + facet.getFacetId(); - Map map = new HashMap<>(); - for ( Object key : new ArrayList( properties.keySet() ) ) + MetadataFacetFactory factory = metadataFacetFactories.get( facetId ); + if ( factory == null ) { - String property = (String) key; - if ( property.startsWith( prefix ) ) + log.error( "Attempted to load unknown artifact metadata facet: " + facetId ); + } + else + { + MetadataFacet facet = factory.createMetadataFacet(); + String prefix = propertyPrefix + facet.getFacetId(); + Map map = new HashMap<>(); + for ( Object key : new ArrayList( properties.keySet() ) ) { - map.put( property.substring( prefix.length() + 1 ), - properties.getProperty( property ) ); + String property = (String) key; + if ( property.startsWith( prefix ) ) + { + map.put( property.substring( prefix.length() + 1 ), + properties.getProperty( property ) ); + } } + facet.fromProperties( map ); + artifact.addFacet( facet ); } - facet.fromProperties( map ); - artifact.addFacet( facet ); } } - } - updateArtifactFacets( artifact, properties ); + updateArtifactFacets( artifact, properties ); + } } } + return artifacts.values(); + } + catch ( IOException e ) + { + throw new MetadataResolutionException( e.getMessage(), e ); } - return artifacts.values(); } @Override @@ -610,18 +660,26 @@ public class FileMetadataRepository @Override public List getArtifactsByChecksum( String repositoryId, String checksum ) + throws MetadataRepositoryException { - // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index - // of this information (eg. in Lucene, as before) - // alternatively, we could build a referential tree in the content repository, however it would need some levels - // of depth to avoid being too broad to be useful (eg. /repository/checksums/a/ab/abcdef1234567) + try + { + // TODO: this is quite slow - if we are to persist with this repository implementation we should build an index + // of this information (eg. in Lucene, as before) + // alternatively, we could build a referential tree in the content repository, however it would need some levels + // of depth to avoid being too broad to be useful (eg. /repository/checksums/a/ab/abcdef1234567) - List artifacts = new ArrayList<>(); - for ( String ns : getRootNamespaces( repositoryId ) ) + List artifacts = new ArrayList<>(); + for ( String ns : getRootNamespaces( repositoryId ) ) + { + getArtifactsByChecksum( artifacts, repositoryId, ns, checksum ); + } + return artifacts; + } + catch ( MetadataResolutionException e ) { - getArtifactsByChecksum( artifacts, repositoryId, ns, checksum ); + throw new MetadataRepositoryException( e.getMessage(), e ); } - return artifacts; } @Override @@ -648,82 +706,77 @@ public class FileMetadataRepository throws MetadataRepositoryException { - File directory = new File( getDirectory( artifactMetadata.getRepositoryId() ), - artifactMetadata.getNamespace() + "/" + artifactMetadata.getProject() + "/" - + baseVersion - ); + try + { + File directory = new File( getDirectory( artifactMetadata.getRepositoryId() ), + artifactMetadata.getNamespace() + "/" + artifactMetadata.getProject() + "/" + + baseVersion ); - Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); + Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - String id = artifactMetadata.getId(); + String id = artifactMetadata.getId(); - properties.remove( "artifact:updated:" + id ); - properties.remove( "artifact:whenGathered:" + id ); - properties.remove( "artifact:size:" + id ); - properties.remove( "artifact:md5:" + id ); - properties.remove( "artifact:sha1:" + id ); - properties.remove( "artifact:version:" + id ); - properties.remove( "artifact:facetIds:" + id ); + properties.remove( "artifact:updated:" + id ); + properties.remove( "artifact:whenGathered:" + id ); + properties.remove( "artifact:size:" + id ); + properties.remove( "artifact:md5:" + id ); + properties.remove( "artifact:sha1:" + id ); + properties.remove( "artifact:version:" + id ); + properties.remove( "artifact:facetIds:" + id ); - String prefix = "artifact:facet:" + id + ":"; - for ( Object key : new ArrayList( properties.keySet() ) ) - { - String property = (String) key; - if ( property.startsWith( prefix ) ) + String prefix = "artifact:facet:" + id + ":"; + for ( Object key : new ArrayList( properties.keySet() ) ) { - properties.remove( property ); + String property = (String) key; + if ( property.startsWith( prefix ) ) + { + properties.remove( property ); + } } - } - try - { writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY ); } catch ( IOException e ) { - // TODO - log.error( e.getMessage(), e ); + throw new MetadataRepositoryException( e.getMessage(), e ); } } @Override public void removeArtifact( String repoId, String namespace, String project, String version, String id ) + throws MetadataRepositoryException { + try + { + File directory = new File( getDirectory( repoId ), namespace + "/" + project + "/" + version ); - File directory = new File( getDirectory( repoId ), namespace + "/" + project + "/" + version ); - - Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); + Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - properties.remove( "artifact:updated:" + id ); - properties.remove( "artifact:whenGathered:" + id ); - properties.remove( "artifact:size:" + id ); - properties.remove( "artifact:md5:" + id ); - properties.remove( "artifact:sha1:" + id ); - properties.remove( "artifact:version:" + id ); - properties.remove( "artifact:facetIds:" + id ); + properties.remove( "artifact:updated:" + id ); + properties.remove( "artifact:whenGathered:" + id ); + properties.remove( "artifact:size:" + id ); + properties.remove( "artifact:md5:" + id ); + properties.remove( "artifact:sha1:" + id ); + properties.remove( "artifact:version:" + id ); + properties.remove( "artifact:facetIds:" + id ); - String prefix = "artifact:facet:" + id + ":"; - for ( Object key : new ArrayList( properties.keySet() ) ) - { - String property = (String) key; - if ( property.startsWith( prefix ) ) + String prefix = "artifact:facet:" + id + ":"; + for ( Object key : new ArrayList( properties.keySet() ) ) { - properties.remove( property ); + String property = (String) key; + if ( property.startsWith( prefix ) ) + { + properties.remove( property ); + } } - } - - try - { - - FileUtils.deleteDirectory( directory ); + Files.deleteIfExists( directory.toPath() ); //writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY ); } catch ( IOException e ) { - // TODO - log.error( e.getMessage(), e ); + throw new MetadataRepositoryException( e.getMessage(), e ); } } @@ -747,35 +800,51 @@ public class FileMetadataRepository @Override public void removeRepository( String repoId ) + throws MetadataRepositoryException { - File dir = getDirectory( repoId ); - if ( !FileUtils.deleteQuietly( dir ) ) + try { - log.error( "Cannot delete repository {}", dir ); + File dir = getDirectory( repoId ); + if ( !Files.deleteIfExists( dir.toPath() ) ) + { + log.error( "Cannot delete repository {}", dir ); + } + } + catch ( IOException e ) + { + throw new MetadataRepositoryException( e.getMessage(), e ); } } private void getArtifactsByChecksum( List artifacts, String repositoryId, String ns, String checksum ) + throws MetadataRepositoryException { - for ( String namespace : getNamespaces( repositoryId, ns ) ) + try { - getArtifactsByChecksum( artifacts, repositoryId, ns + "." + namespace, checksum ); - } + for ( String namespace : getNamespaces( repositoryId, ns ) ) + { + getArtifactsByChecksum( artifacts, repositoryId, ns + "." + namespace, checksum ); + } - for ( String project : getProjects( repositoryId, ns ) ) - { - for ( String version : getProjectVersions( repositoryId, ns, project ) ) + for ( String project : getProjects( repositoryId, ns ) ) { - for ( ArtifactMetadata artifact : getArtifacts( repositoryId, ns, project, version ) ) + for ( String version : getProjectVersions( repositoryId, ns, project ) ) { - if ( checksum.equals( artifact.getMd5() ) || checksum.equals( artifact.getSha1() ) ) + for ( ArtifactMetadata artifact : getArtifacts( repositoryId, ns, project, version ) ) { - artifacts.add( artifact ); + if ( checksum.equals( artifact.getMd5() ) || checksum.equals( artifact.getSha1() ) ) + { + artifacts.add( artifact ); + } } } } } + catch ( MetadataResolutionException e ) + { + throw new MetadataRepositoryException( e.getMessage(), e ); + } } @Override @@ -800,6 +869,7 @@ public class FileMetadataRepository } private File getMetadataDirectory( String repoId, String facetId ) + throws IOException { return new File( getBaseDirectory( repoId ), "facets/" + facetId ); } @@ -831,39 +901,42 @@ public class FileMetadataRepository public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion, ArtifactMetadata artifact ) { - ProjectVersionMetadata metadata = new ProjectVersionMetadata(); - metadata.setId( projectVersion ); - updateProjectVersion( repoId, namespace, projectId, metadata ); + try + { + ProjectVersionMetadata metadata = new ProjectVersionMetadata(); + metadata.setId( projectVersion ); + updateProjectVersion( repoId, namespace, projectId, metadata ); - File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); + File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); - Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); + Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - clearMetadataFacetProperties( artifact.getFacetList(), properties, "artifact:facet:" + artifact.getId() + ":" ); + clearMetadataFacetProperties( artifact.getFacetList(), properties, + "artifact:facet:" + artifact.getId() + ":" ); - String id = artifact.getId(); - properties.setProperty( "artifact:updated:" + id, Long.toString( artifact.getFileLastModified().getTime() ) ); - properties.setProperty( "artifact:whenGathered:" + id, Long.toString( artifact.getWhenGathered().getTime() ) ); - properties.setProperty( "artifact:size:" + id, Long.toString( artifact.getSize() ) ); - if ( artifact.getMd5() != null ) - { - properties.setProperty( "artifact:md5:" + id, artifact.getMd5() ); - } - if ( artifact.getSha1() != null ) - { - properties.setProperty( "artifact:sha1:" + id, artifact.getSha1() ); - } - properties.setProperty( "artifact:version:" + id, artifact.getVersion() ); + String id = artifact.getId(); + properties.setProperty( "artifact:updated:" + id, + Long.toString( artifact.getFileLastModified().getTime() ) ); + properties.setProperty( "artifact:whenGathered:" + id, + Long.toString( artifact.getWhenGathered().getTime() ) ); + properties.setProperty( "artifact:size:" + id, Long.toString( artifact.getSize() ) ); + if ( artifact.getMd5() != null ) + { + properties.setProperty( "artifact:md5:" + id, artifact.getMd5() ); + } + if ( artifact.getSha1() != null ) + { + properties.setProperty( "artifact:sha1:" + id, artifact.getSha1() ); + } + properties.setProperty( "artifact:version:" + id, artifact.getVersion() ); - Set facetIds = new LinkedHashSet( artifact.getFacetIds() ); - String property = "artifact:facetIds:" + id; - facetIds.addAll( Arrays.asList( properties.getProperty( property, "" ).split( "," ) ) ); - properties.setProperty( property, join( facetIds ) ); + Set facetIds = new LinkedHashSet( artifact.getFacetIds() ); + String property = "artifact:facetIds:" + id; + facetIds.addAll( Arrays.asList( properties.getProperty( property, "" ).split( "," ) ) ); + properties.setProperty( property, join( facetIds ) ); - updateArtifactFacets( artifact, properties ); + updateArtifactFacets( artifact, properties ); - try - { writeProperties( properties, directory, PROJECT_VERSION_METADATA_KEY ); } catch ( IOException e ) @@ -905,332 +978,387 @@ public class FileMetadataRepository @Override public ProjectMetadata getProject( String repoId, String namespace, String projectId ) + throws MetadataResolutionException { - File directory = new File( getDirectory( repoId ), namespace + "/" + projectId ); + try + { + File directory = new File( getDirectory( repoId ), namespace + "/" + projectId ); + + Properties properties = readOrCreateProperties( directory, PROJECT_METADATA_KEY ); - Properties properties = readOrCreateProperties( directory, PROJECT_METADATA_KEY ); + ProjectMetadata project = null; - ProjectMetadata project = null; + String id = properties.getProperty( "id" ); + if ( id != null ) + { + project = new ProjectMetadata(); + project.setNamespace( properties.getProperty( "namespace" ) ); + project.setId( id ); + } - String id = properties.getProperty( "id" ); - if ( id != null ) + return project; + } + catch ( IOException e ) { - project = new ProjectMetadata(); - project.setNamespace( properties.getProperty( "namespace" ) ); - project.setId( id ); + throw new MetadataResolutionException( e.getMessage(), e ); } - - return project; } @Override public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId, String projectVersion ) + throws MetadataResolutionException { - File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); - - Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - String id = properties.getProperty( "id" ); - ProjectVersionMetadata versionMetadata = null; - if ( id != null ) - { - versionMetadata = new ProjectVersionMetadata(); - versionMetadata.setId( id ); - versionMetadata.setName( properties.getProperty( "name" ) ); - versionMetadata.setDescription( properties.getProperty( "description" ) ); - versionMetadata.setUrl( properties.getProperty( "url" ) ); - versionMetadata.setIncomplete( Boolean.valueOf( properties.getProperty( "incomplete", "false" ) ) ); - - String scmConnection = properties.getProperty( "scm.connection" ); - String scmDeveloperConnection = properties.getProperty( "scm.developerConnection" ); - String scmUrl = properties.getProperty( "scm.url" ); - if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null ) - { - Scm scm = new Scm(); - scm.setConnection( scmConnection ); - scm.setDeveloperConnection( scmDeveloperConnection ); - scm.setUrl( scmUrl ); - versionMetadata.setScm( scm ); - } - - String ciSystem = properties.getProperty( "ci.system" ); - String ciUrl = properties.getProperty( "ci.url" ); - if ( ciSystem != null || ciUrl != null ) - { - CiManagement ci = new CiManagement(); - ci.setSystem( ciSystem ); - ci.setUrl( ciUrl ); - versionMetadata.setCiManagement( ci ); - } + try + { + File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); - String issueSystem = properties.getProperty( "issue.system" ); - String issueUrl = properties.getProperty( "issue.url" ); - if ( issueSystem != null || issueUrl != null ) + Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); + String id = properties.getProperty( "id" ); + ProjectVersionMetadata versionMetadata = null; + if ( id != null ) { - IssueManagement issueManagement = new IssueManagement(); - issueManagement.setSystem( issueSystem ); - issueManagement.setUrl( issueUrl ); - versionMetadata.setIssueManagement( issueManagement ); - } + versionMetadata = new ProjectVersionMetadata(); + versionMetadata.setId( id ); + versionMetadata.setName( properties.getProperty( "name" ) ); + versionMetadata.setDescription( properties.getProperty( "description" ) ); + versionMetadata.setUrl( properties.getProperty( "url" ) ); + versionMetadata.setIncomplete( Boolean.valueOf( properties.getProperty( "incomplete", "false" ) ) ); + + String scmConnection = properties.getProperty( "scm.connection" ); + String scmDeveloperConnection = properties.getProperty( "scm.developerConnection" ); + String scmUrl = properties.getProperty( "scm.url" ); + if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null ) + { + Scm scm = new Scm(); + scm.setConnection( scmConnection ); + scm.setDeveloperConnection( scmDeveloperConnection ); + scm.setUrl( scmUrl ); + versionMetadata.setScm( scm ); + } - String orgName = properties.getProperty( "org.name" ); - String orgUrl = properties.getProperty( "org.url" ); - if ( orgName != null || orgUrl != null ) - { - Organization org = new Organization(); - org.setName( orgName ); - org.setUrl( orgUrl ); - versionMetadata.setOrganization( org ); - } + String ciSystem = properties.getProperty( "ci.system" ); + String ciUrl = properties.getProperty( "ci.url" ); + if ( ciSystem != null || ciUrl != null ) + { + CiManagement ci = new CiManagement(); + ci.setSystem( ciSystem ); + ci.setUrl( ciUrl ); + versionMetadata.setCiManagement( ci ); + } - boolean done = false; - int i = 0; - while ( !done ) - { - String licenseName = properties.getProperty( "license." + i + ".name" ); - String licenseUrl = properties.getProperty( "license." + i + ".url" ); - if ( licenseName != null || licenseUrl != null ) + String issueSystem = properties.getProperty( "issue.system" ); + String issueUrl = properties.getProperty( "issue.url" ); + if ( issueSystem != null || issueUrl != null ) { - License license = new License(); - license.setName( licenseName ); - license.setUrl( licenseUrl ); - versionMetadata.addLicense( license ); + IssueManagement issueManagement = new IssueManagement(); + issueManagement.setSystem( issueSystem ); + issueManagement.setUrl( issueUrl ); + versionMetadata.setIssueManagement( issueManagement ); } - else + + String orgName = properties.getProperty( "org.name" ); + String orgUrl = properties.getProperty( "org.url" ); + if ( orgName != null || orgUrl != null ) { - done = true; + Organization org = new Organization(); + org.setName( orgName ); + org.setUrl( orgUrl ); + versionMetadata.setOrganization( org ); } - i++; - } - done = false; - i = 0; - while ( !done ) - { - String mailingListName = properties.getProperty( "mailingList." + i + ".name" ); - if ( mailingListName != null ) + boolean done = false; + int i = 0; + while ( !done ) { - MailingList mailingList = new MailingList(); - mailingList.setName( mailingListName ); - mailingList.setMainArchiveUrl( properties.getProperty( "mailingList." + i + ".archive" ) ); - String p = properties.getProperty( "mailingList." + i + ".otherArchives" ); - if ( p != null && p.length() > 0 ) + String licenseName = properties.getProperty( "license." + i + ".name" ); + String licenseUrl = properties.getProperty( "license." + i + ".url" ); + if ( licenseName != null || licenseUrl != null ) { - mailingList.setOtherArchives( Arrays.asList( p.split( "," ) ) ); + License license = new License(); + license.setName( licenseName ); + license.setUrl( licenseUrl ); + versionMetadata.addLicense( license ); } else { - mailingList.setOtherArchives( Collections.emptyList() ); + done = true; } - mailingList.setPostAddress( properties.getProperty( "mailingList." + i + ".post" ) ); - mailingList.setSubscribeAddress( properties.getProperty( "mailingList." + i + ".subscribe" ) ); - mailingList.setUnsubscribeAddress( properties.getProperty( "mailingList." + i + ".unsubscribe" ) ); - versionMetadata.addMailingList( mailingList ); - } - else - { - done = true; + i++; } - i++; - } - done = false; - i = 0; - while ( !done ) - { - String dependencyArtifactId = properties.getProperty( "dependency." + i + ".artifactId" ); - if ( dependencyArtifactId != null ) + done = false; + i = 0; + while ( !done ) { - Dependency dependency = new Dependency(); - dependency.setArtifactId( dependencyArtifactId ); - dependency.setGroupId( properties.getProperty( "dependency." + i + ".groupId" ) ); - dependency.setClassifier( properties.getProperty( "dependency." + i + ".classifier" ) ); - dependency.setOptional( - Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) ); - dependency.setScope( properties.getProperty( "dependency." + i + ".scope" ) ); - dependency.setSystemPath( properties.getProperty( "dependency." + i + ".systemPath" ) ); - dependency.setType( properties.getProperty( "dependency." + i + ".type" ) ); - dependency.setVersion( properties.getProperty( "dependency." + i + ".version" ) ); - dependency.setOptional( - Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) ); - versionMetadata.addDependency( dependency ); - } - else - { - done = true; + String mailingListName = properties.getProperty( "mailingList." + i + ".name" ); + if ( mailingListName != null ) + { + MailingList mailingList = new MailingList(); + mailingList.setName( mailingListName ); + mailingList.setMainArchiveUrl( properties.getProperty( "mailingList." + i + ".archive" ) ); + String p = properties.getProperty( "mailingList." + i + ".otherArchives" ); + if ( p != null && p.length() > 0 ) + { + mailingList.setOtherArchives( Arrays.asList( p.split( "," ) ) ); + } + else + { + mailingList.setOtherArchives( Collections.emptyList() ); + } + mailingList.setPostAddress( properties.getProperty( "mailingList." + i + ".post" ) ); + mailingList.setSubscribeAddress( properties.getProperty( "mailingList." + i + ".subscribe" ) ); + mailingList.setUnsubscribeAddress( + properties.getProperty( "mailingList." + i + ".unsubscribe" ) ); + versionMetadata.addMailingList( mailingList ); + } + else + { + done = true; + } + i++; } - i++; - } - String facetIds = properties.getProperty( "facetIds", "" ); - if ( facetIds.length() > 0 ) - { - for ( String facetId : facetIds.split( "," ) ) + done = false; + i = 0; + while ( !done ) { - MetadataFacetFactory factory = metadataFacetFactories.get( facetId ); - if ( factory == null ) + String dependencyArtifactId = properties.getProperty( "dependency." + i + ".artifactId" ); + if ( dependencyArtifactId != null ) { - log.error( "Attempted to load unknown project version metadata facet: {}", facetId ); + Dependency dependency = new Dependency(); + dependency.setArtifactId( dependencyArtifactId ); + dependency.setGroupId( properties.getProperty( "dependency." + i + ".groupId" ) ); + dependency.setClassifier( properties.getProperty( "dependency." + i + ".classifier" ) ); + dependency.setOptional( + Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) ); + dependency.setScope( properties.getProperty( "dependency." + i + ".scope" ) ); + dependency.setSystemPath( properties.getProperty( "dependency." + i + ".systemPath" ) ); + dependency.setType( properties.getProperty( "dependency." + i + ".type" ) ); + dependency.setVersion( properties.getProperty( "dependency." + i + ".version" ) ); + dependency.setOptional( + Boolean.valueOf( properties.getProperty( "dependency." + i + ".optional" ) ) ); + versionMetadata.addDependency( dependency ); } else { - MetadataFacet facet = factory.createMetadataFacet(); - Map map = new HashMap<>(); - for ( Object key : new ArrayList( properties.keySet() ) ) + done = true; + } + i++; + } + + String facetIds = properties.getProperty( "facetIds", "" ); + if ( facetIds.length() > 0 ) + { + for ( String facetId : facetIds.split( "," ) ) + { + MetadataFacetFactory factory = metadataFacetFactories.get( facetId ); + if ( factory == null ) { - String property = (String) key; - if ( property.startsWith( facet.getFacetId() ) ) + log.error( "Attempted to load unknown project version metadata facet: {}", facetId ); + } + else + { + MetadataFacet facet = factory.createMetadataFacet(); + Map map = new HashMap<>(); + for ( Object key : new ArrayList( properties.keySet() ) ) { - map.put( property.substring( facet.getFacetId().length() + 1 ), - properties.getProperty( property ) ); + String property = (String) key; + if ( property.startsWith( facet.getFacetId() ) ) + { + map.put( property.substring( facet.getFacetId().length() + 1 ), + properties.getProperty( property ) ); + } } + facet.fromProperties( map ); + versionMetadata.addFacet( facet ); } - facet.fromProperties( map ); - versionMetadata.addFacet( facet ); } } - } - updateProjectVersionFacets( versionMetadata, properties ); + updateProjectVersionFacets( versionMetadata, properties ); + } + return versionMetadata; + } + catch ( IOException e ) + { + throw new MetadataResolutionException( e.getMessage(), e ); } - return versionMetadata; } @Override public Collection getArtifactVersions( String repoId, String namespace, String projectId, String projectVersion ) + throws MetadataResolutionException { - File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); + try + { + File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); - Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); + Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - Set versions = new HashSet(); - for ( Map.Entry entry : properties.entrySet() ) - { - String name = (String) entry.getKey(); - if ( name.startsWith( "artifact:version:" ) ) + Set versions = new HashSet(); + for ( Map.Entry entry : properties.entrySet() ) { - versions.add( (String) entry.getValue() ); + String name = (String) entry.getKey(); + if ( name.startsWith( "artifact:version:" ) ) + { + versions.add( (String) entry.getValue() ); + } } + return versions; + } + catch ( IOException e ) + { + throw new MetadataResolutionException( e.getMessage(), e ); } - return versions; } @Override public Collection getProjectReferences( String repoId, String namespace, String projectId, String projectVersion ) + throws MetadataResolutionException { - File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); + try + { + File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); - Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); - int numberOfRefs = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1; + Properties properties = readOrCreateProperties( directory, PROJECT_VERSION_METADATA_KEY ); + int numberOfRefs = Integer.parseInt( properties.getProperty( "ref:lastReferenceNum", "-1" ) ) + 1; - List references = new ArrayList<>(); - for ( int i = 0; i < numberOfRefs; i++ ) + List references = new ArrayList<>(); + for ( int i = 0; i < numberOfRefs; i++ ) + { + ProjectVersionReference reference = new ProjectVersionReference(); + reference.setProjectId( properties.getProperty( "ref:reference." + i + ".projectId" ) ); + reference.setNamespace( properties.getProperty( "ref:reference." + i + ".namespace" ) ); + reference.setProjectVersion( properties.getProperty( "ref:reference." + i + ".projectVersion" ) ); + reference.setReferenceType( ProjectVersionReference.ReferenceType.valueOf( + properties.getProperty( "ref:reference." + i + ".referenceType" ) ) ); + references.add( reference ); + } + return references; + } + catch ( IOException e ) { - ProjectVersionReference reference = new ProjectVersionReference(); - reference.setProjectId( properties.getProperty( "ref:reference." + i + ".projectId" ) ); - reference.setNamespace( properties.getProperty( "ref:reference." + i + ".namespace" ) ); - reference.setProjectVersion( properties.getProperty( "ref:reference." + i + ".projectVersion" ) ); - reference.setReferenceType( ProjectVersionReference.ReferenceType.valueOf( - properties.getProperty( "ref:reference." + i + ".referenceType" ) ) ); - references.add( reference ); - } - return references; + throw new MetadataResolutionException( e.getMessage(), e ); + } } @Override public Collection getRootNamespaces( String repoId ) + throws MetadataResolutionException { return getNamespaces( repoId, null ); } @Override public Collection getNamespaces( String repoId, String baseNamespace ) + throws MetadataResolutionException { - List allNamespaces = new ArrayList<>(); - File directory = getDirectory( repoId ); - File[] files = directory.listFiles(); - if ( files != null ) + try { - for ( File namespace : files ) + List allNamespaces = new ArrayList<>(); + File directory = getDirectory( repoId ); + File[] files = directory.listFiles(); + if ( files != null ) { - if ( new File( namespace, NAMESPACE_METADATA_KEY + ".properties" ).exists() ) + for ( File namespace : files ) { - allNamespaces.add( namespace.getName() ); + if ( new File( namespace, NAMESPACE_METADATA_KEY + ".properties" ).exists() ) + { + allNamespaces.add( namespace.getName() ); + } } } - } - Set namespaces = new LinkedHashSet<>(); - int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0; - for ( String namespace : allNamespaces ) - { - if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) ) + Set namespaces = new LinkedHashSet<>(); + int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0; + for ( String namespace : allNamespaces ) { - int i = namespace.indexOf( '.', fromIndex ); - if ( i >= 0 ) - { - namespaces.add( namespace.substring( fromIndex, i ) ); - } - else + if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) ) { - namespaces.add( namespace.substring( fromIndex ) ); + int i = namespace.indexOf( '.', fromIndex ); + if ( i >= 0 ) + { + namespaces.add( namespace.substring( fromIndex, i ) ); + } + else + { + namespaces.add( namespace.substring( fromIndex ) ); + } } } + return new ArrayList<>( namespaces ); + } + catch ( IOException e ) + { + throw new MetadataResolutionException( e.getMessage(), e ); } - return new ArrayList<>( namespaces ); } @Override public Collection getProjects( String repoId, String namespace ) + throws MetadataResolutionException { - List projects = new ArrayList<>(); - File directory = new File( getDirectory( repoId ), namespace ); - File[] files = directory.listFiles(); - if ( files != null ) + try { - for ( File project : files ) + List projects = new ArrayList<>(); + File directory = new File( getDirectory( repoId ), namespace ); + File[] files = directory.listFiles(); + if ( files != null ) { - if ( new File( project, PROJECT_METADATA_KEY + ".properties" ).exists() ) + for ( File project : files ) { - projects.add( project.getName() ); + if ( new File( project, PROJECT_METADATA_KEY + ".properties" ).exists() ) + { + projects.add( project.getName() ); + } } } + return projects; + } + catch ( IOException e ) + { + throw new MetadataResolutionException( e.getMessage(), e ); } - return projects; } @Override public Collection getProjectVersions( String repoId, String namespace, String projectId ) + throws MetadataResolutionException { - List projectVersions = new ArrayList<>(); - File directory = new File( getDirectory( repoId ), namespace + "/" + projectId ); - File[] files = directory.listFiles(); - if ( files != null ) + try { - for ( File projectVersion : files ) + List projectVersions = new ArrayList<>(); + File directory = new File( getDirectory( repoId ), namespace + "/" + projectId ); + File[] files = directory.listFiles(); + if ( files != null ) { - if ( new File( projectVersion, PROJECT_VERSION_METADATA_KEY + ".properties" ).exists() ) + for ( File projectVersion : files ) { - projectVersions.add( projectVersion.getName() ); + if ( new File( projectVersion, PROJECT_VERSION_METADATA_KEY + ".properties" ).exists() ) + { + projectVersions.add( projectVersion.getName() ); + } } } + return projectVersions; + } + catch ( IOException e ) + { + throw new MetadataResolutionException( e.getMessage(), e ); } - return projectVersions; } @Override public void removeProject( String repositoryId, String namespace, String projectId ) throws MetadataRepositoryException { - File directory = new File( getDirectory( repositoryId ), namespace + "/" + projectId ); try { - if ( directory.exists() ) - { - FileUtils.deleteDirectory( directory ); - } + File directory = new File( getDirectory( repositoryId ), namespace + "/" + projectId ); + Files.deleteIfExists( directory.toPath() ); } catch ( IOException e ) { @@ -1242,18 +1370,16 @@ public class FileMetadataRepository public void removeProjectVersion( String repoId, String namespace, String projectId, String projectVersion ) throws MetadataRepositoryException { - File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); - if ( directory.exists() ) + try { - try - { - FileUtils.deleteDirectory( directory ); - } - catch ( IOException e ) - { - throw new MetadataRepositoryException( e.getMessage(), e ); - } + File directory = new File( getDirectory( repoId ), namespace + "/" + projectId + "/" + projectVersion ); + Files.deleteIfExists( directory.toPath() ); } + catch ( IOException e ) + { + throw new MetadataRepositoryException( e.getMessage(), e ); + } + } private void writeProperties( Properties properties, File directory, String propertiesKey ) @@ -1290,16 +1416,25 @@ public class FileMetadataRepository @Override public List getArtifacts( String repoId ) + throws MetadataRepositoryException { - List artifacts = new ArrayList<>(); - for ( String ns : getRootNamespaces( repoId ) ) + try + { + List artifacts = new ArrayList<>(); + for ( String ns : getRootNamespaces( repoId ) ) + { + getArtifacts( artifacts, repoId, ns ); + } + return artifacts; + } + catch ( MetadataResolutionException e ) { - getArtifacts( artifacts, repoId, ns ); + throw new MetadataRepositoryException( e.getMessage(), e ); } - return artifacts; } private void getArtifacts( List artifacts, String repoId, String ns ) + throws MetadataResolutionException { for ( String namespace : getNamespaces( repoId, ns ) ) { -- 2.39.5