1 package org.apache.maven.plugin.ejb.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
73
74
75
76
77
78
79 public boolean isOK( JarFile jarFile )
80 {
81 boolean bRetVal = false;
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
96
97
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
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
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 }