001: /*
002: * Created on Sep 23, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package org.xdev.base.xssl.util.build;
008:
009: import org.xdev.base.util.GuidGenerator;
010: import org.xdev.base.xssl.XSSLAction;
011: import org.xdev.base.xssl.XSSLReturn;
012: import org.xdev.base.xssl.manage.ITransactionFinalizable;
013: import org.xdev.base.xssl.text.TextAppend;
014:
015: import java.io.BufferedReader;
016: import java.io.File;
017: import java.io.FileWriter;
018: import java.io.InputStream;
019: import java.io.InputStreamReader;
020: import java.io.Reader;
021: import java.io.StringReader;
022:
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Properties;
027:
028: import org.jdom.xpath.XPath;
029:
030: /**
031: * @author Administrator
032: *
033: * TODO To change the template for this generated type comment go to
034: * Window - Preferences - Java - Code Style - Code Templates
035: */
036: public class AntBuilder extends TextAppend implements
037: ITransactionFinalizable {
038:
039: protected File buildFile = null;
040:
041: /**
042: * @param id
043: */
044: public AntBuilder(String id) {
045: super (id);
046: // TODO Auto-generated constructor stub
047: }
048:
049: /**
050: * @param id
051: * @param properties
052: */
053: public AntBuilder(String id, HashMap properties) {
054: super (id, properties);
055: // TODO Auto-generated constructor stub
056: }
057:
058: protected void invoke(XSSLAction component) throws Exception {
059: if (!component.getBooleanProperty("ant-property")) {
060: super .invoke(component);
061: }
062: }
063:
064: protected void invoke() throws Exception {
065:
066: super .invoke();
067:
068: this .buildAnt();
069: }
070:
071: protected File getBuildFile(Object source) {
072:
073: File buildFile = null;
074:
075: if (source == null) {
076:
077: this .logError(new Exception("No content to use for Ant!"));
078:
079: return null;
080: }
081:
082: if (source instanceof File) {
083:
084: buildFile = (File) source;
085: } else if (source instanceof InputStream) {
086: source = new InputStreamReader((InputStream) source);
087: } else {
088: source = new StringReader("<project name=\""
089: + this .getProperty("ant-name^dummy-project")
090: + "\" basedir=\"" + this .getProperty("ant-basedir")
091: + "\">" + source.toString() + "</project>");
092: }
093:
094: if (source instanceof Reader) {
095: try {
096: buildFile = new File("build."
097: + GuidGenerator.getGuid(false) + ".xml");
098:
099: FileWriter writer = new FileWriter(buildFile);
100:
101: BufferedReader reader = new BufferedReader(
102: (Reader) source);
103:
104: String line = null;
105:
106: while ((line = reader.readLine()) != null) {
107: writer.write(line + "\n");
108: }
109:
110: writer.flush();
111: writer.close();
112:
113: reader.close();
114:
115: } catch (Exception ex) {
116:
117: }
118: }
119:
120: return buildFile;
121: }
122:
123: public void buildAnt() {
124: if (buildFile == null) {
125:
126: if (this .hasProperty("build-file")) {
127:
128: buildFile = this .getFileProperty("build-file");
129: } else {
130:
131: buildFile = this .getBuildFile(super .getObjectValue());
132: }
133: }
134:
135: if (buildFile == null || !buildFile.exists()) {
136:
137: buildFile = null;
138:
139: this .logError(new Exception(
140: "Build file was not properly generated."));
141: }
142:
143: String[] staticArgs = new String[] {
144: this .getProperty("ant-target"),
145: "-f",
146: this .buildFile.getAbsolutePath(),
147: this .getBooleanProperty("ant-debug") ? "-debug" : "",
148: this .getBooleanProperty("ant-diagnostics") ? "-diagnostics"
149: : "",
150: this .getBooleanProperty("ant-quiet") ? "-quiet" : "",
151: this .getBooleanProperty("ant-verbose") ? "-verbose"
152: : "",
153: this .getBooleanProperty("ant-emacs") ? "-emacs" : "",
154: this .getBooleanProperty("ant-noinput") ? "-noinput"
155: : "",
156: this .getBooleanProperty("ant-keep-going") ? "-keep-going"
157: : "",
158: this .hasProperty("ant-lib") ? "-lib" : "",
159: this .hasProperty("ant-lib") ? this
160: .getProperty("ant-lib") : "",
161: this .hasProperty("ant-logfile") ? "-logfile" : "",
162: this .hasProperty("ant-logfile") ? this
163: .getProperty("ant-logfile") : "",
164: this .hasProperty("ant-logger") ? "-logger" : "",
165: this .hasProperty("ant-logger") ? this
166: .getProperty("ant-logger") : "",
167: this .hasProperty("ant-listener") ? "-listener" : "",
168: this .hasProperty("ant-listener") ? this
169: .getProperty("ant-listener") : "",
170: this .hasProperty("ant-propertyfile") ? "-propertyfile"
171: : "",
172: this .hasProperty("ant-propertyfile") ? this
173: .getProperty("ant-propertyfile") : "",
174: this .hasProperty("ant-inputhandler") ? "-inputhandler"
175: : "",
176: this .hasProperty("ant-inputhandler") ? this
177: .getProperty("ant-inputhandler") : "",
178: this .hasProperty("ant-find") ? "-find" : "",
179: this .hasProperty("ant-find") ? this
180: .getProperty("ant-find") : "",
181: this .hasProperty("ant-nice") ? "-nice" : "",
182: this .hasProperty("ant-nice") ? this
183: .getProperty("ant-nice") : "" };
184:
185: List dynProps = null;
186:
187: try {
188: dynProps = XPath
189: .selectNodes(this , "*[ant-property='true']");
190: } catch (Exception ex) {
191: this .logError(ex);
192: }
193:
194: if (dynProps == null) {
195: dynProps = new ArrayList();
196: }
197:
198: String[] args = new String[staticArgs.length];//+dynProps.size()];
199:
200: System.arraycopy(staticArgs, 0, args, 0, staticArgs.length);
201:
202: XSSLAction propValue = null;
203:
204: Object obj = null;
205:
206: int size = dynProps.size();
207:
208: Properties props = new Properties();
209:
210: for (int i = 0; i < size; i++) {
211:
212: propValue = (XSSLAction) dynProps.get(i);
213:
214: try {
215: if (propValue.execute(this )
216: && propValue instanceof XSSLReturn) {
217: obj = ((XSSLReturn) propValue).getObjectValue();
218:
219: if (obj == null) {
220: propValue
221: .logWarn("Value is null. Cannot be set for Ant's execution.");
222:
223: obj = "";
224: }
225:
226: if (propValue.hasProperty("ant-property-name")) {
227: //obj = "-D"+propValue.getProperty("ant-property-name")+obj.toString();
228: props.setProperty(propValue
229: .getProperty("ant-property-name"), obj
230: .toString());
231: }
232: } else {
233: obj = "";
234: }
235: } catch (Exception ex) {
236: propValue.logError(ex);
237:
238: obj = "";
239: }
240:
241: //args[i+staticArgs.length] = obj.toString();
242: }
243:
244: AntMainWrapper antMain = new AntMainWrapper();
245:
246: antMain.startAnt(args, props, this .getClass().getClassLoader());
247:
248: if (!this .getBooleanProperty("keep")) {
249:
250: this .buildFile.delete();
251:
252: this .buildFile = null;
253: }
254:
255: }
256:
257: public Object getObjectValue() {
258: return null;
259: }
260:
261: public void doFinalize() throws Exception {
262:
263: super.doFinalize();
264:
265: this.buildFile = null;
266: }
267:
268: }
|