1 11 package org.eclipse.pde.internal.build.packager; 12 13 import java.io.*; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 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 ENTRY_SEPARATOR = "%"; private static final String FILTER_SEPARATOR = "&"; private static final String DATA_SEPARATOR = "|"; 26 private static final String UNKNOWN = "*"; 29 private String [] filters; 30 private String mapLocation; 31 private String collectedFiles; 32 private String [] 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")); System.out.println("Component filter: " + (componentFilter != null ? Utils.getStringFromArray(componentFilter, ", ") : "NONE")); System.out.println("Map location: " + mapLocation); } 44 45 public void generate() throws CoreException { 46 collectedFiles = ""; 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, "."); 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 fileName, String baseurl, String loginInfo) { 73 String login = null; 74 String password = null; 75 String [] login_password = Utils.getArrayFromString(loginInfo, ":"); 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 filters) { 87 this.filters = Utils.getArrayFromStringWithBlank(filters, ","); } 89 90 public void setMapLocation(String mapLocation) { 91 this.mapLocation = mapLocation; 92 } 93 94 private void writeDirectory() throws CoreException { 95 Properties selectedFiles = new Properties(); 96 selectedFiles.put("toUnzip", collectedFiles); 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 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 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); 122 for (Iterator iter = mapContent.entrySet().iterator(); iter.hasNext();) { 123 Map.Entry mapEntry = (Map.Entry) iter.next(); 124 String fileName = (String ) mapEntry.getKey(); 125 String [] fileDescription = Utils.getArrayFromStringWithBlank((String ) mapEntry.getValue(), DATA_SEPARATOR); 126 127 if (fileDescription.length < 4) { 128 String message = NLS.bind(Messages.error_incorrectDirectoryEntry, (String ) mapEntry.getKey() + '=' + (String ) mapEntry.getValue()); 129 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_ENTRY_MISSING, message, null)); 130 } 131 132 String userInfos = ""; try { 135 userInfos = new URL (fileDescription[URL]).getUserInfo(); 136 } catch (MalformedURLException 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; } 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 private boolean filterByFilter(String filterString) { 156 if (filters.length == 0) 157 return true; 158 159 String [] entryFilters = Utils.getArrayFromStringWithBlank(filterString, ","); 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 private boolean filterByConfig(String entryConfigString) { 174 String [] 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 private boolean filterByComponentName(String 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 componentFiler) { 214 this.componentFilter = Utils.getArrayFromStringWithBlank(componentFiler, ","); } 216 } 217 | Popular Tags |