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.index.reader;
20  
21  import java.util.Map;
22  import java.util.Objects;
23  
24  import static java.util.Objects.requireNonNull;
25  
26  /**
27   * Maven Index record.
28   *
29   * @since 5.1.2
30   */
31  public final class Record {
32      /**
33       * Entry key is field key with some metadata.
34       */
35      public static final class EntryKey {
36          private final String name;
37  
38          private final Class<?> proto;
39  
40          public EntryKey(final String name, final Class<?> proto) {
41              requireNonNull(name, "name is null");
42              requireNonNull(proto, "proto is null");
43              this.name = name;
44              this.proto = proto;
45          }
46  
47          public String getName() {
48              return name;
49          }
50  
51          public Class<?> getProto() {
52              return proto;
53          }
54  
55          @Override
56          public boolean equals(Object o) {
57              if (this == o) {
58                  return true;
59              }
60              if (o == null || getClass() != o.getClass()) {
61                  return false;
62              }
63              EntryKey entryKey = (EntryKey) o;
64              return Objects.equals(name, entryKey.name);
65          }
66  
67          @Override
68          public int hashCode() {
69              return Objects.hash(name);
70          }
71  
72          @Override
73          public String toString() {
74              return "Key{" + "name='" + name + '\'' + ", type=" + proto.getSimpleName() + '}';
75          }
76      }
77  
78      /**
79       * Key of repository ID entry, that contains {@link String}.
80       */
81      public static final EntryKey REPOSITORY_ID = new EntryKey("repositoryId", String.class);
82  
83      /**
84       * Key of all groups list entry, that contains {@link java.util.List<String>}.
85       */
86      public static final EntryKey ALL_GROUPS = new EntryKey("allGroups", String[].class);
87  
88      /**
89       * Key of root groups list entry, that contains {@link java.util.List<String>}.
90       */
91      public static final EntryKey ROOT_GROUPS = new EntryKey("rootGroups", String[].class);
92  
93      /**
94       * Key of index record modification (added to index or removed from index) timestamp entry, that contains {@link
95       * Long}.
96       */
97      public static final EntryKey REC_MODIFIED = new EntryKey("recordModified", Long.class);
98  
99      /**
100      * Key of artifact groupId entry, that contains {@link String}.
101      */
102     public static final EntryKey GROUP_ID = new EntryKey("groupId", String.class);
103 
104     /**
105      * Key of artifact artifactId entry, that contains {@link String}.
106      */
107     public static final EntryKey ARTIFACT_ID = new EntryKey("artifactId", String.class);
108 
109     /**
110      * Key of artifact version entry, that contains {@link String}.
111      */
112     public static final EntryKey VERSION = new EntryKey("version", String.class);
113 
114     /**
115      * Key of artifact classifier entry, that contains {@link String}.
116      */
117     public static final EntryKey CLASSIFIER = new EntryKey("classifier", String.class);
118 
119     /**
120      * Key of artifact packaging entry, that contains {@link String}.
121      */
122     public static final EntryKey PACKAGING = new EntryKey("packaging", String.class);
123 
124     /**
125      * Key of artifact file extension, that contains {@link String}.
126      */
127     public static final EntryKey FILE_EXTENSION = new EntryKey("fileExtension", String.class);
128 
129     /**
130      * Key of artifact file last modified timestamp, that contains {@link Long}.
131      */
132     public static final EntryKey FILE_MODIFIED = new EntryKey("fileModified", Long.class);
133 
134     /**
135      * Key of artifact file size in bytes, that contains {@link Long}.
136      */
137     public static final EntryKey FILE_SIZE = new EntryKey("fileSize", Long.class);
138 
139     /**
140      * Key of artifact Sources presence flag, that contains {@link Boolean}.
141      */
142     public static final EntryKey HAS_SOURCES = new EntryKey("hasSources", Boolean.class);
143 
144     /**
145      * Key of artifact Javadoc presence flag, that contains {@link Boolean}.
146      */
147     public static final EntryKey HAS_JAVADOC = new EntryKey("hasJavadoc", Boolean.class);
148 
149     /**
150      * Key of artifact signature presence flag, that contains {@link Boolean}.
151      */
152     public static final EntryKey HAS_SIGNATURE = new EntryKey("hasSignature", Boolean.class);
153 
154     /**
155      * Key of artifact name (as set in POM), that contains {@link String}.
156      */
157     public static final EntryKey NAME = new EntryKey("name", String.class);
158 
159     /**
160      * Key of artifact description (as set in POM), that contains {@link String}.
161      */
162     public static final EntryKey DESCRIPTION = new EntryKey("description", String.class);
163 
164     /**
165      * Key of artifact SHA1 digest, that contains {@link String}.
166      */
167     public static final EntryKey SHA1 = new EntryKey("sha1", String.class);
168 
169     /**
170      * Key of artifact contained class names, that contains {@link java.util.List<String>}. Extracted by {@code
171      * JarFileContentsIndexCreator}.
172      */
173     public static final EntryKey CLASSNAMES = new EntryKey("classNames", String[].class);
174 
175     /**
176      * Key of plugin artifact prefix, that contains {@link String}. Extracted by {@code
177      * MavenPluginArtifactInfoIndexCreator}.
178      */
179     public static final EntryKey PLUGIN_PREFIX = new EntryKey("pluginPrefix", String.class);
180 
181     /**
182      * Key of plugin artifact goals, that contains {@link java.util.List<String>}. Extracted by {@code
183      * MavenPluginArtifactInfoIndexCreator}.
184      */
185     public static final EntryKey PLUGIN_GOALS = new EntryKey("pluginGoals", String[].class);
186 
187     /**
188      * Key of OSGi "Bundle-SymbolicName" manifest entry, that contains {@link String}. Extracted by {@code
189      * OsgiArtifactIndexCreator}.
190      */
191     public static final EntryKey OSGI_BUNDLE_SYMBOLIC_NAME = new EntryKey("Bundle-SymbolicName", String.class);
192 
193     /**
194      * Key of OSGi "Bundle-Version" manifest entry, that contains {@link String}. Extracted by {@code
195      * OsgiArtifactIndexCreator}.
196      */
197     public static final EntryKey OSGI_BUNDLE_VERSION = new EntryKey("Bundle-Version", String.class);
198 
199     /**
200      * Key of OSGi "Export-Package" manifest entry, that contains {@link String}. Extracted by {@code
201      * OsgiArtifactIndexCreator}.
202      */
203     public static final EntryKey OSGI_EXPORT_PACKAGE = new EntryKey("Export-Package", String.class);
204 
205     /**
206      * Key of OSGi "Export-Service" manifest entry, that contains {@link String}. Extracted by {@code
207      * OsgiArtifactIndexCreator}.
208      */
209     public static final EntryKey OSGI_EXPORT_SERVICE = new EntryKey("Export-Service", String.class);
210 
211     /**
212      * Key of OSGi "Bundle-Description" manifest entry, that contains {@link String}. Extracted by {@code
213      * OsgiArtifactIndexCreator}.
214      */
215     public static final EntryKey OSGI_BUNDLE_DESCRIPTION = new EntryKey("Bundle-Description", String.class);
216 
217     /**
218      * Key of OSGi "Bundle-Name" manifest entry, that contains {@link String}. Extracted by {@code
219      * OsgiArtifactIndexCreator}.
220      */
221     public static final EntryKey OSGI_BUNDLE_NAME = new EntryKey("Bundle-Name", String.class);
222 
223     /**
224      * Key of OSGi "Bundle-License" manifest entry, that contains {@link String}. Extracted by {@code
225      * OsgiArtifactIndexCreator}.
226      */
227     public static final EntryKey OSGI_BUNDLE_LICENSE = new EntryKey("Bundle-License", String.class);
228 
229     /**
230      * Key of OSGi "Bundle-DocURL" manifest entry, that contains {@link String}. Extracted by {@code
231      * OsgiArtifactIndexCreator}.
232      */
233     public static final EntryKey OSGI_EXPORT_DOCURL = new EntryKey("Bundle-DocURL", String.class);
234 
235     /**
236      * Key of OSGi "Import-Package" manifest entry, that contains {@link String}. Extracted by {@code
237      * OsgiArtifactIndexCreator}.
238      */
239     public static final EntryKey OSGI_IMPORT_PACKAGE = new EntryKey("Import-Package", String.class);
240 
241     /**
242      * Key of OSGi "Require-Bundle" manifest entry, that contains {@link String}. Extracted by {@code
243      * OsgiArtifactIndexCreator}.
244      */
245     public static final EntryKey OSGI_REQUIRE_BUNDLE = new EntryKey("Require-Bundle", String.class);
246 
247     /**
248      * Key of OSGi "Provide-Capability" manifest entry, that contains {@link String}. Extracted by {@code
249      * OsgiArtifactIndexCreator}.
250      */
251     public static final EntryKey OSGI_PROVIDE_CAPABILITY = new EntryKey("Provide-Capability", String.class);
252 
253     /**
254      * Key of OSGi "Require-Capability" manifest entry, that contains {@link String}. Extracted by {@code
255      * OsgiArtifactIndexCreator}.
256      */
257     public static final EntryKey OSGI_REQUIRE_CAPABILITY = new EntryKey("Require-Capability", String.class);
258 
259     /**
260      * Key of OSGi "Fragment-Host" manifest entry, that contains {@link String}. Extracted by {@code
261      * OsgiArtifactIndexCreator}.
262      */
263     public static final EntryKey OSGI_FRAGMENT_HOST = new EntryKey("Fragment-Host", String.class);
264 
265     /**
266      * Key of deprecated OSGi "Bundle-RequiredExecutionEnvironment" manifest entry, that contains {@link String}.
267      * Extracted by {@code OsgiArtifactIndexCreator}.
268      */
269     public static final EntryKey OSGI_BREE = new EntryKey("Bundle-RequiredExecutionEnvironment", String.class);
270 
271     /**
272      * Key for SHA-256 checksum  needed for OSGI content capability that contains {@link String}. Extracted by {@code
273      * OsgiArtifactIndexCreator}.
274      */
275     public static final EntryKey SHA_256 = new EntryKey("sha256", String.class);
276 
277     /**
278      * Types of returned records returned from index.
279      */
280     public enum Type {
281         /**
282          * Descriptor record. Can be safely ignored.
283          * Contains following entries:
284          * <ul>
285          * <li>{@link #REPOSITORY_ID}</li>
286          * </ul>
287          */
288         DESCRIPTOR,
289 
290         /**
291          * Artifact ADD record. Records of this type should be added to your indexing system.
292          * Contains following entries:
293          * <ul>
294          * <li>{@link #REC_MODIFIED} (when record was added/modified on index)</li>
295          * <li>{@link #GROUP_ID}</li>
296          * <li>{@link #ARTIFACT_ID}</li>
297          * <li>{@link #VERSION}</li>
298          * <li>{@link #CLASSIFIER} (optional)</li>
299          * <li>{@link #FILE_EXTENSION}</li>
300          * <li>{@link #FILE_MODIFIED}</li>
301          * <li>{@link #FILE_SIZE}</li>
302          * <li>{@link #PACKAGING}</li>
303          * <li>{@link #HAS_SOURCES}</li>
304          * <li>{@link #HAS_JAVADOC}</li>
305          * <li>{@link #HAS_SIGNATURE}</li>
306          * <li>{@link #NAME}</li>
307          * <li>{@link #DESCRIPTION}</li>
308          * <li>{@link #SHA1}</li>
309          * <li>{@link #CLASSNAMES} (optional)</li>
310          * <li>{@link #PLUGIN_PREFIX} (optional, for maven-plugins only)</li>
311          * <li>{@link #PLUGIN_GOALS} (optional, for maven-plugins only)</li>
312          * </ul>
313          */
314         ARTIFACT_ADD,
315 
316         /**
317          * Artifact REMOVE record. In case of incremental updates, signals that this artifact was removed. Records of
318          * this type should be removed from your indexing system.
319          * Contains following entries:
320          * <ul>
321          * <li>{@link #REC_MODIFIED} (when record was deleted from index)</li>
322          * <li>{@link #GROUP_ID}</li>
323          * <li>{@link #ARTIFACT_ID}</li>
324          * <li>{@link #VERSION}</li>
325          * <li>{@link #CLASSIFIER} (optional)</li>
326          * <li>{@link #FILE_EXTENSION} (if {@link #CLASSIFIER} present)</li>
327          * <li>{@link #PACKAGING} (optional)</li>
328          * </ul>
329          */
330         ARTIFACT_REMOVE,
331 
332         /**
333          * Special record, containing all the Maven "groupId"s that are enlisted on the index. Can be safely ignored.
334          * Contains following entries:
335          * <ul>
336          * <li>{@link #ALL_GROUPS}</li>
337          * </ul>
338          */
339         ALL_GROUPS,
340 
341         /**
342          * Special record, containing all the root groups of Maven "groupId"s that are enlisted on the index. Can be
343          * safely ignored.
344          * Contains following entries:
345          * <ul>
346          * <li>{@link #ROOT_GROUPS}</li>
347          * </ul>
348          */
349         ROOT_GROUPS
350     }
351 
352     private final Type type;
353 
354     private final Map<EntryKey, Object> expanded;
355 
356     public Record(final Type type, final Map<EntryKey, Object> expanded) {
357         this.type = type;
358         this.expanded = expanded;
359     }
360 
361     /**
362      * Returns the {@link Type} of this record. Usually users would be interested in {@link Type#ARTIFACT_ADD} and
363      * {@link Type#ARTIFACT_REMOVE} types only to maintain their own index. Still, indexer offers extra records too,
364      * see {@link Type} for all existing types.
365      */
366     public Type getType() {
367         return type;
368     }
369 
370     /**
371      * Returns the expanded (processed and expanded synthetic fields) record as {@link Map} ready for consumption.
372      */
373     public Map<EntryKey, Object> getExpanded() {
374         return expanded;
375     }
376 
377     /**
378      * Returns {@code true} if this record contains given {@link EntryKey}.
379      */
380     boolean containsKey(final EntryKey entryKey) {
381         return expanded.containsKey(entryKey);
382     }
383 
384     /**
385      * Type safe handy method to get value from expanded map.
386      */
387     public Object get(final EntryKey entryKey) {
388         return expanded.get(entryKey);
389     }
390 
391     /**
392      * Type safe handy method to get string value from expanded map.
393      *
394      * @since TBD
395      */
396     public String getString(final EntryKey entryKey) {
397         if (!String.class.isAssignableFrom(entryKey.proto)) {
398             throw new IllegalArgumentException(
399                     "Key " + entryKey + " does not hold type compatible to java.lang.String");
400         }
401         return (String) expanded.get(entryKey);
402     }
403 
404     /**
405      * Type safe handy method to get String[] value from expanded map.
406      *
407      * @since TBD
408      */
409     public String[] getStringArray(final EntryKey entryKey) {
410         if (!String[].class.isAssignableFrom(entryKey.proto)) {
411             throw new IllegalArgumentException(
412                     "Key " + entryKey + " does not hold type compatible to java.lang.String[]");
413         }
414         return (String[]) expanded.get(entryKey);
415     }
416 
417     /**
418      * Type safe handy method to get Long value from expanded map.
419      *
420      * @since TBD
421      */
422     public Long getLong(final EntryKey entryKey) {
423         if (!Long.class.isAssignableFrom(entryKey.proto)) {
424             throw new IllegalArgumentException("Key " + entryKey + " does not hold type compatible to java.lang.Long");
425         }
426         return (Long) expanded.get(entryKey);
427     }
428 
429     /**
430      * Type safe handy method to get Boolean value from expanded map.
431      *
432      * @since TBD
433      */
434     public Boolean getBoolean(final EntryKey entryKey) {
435         if (!Boolean.class.isAssignableFrom(entryKey.proto)) {
436             throw new IllegalArgumentException(
437                     "Key " + entryKey + " does not hold type compatible to java.lang.Boolean");
438         }
439         return (Boolean) expanded.get(entryKey);
440     }
441 
442     /**
443      * Type safe handy method to put value to expanded map. Accepts {@code null} values, that removes the mapping.
444      */
445     public Object put(final EntryKey entryKey, final Object value) {
446         if (value == null) {
447             return expanded.remove(entryKey);
448         } else {
449             if (!entryKey.proto.isAssignableFrom(value.getClass())) {
450                 throw new IllegalArgumentException("Key " + entryKey + " does not accepts value " + value);
451             }
452             return expanded.put(entryKey, value);
453         }
454     }
455 
456     @Override
457     public String toString() {
458         return "Record{" + "type=" + type + ", expanded=" + expanded + '}';
459     }
460 }