1 package net.sf.jpkgmk.prototype;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Iterator;
8 import java.util.List;
9
10 import net.sf.jpkgmk.PackageException;
11 import net.sf.jpkgmk.pkgmap.PkgMap;
12 import net.sf.jpkgmk.pkgmap.PkgMapEntry;
13 import net.sf.jpkgmk.util.VariableMap;
14 import net.sf.jpkgmk.util.VariableResolver;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class PrototypeEntryCommandSearch extends PrototypeEntryCommand
33 {
34 private Log log = LogFactory.getLog(PrototypeEntryCommandSearch.class);
35 private String[] searchPaths;
36
37 private Prototype prototype;
38
39 public PrototypeEntryCommandSearch(String[] searchPaths) {
40 super(CommandType.SEARCH);
41 this.searchPaths = searchPaths;
42 }
43
44 @Override
45 protected String getCommandLinePart() {
46 StringBuffer sb = new StringBuffer();
47
48 for (int i = 0; i < searchPaths.length; i++) {
49 sb.append(searchPaths[i]);
50
51 if(i<searchPaths.length-1) {
52 sb.append(" ");
53 }
54 }
55 return sb.toString();
56 }
57
58 public List<PkgMapEntry> createPkgMapEntry(File targetDir, VariableResolver variableResolver) {
59 try {
60 Prototype prototype = getPrototype(variableResolver);
61
62 PkgMap pkgMap = prototype.createPkgMap(targetDir);
63 return pkgMap.getPkgMapEntries();
64 } catch (IOException e) {
65 throw new PackageException("Exception while building prototype entries for 'search' command. dir="+targetDir, e);
66 }
67 }
68
69 public void create(File targetDir, VariableResolver variableResolver) throws IOException {
70 log.debug("<Entering> create(File basedir)");
71 try {
72 Prototype prototype = getPrototype(variableResolver);
73 prototype.createFiles(targetDir);
74 } catch (IOException e) {
75 throw new PackageException("Exception while building prototype entries for 'search' command. dir="+targetDir, e);
76 }
77 }
78
79 List<File> getResolvedSearchPaths(VariableResolver variableResolver) throws FileNotFoundException {
80 List<File> resultDirs = new ArrayList<File>();
81 for (int i = 0; i < this.searchPaths.length; i++) {
82 log.debug("Resolving search path '" + this.searchPaths[i] + "'...");
83 File dir = super.resolveFile(this.searchPaths[i], variableResolver);
84 log.debug("Resolved search path '" + this.searchPaths[i] + "' to '" + dir.getAbsolutePath() + "'.");
85 resultDirs.add(dir);
86 }
87 return resultDirs;
88 }
89
90
91
92
93
94
95 Prototype getPrototype(VariableResolver variableResolver) throws IOException {
96 if(this.prototype == null) {
97 log.debug("Will create the prototype lazily...");
98 try {
99 List<File> searchPaths = getResolvedSearchPaths(variableResolver);
100 PrototypeBuilder prototypeBuilder = new PrototypeBuilder();
101 for (Iterator<File> iterator = searchPaths.iterator(); iterator.hasNext();) {
102 File file = iterator.next();
103
104 prototypeBuilder.addDirectory(file, false);
105 }
106 this.prototype = prototypeBuilder.getPrototype();
107
108
109 this.prototype.setVariableMap((VariableMap)variableResolver);
110 } catch (FileNotFoundException e) {
111 throw new PackageException("Problem while resolving the search paths " + this.getLine() + ".", e);
112 }
113 }
114 return this.prototype;
115 }
116
117 @Override
118 public String toString()
119 {
120 StringBuffer sb = new StringBuffer();
121 sb.append(super.toString());
122 sb.append(this.getClass().getName()).append("[");
123 sb.append("searchPaths=").append(this.searchPaths);
124 sb.append("]");
125 return sb.toString();
126 }
127 }