View Javadoc
1   package org.apache.maven.plugin.ejb.utils;
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 java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.Enumeration;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.jar.JarFile;
29  import java.util.zip.ZipEntry;
30  
31  /**
32   * Jar Content Checker
33   */
34  public class JarContentChecker
35  {
36      private final static String FOUND = "1";
37  
38      private final static String NOT_FOUND = "0";
39  
40      private HashMap fileMap;
41  
42      private HashMap directoryMap;
43  
44      private HashMap dataMap;
45  
46  
47      public JarContentChecker()
48      {
49          fileMap = new HashMap();
50          directoryMap = new HashMap();
51          dataMap = new HashMap();
52      }
53  
54      public void addDirectory( File dir )
55      {
56          directoryMap.put( dir, NOT_FOUND );
57      }
58  
59      public void addFile( File file )
60      {
61          fileMap.put( file, NOT_FOUND );
62      }
63  
64      public void addFile( String file, String data )
65      {
66          fileMap.put( file, NOT_FOUND );
67          dataMap.put( file, data );
68      }
69  
70  
71      /**
72       * checks whether the jar file contains the files for this checker,
73       * files with the same file name but with different data will not
74       * be considered.
75       *
76       * @param jarFile
77       * @return boolean
78       */
79      public boolean isOK( JarFile jarFile )
80      {
81          boolean bRetVal;
82          Enumeration zipentries = jarFile.entries();
83          ZipEntry entry;
84          File entryFile;
85  
86          resetList();
87  
88          while ( zipentries.hasMoreElements() )
89          {
90              entry = (ZipEntry) zipentries.nextElement();
91              entryFile = new File( entry.getName() );
92  
93              if ( entry.isDirectory() )
94              {
95                  // cross out all files found in the jar file
96                  // found files with incorrect content will not
97                  // be counted
98                  if ( directoryMap.containsKey( entryFile ) )
99                  {
100                     directoryMap.put( entryFile, FOUND );
101                 }
102             }
103             else if ( fileMap.containsKey( entryFile ) )
104             {
105                 try
106                 {
107                     if ( checkContent( entryFile, jarFile.getInputStream( entry ) ) )
108                     {
109                         fileMap.put( entryFile, FOUND );
110                     }
111                 }
112                 catch ( IOException ex )
113                 {
114                     // TODO: handle exception
115                 }
116             }
117         }
118 
119         bRetVal = checkFinalResult();
120 
121         return bRetVal;
122     }
123 
124 
125     private boolean checkContent( File file, InputStream istream )
126     {
127         boolean bRetVal = true;
128 
129         if ( dataMap.containsKey( file ) )
130         {
131             // TODO: do content checking here
132         }
133 
134         return bRetVal;
135     }
136 
137     private boolean checkFinalResult()
138     {
139         boolean bRetVal = true;
140 
141         Iterator keys = fileMap.keySet().iterator();
142 
143         while ( keys.hasNext() && bRetVal )
144         {
145             if ( fileMap.get( keys.next() ).equals( NOT_FOUND ) )
146             {
147                 bRetVal = false;
148             }
149         }
150 
151         keys = directoryMap.keySet().iterator();
152 
153         while ( keys.hasNext() && bRetVal )
154         {
155             if ( directoryMap.get( keys.next() ).equals( NOT_FOUND ) )
156             {
157                 bRetVal = false;
158             }
159         }
160 
161         return bRetVal;
162     }
163 
164     private void resetList()
165     {
166         Iterator keys = fileMap.keySet().iterator();
167 
168         while ( keys.hasNext() )
169         {
170             fileMap.put( keys.next(), NOT_FOUND );
171         }
172 
173         keys = directoryMap.keySet().iterator();
174 
175         while ( keys.hasNext() )
176         {
177             directoryMap.put( keys.next(), NOT_FOUND );
178         }
179     }
180 }