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