KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > handler > DistCopyCommandsHandler


1 package net.sf.invicta.handler;
2 import java.util.ArrayList JavaDoc;
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7
8 import net.sf.invicta.InvictaConstants;
9 import net.sf.invicta.InvictaException;
10 import net.sf.invicta.api.DefinedProperty;
11 import net.sf.invicta.api.Product;
12
13 /**
14  * Map:
15  * distDirProperty - (String, Required) Property name of the distribution destination directory.
16  * addComponentName - (Boolean, optional, false) Whether to add the name of the component to the destination directory.
17  * srcListProperty - (String, optiona) A comma-separated list of source files.
18  * or
19  * srcDirProperty - (String, Optional) Property name of the source file.
20  * flatten - (Boolean, optional, false) - Whether to flatten the source files when copying, or create the directory structure.
21  */

22 public class DistCopyCommandsHandler extends InvictaBasicHandler {
23     private final static boolean DEFAULT_ADD_COMPONENT_NAME = false;
24     private final static boolean DEFAULT_IS_SRC_PROPERTY = true;
25     private final static boolean DEFAULT_FLATTEN = false;
26     private final static String JavaDoc DIST_LIST_SEPARATOR = ",";
27     
28     protected boolean flatten;
29     protected boolean addComponentName;
30     protected String JavaDoc projectDistDir;
31     protected String JavaDoc srcDir;
32     protected List JavaDoc srcList;
33     
34     /**
35      *
36      */

37     public String JavaDoc getName() {
38         return "distCopyCommands";
39     }
40     
41     /**
42      *
43      */

44     public String JavaDoc handle(Map JavaDoc paramsMap) throws InvictaException {
45         
46         // Get the destination directory property and make sure it exists.
47
String JavaDoc projectDistDirProperty = getRequiredParameter("distDirProperty");
48         this.projectDistDir =
49             getPropertyReference(projectDistDirProperty);
50         this.addComponentName = getBooleanParameter("addComponentName", DEFAULT_ADD_COMPONENT_NAME);
51         this.flatten = getBooleanParameter("flatten", DEFAULT_FLATTEN);
52         String JavaDoc srcDirProperty = getParameter("srcDirProperty");
53         String JavaDoc srcListProperty = getParameter("srcListProperty");
54         
55         // If no source was specified, then the products of
56
// the component should be copied.
57
if ((srcListProperty == null) && (srcDirProperty == null)) {
58             return getProductsCopyCommands();
59         }
60         
61         if (srcDirProperty != null) {
62             this.srcDir = getPropertyReference(srcDirProperty);
63         } else {
64             this.srcDir = getComponent().getDir();
65             getPropertyReference(srcListProperty);
66             String JavaDoc srcDirListStr = getComponent().getPropertyValue(srcListProperty);
67             this.srcList = new ArrayList JavaDoc();
68             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(srcDirListStr, DIST_LIST_SEPARATOR);
69             while (st.hasMoreTokens())
70                 this.srcList.add(st.nextToken().trim());
71                 
72         }
73                     
74         return getFilesetCopyCommand();
75     }
76
77     
78     /**
79      * Return the commands of the files of the product.
80      *
81      * @return String
82      */

83     private String JavaDoc getProductsCopyCommands() {
84         String JavaDoc result = "";
85         
86         // Get the component's dist dir.
87
String JavaDoc localDistDir = getComponent().getDir();
88         String JavaDoc distDir = this.projectDistDir + InvictaConstants.FILE_SEPARATOR + localDistDir;
89         
90         // Add copy commands for all products
91
for (Iterator JavaDoc prodIter = getComponent().getSelfProducts().iterator(); prodIter.hasNext();) {
92             Product product = (Product) prodIter.next();
93         
94             if (product.getFile() != null) {
95                 
96                 String JavaDoc productDir = product.getRelativeDir();
97                 if (product.getFile().charAt(0) != InvictaConstants.FILE_SEPARATOR_CHAR)
98                     productDir = InvictaConstants.FILE_SEPARATOR + productDir;
99                 result += getCopyCommand(product.getPath(), distDir + productDir);
100             }
101         }
102         return result;
103     }
104     
105     /**
106      * Return ANT commands for creating dstDir directory and copying
107      * (with flatten) the srcFile to dstDir
108      *
109      * @param srcFile
110      * @param distDir
111      * @return String
112      */

113     private String JavaDoc getCopyCommand(String JavaDoc srcFile, String JavaDoc distDir) {
114         return "<mkdir dir=\"" + this.projectDistDir + "\"/>\n"
115             + "<copy file=\"" + srcFile + "\" "
116             + "flatten=\"" + String.valueOf(flatten) + "\" "
117             + "todir=\"" + distDir
118             + "\" preservelastmodified=\"true\""
119             + " includeemptydirs=\"false\"" + "/>\n";
120     }
121     
122     /**
123      * Return ANT command for creating dstDir directory and copying
124      * a fileset of all recursive content of srcDir to dstDir
125      * @param srcDir
126      * @param dstDir
127      * @return String
128      */

129     private String JavaDoc getFilesetCopyCommand() {
130         String JavaDoc fullDistDir;
131         if (addComponentName)
132             fullDistDir = this.projectDistDir + InvictaConstants.FILE_SEPARATOR + getComponent().getDir();
133         else
134             fullDistDir = this.projectDistDir;
135         
136         String JavaDoc command = "<mkdir dir=\""
137             + fullDistDir
138             + "\"/>\n"
139             + "<copy todir=\""
140             + fullDistDir
141             + "\" preservelastmodified=\"true\""
142             + " includeemptydirs=\"false\""
143             + " flatten=\"" + String.valueOf(flatten) + "\""
144             + ">\n"
145             + "<fileset dir=\"" + srcDir + "\">\n";
146             
147         if (this.srcList != null) {
148             for (Iterator JavaDoc iter = this.srcList.iterator(); iter.hasNext();) {
149                 String JavaDoc include = (String JavaDoc) iter.next();
150                 command += "<include name=\"" + include + "/**/*\"/>\n";
151                 command += "<include name=\"" + include + "\"/>\n";
152             }
153         }
154                     
155         command += "</fileset>\n";
156         command += "</copy>\n";
157         return command;
158     }
159         
160     
161     /**
162      *
163      */

164     protected String JavaDoc getPropertyReference(String JavaDoc propertyName) throws InvictaHandlerException {
165         DefinedProperty property = getComponent().getDefinedProperty(propertyName);
166         if (property == null) {
167             throw new InvictaHandlerException(this,
168                 "Property not defined: " + propertyName
169                 + " for component '" + getComponent().getName()
170                 + "' (type '" + getComponent().getTypeName() + "')");
171         }
172         return property.getReferenceValue();
173     }
174 }
175
Popular Tags