001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.repository; 020 021import java.io.File; 022import java.net.URI; 023import java.nio.file.Path; 024import java.nio.file.Paths; 025import java.util.Objects; 026 027/** 028 * A repository on the local file system used to cache contents of remote repositories and to store locally installed 029 * artifacts. Note that this class merely describes such a repository, actual access to the contained artifacts is 030 * handled by a {@link LocalRepositoryManager} which is usually determined from the {@link #getContentType() type} of 031 * the repository. 032 */ 033public final class LocalRepository implements ArtifactRepository { 034 /** 035 * Repository key constant representing the local repository, used in place of a remote repository key where 036 * none applies, for example when composing local repository paths for locally installed metadata. 037 */ 038 public static final String ID = "local"; 039 040 private final Path basePath; 041 042 private final String type; 043 044 private final int hashCode; 045 046 /** 047 * Creates a new local repository with the specified base directory and unknown type. 048 * 049 * @param basedir The base directory of the repository, may be {@code null}. 050 * @deprecated Use {@link LocalRepository(Path)} instead. 051 */ 052 @Deprecated 053 public LocalRepository(String basedir) { 054 this.basePath = Paths.get(RepositoryUriUtils.toUri(basedir)).toAbsolutePath(); 055 this.type = ""; 056 this.hashCode = Objects.hash(this.basePath, this.type); 057 } 058 059 /** 060 * Creates a new local repository with the specified base directory and unknown type. 061 * 062 * @param basedir The base directory of the repository, may be {@code null}. 063 * @since 2.0.0 064 */ 065 public LocalRepository(URI basedir) { 066 this((basedir != null) ? Paths.get(basedir) : null, ""); 067 } 068 069 /** 070 * Creates a new local repository with the specified base directory and unknown type. 071 * 072 * @param basedir The base directory of the repository, may be {@code null}. 073 * @deprecated Use {@link #LocalRepository(Path)} instead. 074 */ 075 @Deprecated 076 public LocalRepository(File basedir) { 077 this(basedir, ""); 078 } 079 080 /** 081 * Creates a new local repository with the specified base directory and unknown type. 082 * 083 * @param basePath The base directory of the repository, may be {@code null}. 084 * @since 2.0.0 085 */ 086 public LocalRepository(Path basePath) { 087 this(basePath, ""); 088 } 089 090 /** 091 * Creates a new local repository with the specified properties. 092 * 093 * @param basedir The base directory of the repository, may be {@code null}. 094 * @param type The type of the repository, may be {@code null}. 095 * @deprecated Use {@link #LocalRepository(Path, String)} instead. 096 */ 097 @Deprecated 098 public LocalRepository(File basedir, String type) { 099 this(basedir != null ? basedir.toPath() : null, type); 100 } 101 102 /** 103 * Creates a new local repository with the specified properties. 104 * 105 * @param basePath The base directory of the repository, may be {@code null}. 106 * @param type The type of the repository, may be {@code null}. 107 * @since 2.0.0 108 */ 109 public LocalRepository(Path basePath, String type) { 110 this.basePath = basePath; 111 this.type = (type != null) ? type : ""; 112 this.hashCode = Objects.hash(this.basePath, this.type); 113 } 114 115 @Override 116 public String getContentType() { 117 return type; 118 } 119 120 @Override 121 public String getId() { 122 return ID; 123 } 124 125 /** 126 * Gets the base directory of the repository. 127 * 128 * @return The base directory or {@code null} if none. 129 * @deprecated Use {@link #getBasePath()} instead. 130 */ 131 @Deprecated 132 public File getBasedir() { 133 return basePath != null ? basePath.toFile() : null; 134 } 135 136 /** 137 * Gets the base directory of the repository. 138 * 139 * @return The base directory or {@code null} if none. 140 * @since 2.0.0 141 */ 142 public Path getBasePath() { 143 return basePath; 144 } 145 146 @Override 147 public String toString() { 148 return getBasePath() + " (" + getContentType() + ")"; 149 } 150 151 @Override 152 public boolean equals(Object obj) { 153 if (this == obj) { 154 return true; 155 } 156 if (obj == null || !getClass().equals(obj.getClass())) { 157 return false; 158 } 159 160 LocalRepository that = (LocalRepository) obj; 161 162 return Objects.equals(basePath, that.basePath) && Objects.equals(type, that.type); 163 } 164 165 @Override 166 public int hashCode() { 167 return hashCode; 168 } 169}