首页

基于aether-api关于artifact基础构件相关的源码细节设计实现原理分析说明

标签:aether,构件设计,系统设计,sonatype,artifact,aether,源码     发布时间:2017-12-06   

一、前言

这边基于aether-apiaether-util包分别对应定义基础构件的主体(Artifact)、构件类型(ArtifactType)及注入调用方式(ArtifactTypeRegistry)定义了标准接口,并分别在util包的定义默认实现类,有org.sonatype.aether.util.artifact.DefaultArtifact、org.sonatype.aether.util.artifact.DefaultArtifactType及org.sonatype.aether.util.artifact.DefaultArtifactTypeRegistry。

二、接口源码

1.org.sonatype.aether.artifact.Artifact主体构件接口

package org.sonatype.aether.artifact;@b@@b@import java.io.File;@b@import java.util.Map;@b@@b@public abstract interface Artifact@b@{@b@  public abstract String getGroupId();@b@@b@  public abstract String getArtifactId();@b@@b@  public abstract String getVersion();@b@@b@  public abstract Artifact setVersion(String paramString);@b@@b@  public abstract String getBaseVersion();@b@@b@  public abstract boolean isSnapshot();@b@@b@  public abstract String getClassifier();@b@@b@  public abstract String getExtension();@b@@b@  public abstract File getFile();@b@@b@  public abstract Artifact setFile(File paramFile);@b@@b@  public abstract String getProperty(String paramString1, String paramString2);@b@@b@  public abstract Map<String, String> getProperties();@b@@b@  public abstract Artifact setProperties(Map<String, String> paramMap);@b@}

2.org.sonatype.aether.artifact.ArtifactType构件类别接口

package org.sonatype.aether.artifact;@b@@b@import java.util.Map;@b@@b@public abstract interface ArtifactType@b@{@b@  public abstract String getId();@b@@b@  public abstract String getExtension();@b@@b@  public abstract String getClassifier();@b@@b@  public abstract Map<String, String> getProperties();@b@}

3.org.sonatype.aether.artifact.ArtifactTypeRegistry构件类型注入

package org.sonatype.aether.artifact;@b@@b@public abstract interface ArtifactTypeRegistry@b@{@b@  public abstract ArtifactType get(String paramString);@b@}

三、默认源码实现类

1.org.sonatype.aether.util.artifact.DefaultArtifact默认构件主体实现类

