1 package net.sf.jpkgmk.prototype;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.security.NoSuchAlgorithmException;
6 import java.util.Arrays;
7
8 import net.sf.jpkgmk.PackageException;
9 import net.sf.jpkgmk.pkgmap.PkgMapEntry;
10 import net.sf.jpkgmk.pkgmap.PkgMapEntryFile;
11 import net.sf.jpkgmk.pkgmap.PkgMapEntryType;
12 import net.sf.jpkgmk.util.ChecksumUtil;
13 import net.sf.jpkgmk.util.FileUtil;
14 import net.sf.jpkgmk.util.StringUtil;
15 import net.sf.jpkgmk.util.StringUtil.KeyValuePair;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20
21
22
23
24
25
26 public class PrototypeEntryFile extends AbstractPrototypeEntry {
27
28 private Log logger = LogFactory.getLog(PrototypeEntryFile.class);
29
30
31
32
33
34 public PrototypeEntryFile(String entryPath)
35 {
36 this(null, null, entryPath, null, null, null, null, null);
37 }
38
39
40
41
42
43
44
45
46
47
48 public PrototypeEntryFile(Integer part, String fileClass, String entryPath, String entryPathSource, String perm, String owner, String group, PrototypeEntryCommandDefault entryCommandDefault)
49 {
50 this(part, PrototypeEntryType.F, fileClass, entryPath, entryPathSource, perm, owner, group, entryCommandDefault);
51 }
52
53
54
55
56
57
58
59
60
61
62
63 protected PrototypeEntryFile(Integer part, PrototypeEntryType type, String fileClass, String entryPath, String entryPathSource, String perm, String owner, String group, PrototypeEntryCommandDefault entryCommandDefault)
64 {
65 super(part, type, fileClass, entryPath, entryPathSource, perm, owner, group, entryCommandDefault);
66
67 validate();
68 }
69
70
71 private void validate() {
72
73 if(getEntryPath() == null) {
74 throw new NullPointerException("The parameter 'entryPath' must not be null");
75 }
76
77
78
79
80
81
82
83
84
85
86 }
87
88
89 @Override
90 protected void create(File targetDir, String entryPathExpanded,
91 String entryPathSourceExpanded) throws IOException {
92
93 File target = buildAbsolutePath(targetDir, entryPathExpanded);
94
95
96 if(!FileUtil.isSubdir(target, targetDir)) {
97 throw new IllegalStateException("The target file " + target + " must be in the target dir '" + targetDir + "'");
98 }
99
100 File sourceFile;
101 if(entryPathSourceExpanded != null) {
102 logger.debug("Using the entryPathSource '" + entryPathSourceExpanded + "' for copying source file(s) into target directory");
103 sourceFile = new File(entryPathSourceExpanded);
104
105 }
106 else {
107 logger.debug("Using the entryPath '" + entryPathExpanded + "' for copying source file(s) into target directory - no explicit source path available");
108 sourceFile = new File(entryPathExpanded);
109
110 }
111
112 FileUtil.copyFile(sourceFile, target);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 }
131
132
133 @Override
134 protected PkgMapEntry createPkgMapEntry(Integer part,
135 PkgMapEntryType resultType, String entryClass, String entryPath, String entryPathSource,
136 Integer major, Integer minor, String mode, String owner, String group, File targetDir) {
137 PkgMapEntryFile pkgMapEntry = new PkgMapEntryFile(part, resultType, entryClass, entryPath, mode, owner, group);
138
139 File target = buildAbsolutePath(targetDir, entryPath);
140 if(!target.exists()) {
141 throw new PackageException("The target file '" + target + "' does not exist. Cannot create PkgMapEntry.");
142 }
143 pkgMapEntry.setFilesize(getFileSize(target));
144 pkgMapEntry.setChecksum(getChecksum(target));
145 pkgMapEntry.setModtime(getLastModifiedTime(target));
146 return pkgMapEntry;
147 }
148
149
150
151
152
153
154 public Long getFileSize(File filePath) {
155 if(filePath == null) {
156 throw new NullPointerException("The parameter 'filePath' must not be null");
157 }
158
159 return Long.valueOf(filePath.length());
160 }
161
162
163
164
165
166
167 public Long getChecksum(File filePath) {
168 if(filePath == null) {
169 throw new NullPointerException("The parameter 'filePath' must not be null");
170 }
171
172 try {
173 return Long.valueOf(ChecksumUtil.createUnixCksum(filePath));
174 } catch (NoSuchAlgorithmException e) {
175 throw new PackageException("Exception while computing MD5 checksum.", e);
176 } catch (IOException e) {
177 throw new PackageException("Exception while computing MD5 checksum.", e);
178 }
179 }
180
181 public Long getLastModifiedTime(File filePath) {
182 if(filePath == null) {
183 throw new NullPointerException("The parameter 'filePath' must not be null");
184 }
185
186 return Long.valueOf(filePath.lastModified());
187 }
188
189
190
191
192
193 public static class PrototypeEntryFileParser extends AbstractPrototypeEntryParser implements PrototypeEntryParser
194 {
195
196 public PrototypeEntryFileParser()
197 {
198 super();
199 }
200
201 @Override
202 protected PrototypeEntry parseItems(String[] items, Integer part, PrototypeEntryType type, int currentIndex, PrototypeEntryCommandDefault entryCommandDefault)
203 {
204
205 if(items.length < 3) {
206 throw new IllegalArgumentException("The given array must contain at least 3 items to be a valid file: " + Arrays.asList(items));
207 }
208
209 String fileClass = items[currentIndex++];
210 String path = items[currentIndex++];
211
212 KeyValuePair keyValue = StringUtil.resolveKeyValue(path);
213 String entryPath = keyValue.getKey();
214 String entryPathSource = keyValue.getValue();
215
216 String mode = getArrayValue(items, currentIndex++);
217 String owner = getArrayValue(items, currentIndex++);
218 String group = getArrayValue(items, currentIndex++);
219
220 return createEntry(part, fileClass, entryPath, entryPathSource, mode, owner, group, entryCommandDefault);
221 }
222
223 protected PrototypeEntry createEntry(Integer part, String fileClass, String entryPath,
224 String entryPathSource, String mode, String owner, String group, PrototypeEntryCommandDefault entryCommandDefault) {
225 PrototypeEntry entry = new PrototypeEntryFile(part, fileClass, entryPath, entryPathSource, mode, owner, group, entryCommandDefault);
226 return entry;
227 }
228
229 }
230
231
232 }