KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > fetch > CVSFetchTaskFactory


1 /**********************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Gunnar Wagenknecht - adaption to new fetch script builder API
11  **********************************************************************/

12 package org.eclipse.pde.internal.build.fetch;
13
14 import java.util.*;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.pde.build.*;
18 import org.eclipse.pde.internal.build.*;
19
20 /**
21  * An <code>FetchTaskFactory</code> for building fetch scripts that will
22  * fetch content from a CVS repository (id: <code>CVS</code>).
23  * <p>
24  * <code><pre>
25  * Map file format:
26  * type@id,[version]=CVS,args
27  * args is a comma-separated list of key/value pairs
28  * Accepted args include:
29  * cvsPassFile - optional password file
30  * cvsRoot - mandatory repo location
31  * password - optional password
32  * path - optional path relative to the cvsRoot
33  * prebuilt - optional boolean value indicating that the entry points to a pre-built bundle in the repository
34  * tag - mandatory CVS tag
35  * </pre></code>
36  * </p>
37  */

38 public class CVSFetchTaskFactory implements IFetchFactory {
39     public static final String JavaDoc ID = "CVS"; //$NON-NLS-1$
40

41     private static final String JavaDoc TARGET_GET_FROM_CVS = "FetchFromCVS"; //$NON-NLS-1$
42
private static final String JavaDoc SEPARATOR = ","; //$NON-NLS-1$
43
public static final String JavaDoc OVERRIDE_TAG = ID;
44
45     //CVS specific keys used in the map being passed around.
46
private static final String JavaDoc KEY_CVSROOT = "cvsRoot"; //$NON-NLS-1$
47
private static final String JavaDoc KEY_CVSPASSFILE = "cvsPassFile"; //$NON-NLS-1$
48
private static final String JavaDoc KEY_PASSWORD = "password"; //$NON-NLS-1$
49
private static final String JavaDoc KEY_PATH = "path"; //$NON-NLS-1$
50
private static final String JavaDoc KEY_PREBUILT = "prebuilt"; //$NON-NLS-1$
51

52     //Properties used in the CVS part of the scripts
53
private static final String JavaDoc PROP_DESTINATIONFOLDER = "destinationFolder"; //$NON-NLS-1$
54
private static final String JavaDoc PROP_CVSROOT = "cvsRoot"; //$NON-NLS-1$
55
private static final String JavaDoc PROP_MODULE = "module"; //$NON-NLS-1$
56
private static final String JavaDoc PROP_TAG = "tag"; //$NON-NLS-1$
57
private static final String JavaDoc PROP_QUIET = "quiet"; //$NON-NLS-1$
58
private static final String JavaDoc PROP_FILETOCHECK = "fileToCheck"; //$NON-NLS-1$
59
private static final String JavaDoc PROP_ELEMENTNAME = "elementName"; //$NON-NLS-1$
60

61     private void generateAuthentificationAntTask(Map entryInfos, IAntScript script) {
62         String JavaDoc password = (String JavaDoc) entryInfos.get(KEY_PASSWORD);
63         String JavaDoc cvsPassFileLocation = (String JavaDoc) entryInfos.get(KEY_CVSPASSFILE);
64         if (password != null)
65             printCVSPassTask((String JavaDoc) entryInfos.get(KEY_CVSROOT), password, cvsPassFileLocation, script);
66     }
67
68     public void generateRetrieveElementCall(Map entryInfos, IPath destination, IAntScript script) {
69         String JavaDoc type = (String JavaDoc) entryInfos.get(KEY_ELEMENT_TYPE);
70         String JavaDoc element = (String JavaDoc) entryInfos.get(KEY_ELEMENT_NAME);
71         boolean prebuilt = "true".equalsIgnoreCase((String JavaDoc) entryInfos.get(KEY_PREBUILT));
72
73         Map params = new HashMap(5);
74         // we directly export the CVS content into the destination. if we have a pre-built JAR then
75
// we want to put it right in the /plugins directory and not a sub-directory so strip off 2 segments
76
// to leave us with the build directory (/plugins will be added by the "element" attribute)
77
int remove = prebuilt ? 2 : 1;
78         String JavaDoc suggestedPath = destination.lastSegment();
79         params.put(PROP_DESTINATIONFOLDER, destination.removeLastSegments(remove).toString());
80         params.put(PROP_TAG, entryInfos.get(IFetchFactory.KEY_ELEMENT_TAG));
81         params.put(PROP_CVSROOT, entryInfos.get(KEY_CVSROOT));
82         params.put(PROP_QUIET, "${cvs.quiet}"); //$NON-NLS-1$
83
// the call to CVS requires us to pass a destination directory for the files that we are
84
// retrieving, so give it the /plugins dir here
85
if (prebuilt) {
86             if (type.equals(ELEMENT_TYPE_PLUGIN))
87                 element = IPDEBuildConstants.DEFAULT_PLUGIN_LOCATION;
88             else if (type.equals(ELEMENT_TYPE_FEATURE))
89                 element = IPDEBuildConstants.DEFAULT_FEATURE_LOCATION;
90         } else {
91             if (suggestedPath != null)
92                 element = suggestedPath;
93         }
94         params.put(PROP_ELEMENTNAME, element);
95         String JavaDoc module = entryInfos.get(KEY_PATH) == null ? element : (String JavaDoc) entryInfos.get(KEY_PATH);
96         params.put(PROP_MODULE, module);
97
98         IPath locationToCheck = (IPath) destination.clone();
99         // if we have a pre-built plug-in then we want to check the existence of the JAR file
100
// rather than the plug-in manifest.
101
if (prebuilt) {
102             locationToCheck = locationToCheck.removeLastSegments(1);
103             locationToCheck = locationToCheck.append(new Path(module).lastSegment());
104         } else {
105             if (type.equals(ELEMENT_TYPE_FEATURE)) {
106                 locationToCheck = locationToCheck.append(Constants.FEATURE_FILENAME_DESCRIPTOR);
107             } else if (type.equals(ELEMENT_TYPE_PLUGIN)) {
108                 locationToCheck = locationToCheck.append(Constants.PLUGIN_FILENAME_DESCRIPTOR);
109             } else if (type.equals(ELEMENT_TYPE_FRAGMENT)) {
110                 locationToCheck = locationToCheck.append(Constants.FRAGMENT_FILENAME_DESCRIPTOR);
111             } else if (type.equals(ELEMENT_TYPE_BUNDLE)) {
112                 locationToCheck = locationToCheck.append(Constants.BUNDLE_FILENAME_DESCRIPTOR);
113             }
114         }
115         params.put(PROP_FILETOCHECK, locationToCheck.toString());
116
117         printAvailableTask(locationToCheck.toString(), locationToCheck.toString(), script);
118         if (!prebuilt&& (type.equals(IFetchFactory.ELEMENT_TYPE_PLUGIN) || type.equals(IFetchFactory.ELEMENT_TYPE_FRAGMENT))) {
119             printAvailableTask(locationToCheck.toString(), locationToCheck.removeLastSegments(1).append(Constants.BUNDLE_FILENAME_DESCRIPTOR).toString(), script);
120         }
121
122         generateAuthentificationAntTask(entryInfos, script);
123         script.printAntCallTask(TARGET_GET_FROM_CVS, true, params);
124     }
125
126     public void generateRetrieveFilesCall(final Map entryInfos, IPath destination, final String JavaDoc[] files, IAntScript script) {
127         generateAuthentificationAntTask(entryInfos, script);
128         String JavaDoc path = (String JavaDoc) entryInfos.get(KEY_PATH);
129         for (int i = 0; i < files.length; i++) {
130             String JavaDoc file = files[i];
131             IPath filePath;
132             if (path != null) {
133                 filePath = new Path(path).append(file);
134             } else {
135                 filePath = new Path((String JavaDoc) entryInfos.get(KEY_ELEMENT_NAME)).append(file);
136             }
137             String JavaDoc tag = (String JavaDoc) entryInfos.get(IFetchFactory.KEY_ELEMENT_TAG);
138             String JavaDoc cvsRoot = (String JavaDoc) entryInfos.get(KEY_CVSROOT);
139             String JavaDoc dest = "true".equalsIgnoreCase((String JavaDoc) entryInfos.get(KEY_PREBUILT)) ? destination.removeLastSegments(1).toString() : destination.toString(); //$NON-NLS-1$
140
printCVSTask("export -r " + tag + ' ' + filePath.toString(), cvsRoot, dest, null, null, "true", null, null, "${fetch.failonerror}", script); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
141
script.println("<move file=\"" + destination + '/' + filePath + "\"" + " tofile=\"" + destination.append(file) + "\" failonerror=\"false\" />"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
142
}
143     }
144
145     public void addTargets(IAntScript script) {
146         script.printTargetDeclaration(TARGET_GET_FROM_CVS, null, null, "${fileToCheck}", null); //$NON-NLS-1$
147
printCVSTask("export -d ${" + PROP_ELEMENTNAME + "}", "${" + PROP_CVSROOT + "}", "${" + PROP_DESTINATIONFOLDER + "}", "${" + PROP_MODULE + "}", "${" + PROP_TAG + "}", "${" + PROP_QUIET + "}", null, "CVS - ${" + PROP_MODULE + "}", script); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ //$NON-NLS-13$ //$NON-NLS-14$
148
script.printTargetEnd();
149     }
150
151     /*
152      * Handle the old file format:
153      * Map file arguments:
154      * <code>&lt;TAG&gt;,&lt;CVSROOT&gt;[,&lt;PASSWORD&gt;[,&lt;PATH&gt;[,&lt;CVSPASSFILE&gt;]]]</code>
155      */

156     private void legacyParseMapFileEntry(String JavaDoc[] arguments, Properties overrideTags, Map entryInfos) {
157         String JavaDoc overrideTag = overrideTags != null ? overrideTags.getProperty(OVERRIDE_TAG) : null;
158         entryInfos.put(KEY_CVSPASSFILE, (arguments.length > 4 && !arguments[4].equals("")) ? arguments[4] : null); //$NON-NLS-1$
159
entryInfos.put(IFetchFactory.KEY_ELEMENT_TAG, (overrideTag != null && overrideTag.trim().length() != 0 ? overrideTag : arguments[0]));
160         entryInfos.put(KEY_CVSROOT, arguments[1]);
161         entryInfos.put(KEY_PASSWORD, (arguments.length > 2 && !arguments[2].equals("")) ? arguments[2] : null); //$NON-NLS-1$
162
entryInfos.put(KEY_PATH, (arguments.length > 3 && !arguments[3].equals("")) ? arguments[3] : null); //$NON-NLS-1$
163
}
164
165     public void parseMapFileEntry(String JavaDoc repoSpecificentry, Properties overrideTags, Map entryInfos) throws CoreException {
166         String JavaDoc[] arguments = Utils.getArrayFromStringWithBlank(repoSpecificentry, SEPARATOR);
167         if (arguments.length < 2) {
168             String JavaDoc message = NLS.bind(Messages.error_incorrectDirectoryEntry, entryInfos.get(KEY_ELEMENT_NAME));
169             throw new CoreException(new Status(IStatus.ERROR, IPDEBuildConstants.PI_PDEBUILD, 1, message, null)); //TODO Need to fix this
170
}
171
172         // build up the table of arguments in the map file entry
173
Map table = new HashMap();
174         for (int i=0; i<arguments.length; i++) {
175             String JavaDoc arg = arguments[i];
176             // if we have at least one arg without an equals sign, then
177
// revert back to the legacy parsing
178
int index = arg.indexOf('=');
179             if (index == -1) {
180                 legacyParseMapFileEntry(arguments, overrideTags, entryInfos);
181                 return;
182             }
183             String JavaDoc key = arg.substring(0, index);
184             String JavaDoc value = arg.substring(index+1);
185             table.put(key, value);
186         }
187
188         // add entries to the entryInfo map here instead of inside the loop
189
// in case we revert to legacy parsing in the middle of the loop (we
190
// don't want to contaminate entryInfos
191
entryInfos.put(KEY_CVSPASSFILE, table.get(KEY_CVSPASSFILE));
192         String JavaDoc overrideTag = overrideTags != null ? overrideTags.getProperty(OVERRIDE_TAG) : null;
193         entryInfos.put(IFetchFactory.KEY_ELEMENT_TAG, (overrideTag != null && overrideTag.trim().length() != 0 ? overrideTag : table.get(IFetchFactory.KEY_ELEMENT_TAG)));
194         entryInfos.put(KEY_CVSROOT, table.get(KEY_CVSROOT));
195         entryInfos.put(KEY_PASSWORD, table.get(KEY_PASSWORD));
196         entryInfos.put(KEY_PATH, table.get(KEY_PATH));
197         entryInfos.put(KEY_PREBUILT, table.get(KEY_PREBUILT));
198     }
199
200     /**
201      * Print a <code>cvs</code> task to the Ant script.
202      *
203      * @param command the CVS command to run
204      * @param cvsRoot value for the CVSROOT variable
205      * @param dest the destination directory for the checked out resources
206      * @param module the module name to check out
207      * @param tag the tag of the module to check out
208      * @param quiet whether or not to print informational messages to the output
209      * @param passFile the name of the password file
210      */

211     private void printCVSTask(String JavaDoc command, String JavaDoc cvsRoot, String JavaDoc dest, String JavaDoc module, String JavaDoc tag, String JavaDoc quiet, String JavaDoc passFile, String JavaDoc taskname, IAntScript script) {
212         printCVSTask(command, cvsRoot, dest, module, tag, quiet, passFile, taskname, null, script);
213     }
214
215     /**
216      * Print a <code>cvs</code> task to the Ant script.
217      *
218      * @param command the CVS command to run
219      * @param cvsRoot value for the CVSROOT variable
220      * @param dest the destination directory for the checked out resources
221      * @param module the module name to check out
222      * @param tag the tag of the module to check out
223      * @param quiet whether or not to print informational messages to the output
224      * @param passFile the name of the password file
225      * @param failOnError whether or not to throw an exception if something goes wrong
226      */

227     private void printCVSTask(String JavaDoc command, String JavaDoc cvsRoot, String JavaDoc dest, String JavaDoc module, String JavaDoc tag, String JavaDoc quiet, String JavaDoc passFile, String JavaDoc taskname, String JavaDoc failOnError, IAntScript script) {
228         script.printTabs();
229         script.print("<cvs"); //$NON-NLS-1$
230
script.printAttribute("command", command, false); //$NON-NLS-1$
231
script.printAttribute("cvsRoot", cvsRoot, false); //$NON-NLS-1$
232
script.printAttribute("dest", dest, false); //$NON-NLS-1$
233
script.printAttribute("package", module, false); //$NON-NLS-1$
234
script.printAttribute("tag", tag, false); //$NON-NLS-1$
235
script.printAttribute("quiet", quiet, false); //$NON-NLS-1$
236
script.printAttribute("passfile", passFile, false); //$NON-NLS-1$
237
script.printAttribute("taskname", taskname, false); //$NON-NLS-1$
238
script.printAttribute("failonerror", failOnError, false); //$NON-NLS-1$
239
script.println("/>"); //$NON-NLS-1$
240
}
241     
242     /**
243      * Print a <code>cvspass</code> task to the Ant script.
244      *
245      * @param cvsRoot the name of the repository
246      * @param password the password
247      * @param passFile the name of the password file
248      */

249     private void printCVSPassTask(String JavaDoc cvsRoot, String JavaDoc password, String JavaDoc passFile, IAntScript script) {
250         script.printTabs();
251         script.print("<cvspass"); //$NON-NLS-1$
252
script.printAttribute("cvsRoot", cvsRoot, true); //$NON-NLS-1$
253
script.printAttribute("password", password, true); //$NON-NLS-1$
254
script.printAttribute("passfile", passFile, false); //$NON-NLS-1$
255
script.println("/>"); //$NON-NLS-1$
256
}
257
258     /**
259      * Print the <code>available</code> Ant task to this script. This task sets a property
260      * value if the given file exists at runtime.
261      *
262      * @param property the property to set
263      * @param file the file to look for
264      */

265     private void printAvailableTask(String JavaDoc property, String JavaDoc file, IAntScript script) {
266         script.printTabs();
267         script.print("<available"); //$NON-NLS-1$
268
script.printAttribute("property", property, true); //$NON-NLS-1$
269
script.printAttribute("file", file, false); //$NON-NLS-1$
270
script.println("/>"); //$NON-NLS-1$
271
}
272 }
273
Popular Tags