001: package xdoclet.junit;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.*;
019: import java.lang.reflect.Method;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.Iterator;
023:
024: import org.apache.tools.ant.Project;
025: import org.apache.tools.ant.types.FileSet;
026:
027: import xdoclet.DocletTask;
028: import xdoclet.XmlSubTask;
029: import xdoclet.template.TemplateEngine;
030:
031: /**
032: * Runs xdoclet in a separate thread (with a special class loader).
033: *
034: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
035: */
036: public class XDocletRunner extends Thread {
037: private FileHandling _classWriter = new FileHandling();
038: private ArrayList _srcFiles = new ArrayList();
039: private ArrayList _srcDirectories = new ArrayList();
040: private File _destFile = null;
041: private String _taskName = null;
042: private String _subTaskName = null;
043: private HashMap _taskProperties = new HashMap();
044: private HashMap _subTaskProperties = new HashMap();
045: private String _result = null;
046:
047: public XDocletRunner() {
048: super ("XDocletRunner");
049: }
050:
051: public void addClass(String name, String content) {
052: try {
053: _srcFiles.add(_classWriter.write(name, content));
054: } catch (IOException ex) {
055: cleanFiles();
056: throw new RuntimeException(ex);
057: }
058: }
059:
060: public void setDestFile(String path) {
061: _destFile = _classWriter.ensurePath(path);
062: _destFile.delete();
063: }
064:
065: public void setTaskName(String className) {
066: _taskName = className;
067: }
068:
069: public void setSubTaskName(String className) {
070: _subTaskName = className;
071: }
072:
073: public void setTaskProperty(String name, Object value) {
074: _taskProperties.put(name, value);
075: }
076:
077: public void setSubTaskProperty(String name, Object value) {
078: _subTaskProperties.put(name, value);
079: }
080:
081: /* (non-Javadoc)
082: * @see java.lang.Runnable#run()
083: */
084: public void run() {
085: _result = null;
086:
087: DocletTask task = null;
088:
089: try {
090: task = initTask();
091: if (_subTaskName != null) {
092: task.addSubTask(initSubTask());
093: }
094: } catch (Exception ex) {
095: cleanFiles();
096: throw new RuntimeException(ex);
097: }
098:
099: try {
100: task.setVerbose(false);
101: task.init();
102: task.execute();
103: } catch (Exception ex) {
104: // what to do with any exception ? rethrow it ?
105: cleanFiles();
106: return;
107: }
108:
109: if (_destFile != null) {
110: if (_destFile.exists()) {
111: try {
112: _result = getDestFileContents(_destFile
113: .getAbsolutePath());
114: } catch (IOException ex) {
115: }
116: }
117: }
118: cleanFiles();
119: }
120:
121: public String getResult() {
122: return _result;
123: }
124:
125: public void destroy() {
126: cleanFiles();
127: }
128:
129: private void cleanFiles() {
130: _destFile = null;
131: _srcFiles.clear();
132: _classWriter.removeTmpDir();
133: }
134:
135: private DocletTask initTask() throws Exception {
136: DocletTask task = (DocletTask) this .getContextClassLoader()
137: .loadClass(_taskName).newInstance();
138: FileSet files = new FileSet();
139:
140: task.setDestDir(_classWriter.getTmpDir());
141: files.setDir(_classWriter.getTmpDir());
142:
143: StringBuffer includes = new StringBuffer();
144:
145: for (Iterator it = _srcFiles.iterator(); it.hasNext();) {
146: if (includes.length() > 0) {
147: includes.append(" ");
148: }
149: includes.append(((File) it.next()).getName());
150: }
151:
152: task.addFileset(files);
153: task.setProject(new Project());
154:
155: String name;
156:
157: for (Iterator it = _taskProperties.keySet().iterator(); it
158: .hasNext();) {
159: name = (String) it.next();
160: setProperty(task, name, _taskProperties.get(name));
161: }
162:
163: return task;
164: }
165:
166: private XmlSubTask initSubTask() throws Exception {
167: XmlSubTask subTask = (XmlSubTask) this .getContextClassLoader()
168: .loadClass(_subTaskName).newInstance();
169:
170: subTask.setEngine(TemplateEngine.getEngineInstance());
171: if (_destFile != null) {
172: subTask.setDestinationFile(_destFile.getName());
173: }
174:
175: String name;
176:
177: for (Iterator it = _subTaskProperties.keySet().iterator(); it
178: .hasNext();) {
179: name = (String) it.next();
180: setProperty(subTask, name, _subTaskProperties.get(name));
181: }
182:
183: return subTask;
184: }
185:
186: private void setProperty(Object obj, String propertyName,
187: Object propertyValue) throws Exception {
188: String methodName = "set"
189: + Character.toUpperCase(propertyName.charAt(0));
190:
191: if (propertyName.length() > 1) {
192: methodName += propertyName.substring(1);
193: }
194:
195: Method method = null;
196:
197: try {
198: method = obj.getClass().getMethod(methodName,
199: new Class[] { propertyValue.getClass() });
200: } catch (NoSuchMethodException ex) {
201: // we trying any method with that name then
202: Method[] methods = obj.getClass().getMethods();
203:
204: for (int idx = 0; idx < methods.length; idx++) {
205: if (methodName.equals(methods[idx].getName())) {
206: method = methods[idx];
207: break;
208: }
209: }
210: if (method == null) {
211: throw ex;
212: }
213: }
214: method.invoke(obj, new Object[] { propertyValue });
215: }
216:
217: private String getDestFileContents(String filename)
218: throws IOException {
219: BufferedReader input = new BufferedReader(new FileReader(
220: filename));
221: StringBuffer result = new StringBuffer();
222: String line;
223:
224: while ((line = input.readLine()) != null) {
225: result.append(line);
226: result.append("\n");
227: }
228: input.close();
229: return result.toString().trim();
230: }
231: }
|