package org.sonatype.aether.util.artifact;@b@@b@import java.io.File;@b@import java.util.Collections;@b@import java.util.HashMap;@b@import java.util.Map;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@import org.sonatype.aether.artifact.Artifact;@b@import org.sonatype.aether.artifact.ArtifactType;@b@@b@public final class DefaultArtifact extends AbstractArtifact@b@{@b@  private final String groupId;@b@  private final String artifactId;@b@  private final String version;@b@  private final String classifier;@b@  private final String extension;@b@  private final File file;@b@  private final Map<String, String> properties;@b@  private String baseVersion;@b@@b@  public DefaultArtifact(String coords)@b@  {@b@    this(coords, Collections.emptyMap());@b@  }@b@@b@  public DefaultArtifact(String coords, Map<String, String> properties)@b@  {@b@    Pattern p = Pattern.compile("([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?:([^: ]+)");@b@    Matcher m = p.matcher(coords);@b@    if (!(m.matches()))@b@    {@b@      throw new IllegalArgumentException("Bad artifact coordinates " + coords + ", expected format is <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>");@b@    }@b@@b@    this.groupId = m.group(1);@b@    this.artifactId = m.group(2);@b@    this.extension = get(m.group(4), "jar");@b@    this.classifier = get(m.group(6), "");@b@    this.version = m.group(7);@b@    this.file = null;@b@    if ((properties != null) && (!(properties.isEmpty())))@b@    {@b@      this.properties = new HashMap(properties);@b@    }@b@    else@b@    {@b@      this.properties = Collections.emptyMap();@b@    }@b@  }@b@@b@  private static String get(String value, String defaultValue)@b@  {@b@    return (((value == null) || (value.length() <= 0)) ? defaultValue : value);@b@  }@b@@b@  public DefaultArtifact(String groupId, String artifactId, String extension, String version)@b@  {@b@    this(groupId, artifactId, "", extension, version);@b@  }@b@@b@  public DefaultArtifact(String groupId, String artifactId, String classifier, String extension, String version)@b@  {@b@    this(groupId, artifactId, classifier, extension, version, null, (File)null);@b@  }@b@@b@  public DefaultArtifact(String groupId, String artifactId, String classifier, String extension, String version, ArtifactType type)@b@  {@b@    this(groupId, artifactId, classifier, extension, version, null, type);@b@  }@b@@b@  public DefaultArtifact(String groupId, String artifactId, String classifier, String extension, String version, Map<String, String> properties, ArtifactType type)@b@  {@b@    this.groupId = emptify(groupId);@b@    this.artifactId = emptify(artifactId);@b@    if ((classifier != null) || (type == null))@b@    {@b@      this.classifier = emptify(classifier);@b@    }@b@    else@b@    {@b@      this.classifier = emptify(type.getClassifier());@b@    }@b@    if ((extension != null) || (type == null))@b@    {@b@      this.extension = emptify(extension);@b@    }@b@    else@b@    {@b@      this.extension = emptify(type.getExtension());@b@    }@b@    this.version = emptify(version);@b@    this.file = null;@b@    this.properties = merge(properties, (type != null) ? type.getProperties() : null);@b@  }@b@@b@  private static Map<String, String> merge(Map<String, String> dominant, Map<String, String> recessive)@b@  {@b@    Map properties;@b@    if ((((dominant == null) || (dominant.isEmpty()))) && (((recessive == null) || (recessive.isEmpty()))))@b@    {@b@      properties = Collections.emptyMap();@b@    }@b@    else@b@    {@b@      properties = new HashMap();@b@      if (recessive != null)@b@      {@b@        properties.putAll(recessive);@b@      }@b@      if (dominant != null)@b@      {@b@        properties.putAll(dominant);@b@      }@b@    }@b@@b@    return properties;@b@  }@b@@b@  public DefaultArtifact(String groupId, String artifactId, String classifier, String extension, String version, Map<String, String> properties, File file)@b@  {@b@    this.groupId = emptify(groupId);@b@    this.artifactId = emptify(artifactId);@b@    this.classifier = emptify(classifier);@b@    this.extension = emptify(extension);@b@    this.version = emptify(version);@b@    this.file = file;@b@    if ((properties != null) && (!(properties.isEmpty())))@b@    {@b@      this.properties = new HashMap(properties);@b@    }@b@    else@b@    {@b@      this.properties = Collections.emptyMap();@b@    }@b@  }@b@@b@  DefaultArtifact(String groupId, String artifactId, String classifier, String extension, String version, File file, Map<String, String> properties)@b@  {@b@    this.groupId = emptify(groupId);@b@    this.artifactId = emptify(artifactId);@b@    this.classifier = emptify(classifier);@b@    this.extension = emptify(extension);@b@    this.version = emptify(version);@b@    this.file = file;@b@    this.properties = properties;@b@  }@b@@b@  private static String emptify(String str)@b@  {@b@    return ((str == null) ? "" : str);@b@  }@b@@b@  public String getGroupId()@b@  {@b@    return this.groupId;@b@  }@b@@b@  public String getArtifactId()@b@  {@b@    return this.artifactId;@b@  }@b@@b@  public String getBaseVersion()@b@  {@b@    if (this.baseVersion == null)@b@    {@b@      this.baseVersion = toBaseVersion(getVersion());@b@    }@b@    return this.baseVersion;@b@  }@b@@b@  public String getVersion()@b@  {@b@    return this.version;@b@  }@b@@b@  public Artifact setVersion(String version)@b@  {@b@    if ((this.version.equals(version)) || ((version == null) && (this.version.length() <= 0)))@b@    {@b@      return this;@b@    }@b@    return new DefaultArtifact(this.groupId, this.artifactId, this.classifier, this.extension, version, this.file, this.properties);@b@  }@b@@b@  public boolean isSnapshot()@b@  {@b@    return isSnapshot(getVersion());@b@  }@b@@b@  public String getClassifier()@b@  {@b@    return this.classifier;@b@  }@b@@b@  public String getExtension()@b@  {@b@    return this.extension;@b@  }@b@@b@  public File getFile()@b@  {@b@    return this.file;@b@  }@b@@b@  public Artifact setFile(File file)@b@  {@b@    if (this.file == null) if (file != null) break label27; @b@    else if (!(this.file.equals(file)))@b@        break label27;@b@    return this;@b@@b@    label27: return new DefaultArtifact(this.groupId, this.artifactId, this.classifier, this.extension, this.version, file, this.properties);@b@  }@b@@b@  public String getProperty(String key, String defaultValue)@b@  {@b@    String value = (String)this.properties.get(key);@b@    return ((value != null) ? value : defaultValue);@b@  }@b@@b@  public Map<String, String> getProperties()@b@  {@b@    return Collections.unmodifiableMap(this.properties);@b@  }@b@@b@  public Artifact setProperties(Map<String, String> properties)@b@  {@b@    if ((this.properties.equals(properties)) || ((properties == null) && (this.properties.isEmpty())))@b@    {@b@      return this;@b@    }@b@    return new DefaultArtifact(this.groupId, this.artifactId, this.classifier, this.extension, this.version, properties, this.file);@b@  }@b@}
package org.sonatype.aether.util.artifact;@b@@b@import java.io.File;@b@import java.util.Map;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@import org.sonatype.aether.artifact.Artifact;@b@@b@public abstract class AbstractArtifact@b@  implements Artifact@b@{@b@  private static final String SNAPSHOT = "SNAPSHOT";@b@  private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile("^(.*-)?([0-9]{8}.[0-9]{6}-[0-9]+)$");@b@@b@  protected static boolean isSnapshot(String version)@b@  {@b@    return ((version.endsWith("SNAPSHOT")) || (SNAPSHOT_TIMESTAMP.matcher(version).matches()));@b@  }@b@@b@  protected static String toBaseVersion(String version)@b@  {@b@    String baseVersion;@b@    if (version == null)@b@    {@b@      baseVersion = version;@b@    }@b@    else if ((version.startsWith("[")) || (version.startsWith("(")))@b@    {@b@      baseVersion = version;@b@    }@b@    else@b@    {@b@      Matcher m = SNAPSHOT_TIMESTAMP.matcher(version);@b@      if (m.matches())@b@      {@b@        if (m.group(1) != null)@b@        {@b@          baseVersion = m.group(1) + "SNAPSHOT";@b@        }@b@        else@b@        {@b@          baseVersion = "SNAPSHOT";@b@        }@b@@b@      }@b@      else@b@        baseVersion = version;@b@@b@    }@b@@b@    return baseVersion;@b@  }@b@@b@  public Artifact setVersion(String version)@b@  {@b@    if (getVersion().equals(version))@b@    {@b@      return this;@b@    }@b@    return new DefaultArtifact(getGroupId(), getArtifactId(), getClassifier(), getExtension(), version, getFile(), getProperties());@b@  }@b@@b@  public Artifact setFile(File file)@b@  {@b@    if (eq(getFile(), file))@b@    {@b@      return this;@b@    }@b@    return new DefaultArtifact(getGroupId(), getArtifactId(), getClassifier(), getExtension(), getVersion(), file, getProperties());@b@  }@b@@b@  public Artifact setProperties(Map<String, String> properties)@b@  {@b@    if (getProperties().equals(properties))@b@    {@b@      return this;@b@    }@b@    return new DefaultArtifact(getGroupId(), getArtifactId(), getClassifier(), getExtension(), getVersion(), properties, getFile());@b@  }@b@@b@  public String toString()@b@  {@b@    StringBuilder buffer = new StringBuilder(128);@b@    buffer.append(getGroupId());@b@    buffer.append(':').append(getArtifactId());@b@    buffer.append(':').append(getExtension());@b@    if (getClassifier().length() > 0)@b@    {@b@      buffer.append(':').append(getClassifier());@b@    }@b@    buffer.append(':').append(getVersion());@b@    return buffer.toString();@b@  }@b@@b@  public boolean equals(Object obj)@b@  {@b@    if (obj == this)@b@    {@b@      return true;@b@    }@b@    if (!(obj instanceof Artifact))@b@    {@b@      return false;@b@    }@b@@b@    Artifact that = (Artifact)obj;@b@@b@    return ((getArtifactId().equals(that.getArtifactId())) && (getGroupId().equals(that.getGroupId())) && (getVersion().equals(that.getVersion())) && (getExtension().equals(that.getExtension())) && (getClassifier().equals(that.getClassifier())) && (eq(getFile(), that.getFile())) && (getProperties().equals(that.getProperties())));@b@  }@b@@b@  private static <T> boolean eq(T s1, T s2)@b@  {@b@    return ((s2 == null) ? true : (s1 != null) ? s1.equals(s2) : false);@b@  }@b@@b@  public int hashCode()@b@  {@b@    int hash = 17;@b@    hash = hash * 31 + getGroupId().hashCode();@b@    hash = hash * 31 + getArtifactId().hashCode();@b@    hash = hash * 31 + getExtension().hashCode();@b@    hash = hash * 31 + getClassifier().hashCode();@b@    hash = hash * 31 + getVersion().hashCode();@b@    hash = hash * 31 + getProperties().hashCode();@b@    hash = hash * 31 + hash(getFile());@b@    return hash;@b@  }@b@@b@  private static int hash(Object obj)@b@  {@b@    return ((obj != null) ? obj.hashCode() : 0);@b@  }@b@}
package org.sonatype.aether.util.artifact;@b@@b@import java.io.File;@b@import java.util.Map;@b@import org.sonatype.aether.artifact.Artifact;@b@@b@public final class SubArtifact extends AbstractArtifact@b@{@b@  private final Artifact mainArtifact;@b@  private final String classifier;@b@  private final String extension;@b@  private final File file;@b@@b@  public SubArtifact(Artifact mainArtifact, String classifier, String extension)@b@  {@b@    this(mainArtifact, classifier, extension, null);@b@  }@b@@b@  public SubArtifact(Artifact mainArtifact, String classifier, String extension, File file)@b@  {@b@    if (mainArtifact == null)@b@    {@b@      throw new IllegalArgumentException("no artifact specified");@b@    }@b@    this.mainArtifact = mainArtifact;@b@    this.classifier = classifier;@b@    this.extension = extension;@b@    this.file = file;@b@  }@b@@b@  public String getGroupId()@b@  {@b@    return this.mainArtifact.getGroupId();@b@  }@b@@b@  public String getArtifactId()@b@  {@b@    return this.mainArtifact.getArtifactId();@b@  }@b@@b@  public String getVersion()@b@  {@b@    return this.mainArtifact.getVersion();@b@  }@b@@b@  public Artifact setVersion(String version)@b@  {@b@    return new DefaultArtifact(getGroupId(), getArtifactId(), getClassifier(), getExtension(), version, getFile(), getProperties());@b@  }@b@@b@  public String getBaseVersion()@b@  {@b@    return this.mainArtifact.getBaseVersion();@b@  }@b@@b@  public boolean isSnapshot()@b@  {@b@    return this.mainArtifact.isSnapshot();@b@  }@b@@b@  public String getClassifier()@b@  {@b@    return expand(this.classifier, this.mainArtifact.getClassifier());@b@  }@b@@b@  public String getExtension()@b@  {@b@    return expand(this.extension, this.mainArtifact.getExtension());@b@  }@b@@b@  public File getFile()@b@  {@b@    return this.file;@b@  }@b@@b@  public Artifact setFile(File file)@b@  {@b@    if (this.file == null) if (file != null) break label27; @b@    else if (!(this.file.equals(file)))@b@        break label27;@b@    return this;@b@@b@    label27: return new SubArtifact(this.mainArtifact, this.classifier, this.extension, file);@b@  }@b@@b@  public String getProperty(String key, String defaultValue)@b@  {@b@    return this.mainArtifact.getProperty(key, defaultValue);@b@  }@b@@b@  public Map<String, String> getProperties()@b@  {@b@    return this.mainArtifact.getProperties();@b@  }@b@@b@  private static String expand(String pattern, String replacement)@b@  {@b@    String result = "";@b@    if (pattern != null)@b@    {@b@      result = pattern.replace("*", replacement);@b@@b@      if (replacement.length() <= 0)@b@      {@b@        int i;@b@        char c;@b@        if (pattern.startsWith("*"))@b@        {@b@          i = 0;@b@          for (; i < result.length(); ++i)@b@          {@b@            c = result.charAt(i);@b@            if ((c != '-') && (c != '.'))@b@            {@b@              break;@b@            }@b@          }@b@          result = result.substring(i);@b@        }@b@        if (pattern.endsWith("*"))@b@        {@b@          i = result.length() - 1;@b@          for (; i >= 0; --i)@b@          {@b@            c = result.charAt(i);@b@            if ((c != '-') && (c != '.'))@b@            {@b@              break;@b@            }@b@          }@b@          result = result.substring(0, i + 1);@b@        }@b@      }@b@    }@b@    return result;@b@  }@b@}

