001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.tools.ant;
019:
020: import java.io.File;
021: import java.net.URL;
022: import java.util.HashSet;
023: import java.util.Set;
024: import java.util.StringTokenizer;
025:
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.types.Path;
028: import org.java.plugin.registry.Library;
029: import org.java.plugin.registry.PluginDescriptor;
030: import org.java.plugin.registry.PluginFragment;
031: import org.java.plugin.registry.PluginPrerequisite;
032: import org.java.plugin.util.IoUtil;
033:
034: /**
035: * The Ant task to prepare classpath according to plug-in manifest(s)
036: * declarations.
037: *
038: * @version $Id: PathTask.java,v 1.3 2007/03/01 19:11:19 ddimon Exp $
039: */
040: public class PathTask extends BaseJpfTask {
041: private String pathId;
042: private String pathIdRef;
043: private String pluginId;
044: private String pluginIds;
045: private boolean followExports = true;
046:
047: /**
048: * @param value the path ID to set
049: */
050: public void setPathId(final String value) {
051: pathId = value;
052: }
053:
054: /**
055: * @param value the path ID reference to set
056: */
057: public void setPathIdRef(final String value) {
058: pathIdRef = value;
059: }
060:
061: /**
062: * @param value the plug-in ID to set
063: */
064: public void setPluginId(final String value) {
065: pluginId = value;
066: }
067:
068: /**
069: * @param value the plug-in ID's to set
070: */
071: public void setPluginIds(final String value) {
072: pluginIds = value;
073: }
074:
075: /**
076: * @param value the follow exports flag to set
077: */
078: public void setFollowExports(final boolean value) {
079: followExports = value;
080: }
081:
082: /**
083: * @see org.apache.tools.ant.Task#execute()
084: */
085: @Override
086: public void execute() throws BuildException {
087: if ((pathId == null) && (pathIdRef == null)) {
088: throw new BuildException(
089: "pathid or pathidref attribute must be set!", //$NON-NLS-1$
090: getLocation());
091: }
092: Set<String> ids = collectTargetIds();
093: if (ids.isEmpty()) {
094: throw new BuildException(
095: "pluginid or/and pluginids attribute must be set!", //$NON-NLS-1$
096: getLocation());
097: }
098: initRegistry(true);
099: Set<String> processedIds = new HashSet<String>();
100: Set<File> result = new HashSet<File>();
101: for (PluginDescriptor descr : getRegistry()
102: .getPluginDescriptors()) {
103: if (!ids.contains(descr.getId())) {
104: continue;
105: }
106: processDescriptor(result, processedIds, descr, true);
107: }
108: for (PluginFragment fragment : getRegistry()
109: .getPluginFragments()) {
110: if (!ids.contains(fragment.getId())) {
111: continue;
112: }
113: processDescriptor(result, processedIds, getRegistry()
114: .getPluginDescriptor(fragment.getPluginId()), true);
115: }
116: Path path;
117: if (pathIdRef != null) {
118: Object ref = getProject().getReference(pathIdRef);
119: if (!(ref instanceof Path)) {
120: throw new BuildException(
121: "invalid reference " + pathIdRef //$NON-NLS-1$
122: + ", expected " + Path.class.getName() //$NON-NLS-1$
123: + ", found " + ref, //$NON-NLS-1$
124: getLocation());
125: }
126: path = (Path) ref;
127: } else {
128: path = new Path(getProject());
129: getProject().addReference(pathId, path);
130: }
131: for (File file : result) {
132: path.setLocation(file);
133: }
134: if (getVerbose()) {
135: log("Collected path entries: " + result.size()); //$NON-NLS-1$
136: }
137: }
138:
139: private void processDescriptor(final Set<File> result,
140: final Set<String> processedIds,
141: final PluginDescriptor descr, final boolean includePrivate) {
142: if (followExports && !includePrivate
143: && processedIds.contains(descr.getId())) {
144: return;
145: }
146: processedIds.add(descr.getId());
147: for (Library lib : descr.getLibraries()) {
148: if (followExports && !includePrivate
149: && lib.getExports().isEmpty()) {
150: continue;
151: }
152: URL url = getPathResolver().resolvePath(lib, lib.getPath());
153: File file = IoUtil.url2file(url);
154: if (file != null) {
155: result.add(file);
156: if (getVerbose()) {
157: log("Collected file " + file //$NON-NLS-1$
158: + " from library " + lib.getUniqueId()); //$NON-NLS-1$
159: }
160: } else {
161: log("Ignoring non-local URL " + url //$NON-NLS-1$
162: + " found in library " + lib.getUniqueId()); //$NON-NLS-1$
163: }
164: }
165: for (PluginPrerequisite pre : descr.getPrerequisites()) {
166: if (!pre.matches()) {
167: continue;
168: }
169: processDescriptor(result, processedIds, getRegistry()
170: .getPluginDescriptor(pre.getPluginId()), false);
171: }
172: }
173:
174: private Set<String> collectTargetIds() {
175: HashSet<String> result = new HashSet<String>();
176: if (pluginId != null) {
177: result.add(pluginId);
178: }
179: if (pluginIds != null) {
180: for (StringTokenizer st = new StringTokenizer(pluginIds,
181: ",", false); //$NON-NLS-1$
182: st.hasMoreTokens();) {
183: result.add(st.nextToken());
184: }
185: }
186: return result;
187: }
188: }
|