From: Martin Stockhammer Date: Wed, 12 Feb 2020 07:41:22 +0000 (+0100) Subject: Updating content item api X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=15187ea422859c2f660f03320fc8b93911a8eef9;p=archiva.git Updating content item api --- diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/UnsupportedConversionException.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/UnsupportedConversionException.java new file mode 100644 index 000000000..f675ea0f0 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/UnsupportedConversionException.java @@ -0,0 +1,45 @@ +package org.apache.archiva.repository; + +/* + * 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. + */ + +/** + * Thrown, if conversion to a specific class is not possible. + */ +public class UnsupportedConversionException extends RepositoryException +{ + public UnsupportedConversionException( ) + { + } + + public UnsupportedConversionException( String message, Throwable cause ) + { + super( message, cause ); + } + + public UnsupportedConversionException( String message ) + { + super( message ); + } + + public UnsupportedConversionException( Throwable cause ) + { + super( cause ); + } +} diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Artifact.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Artifact.java index b2ae2183c..cd1f2e24f 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Artifact.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Artifact.java @@ -39,7 +39,7 @@ import java.util.Map; * * @author Martin Stockhammer */ -public interface Artifact extends Comparable +public interface Artifact extends ContentItem { /** * The namespace is the location of the artifact. @@ -146,30 +146,5 @@ public interface Artifact extends Comparable */ StorageAsset getAsset(); - /** - * Returns additional attributes of the artifact. Implementations may add additional attributes, e.g. author name - * or creation date. Specific implementation attributes can be accessed either by the {@link #getAttributes()} method - * or by a specific implementation class (see {@link #adapt(Class)}. Use via the adapter is type safe. - * - * @return a map of attribute key, value pairs - */ - Map getAttributes(); - - - /** - * Returns the repository type specific implementation - * @param clazz the specific implementation class - * @param the class or interface - * @return the specific project implementation - */ - T adapt(Class clazz) throws UnsupportedRepositoryTypeException; - - /** - * Returns true, if this project supports the given adaptor class. - * @param clazz the class to convert this project to - * @param the type - * @return true/code>, if the implementation is supported, otherwise false - */ - boolean supports(Class clazz); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/ContentItem.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/ContentItem.java new file mode 100644 index 000000000..ec9fca6d3 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/ContentItem.java @@ -0,0 +1,58 @@ +package org.apache.archiva.repository.content; + +/* + * 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.UnsupportedConversionException; + +import java.util.Map; + +public interface ContentItem +{ + /** + * Returns the repository type specific implementation + * @param clazz the specific implementation class + * @param the class or interface + * @return the specific project implementation + */ + T adapt( Class clazz ) throws UnsupportedConversionException; + + /** + * Returns true, if this project supports the given adaptor class. + * @param clazz the class to convert this project to + * @param the type + * @return true/code>, if the implementation is supported, otherwise false + */ + boolean supports( Class clazz ); + + /** + * Additional attributes + * @return the additional attributes + */ + Map getAttributes( ); + + /** + * Returns the attribute value for the given key. + * + * @param key the attribute key + * @return the value, if the key exists, otherwise null + * + */ + String getAttribute( String key ); +} diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Project.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Project.java index d52339d8f..2ad2c88db 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Project.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Project.java @@ -20,6 +20,7 @@ package org.apache.archiva.repository.content; */ import org.apache.archiva.repository.RepositoryContent; +import org.apache.archiva.repository.UnsupportedConversionException; import org.apache.archiva.repository.UnsupportedRepositoryTypeException; import java.util.Map; @@ -40,7 +41,7 @@ import java.util.Map; * * @author Martin Stockhammer */ -public interface Project extends Comparable +public interface Project extends ContentItem { /** @@ -61,25 +62,4 @@ public interface Project extends Comparable */ RepositoryContent getRepository(); - /** - * Additional attributes - * @return the additional attributes - */ - Map getAttributes(); - - /** - * Returns the repository type specific implementation - * @param clazz the specific implementation class - * @param the class or interface - * @return the specific project implementation - */ - T adapt(Class clazz) throws UnsupportedRepositoryTypeException; - - /** - * Returns true, if this project supports the given adaptor class. - * @param clazz the class to convert this project to - * @param the type - * @return true/code>, if the implementation is supported, otherwise false - */ - boolean supports(Class clazz); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Version.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Version.java index 6c17dee75..011493946 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Version.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/content/Version.java @@ -32,7 +32,7 @@ import java.util.Map; * * @author Martin Stockhammer */ -public interface Version extends Comparable +public interface Version extends ContentItem { /** * Returns the version string. @@ -55,26 +55,5 @@ public interface Version extends Comparable */ Project getProject(); - /** - * Returns additional attributes. - * @return the map of attribute key, value pairs - */ - Map getAttributes(); - - /** - * Returns the repository type specific implementation - * @param clazz the specific implementation class - * @param the class or interface - * @return the specific project implementation - */ - T adapt(Class clazz) throws UnsupportedRepositoryTypeException; - - /** - * Returns true, if this project supports the given adaptor class. - * @param clazz the class to convert this project to - * @param the type - * @return true/code>, if the implementation is supported, otherwise false - */ - boolean supports(Class clazz); } 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 new file mode 100644 index 000000000..b538bb13b --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaContentItem.java @@ -0,0 +1,93 @@ +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.UnsupportedConversionException; +import org.apache.archiva.repository.content.ContentItem; +import org.apache.archiva.repository.content.Project; +import org.apache.commons.lang3.StringUtils; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * Abstract implementation of ContentItem interface. + * + * The attribute map is created, when the first values are put to the map. + * + */ +public abstract class ArchivaContentItem implements ContentItem +{ + + private Map attributes; + + @Override + public abstract T adapt( Class clazz ) throws UnsupportedConversionException; + + @Override + public abstract boolean supports( Class clazz ); + + + /** + * Does lazy initialization of the attributes map. + * Returns a unmodifiable map. + * + * @return unmodifiable map of attributes + */ + @Override + public Map getAttributes( ) + { + if (this.attributes==null) { + return Collections.emptyMap( ); + } else + { + return Collections.unmodifiableMap( this.attributes ); + } + } + + /** + * Adds a attribute value. The key must not be null. + * + * @param key the attribute key + * @param value the attribute value + * @throws IllegalArgumentException if the key is null or empty + */ + public void putAttribute(String key, String value) throws IllegalArgumentException { + if (this.attributes==null) { + this.attributes = new HashMap<>( ); + } + if ( StringUtils.isEmpty( key ) ) { + throw new IllegalArgumentException( "Key value must not be empty or null" ); + } + this.attributes.put( key, value ); + } + + @Override + public String getAttribute( String key ) + { + if (this.attributes==null) { + return null; + } else + { + return this.attributes.get( key ); + } + } +} 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 new file mode 100644 index 000000000..b2879b6dc --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/content/base/ArchivaProject.java @@ -0,0 +1,78 @@ +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.RepositoryContent; +import org.apache.archiva.repository.UnsupportedConversionException; +import org.apache.archiva.repository.content.Project; + +import java.util.Map; + +/** + * Immutable class, that represents a project. + */ +public class ArchivaProject extends ArchivaContentItem implements Project +{ + String namespace; + String id; + RepositoryContent repositoryContent; + Map attributes; + + + @Override + public String getNamespace( ) + { + return null; + } + + @Override + public String getId( ) + { + return null; + } + + @Override + public RepositoryContent getRepository( ) + { + return null; + } + + @Override + public Map getAttributes( ) + { + return null; + } + + @Override + public T adapt( Class clazz ) throws UnsupportedConversionException + { + if (clazz != ArchivaProject.class) { + throw new UnsupportedConversionException( "Cannot convert to class: " + clazz ); + } else { + return (T) this; + } + } + + @Override + public boolean supports( Class clazz ) + { + return false; + } +}