View Javadoc

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   * @author gommma (gommma AT users.sourceforge.net)
22   * @author Last changed by: $Author: gommma $
23   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
24   * @since 1.0
25   */
26  public class PrototypeEntryFile extends AbstractPrototypeEntry {
27  
28  	private Log logger = LogFactory.getLog(PrototypeEntryFile.class);
29  	
30  	/**
31  	 * Constructor taking the mandatory arguments to create a prototype file entry
32  	 * @param entryPath
33  	 */
34  	public PrototypeEntryFile(String entryPath)
35  	{
36  		this(null, null, entryPath, null, null, null, null, null);
37  	}
38  
39  	/**
40  	 * Full constructor taking all arguments supported for this entry type
41  	 * @param fileClass
42  	 * @param entryPath path to the file or directory
43  	 * @param entryPathSource the target path in that this file should have in the package
44  	 * @param perm
45  	 * @param owner
46  	 * @param group
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  	 * Full constructor taking all arguments supported for this entry type
55  	 * @param part
56  	 * @param fileClass
57  	 * @param entryPath the target path that this file/directory should have in the created package
58  	 * @param entryPathSource path to the local source file
59  	 * @param perm
60  	 * @param owner
61  	 * @param group
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  		// Mandatory parameter check
73  		if(getEntryPath() == null) {
74  			throw new NullPointerException("The parameter 'entryPath' must not be null");
75  		}
76  
77  //		if(getMode() == null) {
78  //			throw new NullPointerException("The parameter 'mode' must not be null");
79  //		}
80  //		if(getOwner() == null) {
81  //			throw new NullPointerException("The parameter 'owner' must not be null");
82  //		}
83  //		if(getGroup() == null) {
84  //			throw new NullPointerException("The parameter 'group' must not be null");
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  		// the entry path must be in the target dir
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 //			sourceFile = buildAbsolutePath(workingDir, entryPathSourceExpanded);
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 //			sourceFile = buildAbsolutePath(workingDir, entryPathExpanded);
110 		}
111 		
112 		FileUtil.copyFile(sourceFile, target);
113 
114 //		File target = resolveTarget(targetDir);
115 //		
116 //		// the entry path must be in the target dir
117 //		if(!FileUtil.isSubdir(target, targetDir)) {
118 //			throw new IllegalStateException("The entryPath " + getEntryPath() + " must be in the target dir '" + targetDir + "'");
119 //		}
120 //
121 //		File sourceFile;
122 //		if(this.getEntryPathSource() != null) {
123 //			sourceFile = new File(this.getEntryPathSource());
124 ////			sourceFile = new File(targetDir, this.entryPathSource);
125 //		}
126 //		else {
127 //			sourceFile = target;
128 //		}
129 //		FileUtil.copyFile(sourceFile, target);
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 	 * Append file size (not specified for named pipes, special devices, directories or linked files)
151 	 * @param filePath
152 	 * @return Returns the file size. If this type has no file size null is returned.
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 	 * This field is not specified for named pipes, special devices, directories, or linked files.
164 	 * @param filePath
165 	 * @return
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 	 * Parser to create a prototype file object from a string array
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 }