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 InputStream} and closes the stream.
72       */
73      public static Properties loadProperties( final InputStream inputStream )
74          throws IOException
75      {
76          try
77          {
78              final Properties properties = new Properties();
79              properties.load( inputStream );
80              return properties;
81          }
82          finally
83          {
84              inputStream.close();
85          }
86      }
87  
88      /**
89       * Creates and loads {@link Properties} from provided {@link Resource} if exists, and closes the resource. If not
90       * exists, returns {@code null}.
91       */
92      public static Properties loadProperties( final Resource resource )
93          throws IOException
94      {
95          final InputStream inputStream = resource.read();
96          if ( inputStream == null )
97          {
98              return null;
99          }
100         return loadProperties( inputStream );
101     }
102 
103     /**
104      * Saves {@link Properties} to provided {@link OutputStream} and closes the stream.
105      */
106     public static void storeProperties( final OutputStream outputStream, final Properties properties )
107         throws IOException
108     {
109         try
110         {
111             properties.store( outputStream, "Maven Indexer Writer" );
112         }
113         finally
114         {
115             outputStream.close();
116         }
117     }
118 
119 
120     /**
121      * Saves {@link Properties} to provided {@link WritableResource} and closes the resource.
122      */
123     public static void storeProperties( final WritableResource writableResource, final Properties properties )
124         throws IOException
125     {
126         try
127         {
128             storeProperties( writableResource.write(), properties );
129         }
130         finally
131         {
132             writableResource.close();
133         }
134     }
135 
136     /**
137      * Creates a record of type {@link Type#DESCRIPTOR}.
138      */
139     public static Record descriptor( final String repoId )
140     {
141         HashMap<EntryKey, Object> entries = new HashMap<>();
142         entries.put( Record.REPOSITORY_ID, repoId );
143         return new Record( Type.DESCRIPTOR, entries );
144     }
145 
146     /**
147      * Creates a record of type {@link Type#ALL_GROUPS}.
148      */
149     public static Record allGroups( final Collection<String> allGroups )
150     {
151         HashMap<EntryKey, Object> entries = new HashMap<>();
152         entries.put( Record.ALL_GROUPS, allGroups.toArray( new String[allGroups.size()] ) );
153         return new Record( Type.ALL_GROUPS, entries );
154     }
155 
156     /**
157      * Creates a record of type {@link Type#ROOT_GROUPS}.
158      */
159     public static Record rootGroups( final Collection<String> rootGroups )
160     {
161         HashMap<EntryKey, Object> entries = new HashMap<>();
162         entries.put( Record.ROOT_GROUPS, rootGroups.toArray( new String[rootGroups.size()] ) );
163         return new Record( Type.ROOT_GROUPS, entries );
164     }
165 
166     /**
167      * Helper to translate the "NA" (not available) input into {@code null} value.
168      */
169     public static String renvl( final String v )
170     {
171         return NOT_AVAILABLE.equals( v ) ? null : v;
172     }
173 
174     /**
175      * Helper to translate {@code null} into "NA" (not available) value.
176      */
177     public static String nvl( final String v )
178     {
179         return v == null ? NOT_AVAILABLE : v;
180     }
181 
182     /**
183      * Returns the "root group" of given groupId.
184      */
185     public static String rootGroup( final String groupId )
186     {
187         int n = groupId.indexOf( '.' );
188         if ( n > -1 )
189         {
190             return groupId.substring( 0, n );
191         }
192         return groupId;
193     }
194 }