From f75938cf85ae6354c49c504a596ef1a8645fb9a2 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Wed, 12 Feb 2020 18:41:39 +0100 Subject: [PATCH] Adding content item implementations --- .../content/base/ArchivaContentItem.java | 9 +- .../content/base/ArchivaProject.java | 123 ++++++++++++++---- .../content/base/ArchivaVersion.java | 104 +++++++++++++++ 3 files changed, 208 insertions(+), 28 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaVersion.java diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaContentItem.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaContentItem.java index b538bb13b..3b89a04c9 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaContentItem.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaContentItem.java @@ -19,7 +19,6 @@ package org.apache.archiva.repository.content.base; * under the License. */ -import org.apache.archiva.repository.UnsupportedConversionException; import org.apache.archiva.repository.content.ContentItem; import org.apache.archiva.repository.content.Project; import org.apache.commons.lang3.StringUtils; @@ -40,10 +39,14 @@ public abstract class ArchivaContentItem implements ContentItem private Map attributes; @Override - public abstract T adapt( Class clazz ) throws UnsupportedConversionException; + public T adapt( Class clazz ) { + return (T)this; + } @Override - public abstract boolean supports( Class clazz ); + public boolean supports( Class clazz ) { + return clazz != null && clazz.isAssignableFrom( this.getClass() ); + } /** diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaProject.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaProject.java index b2879b6dc..37e04748d 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaProject.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaProject.java @@ -19,60 +19,133 @@ package org.apache.archiva.repository.content.base; * under the License. */ -import org.apache.archiva.repository.RepositoryContent; -import org.apache.archiva.repository.UnsupportedConversionException; +import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.content.Project; - -import java.util.Map; +import org.apache.archiva.repository.storage.StorageAsset; +import org.apache.commons.lang3.StringUtils; /** * Immutable class, that represents a project. */ public class ArchivaProject extends ArchivaContentItem implements Project { - String namespace; - String id; - RepositoryContent repositoryContent; - Map attributes; + private String namespace; + private String id; + private ManagedRepositoryContent repositoryContent; + private StorageAsset asset; + + // Setting all setters to private. Builder is the way to go. + private ArchivaProject() { + + } + + + /** + * Creates the builder that allows to create a new instance. + * @param id the project id, must not be null + * @return a builder instance + */ + public static Builder withId( String id) { + return new Builder( ).withId( id ); + } @Override public String getNamespace( ) { - return null; + return this.namespace; } @Override public String getId( ) { - return null; + return this.id; } @Override - public RepositoryContent getRepository( ) + public ManagedRepositoryContent getRepository( ) { - return null; + return this.repositoryContent; } @Override - public Map getAttributes( ) + public StorageAsset getAsset( ) { - return null; + return asset; } - @Override - public T adapt( Class clazz ) throws UnsupportedConversionException + + + /* + * Builder interface chaining is used to restrict mandatory attributes + * This interface is for the optional arguments. + */ + public interface OptBuilder { + + OptBuilder withAsset( StorageAsset asset ); + + OptBuilder withNamespace( String namespace); + + OptBuilder withAttribute( String key, String value ); + + } + /* + * Builder classes for instantiation + */ + public static final class Builder implements OptBuilder { - if (clazz != ArchivaProject.class) { - throw new UnsupportedConversionException( "Cannot convert to class: " + clazz ); - } else { - return (T) this; + final private ArchivaProject project = new ArchivaProject(); + + private Builder( ) + { + + } + + private Builder withId(String id) { + if ( StringUtils.isEmpty( id ) ) { + throw new IllegalArgumentException( "Null or empty value not allowed for id" ); + } + project.id = id; + return this; + } + + + public OptBuilder withRepository( ManagedRepositoryContent repository ) { + project.repositoryContent = repository; + return this; + } + + @Override + public OptBuilder withAsset( StorageAsset asset ) + { + project.asset = asset; + return this; + } + + public OptBuilder withNamespace( String namespace) { + if (namespace==null) { + throw new IllegalArgumentException( "Null value not allowed for namespace" ); + } + project.namespace = namespace; + return this; + } + + public OptBuilder withAttribute( String key, String value) { + project.putAttribute( key, value ); + return this; + } + + ArchivaProject build() { + if (project.namespace==null) { + project.namespace = ""; + } + if (project.asset == null) { + project.asset = project.getRepository( ).getRepository( ).getAsset( "" ); + } + return project; } } - @Override - public boolean supports( Class clazz ) - { - return false; - } + + } diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaVersion.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaVersion.java new file mode 100644 index 000000000..194f07159 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaVersion.java @@ -0,0 +1,104 @@ +package org.apache.archiva.repository.content.base; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.repository.content.Project; +import org.apache.archiva.repository.content.Version; +import org.apache.archiva.repository.storage.StorageAsset; +import org.apache.commons.lang3.StringUtils; + +public class ArchivaVersion extends ArchivaContentItem implements Version +{ + + private String version; + private StorageAsset asset; + private Project project; + + private ArchivaVersion() { + + } + + public static ProjectBuilder withVersion(String version) { + return new Builder( ).withVersion( version ); + } + + @Override + public String getVersion( ) + { + return version; + } + + @Override + public StorageAsset getAsset( ) + { + return asset; + } + + @Override + public Project getProject( ) + { + return project; + } + + public interface ProjectBuilder { + Builder withProject( Project project ); + } + + public static final class Builder implements ProjectBuilder { + + private ArchivaVersion version = new ArchivaVersion(); + + ProjectBuilder withVersion( String version ) + { + if ( StringUtils.isEmpty( version ) ) { + throw new IllegalArgumentException( "Version parameter must not be empty or null." ); + } + this.version.version = version; + return this; + } + + + @Override + public Builder withProject( Project project ) + { + this.version.project = project; + return this; + } + + public Builder withAsset( StorageAsset asset ) + { + this.version.asset = asset; + return this; + } + + public Builder withAttribute(String key, String value) { + this.version.putAttribute( key, value ); + return this; + } + + public ArchivaVersion build() { + if (this.version.asset == null) { + this.version.project.getRepository( ).getRepository( ).getAsset( "" ); + } + return this.version; + } + } + +}