KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > packager > FetchFileGenerator


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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build.packager;
12
13 import java.io.*;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.pde.internal.build.*;
20
21 public class FetchFileGenerator extends AbstractScriptGenerator {
22     private static final String JavaDoc ENTRY_SEPARATOR = "%"; //$NON-NLS-1$
23
private static final String JavaDoc FILTER_SEPARATOR = "&"; //$NON-NLS-1$
24
private static final String JavaDoc DATA_SEPARATOR = "|"; //$NON-NLS-1$
25

26     // Unknown component name
27
private static final String JavaDoc UNKNOWN = "*"; //$NON-NLS-1$
28

29     private String JavaDoc[] filters;
30     private String JavaDoc mapLocation;
31     private String JavaDoc collectedFiles;
32     private String JavaDoc[] componentFilter;
33
34     private Properties mapContent;
35
36     private void displayDebugInfo() {
37         if (!BundleHelper.getDefault().isDebugging())
38             return;
39
40         System.out.println("Filters: " + (filters != null ? Utils.getStringFromArray(filters, ", ") : "NONE")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
41
System.out.println("Component filter: " + (componentFilter != null ? Utils.getStringFromArray(componentFilter, ", ") : "NONE")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
42
System.out.println("Map location: " + mapLocation); //$NON-NLS-1$
43
}
44
45     public void generate() throws CoreException {
46         collectedFiles = ""; //$NON-NLS-1$
47
displayDebugInfo();
48
49         openScript(workingDirectory, DEFAULT_FETCH_SCRIPT_FILENAME);
50         generatePrologue();
51         try {
52             processMapFile();
53             writeDirectory();
54             generateEpilogue();
55         } finally {
56             closeScript();
57         }
58     }
59
60     private void generatePrologue() {
61         script.printProjectDeclaration("Packager' File fetcher", TARGET_MAIN, "."); //$NON-NLS-1$ //$NON-NLS-2$
62
script.println();
63         script.printTargetDeclaration(TARGET_MAIN, null, null, null, null);
64     }
65
66     private void generateEpilogue() {
67         script.printTargetEnd();
68         script.printProjectEnd();
69         script.close();
70     }
71
72     public void generateFetchFileFor(String JavaDoc fileName, String JavaDoc baseurl, String JavaDoc loginInfo) {
73         String JavaDoc login = null;
74         String JavaDoc password = null;
75         String JavaDoc[] login_password = Utils.getArrayFromString(loginInfo, ":"); //$NON-NLS-1$
76
if (login_password.length == 2) {
77             login = login_password[0];
78             password = login_password[1];
79         } else {
80             IStatus status = new Status(IStatus.WARNING, PI_PDEBUILD, 1, NLS.bind(Messages.warning_missingPassword, fileName), null);
81             BundleHelper.getDefault().getLog().log(status);
82         }
83         script.printGet(baseurl + fileName, Utils.getPropertyFormat(PROPERTY_DOWNLOAD_DIRECTORY) + '/' + fileName, login, password, true);
84     }
85
86     public void setContentFilter(String JavaDoc filters) {
87         this.filters = Utils.getArrayFromStringWithBlank(filters, ","); //$NON-NLS-1$
88
}
89
90     public void setMapLocation(String JavaDoc mapLocation) {
91         this.mapLocation = mapLocation;
92     }
93
94     private void writeDirectory() throws CoreException {
95         Properties selectedFiles = new Properties();
96         selectedFiles.put("toUnzip", collectedFiles); //$NON-NLS-1$
97
try {
98             OutputStream stream = new BufferedOutputStream(new FileOutputStream(workingDirectory + '/' + DEFAULT_PACKAGER_DIRECTORY_FILENAME_DESCRIPTOR));
99             try {
100                 selectedFiles.store(stream, null);
101             } finally {
102                 stream.close();
103             }
104         } catch (FileNotFoundException e) {
105             String JavaDoc message = NLS.bind(Messages.exception_writingFile, workingDirectory + '/' + DEFAULT_PACKAGER_DIRECTORY_FILENAME_DESCRIPTOR);
106             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_WRITING_FILE, message, e));
107         } catch (IOException e) {
108             String JavaDoc message = NLS.bind(Messages.exception_writingFile, workingDirectory + '/' + DEFAULT_PACKAGER_DIRECTORY_FILENAME_DESCRIPTOR);
109             throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_WRITING_FILE, message, e));
110         }
111     }
112
113     private void processMapFile() throws CoreException {
114         final int URL = 0;
115         final int CONFIGS = 1;
116         final int DIRECTORY = 2;
117         final int FILTERS = 3;
118         final int COMPONENT = 4;
119
120         mapContent = readProperties(mapLocation, "", IStatus.ERROR); //$NON-NLS-1$
121

122         for (Iterator iter = mapContent.entrySet().iterator(); iter.hasNext();) {
123             Map.Entry mapEntry = (Map.Entry) iter.next();
124             String JavaDoc fileName = (String JavaDoc) mapEntry.getKey();
125             String JavaDoc[] fileDescription = Utils.getArrayFromStringWithBlank((String JavaDoc) mapEntry.getValue(), DATA_SEPARATOR);
126
127             if (fileDescription.length < 4) {
128                 String JavaDoc message = NLS.bind(Messages.error_incorrectDirectoryEntry, (String JavaDoc) mapEntry.getKey() + '=' + (String JavaDoc) mapEntry.getValue());
129                 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_ENTRY_MISSING, message, null));
130             }
131
132             // check if the entry can be used for the current config
133
String JavaDoc userInfos = ""; //$NON-NLS-1$
134
try {
135                 userInfos = new URL JavaDoc(fileDescription[URL]).getUserInfo();
136             } catch (MalformedURLException JavaDoc e) {
137                 IStatus status = new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_MALFORMED_URL, NLS.bind(Messages.exception_url, fileDescription[URL]), e);
138                 throw new CoreException(status);
139             }
140
141             if (filterByConfig(fileDescription[CONFIGS]) && filterByFilter(fileDescription[FILTERS]) && filterByComponentName(fileDescription.length > 4 ? fileDescription[COMPONENT] : UNKNOWN)) {
142                 generateFetchFileFor(fileName, fileDescription[URL], userInfos);
143                 collectedFiles += fileName + DATA_SEPARATOR + (fileDescription[DIRECTORY].equals("") ? "." : fileDescription[DIRECTORY]) + DATA_SEPARATOR + fileDescription[CONFIGS] + ENTRY_SEPARATOR; //$NON-NLS-1$ //$NON-NLS-2$
144
} else {
145                 if (BundleHelper.getDefault().isDebugging()) {
146                     IStatus status = new Status(IStatus.INFO, PI_PDEBUILD, WARNING_ELEMENT_NOT_FETCHED, NLS.bind(Messages.error_fetchingFailed, fileDescription[DIRECTORY]), null);
147                     BundleHelper.getDefault().getLog().log(status);
148                 }
149             }
150         }
151     }
152
153     //Return true if the filters specified to be packaged match the entry.
154
//When no filter is specified on the entry or there is no filtering, then the file is fetched
155
private boolean filterByFilter(String JavaDoc filterString) {
156         if (filters.length == 0)
157             return true;
158
159         String JavaDoc[] entryFilters = Utils.getArrayFromStringWithBlank(filterString, ","); //$NON-NLS-1$
160
if (entryFilters.length == 0)
161             return true;
162
163         for (int i = 0; i < entryFilters.length; i++) {
164             for (int j = 0; j < filters.length; j++) {
165                 if (filters[j].equals(entryFilters[i]))
166                     return true;
167             }
168         }
169         return false;
170     }
171
172     //Return true, if the entryConfigs match the config we are packaging
173
private boolean filterByConfig(String JavaDoc entryConfigString) {
174         String JavaDoc[] entryConfigs = Utils.getArrayFromStringWithBlank(entryConfigString, FILTER_SEPARATOR);
175         if (entryConfigs.length == 0 || containsGenericConfig(getConfigInfos()))
176             return true;
177
178         for (int i = 0; i < entryConfigs.length; i++) {
179             Iterator iter = getConfigInfos().iterator();
180             Config aConfig = new Config(entryConfigs[i]);
181             while (iter.hasNext()) {
182                 if (aConfig.equals(iter.next()) || aConfig.equals(Config.genericConfig())) {
183                     return true;
184                 }
185             }
186         }
187         return false;
188     }
189
190     boolean containsGenericConfig(List configs) {
191         if (configs == null)
192             return false;
193         Iterator iter = configs.iterator();
194         while (iter.hasNext()) {
195             if (Config.genericConfig().equals(iter.next()))
196                 return true;
197         }
198         return false;
199     }
200     
201     //Return true if the componentName is listed in the component filter, or if no filter is specified
202
private boolean filterByComponentName(String JavaDoc componentName) {
203         if (componentName.equals(UNKNOWN) || componentFilter == null)
204             return true;
205
206         for (int i = 0; i < componentFilter.length; i++) {
207             if (componentFilter[i].equalsIgnoreCase(componentName) || componentFilter[i].equalsIgnoreCase(UNKNOWN))
208                 return true;
209         }
210         return false;
211     }
212
213     public void setComponentFilter(String JavaDoc componentFiler) {
214         this.componentFilter = Utils.getArrayFromStringWithBlank(componentFiler, ","); //$NON-NLS-1$
215
}
216 }
217
Popular Tags