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