KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2007 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  *******************************************************************************/

11 package org.eclipse.pde.internal.build.fetch;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.*;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.pde.build.IAntScript;
18 import org.eclipse.pde.build.IFetchFactory;
19 import org.eclipse.pde.internal.build.*;
20
21 /**
22  * This class implements a fetch factory which calls the Ant Get task on a given URL. The
23  * format of the map file entry is as follows:
24  * type@id=GET,url,[args]
25  * where:
26  * type = feature | plugin
27  * id = plug-in or feature identifier (symbolic name)
28  * GET = mandatory constant (to call this fetch factory)
29  * url = url to retrieve the data from (suitable to be used in the Ant Get task)
30  * args = optional comma-separated list of key/value pairs, specify unpack=true to indicate the element should be unzipped
31  *
32  * e.g.
33  * plugin@com.example.foobar=GET,http://downloads.example.com/com.example.foobar_1.0.jar
34  * plugin@com.example.foobar=GET,http://downloads.example.com/com.example.foobar_1.0.jar,unpack=true
35  * plugin@com.example.foobar=GET,http://downloads.example.com/com.example.foobar_1.0.zip,unpack=true, username=foo, password=bar
36  *
37  * @since 3.2.100
38  */

39 public class GETFetchFactory implements IFetchFactory {
40
41     private static final String JavaDoc UNPACK = "unpack"; //$NON-NLS-1$
42
private static final String JavaDoc SEPARATOR = ","; //$NON-NLS-1$
43
private static final String JavaDoc TASK_GET = "get"; //$NON-NLS-1$
44
private static final String JavaDoc TASK_DELETE = "delete"; //$NON-NLS-1$
45
private static final String JavaDoc TASK_UNZIP = "unzip"; //$NON-NLS-1$
46
private static final String JavaDoc ATTRIBUTE_SRC = "src"; //$NON-NLS-1$
47
private static final String JavaDoc ATTRIBUTE_DEST = "dest"; //$NON-NLS-1$
48
private static final String JavaDoc ATTRIBUTE_FILE = "file"; //$NON-NLS-1$
49
private static final String JavaDoc ATTRIBUTE_VERBOSE = "verbose"; //$NON-NLS-1$
50
private static final String JavaDoc ATTRIBUTE_IGNORE_ERRORS = "ignoreerrors"; //$NON-NLS-1$
51
private static final String JavaDoc ATTRIBUTE_USE_TIMESTAMP = "usetimestamp"; //$NON-NLS-1$
52
private static final String JavaDoc ATTRIBUTE_USERNAME = "username"; //$NON-NLS-1$
53
private static final String JavaDoc ATTRIBUTE_PASSWORD = "password"; //$NON-NLS-1$
54
private static final String JavaDoc TAG_OPEN = "<"; //$NON-NLS-1$
55
private static final String JavaDoc TAG_CLOSE = "/>"; //$NON-NLS-1$
56

57     /* (non-Javadoc)
58      * @see org.eclipse.pde.build.IFetchFactory#addTargets(org.eclipse.pde.build.IAntScript)
59      */

60     public void addTargets(IAntScript script) {
61         //
62
}
63
64     /* (non-Javadoc)
65      * @see org.eclipse.pde.build.IFetchFactory#generateRetrieveElementCall(java.util.Map, org.eclipse.core.runtime.IPath, org.eclipse.pde.build.IAntScript)
66      */

67     public void generateRetrieveElementCall(Map entryInfos, IPath destination, IAntScript script) {
68         printGetTask(destination, script, entryInfos);
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.pde.build.IFetchFactory#generateRetrieveFilesCall(java.util.Map, org.eclipse.core.runtime.IPath, java.lang.String[], org.eclipse.pde.build.IAntScript)
73      */

74     public void generateRetrieveFilesCall(Map entryInfos, IPath destination, String JavaDoc[] files, IAntScript script) {
75         //
76
}
77
78     /* (non-Javadoc)
79      * @see org.eclipse.pde.build.IFetchFactory#parseMapFileEntry(java.lang.String, java.util.Properties, java.util.Map)
80      */

81     public void parseMapFileEntry(String JavaDoc rawEntry, Properties overrideTags, Map entryInfos) throws CoreException {
82         String JavaDoc url = rawEntry;
83         if (rawEntry.indexOf(',') != -1) {
84             StringTokenizer tokenizer = new StringTokenizer(rawEntry, SEPARATOR);
85             if (tokenizer.hasMoreTokens())
86                 url = tokenizer.nextToken();
87             while (tokenizer.hasMoreTokens()) {
88                 String JavaDoc token = tokenizer.nextToken();
89                 int index = token.indexOf('=');
90                 if (index == -1) {
91                     // invalid format...we require key=value...log and continue
92
IStatus status = new Status(IStatus.WARNING, IPDEBuildConstants.PI_PDEBUILD, "Problems parsing map file entry: " + rawEntry);
93                     BundleHelper.getDefault().getLog().log(status);
94                 } else {
95                     String JavaDoc key = token.substring(0, index).trim();
96                     String JavaDoc value = token.substring(index + 1).trim();
97                     entryInfos.put(key, value);
98                 }
99             }
100         }
101         try {
102             new URL JavaDoc(url);
103         } catch (MalformedURLException JavaDoc e) {
104             throw new CoreException(new Status(IStatus.ERROR, IPDEBuildConstants.PI_PDEBUILD, "Invalid URL in map file entry: " + rawEntry));
105         }
106         entryInfos.put(ATTRIBUTE_SRC, url);
107     }
108
109     /*
110      * Print out the Ant GET task to the Ant script.
111      */

112     private void printGetTask(IPath destination, IAntScript script, Map entryInfos) {
113         String JavaDoc src = (String JavaDoc) entryInfos.get(ATTRIBUTE_SRC);
114         int index = src.lastIndexOf('/');
115         String JavaDoc filename = index == -1 ? src : src.substring(index);
116
117         // "src" attribute is mandatory
118
script.printTabs();
119         script.print(TAG_OPEN + TASK_GET);
120         script.printAttribute(ATTRIBUTE_SRC, src, true);
121
122         // "dest" attribute is mandatory
123
String JavaDoc dest = (String JavaDoc) entryInfos.get(ATTRIBUTE_DEST);
124         if (dest == null)
125             dest = destination.removeLastSegments(1).append(filename).toOSString();
126         script.printAttribute(ATTRIBUTE_DEST, dest, true);
127
128         // the rest of the attributes are optional so check if they exist before writing in the file
129
String JavaDoc ignoreErrors = (String JavaDoc) entryInfos.get(ATTRIBUTE_IGNORE_ERRORS);
130         if (ignoreErrors != null)
131             script.printAttribute(ATTRIBUTE_IGNORE_ERRORS, ignoreErrors, false);
132
133         String JavaDoc useTimestamp = (String JavaDoc) entryInfos.get(ATTRIBUTE_USE_TIMESTAMP);
134         if (useTimestamp != null)
135             script.printAttribute(ATTRIBUTE_USE_TIMESTAMP, useTimestamp, false);
136
137         String JavaDoc verbose = (String JavaDoc) entryInfos.get(ATTRIBUTE_VERBOSE);
138         if (verbose != null)
139             script.printAttribute(ATTRIBUTE_VERBOSE, verbose, false);
140
141         String JavaDoc username = (String JavaDoc) entryInfos.get(ATTRIBUTE_USERNAME);
142         String JavaDoc password = (String JavaDoc) entryInfos.get(ATTRIBUTE_PASSWORD);
143         if (username != null)
144             script.printAttribute(ATTRIBUTE_USERNAME, username, password != null);
145         if (password != null)
146             script.printAttribute(ATTRIBUTE_PASSWORD, password, username != null);
147
148         script.print(TAG_CLOSE);
149
150         // if we have a feature or un-packed plug-in then we need to unzip it
151
boolean unpack = Boolean.valueOf((String JavaDoc) entryInfos.get(UNPACK)).booleanValue();
152         if (unpack || ELEMENT_TYPE_FEATURE.equals(entryInfos.get(KEY_ELEMENT_TYPE))) {
153             script.printTabs();
154             script.print(TAG_OPEN + TASK_UNZIP);
155             script.printAttribute(ATTRIBUTE_SRC, dest, true);
156             script.printAttribute(ATTRIBUTE_DEST, new Path(dest).removeLastSegments(1).toOSString(), true);
157             script.print(TAG_CLOSE);
158
159             script.printTabs();
160             script.print(TAG_OPEN + TASK_DELETE);
161             script.printAttribute(ATTRIBUTE_FILE, dest, true);
162             script.print(TAG_CLOSE);
163         }
164         script.println();
165     }
166
167 }
168
Popular Tags