2.org.sonatype.aether.util.artifact.DefaultArtifactType默认构件类别

package org.sonatype.aether.util.artifact;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@import org.sonatype.aether.artifact.ArtifactType;@b@@b@public class DefaultArtifactType@b@  implements ArtifactType@b@{@b@  private final String id;@b@  private final String extension;@b@  private final String classifier;@b@  private final Map<String, String> properties;@b@@b@  public DefaultArtifactType(String id)@b@  {@b@    this(id, id, "", "none", false, false);@b@  }@b@@b@  public DefaultArtifactType(String id, String extension, String classifier, String language)@b@  {@b@    this(id, extension, classifier, language, true, false);@b@  }@b@@b@  public DefaultArtifactType(String id, String extension, String classifier, String language, boolean constitutesBuildPath, boolean includesDependencies)@b@  {@b@    if ((id == null) || (id.length() < 0))@b@    {@b@      throw new IllegalArgumentException("no type id specified");@b@    }@b@    this.id = id;@b@    this.extension = (((extension != null) && (extension.length() > 0)) ? extension : id);@b@    this.classifier = ((classifier != null) ? classifier : "");@b@    Map props = new HashMap();@b@    props.put("type", id);@b@    props.put("language", ((language != null) && (language.length() > 0)) ? language : "none");@b@    props.put("includesDependencies", Boolean.toString(includesDependencies));@b@    props.put("constitutesBuildPath", Boolean.toString(constitutesBuildPath));@b@    this.properties = props;@b@  }@b@@b@  public String getId()@b@  {@b@    return this.id;@b@  }@b@@b@  public String getExtension()@b@  {@b@    return this.extension;@b@  }@b@@b@  public String getClassifier()@b@  {@b@    return this.classifier;@b@  }@b@@b@  public Map<String, String> getProperties()@b@  {@b@    return this.properties;@b@  }@b@}

3.org.sonatype.aether.util.artifact.DefaultArtifactTypeRegistry构件类型注入实现

package org.sonatype.aether.util.artifact;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@import org.sonatype.aether.artifact.ArtifactType;@b@import org.sonatype.aether.artifact.ArtifactTypeRegistry;@b@@b@public class DefaultArtifactTypeRegistry@b@  implements ArtifactTypeRegistry@b@{@b@  private final Map<String, ArtifactType> stereotypes;@b@@b@  public DefaultArtifactTypeRegistry()@b@  {@b@    this.stereotypes = new HashMap();@b@  }@b@@b@  public DefaultArtifactTypeRegistry add(ArtifactType stereotype)@b@  {@b@    this.stereotypes.put(stereotype.getId(), stereotype);@b@    return this;@b@  }@b@@b@  public ArtifactType get(String stereotypeId)@b@  {@b@    ArtifactType stereotype = (ArtifactType)this.stereotypes.get(stereotypeId);@b@@b@    return stereotype;@b@  }@b@}