001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2008 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.build.ant;
025:
026: import java.io.File;
027: import java.util.Enumeration;
028: import java.util.List;
029: import java.util.StringTokenizer;
030: import java.util.Vector;
031:
032: import org.apache.tools.ant.BuildException;
033: import org.apache.tools.ant.Task;
034: import org.apache.tools.ant.TaskContainer;
035: import org.apache.tools.ant.taskdefs.Ant;
036: import org.apache.tools.ant.taskdefs.CallTarget;
037: import org.apache.tools.ant.taskdefs.Property;
038:
039: import com.bostechcorp.cbesb.build.ant.util.EclipseClasspathFile;
040: import com.bostechcorp.cbesb.build.ant.util.EclipseManifestFile;
041: import com.bostechcorp.cbesb.build.ant.util.EclipseClasspathFile.ClassPathEntry;
042:
043: public class ForeachDependentProjectTask extends Task {
044:
045: private String workspace;
046: private String projectName;
047: private String target;
048: private boolean inheritAll;
049: private boolean inheritRefs;
050: private String param;
051: private Vector excludeProjects;
052: private Vector params;
053: private Vector references;
054:
055: public ForeachDependentProjectTask() {
056: super ();
057: workspace = null;
058: projectName = null;
059: target = null;
060: param = null;
061: inheritAll = true;
062: inheritRefs = false;
063: excludeProjects = new Vector();
064: params = new Vector();
065: references = new Vector();
066:
067: }
068:
069: public void setProjectName(String projectName) {
070: this .projectName = projectName;
071: }
072:
073: public void setTarget(String target) {
074: this .target = target;
075: }
076:
077: public void setWorkspace(String workspace) {
078: this .workspace = workspace;
079: }
080:
081: public void setInheritAll(boolean inheritAll) {
082: this .inheritAll = inheritAll;
083: }
084:
085: public void setInheritRefs(boolean inheritRefs) {
086: this .inheritRefs = inheritRefs;
087: }
088:
089: public void setParam(String param) {
090: this .param = param;
091: }
092:
093: public void setExcludeProjects(String excludeProjects) {
094: StringTokenizer st = new StringTokenizer(excludeProjects, ",");
095: while (st.hasMoreTokens()) {
096: String tok = st.nextToken();
097: tok = tok.trim();
098: this .excludeProjects.add(tok);
099: }
100: }
101:
102: /**
103: * Corresponds to <code><antcall></code>'s nested
104: * <code><param></code> element.
105: */
106: public void addParam(Property p) {
107: params.addElement(p);
108: }
109:
110: /**
111: * Corresponds to <code><antcall></code>'s nested
112: * <code><reference></code> element.
113: */
114: public void addReference(Ant.Reference r) {
115: references.addElement(r);
116: }
117:
118: public void execute() throws BuildException {
119: if (workspace == null) {
120: throw new BuildException("Missing workspace attribute.");
121: }
122: if (projectName == null) {
123: throw new BuildException("Missing projectName attribute.");
124: }
125: if (param == null) {
126: throw new BuildException(
127: "You must supply a property name to set on each iteration in param");
128: }
129: if (target == null) {
130: throw new BuildException("Missing target attribute.");
131: }
132:
133: Vector<String> projectList = new Vector<String>();
134: getDependentProjectsFromClasspath(projectList);
135: getDependentProjectsFromManifest(projectList);
136:
137: Vector<Task> tasks = new Vector<Task>();
138: for (int i = 0; i < projectList.size(); i++) {
139: String depProj = projectList.get(i);
140: if (!excludeProjects.contains(depProj)) {
141: CallTarget ct = createCallTarget();
142: Property p = ct.createParam();
143: p.setName(param);
144: p.setValue(depProj);
145: tasks.addElement(ct);
146: }
147: }
148:
149: TaskContainer tc = (TaskContainer) getProject().createTask(
150: "sequential");
151: Enumeration e = tasks.elements();
152: Task t = null;
153: while (e.hasMoreElements()) {
154: t = (Task) e.nextElement();
155: tc.addTask(t);
156: }
157: ((Task) tc).execute();
158: }
159:
160: private void getDependentProjectsFromClasspath(
161: Vector<String> projectList) throws BuildException {
162: File workspaceFile = new File(workspace);
163: File classpathFile = new File(workspaceFile, projectName
164: + File.separator + ".classpath");
165: if (!classpathFile.exists()) {
166: throw new BuildException(
167: "Could not find project classpath file: "
168: + classpathFile.getAbsolutePath());
169: }
170:
171: EclipseClasspathFile eclClasspathFile = new EclipseClasspathFile();
172: try {
173: eclClasspathFile.load(classpathFile);
174: } catch (Exception e) {
175: getProject().log(e.getLocalizedMessage());
176: throw new BuildException(
177: "Exception loading .classpath file:"
178: + classpathFile.getAbsolutePath(), e);
179: }
180:
181: List<ClassPathEntry> entryList = eclClasspathFile.getEntries();
182: for (int i = 0; i < entryList.size(); i++) {
183: ClassPathEntry entry = entryList.get(i);
184: if (entry.isProjectReference()) {
185: File path = new File(entry.getPath());
186: String depProj = path.getName();
187: if (!projectList.contains(depProj)) {
188: projectList.add(depProj);
189: }
190: }
191: }
192:
193: }
194:
195: private void getDependentProjectsFromManifest(
196: Vector<String> projectList) throws BuildException {
197: File workspaceFile = new File(workspace);
198: File manifestFile = new File(workspaceFile, projectName
199: + File.separator + "META-INF" + File.separator
200: + "MANIFEST.MF");
201: if (manifestFile.exists()) {
202: EclipseManifestFile eclipseManifest = new EclipseManifestFile();
203: try {
204: eclipseManifest.load(manifestFile);
205: } catch (Exception e) {
206: getProject().log(e.getLocalizedMessage());
207: throw new BuildException(
208: "Exception loading manifest file:"
209: + manifestFile.getAbsolutePath(), e);
210: }
211: String depProjList = eclipseManifest.getRequireBundle();
212: StringTokenizer tokenizer = new StringTokenizer(
213: depProjList, ",");
214: while (tokenizer.hasMoreTokens()) {
215: String tok = tokenizer.nextToken().trim();
216: StringTokenizer tokenizer2 = new StringTokenizer(tok,
217: ";");
218: String depProj = tokenizer2.nextToken();
219: if (depProj.startsWith("com.bostechcorp")
220: && !projectList.contains(depProj)) {
221: projectList.add(depProj);
222: }
223: }
224:
225: }
226: }
227:
228: private CallTarget createCallTarget() {
229: CallTarget ct = (CallTarget) getProject().createTask("antcall");
230: ct.setOwningTarget(getOwningTarget());
231: ct.init();
232: ct.setTarget(target);
233: ct.setInheritAll(inheritAll);
234: ct.setInheritRefs(inheritRefs);
235: Enumeration e = params.elements();
236: while (e.hasMoreElements()) {
237: Property param = (Property) e.nextElement();
238: Property toSet = ct.createParam();
239: toSet.setName(param.getName());
240: if (param.getValue() != null) {
241: toSet.setValue(param.getValue());
242: }
243: if (param.getFile() != null) {
244: toSet.setFile(param.getFile());
245: }
246: if (param.getResource() != null) {
247: toSet.setResource(param.getResource());
248: }
249: if (param.getPrefix() != null) {
250: toSet.setPrefix(param.getPrefix());
251: }
252: if (param.getRefid() != null) {
253: toSet.setRefid(param.getRefid());
254: }
255: if (param.getEnvironment() != null) {
256: toSet.setEnvironment(param.getEnvironment());
257: }
258: if (param.getClasspath() != null) {
259: toSet.setClasspath(param.getClasspath());
260: }
261: }
262:
263: e = references.elements();
264: while (e.hasMoreElements()) {
265: ct.addReference((Ant.Reference) e.nextElement());
266: }
267:
268: return ct;
269: }
270:
271: }
|