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 private final Path basePath;
36
37 private final String type;
38
39 /**
40 * Creates a new local repository with the specified base directory and unknown type.
41 *
42 * @param basedir The base directory of the repository, may be {@code null}.
43 */
44 public LocalRepository(String basedir) {
45 this.basePath = Paths.get(RepositoryUriUtils.toUri(basedir)).toAbsolutePath();
46 this.type = "";
47 }
48
49 /**
50 * Creates a new local repository with the specified base directory and unknown type.
51 *
52 * @param basedir The base directory of the repository, may be {@code null}.
53 * @since 2.0.0
54 */
55 public LocalRepository(URI basedir) {
56 this((basedir != null) ? Paths.get(basedir) : null, "");
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 * @deprecated Use {@link #LocalRepository(Path)} instead.
64 */
65 @Deprecated
66 public LocalRepository(File basedir) {
67 this(basedir, "");
68 }
69
70 /**
71 * Creates a new local repository with the specified base directory and unknown type.
72 *
73 * @param basePath The base directory of the repository, may be {@code null}.
74 * @since 2.0.0
75 */
76 public LocalRepository(Path basePath) {
77 this(basePath, "");
78 }
79
80 /**
81 * Creates a new local repository with the specified properties.
82 *
83 * @param basedir The base directory of the repository, may be {@code null}.
84 * @param type The type of the repository, may be {@code null}.
85 * @deprecated Use {@link #LocalRepository(Path, String)} instead.
86 */
87 @Deprecated
88 public LocalRepository(File basedir, String type) {
89 this(basedir != null ? basedir.toPath() : null, type);
90 }
91
92 /**
93 * Creates a new local repository with the specified properties.
94 *
95 * @param basePath The base directory of the repository, may be {@code null}.
96 * @param type The type of the repository, may be {@code null}.
97 * @since 2.0.0
98 */
99 public LocalRepository(Path basePath, String type) {
100 this.basePath = basePath;
101 this.type = (type != null) ? type : "";
102 }
103
104 @Override
105 public String getContentType() {
106 return type;
107 }
108
109 @Override
110 public String getId() {
111 return "local";
112 }
113
114 /**
115 * Gets the base directory of the repository.
116 *
117 * @return The base directory or {@code null} if none.
118 * @deprecated Use {@link #getBasePath()} instead.
119 */
120 @Deprecated
121 public File getBasedir() {
122 return basePath != null ? basePath.toFile() : null;
123 }
124
125 /**
126 * Gets the base directory of the repository.
127 *
128 * @return The base directory or {@code null} if none.
129 * @since 2.0.0
130 */
131 public Path getBasePath() {
132 return basePath;
133 }
134
135 @Override
136 public String toString() {
137 return getBasePath() + " (" + getContentType() + ")";
138 }
139
140 @Override
141 public boolean equals(Object obj) {
142 if (this == obj) {
143 return true;
144 }
145 if (obj == null || !getClass().equals(obj.getClass())) {
146 return false;
147 }
148
149 LocalRepository that = (LocalRepository) obj;
150
151 return Objects.equals(basePath, that.basePath) && Objects.equals(type, that.type);
152 }
153
154 @Override
155 public int hashCode() {
156 int hash = 17;
157 hash = hash * 31 + hash(basePath);
158 hash = hash * 31 + hash(type);
159 return hash;
160 }
161
162 private static int hash(Object obj) {
163 return obj != null ? obj.hashCode() : 0;
164 }
165 }