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.apache.maven.repository.internal;
20  
21  import java.util.Map;
22  import java.util.Objects;
23  import java.util.concurrent.ConcurrentHashMap;
24  
25  import org.apache.maven.building.Source;
26  import org.apache.maven.model.building.ModelCache;
27  import org.eclipse.aether.RepositorySystemSession;
28  
29  /**
30   * A model builder cache backed by the repository system cache.
31   *
32   * @author Benjamin Bentmann
33   */
34  public class DefaultModelCache implements ModelCache {
35  
36      private static final String KEY = DefaultModelCache.class.getName();
37  
38      private final Map<Object, Object> cache;
39  
40      public static ModelCache newInstance(RepositorySystemSession session) {
41          Map<Object, Object> cache;
42          if (session.getCache() == null) {
43              cache = new ConcurrentHashMap<>();
44          } else {
45              cache = (Map) session.getCache().get(session, KEY);
46              if (cache == null) {
47                  cache = new ConcurrentHashMap<>();
48                  session.getCache().put(session, KEY, cache);
49              }
50          }
51          return new DefaultModelCache(cache);
52      }
53  
54      private DefaultModelCache(Map<Object, Object> cache) {
55          this.cache = cache;
56      }
57  
58      public Object get(Source path, String tag) {
59          return get(new SourceCacheKey(path, tag));
60      }
61  
62      public void put(Source path, String tag, Object data) {
63          put(new SourceCacheKey(path, tag), data);
64      }
65  
66      public Object get(String groupId, String artifactId, String version, String tag) {
67          return get(new GavCacheKey(groupId, artifactId, version, tag));
68      }
69  
70      public void put(String groupId, String artifactId, String version, String tag, Object data) {
71          put(new GavCacheKey(groupId, artifactId, version, tag), data);
72      }
73  
74      protected Object get(Object key) {
75          return cache.get(key);
76      }
77  
78      protected void put(Object key, Object data) {
79          cache.put(key, data);
80      }
81  
82      static class GavCacheKey {
83  
84          private final String gav;
85  
86          private final String tag;
87  
88          private final int hash;
89  
90          GavCacheKey(String groupId, String artifactId, String version, String tag) {
91              this(gav(groupId, artifactId, version), tag);
92          }
93  
94          GavCacheKey(String gav, String tag) {
95              this.gav = gav;
96              this.tag = tag;
97              this.hash = Objects.hash(gav, tag);
98          }
99  
100         private static String gav(String groupId, String artifactId, String version) {
101             StringBuilder sb = new StringBuilder();
102             if (groupId != null) {
103                 sb.append(groupId);
104             }
105             sb.append(":");
106             if (artifactId != null) {
107                 sb.append(artifactId);
108             }
109             sb.append(":");
110             if (version != null) {
111                 sb.append(version);
112             }
113             return sb.toString();
114         }
115 
116         @Override
117         public boolean equals(Object obj) {
118             if (this == obj) {
119                 return true;
120             }
121             if (null == obj || !getClass().equals(obj.getClass())) {
122                 return false;
123             }
124             GavCacheKey that = (GavCacheKey) obj;
125             return Objects.equals(this.gav, that.gav) && Objects.equals(this.tag, that.tag);
126         }
127 
128         @Override
129         public int hashCode() {
130             return hash;
131         }
132 
133         @Override
134         public String toString() {
135             return "GavCacheKey{" + "gav='" + gav + '\'' + ", tag='" + tag + '\'' + '}';
136         }
137     }
138 
139     private static final class SourceCacheKey {
140         private final Source source;
141 
142         private final String tag;
143 
144         private final int hash;
145 
146         SourceCacheKey(Source source, String tag) {
147             this.source = source;
148             this.tag = tag;
149             this.hash = Objects.hash(source, tag);
150         }
151 
152         @Override
153         public String toString() {
154             return "SourceCacheKey{" + "source=" + source + ", tag='" + tag + '\'' + '}';
155         }
156 
157         @Override
158         public boolean equals(Object obj) {
159             if (this == obj) {
160                 return true;
161             }
162             if (null == obj || !getClass().equals(obj.getClass())) {
163                 return false;
164             }
165             SourceCacheKey that = (SourceCacheKey) obj;
166             return Objects.equals(this.source, that.source) && Objects.equals(this.tag, that.tag);
167         }
168 
169         @Override
170         public int hashCode() {
171             return hash;
172         }
173     }
174 }