001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build.fetch;
011:
012: import java.net.MalformedURLException;
013: import java.net.URL;
014: import java.util.*;
015: import org.eclipse.core.runtime.*;
016: import org.eclipse.pde.build.IAntScript;
017: import org.eclipse.pde.build.IFetchFactory;
018: import org.eclipse.pde.internal.build.*;
019:
020: /**
021: * This class implements a fetch factory which calls the Ant Get task on a given URL. The
022: * format of the map file entry is as follows:
023: * type@id=GET,url,[args]
024: * where:
025: * type = feature | plugin
026: * id = plug-in or feature identifier (symbolic name)
027: * GET = mandatory constant (to call this fetch factory)
028: * url = url to retrieve the data from (suitable to be used in the Ant Get task)
029: * args = optional comma-separated list of key/value pairs, specify unpack=true to indicate the element should be unzipped
030: *
031: * e.g.
032: * plugin@com.example.foobar=GET,http://downloads.example.com/com.example.foobar_1.0.jar
033: * plugin@com.example.foobar=GET,http://downloads.example.com/com.example.foobar_1.0.jar,unpack=true
034: * plugin@com.example.foobar=GET,http://downloads.example.com/com.example.foobar_1.0.zip,unpack=true, username=foo, password=bar
035: *
036: * @since 3.2.100
037: */
038: public class GETFetchFactory implements IFetchFactory {
039:
040: private static final String UNPACK = "unpack"; //$NON-NLS-1$
041: private static final String SEPARATOR = ","; //$NON-NLS-1$
042: private static final String TASK_GET = "get"; //$NON-NLS-1$
043: private static final String TASK_DELETE = "delete"; //$NON-NLS-1$
044: private static final String TASK_UNZIP = "unzip"; //$NON-NLS-1$
045: private static final String ATTRIBUTE_SRC = "src"; //$NON-NLS-1$
046: private static final String ATTRIBUTE_DEST = "dest"; //$NON-NLS-1$
047: private static final String ATTRIBUTE_FILE = "file"; //$NON-NLS-1$
048: private static final String ATTRIBUTE_VERBOSE = "verbose"; //$NON-NLS-1$
049: private static final String ATTRIBUTE_IGNORE_ERRORS = "ignoreerrors"; //$NON-NLS-1$
050: private static final String ATTRIBUTE_USE_TIMESTAMP = "usetimestamp"; //$NON-NLS-1$
051: private static final String ATTRIBUTE_USERNAME = "username"; //$NON-NLS-1$
052: private static final String ATTRIBUTE_PASSWORD = "password"; //$NON-NLS-1$
053: private static final String TAG_OPEN = "<"; //$NON-NLS-1$
054: private static final String TAG_CLOSE = "/>"; //$NON-NLS-1$
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.pde.build.IFetchFactory#addTargets(org.eclipse.pde.build.IAntScript)
058: */
059: public void addTargets(IAntScript script) {
060: //
061: }
062:
063: /* (non-Javadoc)
064: * @see org.eclipse.pde.build.IFetchFactory#generateRetrieveElementCall(java.util.Map, org.eclipse.core.runtime.IPath, org.eclipse.pde.build.IAntScript)
065: */
066: public void generateRetrieveElementCall(Map entryInfos,
067: IPath destination, IAntScript script) {
068: printGetTask(destination, script, entryInfos);
069: }
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.pde.build.IFetchFactory#generateRetrieveFilesCall(java.util.Map, org.eclipse.core.runtime.IPath, java.lang.String[], org.eclipse.pde.build.IAntScript)
073: */
074: public void generateRetrieveFilesCall(Map entryInfos,
075: IPath destination, String[] files, IAntScript script) {
076: //
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.pde.build.IFetchFactory#parseMapFileEntry(java.lang.String, java.util.Properties, java.util.Map)
081: */
082: public void parseMapFileEntry(String rawEntry,
083: Properties overrideTags, Map entryInfos)
084: throws CoreException {
085: String url = rawEntry;
086: if (rawEntry.indexOf(',') != -1) {
087: StringTokenizer tokenizer = new StringTokenizer(rawEntry,
088: SEPARATOR);
089: if (tokenizer.hasMoreTokens())
090: url = tokenizer.nextToken();
091: while (tokenizer.hasMoreTokens()) {
092: String token = tokenizer.nextToken();
093: int index = token.indexOf('=');
094: if (index == -1) {
095: // invalid format...we require key=value...log and continue
096: IStatus status = new Status(IStatus.WARNING,
097: IPDEBuildConstants.PI_PDEBUILD,
098: "Problems parsing map file entry: "
099: + rawEntry);
100: BundleHelper.getDefault().getLog().log(status);
101: } else {
102: String key = token.substring(0, index).trim();
103: String value = token.substring(index + 1).trim();
104: entryInfos.put(key, value);
105: }
106: }
107: }
108: try {
109: new URL(url);
110: } catch (MalformedURLException e) {
111: throw new CoreException(new Status(IStatus.ERROR,
112: IPDEBuildConstants.PI_PDEBUILD,
113: "Invalid URL in map file entry: " + rawEntry));
114: }
115: entryInfos.put(ATTRIBUTE_SRC, url);
116: }
117:
118: /*
119: * Print out the Ant GET task to the Ant script.
120: */
121: private void printGetTask(IPath destination, IAntScript script,
122: Map entryInfos) {
123: String src = (String) entryInfos.get(ATTRIBUTE_SRC);
124: int index = src.lastIndexOf('/');
125: String filename = index == -1 ? src : src.substring(index);
126:
127: // "src" attribute is mandatory
128: script.printTabs();
129: script.print(TAG_OPEN + TASK_GET);
130: script.printAttribute(ATTRIBUTE_SRC, src, true);
131:
132: // "dest" attribute is mandatory
133: String dest = (String) entryInfos.get(ATTRIBUTE_DEST);
134: if (dest == null)
135: dest = destination.removeLastSegments(1).append(filename)
136: .toOSString();
137: script.printAttribute(ATTRIBUTE_DEST, dest, true);
138:
139: // the rest of the attributes are optional so check if they exist before writing in the file
140: String ignoreErrors = (String) entryInfos
141: .get(ATTRIBUTE_IGNORE_ERRORS);
142: if (ignoreErrors != null)
143: script.printAttribute(ATTRIBUTE_IGNORE_ERRORS,
144: ignoreErrors, false);
145:
146: String useTimestamp = (String) entryInfos
147: .get(ATTRIBUTE_USE_TIMESTAMP);
148: if (useTimestamp != null)
149: script.printAttribute(ATTRIBUTE_USE_TIMESTAMP,
150: useTimestamp, false);
151:
152: String verbose = (String) entryInfos.get(ATTRIBUTE_VERBOSE);
153: if (verbose != null)
154: script.printAttribute(ATTRIBUTE_VERBOSE, verbose, false);
155:
156: String username = (String) entryInfos.get(ATTRIBUTE_USERNAME);
157: String password = (String) entryInfos.get(ATTRIBUTE_PASSWORD);
158: if (username != null)
159: script.printAttribute(ATTRIBUTE_USERNAME, username,
160: password != null);
161: if (password != null)
162: script.printAttribute(ATTRIBUTE_PASSWORD, password,
163: username != null);
164:
165: script.print(TAG_CLOSE);
166:
167: // if we have a feature or un-packed plug-in then we need to unzip it
168: boolean unpack = Boolean.valueOf(
169: (String) entryInfos.get(UNPACK)).booleanValue();
170: if (unpack
171: || ELEMENT_TYPE_FEATURE.equals(entryInfos
172: .get(KEY_ELEMENT_TYPE))) {
173: script.printTabs();
174: script.print(TAG_OPEN + TASK_UNZIP);
175: script.printAttribute(ATTRIBUTE_SRC, dest, true);
176: script.printAttribute(ATTRIBUTE_DEST, new Path(dest)
177: .removeLastSegments(1).toOSString(), true);
178: script.print(TAG_CLOSE);
179:
180: script.printTabs();
181: script.print(TAG_OPEN + TASK_DELETE);
182: script.printAttribute(ATTRIBUTE_FILE, dest, true);
183: script.print(TAG_CLOSE);
184: }
185: script.println();
186: }
187:
188: }
|