View Javadoc
1   package org.apache.maven.plugins.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.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.jar.JarEntry;
28  import java.util.jar.JarFile;
29  
30  /**
31   * Jar Content Checker
32   */
33  public class JarContentChecker
34  {
35      private final static Boolean FOUND = Boolean.TRUE;
36  
37      private final static Boolean NOT_FOUND = Boolean.FALSE;
38  
39      private Map<File, Boolean> fileMap;
40  
41      private Map<File, Boolean> directoryMap;
42  
43  
44      public JarContentChecker()
45      {
46          fileMap = new HashMap<File, Boolean>();
47          directoryMap = new HashMap<File, Boolean>();
48      }
49  
50      public void addDirectory( File dir )
51      {
52          directoryMap.put( dir, NOT_FOUND );
53      }
54  
55      public void addFile( File file )
56      {
57          fileMap.put( file, NOT_FOUND );
58      }
59  
60      /**
61       * checks whether the jar file contains the files for this checker,
62       * files with the same file name but with different data will not
63       * be considered.
64       *
65       * @param jarFile the Jar file
66       * @return boolean
67       */
68      public boolean isOK( JarFile jarFile )
69      {
70          boolean bRetVal;
71          Enumeration<JarEntry> zipentries = jarFile.entries();
72          JarEntry entry;
73          File entryFile;
74  
75          resetList();
76  
77          while ( zipentries.hasMoreElements() )
78          {
79              entry = zipentries.nextElement();
80              entryFile = new File( entry.getName() );
81  
82              if ( entry.isDirectory() )
83              {
84                  // cross out all files found in the jar file
85                  // found files with incorrect content will not
86                  // be counted
87                  if ( directoryMap.containsKey( entryFile ) )
88                  {
89                      directoryMap.put( entryFile, FOUND );
90                  }
91              }
92              else if ( fileMap.containsKey( entryFile ) )
93              {
94                  fileMap.put( entryFile, FOUND );
95              }
96          }
97  
98          bRetVal = checkFinalResult();
99  
100         return bRetVal;
101     }
102 
103     private boolean checkFinalResult()
104     {
105         boolean bRetVal = true;
106 
107         Iterator<File> keys = fileMap.keySet().iterator();
108 
109         while ( keys.hasNext() && bRetVal )
110         {
111             if ( fileMap.get( keys.next() ).equals( NOT_FOUND ) )
112             {
113                 bRetVal = false;
114             }
115         }
116 
117         keys = directoryMap.keySet().iterator();
118 
119         while ( keys.hasNext() && bRetVal )
120         {
121             if ( directoryMap.get( keys.next() ).equals( NOT_FOUND ) )
122             {
123                 bRetVal = false;
124             }
125         }
126 
127         return bRetVal;
128     }
129 
130     private void resetList()
131     {
132         Iterator<File> keys = fileMap.keySet().iterator();
133 
134         while ( keys.hasNext() )
135         {
136             fileMap.put( keys.next(), NOT_FOUND );
137         }
138 
139         keys = directoryMap.keySet().iterator();
140 
141         while ( keys.hasNext() )
142         {
143             directoryMap.put( keys.next(), NOT_FOUND );
144         }
145     }
146 }