Adding content item implementations

This commit is contained in:
Martin Stockhammer 2020-02-12 18:41:39 +01:00
parent 15187ea422
commit f75938cf85
3 changed files with 208 additions and 28 deletions

View File

@ -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<String, String> attributes;
@Override
public abstract <T extends Project> T adapt( Class<T> clazz ) throws UnsupportedConversionException;
public <T extends Project> T adapt( Class<T> clazz ) {
return (T)this;
}
@Override
public abstract <T extends Project> boolean supports( Class<T> clazz );
public <T extends Project> boolean supports( Class<T> clazz ) {
return clazz != null && clazz.isAssignableFrom( this.getClass() );
}
/**

View File

@ -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<String, String> 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 <code>null</code>
* @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<String, String> getAttributes( )
public StorageAsset getAsset( )
{
return null;
return asset;
}
@Override
public <T extends Project> T adapt( Class<T> 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 <T extends Project> boolean supports( Class<T> clazz )
{
return false;
}
}

View File

@ -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;
}
}
}