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.HashMap;
028: import java.util.Iterator;
029: import java.util.List;
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.types.Path;
035:
036: import com.bostechcorp.cbesb.build.ant.util.JbiXmlFile;
037:
038: public class GetComponentInfoTask extends Task {
039:
040: private String workspace;
041: private String projectName;
042: private String componentNameProperty;
043: private String fileListProperty;
044: private Vector<Path> paths = new Vector<Path>();
045: private Vector<File> searchPaths = new Vector<File>();
046:
047: public void setProjectName(String projectName) {
048: this .projectName = projectName;
049: }
050:
051: public void setWorkspace(String workspace) {
052: this .workspace = workspace;
053: }
054:
055: public void setComponentNameProperty(String componentNameProperty) {
056: this .componentNameProperty = componentNameProperty;
057: }
058:
059: public void setFileListProperty(String fileListProperty) {
060: this .fileListProperty = fileListProperty;
061: }
062:
063: public void addPath(Path path) {
064: paths.add(path);
065: }
066:
067: public void execute() throws BuildException {
068: if (workspace == null) {
069: throw new BuildException("Missing workspace attribute.");
070: }
071: if (projectName == null) {
072: throw new BuildException("Missing projectName attribute.");
073: }
074: if (componentNameProperty == null) {
075: throw new BuildException(
076: "Missing componentNameProperty attribute.");
077: }
078: if (fileListProperty == null) {
079: throw new BuildException(
080: "Missing fileListProperty attribute.");
081: }
082: if (paths.size() == 0) {
083: throw new BuildException("No search paths specified.");
084: }
085:
086: initSearchPaths();
087:
088: File workspaceFile = new File(workspace);
089: File jbiXmlFile = new File(workspaceFile, projectName
090: + File.separator + "src" + File.separator + "resources"
091: + File.separator + "META-INF" + File.separator
092: + "jbi.xml");
093: if (jbiXmlFile.exists()) {
094: JbiXmlFile jbi = new JbiXmlFile();
095: try {
096: jbi.load(jbiXmlFile);
097: } catch (Exception e) {
098: getProject().log(e.getLocalizedMessage());
099: throw new BuildException(
100: "Exception loading jbi.xml file:"
101: + jbiXmlFile.getAbsolutePath(), e);
102: }
103:
104: if (!jbi.isComponent() && !jbi.isSharedLibrary()) {
105: throw new BuildException("Project: " + projectName
106: + " is not a component or shared library.");
107: }
108:
109: getProject().setProperty(componentNameProperty,
110: jbi.getName());
111:
112: HashMap<String, String> resolvedList = new HashMap<String, String>();
113: if (jbi.isComponent()) {
114: getComponentFileList(resolvedList, jbi
115: .getComponentClasspath());
116: getComponentFileList(resolvedList, jbi
117: .getBootstrapClasspath());
118:
119: } else {
120: getComponentFileList(resolvedList, jbi
121: .getSharedLibraryClasspath());
122: }
123: StringBuffer sb = new StringBuffer();
124: Iterator iter = resolvedList.values().iterator();
125: while (iter.hasNext()) {
126: String path = (String) iter.next();
127: if (sb.length() > 0) {
128: sb.append(",");
129: }
130: sb.append(path);
131: }
132: getProject().setProperty(fileListProperty, sb.toString());
133:
134: } else {
135: throw new BuildException("jbi.xml file does not exist: "
136: + jbiXmlFile.getAbsolutePath());
137: }
138:
139: }
140:
141: private void getComponentFileList(
142: HashMap<String, String> resolvedList,
143: List<String> classpathList) throws BuildException {
144: for (int i = 0; i < classpathList.size(); i++) {
145: String filename = classpathList.get(i);
146: if (!resolvedList.containsKey(filename)) {
147: resolvedList.put(filename, resolveFile(filename));
148: }
149:
150: }
151: }
152:
153: private String resolveFile(String filename) throws BuildException {
154: for (int i = 0; i < searchPaths.size(); i++) {
155: File dir = searchPaths.get(i);
156: File file = new File(dir, filename);
157: if (file.exists()) {
158: return file.getAbsolutePath();
159: }
160: }
161: throw new BuildException("Unable to locate required file: "
162: + filename);
163: }
164:
165: private void initSearchPaths() throws BuildException {
166: for (Iterator itPaths = paths.iterator(); itPaths.hasNext();) {
167: Path path = (Path) itPaths.next();
168: String[] includedPaths = path.list();
169: for (int i = 0; i < includedPaths.length; i++) {
170: File searchDir = new File(includedPaths[i]);
171: if (!searchDir.exists()) {
172: throw new BuildException(
173: "Specified search path does not exist: "
174: + includedPaths[i]);
175: }
176: if (!searchDir.isDirectory()) {
177: throw new BuildException(
178: "Specified search path is not a directory: "
179: + includedPaths[i]);
180: }
181: searchPaths.add(searchDir);
182: }
183: }
184:
185: }
186: }
|