View Javadoc
1   package org.apache.maven.index.reader;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.index.reader.Record.EntryKey;
23  import org.apache.maven.index.reader.Record.Type;
24  import org.apache.maven.index.reader.ResourceHandler.Resource;
25  import org.apache.maven.index.reader.WritableResourceHandler.WritableResource;
26  
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  import java.text.DateFormat;
31  import java.text.SimpleDateFormat;
32  import java.util.Collection;
33  import java.util.HashMap;
34  import java.util.Properties;
35  import java.util.TimeZone;
36  import java.util.regex.Pattern;
37  
38  /**
39   * Reusable code snippets and constants.
40   *
41   * @since 5.1.2
42   */
43  public final class Utils
44  {
45      private Utils()
46      {
47          // nothing
48      }
49  
50      public static final String INDEX_FILE_PREFIX = "nexus-maven-repository-index";
51  
52      public static final DateFormat INDEX_DATE_FORMAT;
53  
54      static
55      {
56          INDEX_DATE_FORMAT = new SimpleDateFormat( "yyyyMMddHHmmss.SSS Z" );
57          INDEX_DATE_FORMAT.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
58      }
59  
60      public static final String FIELD_SEPARATOR = "|";
61  
62      public static final String NOT_AVAILABLE = "NA";
63  
64      public static final String UINFO = "u";
65  
66      public static final String INFO = "i";
67  
68      public static final Pattern FS_PATTERN = Pattern.compile( Pattern.quote( FIELD_SEPARATOR ) );
69  
70      /**
71       * Creates and loads {@link Properties} from provided {@link Resource} if exists, and closes the resource. If not
72       * exists, returns {@code null}.
73       */
74      public static Properties loadProperties( final Resource resource )
75          throws IOException
76      {
77          try ( InputStream inputStream = resource.read() )
78          {
79              if ( inputStream == null )
80              {
81                  return null;
82              }
83              final Properties properties = new Properties();
84              properties.load( inputStream );
85              return properties;
86          }
87      }
88  
89      /**
90       * Saves {@link Properties} to provided {@link WritableResource} and closes the resource.
91       */
92      public static void storeProperties( final WritableResource writableResource, final Properties properties )
93          throws IOException
94      {
95          try ( writableResource )
96          {
97              try ( OutputStream outputStream = writableResource.write() )
98              {
99                  properties.store( outputStream, "Maven Indexer Writer" );
100             }
101         }
102     }
103 
104     /**
105      * Creates a record of type {@link Type#DESCRIPTOR}.
106      */
107     public static Record descriptor( final String repoId )
108     {
109         HashMap<EntryKey, Object> entries = new HashMap<>();
110         entries.put( Record.REPOSITORY_ID, repoId );
111         return new Record( Type.DESCRIPTOR, entries );
112     }
113 
114     /**
115      * Creates a record of type {@link Type#ALL_GROUPS}.
116      */
117     public static Record allGroups( final Collection<String> allGroups )
118     {
119         HashMap<EntryKey, Object> entries = new HashMap<>();
120         entries.put( Record.ALL_GROUPS, allGroups.toArray( new String[0] ) );
121         return new Record( Type.ALL_GROUPS, entries );
122     }
123 
124     /**
125      * Creates a record of type {@link Type#ROOT_GROUPS}.
126      */
127     public static Record rootGroups( final Collection<String> rootGroups )
128     {
129         HashMap<EntryKey, Object> entries = new HashMap<>();
130         entries.put( Record.ROOT_GROUPS, rootGroups.toArray( new String[0] ) );
131         return new Record( Type.ROOT_GROUPS, entries );
132     }
133 
134     /**
135      * Helper to translate the "NA" (not available) input into {@code null} value.
136      */
137     public static String renvl( final String v )
138     {
139         return NOT_AVAILABLE.equals( v ) ? null : v;
140     }
141 
142     /**
143      * Helper to translate {@code null} into "NA" (not available) value.
144      */
145     public static String nvl( final String v )
146     {
147         return v == null ? NOT_AVAILABLE : v;
148     }
149 
150     /**
151      * Returns the "root group" of given groupId.
152      */
153     public static String rootGroup( final String groupId )
154     {
155         int n = groupId.indexOf( '.' );
156         if ( n > -1 )
157         {
158             return groupId.substring( 0, n );
159         }
160         return groupId;
161     }
162 }