View Javadoc

1   package org.apache.maven.shared.jar.identification.exposers;
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.shared.jar.JarAnalyzer;
23  import org.apache.maven.shared.jar.identification.JarIdentification;
24  import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
25  import org.codehaus.plexus.logging.AbstractLogEnabled;
26  import org.codehaus.plexus.util.IOUtil;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  import java.io.BufferedReader;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.InputStreamReader;
33  import java.util.ArrayList;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.jar.JarEntry;
37  
38  
39  /**
40   * Exposer that examines a a JAR for files that contain the text <code>version</code> (case-insensitive) and
41   * adds the contents as potential version(s).
42   *
43   * @plexus.component role="org.apache.maven.shared.jar.identification.JarIdentificationExposer" role-hint="textFile"
44   */
45  public class TextFileExposer
46      extends AbstractLogEnabled
47      implements JarIdentificationExposer
48  {
49      public void expose( JarIdentification identification, JarAnalyzer jarAnalyzer )
50      {
51          List textFiles = findTextFileVersions( jarAnalyzer );
52          if ( !textFiles.isEmpty() )
53          {
54              Iterator ithits = textFiles.iterator();
55              while ( ithits.hasNext() )
56              {
57                  String ver = (String) ithits.next();
58                  identification.addVersion( ver );
59              }
60          }
61      }
62  
63      private List findTextFileVersions( JarAnalyzer jarAnalyzer )
64      {
65          List textVersions = new ArrayList();
66          List hits = jarAnalyzer.getVersionEntries();
67  
68          Iterator it = hits.iterator();
69          while ( it.hasNext() )
70          {
71              JarEntry entry = (JarEntry) it.next();
72  
73              // skip this entry if it's a class file.
74              if ( !entry.getName().endsWith( ".class" ) ) //$NON-NLS-1$
75              {
76                  getLogger().debug( "Version Hit: " + entry.getName() );
77                  InputStream is = null;
78                  try
79                  {
80                      is = jarAnalyzer.getEntryInputStream( entry );
81                      BufferedReader br = new BufferedReader( new InputStreamReader( is ) );
82  
83                      String line = br.readLine();
84                      // TODO: check for key=value pair.
85                      // TODO: maybe even for groupId entries.
86  
87                      getLogger().debug( line );
88                      if ( StringUtils.isNotEmpty( line ) )
89                      {
90                          textVersions.add( line );
91                      }
92                  }
93                  catch ( IOException e )
94                  {
95                      getLogger().warn( "Unable to read line from " + entry.getName(), e );
96                  }
97                  finally
98                  {
99                      IOUtil.close( is );
100                 }
101             }
102         }
103         return textVersions;
104     }
105 }