View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.ejb.utils;
20  
21  import java.io.File;
22  import java.util.Enumeration;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.jar.JarEntry;
27  import java.util.jar.JarFile;
28  
29  /**
30   * Jar Content Checker
31   */
32  public class JarContentChecker {
33      private static final Boolean FOUND = Boolean.TRUE;
34  
35      private static final Boolean NOT_FOUND = Boolean.FALSE;
36  
37      private Map<File, Boolean> fileMap;
38  
39      private Map<File, Boolean> directoryMap;
40  
41      public JarContentChecker() {
42          fileMap = new HashMap<>();
43          directoryMap = new HashMap<>();
44      }
45  
46      public void addDirectory(File dir) {
47          directoryMap.put(dir, NOT_FOUND);
48      }
49  
50      public void addFile(File file) {
51          fileMap.put(file, NOT_FOUND);
52      }
53  
54      /**
55       * checks whether the jar file contains the files for this checker,
56       * files with the same file name but with different data will not
57       * be considered.
58       *
59       * @param jarFile the Jar file
60       * @return boolean
61       */
62      public boolean isOK(JarFile jarFile) {
63          boolean bRetVal;
64          Enumeration<JarEntry> zipentries = jarFile.entries();
65          JarEntry entry;
66          File entryFile;
67  
68          resetList();
69  
70          while (zipentries.hasMoreElements()) {
71              entry = zipentries.nextElement();
72              entryFile = new File(entry.getName());
73  
74              if (entry.isDirectory()) {
75                  // cross out all files found in the jar file
76                  // found files with incorrect content will not
77                  // be counted
78                  if (directoryMap.containsKey(entryFile)) {
79                      directoryMap.put(entryFile, FOUND);
80                  }
81              } else if (fileMap.containsKey(entryFile)) {
82                  fileMap.put(entryFile, FOUND);
83              }
84          }
85  
86          bRetVal = checkFinalResult();
87  
88          return bRetVal;
89      }
90  
91      private boolean checkFinalResult() {
92          boolean bRetVal = true;
93  
94          Iterator<File> keys = fileMap.keySet().iterator();
95  
96          while (keys.hasNext() && bRetVal) {
97              if (fileMap.get(keys.next()).equals(NOT_FOUND)) {
98                  bRetVal = false;
99              }
100         }
101 
102         keys = directoryMap.keySet().iterator();
103 
104         while (keys.hasNext() && bRetVal) {
105             if (directoryMap.get(keys.next()).equals(NOT_FOUND)) {
106                 bRetVal = false;
107             }
108         }
109 
110         return bRetVal;
111     }
112 
113     private void resetList() {
114         Iterator<File> keys = fileMap.keySet().iterator();
115 
116         while (keys.hasNext()) {
117             fileMap.put(keys.next(), NOT_FOUND);
118         }
119 
120         keys = directoryMap.keySet().iterator();
121 
122         while (keys.hasNext()) {
123             directoryMap.put(keys.next(), NOT_FOUND);
124         }
125     }
126 }