1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.repository;
20
21 import java.io.File;
22 import java.net.URI;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.util.Objects;
26
27 /**
28 * A repository on the local file system used to cache contents of remote repositories and to store locally installed
29 * artifacts. Note that this class merely describes such a repository, actual access to the contained artifacts is
30 * handled by a {@link LocalRepositoryManager} which is usually determined from the {@link #getContentType() type} of
31 * the repository.
32 */
33 public final class LocalRepository implements ArtifactRepository {
34 /**
35 * Repository key constant representing the local repository, used in place of a remote repository key where
36 * none applies, for example when composing local repository paths for locally installed metadata.
37 */
38 public static final String ID = "local";
39
40 private final Path basePath;
41
42 private final String type;
43
44 private final int hashCode;
45
46 /**
47 * Creates a new local repository with the specified base directory and unknown type.
48 *
49 * @param basedir The base directory of the repository, may be {@code null}.
50 * @deprecated Use {@link LocalRepository(Path)} instead.
51 */
52 @Deprecated
53 public LocalRepository(String basedir) {
54 this.basePath = Paths.get(RepositoryUriUtils.toUri(basedir)).toAbsolutePath();
55 this.type = "";
56 this.hashCode = Objects.hash(this.basePath, this.type);
57 }
58
59 /**
60 * Creates a new local repository with the specified base directory and unknown type.
61 *
62 * @param basedir The base directory of the repository, may be {@code null}.
63 * @since 2.0.0
64 */
65 public LocalRepository(URI basedir) {
66 this((basedir != null) ? Paths.get(basedir) : null, "");
67 }
68
69 /**
70 * Creates a new local repository with the specified base directory and unknown type.
71 *
72 * @param basedir The base directory of the repository, may be {@code null}.
73 * @deprecated Use {@link #LocalRepository(Path)} instead.
74 */
75 @Deprecated
76 public LocalRepository(File basedir) {
77 this(basedir, "");
78 }
79
80 /**
81 * Creates a new local repository with the specified base directory and unknown type.
82 *
83 * @param basePath The base directory of the repository, may be {@code null}.
84 * @since 2.0.0
85 */
86 public LocalRepository(Path basePath) {
87 this(basePath, "");
88 }
89
90 /**
91 * Creates a new local repository with the specified properties.
92 *
93 * @param basedir The base directory of the repository, may be {@code null}.
94 * @param type The type of the repository, may be {@code null}.
95 * @deprecated Use {@link #LocalRepository(Path, String)} instead.
96 */
97 @Deprecated
98 public LocalRepository(File basedir, String type) {
99 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 }