001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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 - Initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build.packager;
011:
012: import java.io.*;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015: import java.util.*;
016: import org.eclipse.core.runtime.*;
017: import org.eclipse.osgi.util.NLS;
018: import org.eclipse.pde.internal.build.*;
019:
020: public class FetchFileGenerator extends AbstractScriptGenerator {
021: private static final String ENTRY_SEPARATOR = "%"; //$NON-NLS-1$
022: private static final String FILTER_SEPARATOR = "&"; //$NON-NLS-1$
023: private static final String DATA_SEPARATOR = "|"; //$NON-NLS-1$
024:
025: // Unknown component name
026: private static final String UNKNOWN = "*"; //$NON-NLS-1$
027:
028: private String[] filters;
029: private String mapLocation;
030: private String collectedFiles;
031: private String[] componentFilter;
032:
033: private Properties mapContent;
034:
035: private void displayDebugInfo() {
036: if (!BundleHelper.getDefault().isDebugging())
037: return;
038:
039: System.out
040: .println("Filters: " + (filters != null ? Utils.getStringFromArray(filters, ", ") : "NONE")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
041: System.out
042: .println("Component filter: " + (componentFilter != null ? Utils.getStringFromArray(componentFilter, ", ") : "NONE")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
043: System.out.println("Map location: " + mapLocation); //$NON-NLS-1$
044: }
045:
046: public void generate() throws CoreException {
047: collectedFiles = ""; //$NON-NLS-1$
048: displayDebugInfo();
049:
050: openScript(workingDirectory, DEFAULT_FETCH_SCRIPT_FILENAME);
051: generatePrologue();
052: try {
053: processMapFile();
054: writeDirectory();
055: generateEpilogue();
056: } finally {
057: closeScript();
058: }
059: }
060:
061: private void generatePrologue() {
062: script.printProjectDeclaration(
063: "Packager' File fetcher", TARGET_MAIN, "."); //$NON-NLS-1$ //$NON-NLS-2$
064: script.println();
065: script.printTargetDeclaration(TARGET_MAIN, null, null, null,
066: null);
067: }
068:
069: private void generateEpilogue() {
070: script.printTargetEnd();
071: script.printProjectEnd();
072: script.close();
073: }
074:
075: public void generateFetchFileFor(String fileName, String baseurl,
076: String loginInfo) {
077: String login = null;
078: String password = null;
079: String[] login_password = Utils.getArrayFromString(loginInfo,
080: ":"); //$NON-NLS-1$
081: if (login_password.length == 2) {
082: login = login_password[0];
083: password = login_password[1];
084: } else {
085: IStatus status = new Status(IStatus.WARNING, PI_PDEBUILD,
086: 1, NLS.bind(Messages.warning_missingPassword,
087: fileName), null);
088: BundleHelper.getDefault().getLog().log(status);
089: }
090: script.printGet(baseurl + fileName, Utils
091: .getPropertyFormat(PROPERTY_DOWNLOAD_DIRECTORY)
092: + '/' + fileName, login, password, true);
093: }
094:
095: public void setContentFilter(String filters) {
096: this .filters = Utils.getArrayFromStringWithBlank(filters, ","); //$NON-NLS-1$
097: }
098:
099: public void setMapLocation(String mapLocation) {
100: this .mapLocation = mapLocation;
101: }
102:
103: private void writeDirectory() throws CoreException {
104: Properties selectedFiles = new Properties();
105: selectedFiles.put("toUnzip", collectedFiles); //$NON-NLS-1$
106: try {
107: OutputStream stream = new BufferedOutputStream(
108: new FileOutputStream(
109: workingDirectory
110: + '/'
111: + DEFAULT_PACKAGER_DIRECTORY_FILENAME_DESCRIPTOR));
112: try {
113: selectedFiles.store(stream, null);
114: } finally {
115: stream.close();
116: }
117: } catch (FileNotFoundException e) {
118: String message = NLS
119: .bind(
120: Messages.exception_writingFile,
121: workingDirectory
122: + '/'
123: + DEFAULT_PACKAGER_DIRECTORY_FILENAME_DESCRIPTOR);
124: throw new CoreException(new Status(IStatus.ERROR,
125: PI_PDEBUILD, EXCEPTION_WRITING_FILE, message, e));
126: } catch (IOException e) {
127: String message = NLS
128: .bind(
129: Messages.exception_writingFile,
130: workingDirectory
131: + '/'
132: + DEFAULT_PACKAGER_DIRECTORY_FILENAME_DESCRIPTOR);
133: throw new CoreException(new Status(IStatus.ERROR,
134: PI_PDEBUILD, EXCEPTION_WRITING_FILE, message, e));
135: }
136: }
137:
138: private void processMapFile() throws CoreException {
139: final int URL = 0;
140: final int CONFIGS = 1;
141: final int DIRECTORY = 2;
142: final int FILTERS = 3;
143: final int COMPONENT = 4;
144:
145: mapContent = readProperties(mapLocation, "", IStatus.ERROR); //$NON-NLS-1$
146:
147: for (Iterator iter = mapContent.entrySet().iterator(); iter
148: .hasNext();) {
149: Map.Entry mapEntry = (Map.Entry) iter.next();
150: String fileName = (String) mapEntry.getKey();
151: String[] fileDescription = Utils
152: .getArrayFromStringWithBlank((String) mapEntry
153: .getValue(), DATA_SEPARATOR);
154:
155: if (fileDescription.length < 4) {
156: String message = NLS.bind(
157: Messages.error_incorrectDirectoryEntry,
158: (String) mapEntry.getKey() + '='
159: + (String) mapEntry.getValue());
160: throw new CoreException(new Status(IStatus.ERROR,
161: PI_PDEBUILD, EXCEPTION_ENTRY_MISSING, message,
162: null));
163: }
164:
165: // check if the entry can be used for the current config
166: String userInfos = ""; //$NON-NLS-1$
167: try {
168: userInfos = new URL(fileDescription[URL]).getUserInfo();
169: } catch (MalformedURLException e) {
170: IStatus status = new Status(IStatus.ERROR, PI_PDEBUILD,
171: EXCEPTION_MALFORMED_URL, NLS.bind(
172: Messages.exception_url,
173: fileDescription[URL]), e);
174: throw new CoreException(status);
175: }
176:
177: if (filterByConfig(fileDescription[CONFIGS])
178: && filterByFilter(fileDescription[FILTERS])
179: && filterByComponentName(fileDescription.length > 4 ? fileDescription[COMPONENT]
180: : UNKNOWN)) {
181: generateFetchFileFor(fileName, fileDescription[URL],
182: userInfos);
183: collectedFiles += fileName
184: + DATA_SEPARATOR
185: + (fileDescription[DIRECTORY].equals("") ? "." : fileDescription[DIRECTORY]) + DATA_SEPARATOR + fileDescription[CONFIGS] + ENTRY_SEPARATOR; //$NON-NLS-1$ //$NON-NLS-2$
186: } else {
187: if (BundleHelper.getDefault().isDebugging()) {
188: IStatus status = new Status(IStatus.INFO,
189: PI_PDEBUILD, WARNING_ELEMENT_NOT_FETCHED,
190: NLS.bind(Messages.error_fetchingFailed,
191: fileDescription[DIRECTORY]), null);
192: BundleHelper.getDefault().getLog().log(status);
193: }
194: }
195: }
196: }
197:
198: //Return true if the filters specified to be packaged match the entry.
199: //When no filter is specified on the entry or there is no filtering, then the file is fetched
200: private boolean filterByFilter(String filterString) {
201: if (filters.length == 0)
202: return true;
203:
204: String[] entryFilters = Utils.getArrayFromStringWithBlank(
205: filterString, ","); //$NON-NLS-1$
206: if (entryFilters.length == 0)
207: return true;
208:
209: for (int i = 0; i < entryFilters.length; i++) {
210: for (int j = 0; j < filters.length; j++) {
211: if (filters[j].equals(entryFilters[i]))
212: return true;
213: }
214: }
215: return false;
216: }
217:
218: //Return true, if the entryConfigs match the config we are packaging
219: private boolean filterByConfig(String entryConfigString) {
220: String[] entryConfigs = Utils.getArrayFromStringWithBlank(
221: entryConfigString, FILTER_SEPARATOR);
222: if (entryConfigs.length == 0
223: || containsGenericConfig(getConfigInfos()))
224: return true;
225:
226: for (int i = 0; i < entryConfigs.length; i++) {
227: Iterator iter = getConfigInfos().iterator();
228: Config aConfig = new Config(entryConfigs[i]);
229: while (iter.hasNext()) {
230: if (aConfig.equals(iter.next())
231: || aConfig.equals(Config.genericConfig())) {
232: return true;
233: }
234: }
235: }
236: return false;
237: }
238:
239: boolean containsGenericConfig(List configs) {
240: if (configs == null)
241: return false;
242: Iterator iter = configs.iterator();
243: while (iter.hasNext()) {
244: if (Config.genericConfig().equals(iter.next()))
245: return true;
246: }
247: return false;
248: }
249:
250: //Return true if the componentName is listed in the component filter, or if no filter is specified
251: private boolean filterByComponentName(String componentName) {
252: if (componentName.equals(UNKNOWN) || componentFilter == null)
253: return true;
254:
255: for (int i = 0; i < componentFilter.length; i++) {
256: if (componentFilter[i].equalsIgnoreCase(componentName)
257: || componentFilter[i].equalsIgnoreCase(UNKNOWN))
258: return true;
259: }
260: return false;
261: }
262:
263: public void setComponentFilter(String componentFiler) {
264: this .componentFilter = Utils.getArrayFromStringWithBlank(
265: componentFiler, ","); //$NON-NLS-1$
266: }
267: }
|