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.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.text.DateFormat;
25  import java.text.SimpleDateFormat;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Properties;
29  import java.util.TimeZone;
30  import java.util.regex.Pattern;
31  
32  import org.apache.maven.index.reader.Record.EntryKey;
33  import org.apache.maven.index.reader.Record.Type;
34  import org.apache.maven.index.reader.ResourceHandler.Resource;
35  import org.apache.maven.index.reader.WritableResourceHandler.WritableResource;
36  
37  /**
38   * Reusable code snippets and constants.
39   *
40   * @since 5.1.2
41   */
42  public final class Utils {
43      private Utils() {
44          // nothing
45      }
46  
47      public static final String INDEX_FILE_PREFIX = "nexus-maven-repository-index";
48  
49      public static final DateFormat INDEX_DATE_FORMAT;
50  
51      static {
52          INDEX_DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss.SSS Z");
53          INDEX_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
54      }
55  
56      public static final String FIELD_SEPARATOR = "|";
57  
58      public static final String NOT_AVAILABLE = "NA";
59  
60      public static final String UINFO = "u";
61  
62      public static final String INFO = "i";
63  
64      public static final Pattern FS_PATTERN = Pattern.compile(Pattern.quote(FIELD_SEPARATOR));
65  
66      /**
67       * Creates and loads {@link Properties} from provided {@link Resource} if exists, and closes the resource. If not
68       * exists, returns {@code null}.
69       */
70      public static Properties loadProperties(final Resource resource) throws IOException {
71          try (InputStream inputStream = resource.read()) {
72              if (inputStream == null) {
73                  return null;
74              }
75              final Properties properties = new Properties();
76              properties.load(inputStream);
77              return properties;
78          }
79      }
80  
81      /**
82       * Saves {@link Properties} to provided {@link WritableResource} and closes the resource.
83       */
84      public static void storeProperties(final WritableResource writableResource, final Properties properties)
85              throws IOException {
86          try (writableResource) {
87              try (OutputStream outputStream = writableResource.write()) {
88                  properties.store(outputStream, "Maven Indexer Writer");
89              }
90          }
91      }
92  
93      /**
94       * Creates a record of type {@link Type#DESCRIPTOR}.
95       */
96      public static Record descriptor(final String repoId) {
97          HashMap<EntryKey, Object> entries = new HashMap<>();
98          entries.put(Record.REPOSITORY_ID, repoId);
99          return new Record(Type.DESCRIPTOR, entries);
100     }
101 
102     /**
103      * Creates a record of type {@link Type#ALL_GROUPS}.
104      */
105     public static Record allGroups(final Collection<String> allGroups) {
106         HashMap<EntryKey, Object> entries = new HashMap<>();
107         entries.put(Record.ALL_GROUPS, allGroups.toArray(new String[0]));
108         return new Record(Type.ALL_GROUPS, entries);
109     }
110 
111     /**
112      * Creates a record of type {@link Type#ROOT_GROUPS}.
113      */
114     public static Record rootGroups(final Collection<String> rootGroups) {
115         HashMap<EntryKey, Object> entries = new HashMap<>();
116         entries.put(Record.ROOT_GROUPS, rootGroups.toArray(new String[0]));
117         return new Record(Type.ROOT_GROUPS, entries);
118     }
119 
120     /**
121      * Helper to translate the "NA" (not available) input into {@code null} value.
122      */
123     public static String renvl(final String v) {
124         return NOT_AVAILABLE.equals(v) ? null : v;
125     }
126 
127     /**
128      * Helper to translate {@code null} into "NA" (not available) value.
129      */
130     public static String nvl(final String v) {
131         return v == null ? NOT_AVAILABLE : v;
132     }
133 
134     /**
135      * Returns the "root group" of given groupId.
136      */
137     public static String rootGroup(final String groupId) {
138         int n = groupId.indexOf('.');
139         if (n > -1) {
140             return groupId.substring(0, n);
141         }
142         return groupId;
143     }
144 }