View Javadoc

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   * Represents a search command to be used when creating a package from this prototype.
21   * <p>
22   * For details refer to <a href="http://docs.sun.com/app/docs/doc/806-7008/6jftmsc21?a=view">
23   * http://docs.sun.com/app/docs/doc/806-7008/6jftmsc21?a=view</a> 
24   * in section &quot;<i>Providing a Search Path for the pkgmk Command</i>&quot;.
25   * </p>
26   * 
27   * @author gommma (gommma AT users.sourceforge.net)
28   * @author Last changed by: $Author: gommma $
29   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
30   * @since 1.0
31   */
32  public class PrototypeEntryCommandSearch extends PrototypeEntryCommand
33  {
34      private Log log = LogFactory.getLog(PrototypeEntryCommandSearch.class);
35      private String[] searchPaths;
36      /** Intermediate prototype which is used to return the pgkmap entries and to create the physical files */
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              // Create a temporary pkgmap java object for being able to return the pkgMapEntries
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       * @param variableResolver
92       * @return The prototype object which is lazily created if it was not created before.
93       * @throws IOException
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                     // As in the documentation for the "search" command, it will NOT recurse over subdirectories
104                     prototypeBuilder.addDirectory(file, false);
105                 }
106                 this.prototype = prototypeBuilder.getPrototype();
107                 // Inherit all variables from parent prototype to child prototype. From the specs of "param=value" (=variables):
108                 // "Places the indicated parameter in the current environment. Spans to subsequent included prototype files."
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 }