1 11 12 package org.eclipse.core.internal.model; 13 14 import java.io.*; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.Properties ; 18 import org.eclipse.core.internal.runtime.InternalPlatform; 19 import org.eclipse.core.internal.runtime.Messages; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.core.runtime.model.*; 22 import org.eclipse.osgi.util.NLS; 23 import org.xml.sax.InputSource ; 24 import org.xml.sax.SAXParseException ; 25 26 public class RegistryLoader { 27 private Factory factory; 28 29 private boolean debug = false; 31 private long lastTick = System.currentTimeMillis(); 33 private RegistryLoader(Factory factory, boolean debug) { 34 super(); 35 this.debug = debug; 36 this.factory = factory; 37 } 38 39 private void debug(String msg) { 40 long thisTick = System.currentTimeMillis(); 41 System.out.println("RegistryLoader: " + msg + " [+" + (thisTick - lastTick) + "ms]"); lastTick = thisTick; 43 } 44 45 private String [] getPathMembers(URL path) { 46 String [] list = null; 47 String protocol = path.getProtocol(); 48 if (protocol.equals("file")) { list = (new File(path.getFile())).list(); 50 } else { 51 } 53 return list == null ? new String [0] : list; 54 } 55 56 59 private boolean parseProblem(String message) { 60 factory.error(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, message, null)); 61 return true; 62 } 63 64 private PluginRegistryModel parseRegistry(URL [] pluginPath) { 65 long startTick = System.currentTimeMillis(); 66 PluginRegistryModel result = processManifestFiles(pluginPath); 67 if (InternalPlatform.DEBUG) { 68 long endTick = System.currentTimeMillis(); 69 debug("Parsed Registry: " + (endTick - startTick) + "ms"); } 71 return result; 72 } 73 74 public static PluginRegistryModel parseRegistry(URL [] pluginPath, Factory factory, boolean debug) { 75 return new RegistryLoader(factory, debug).parseRegistry(pluginPath); 76 } 77 78 private PluginModel processManifestFile(URL manifest) { 79 InputStream is = null; 80 try { 81 is = manifest.openStream(); 82 } catch (IOException e) { 83 if (debug) 84 debug("No plugin found for: " + manifest); return null; 86 } 87 PluginModel result = null; 88 try { 89 try { 90 InputSource in = new InputSource (is); 91 in.setSystemId(manifest.getFile()); 94 result = new PluginParser((Factory) factory).parsePlugin(in); 95 } finally { 96 is.close(); 97 } 98 } catch (SAXParseException se) { 99 100 factory.error(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, NLS.bind(Messages.parse_errorProcessing, manifest), null)); 101 } catch (Exception e) { 102 factory.error(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, NLS.bind(Messages.parse_errorProcessing, manifest + ": " + e.getMessage()), null)); } 104 return result; 105 } 106 107 private PluginRegistryModel processManifestFiles(URL [] pluginPath) { 108 PluginRegistryModel result = factory.createPluginRegistry(); 109 for (int i = 0; i < pluginPath.length; i++) 110 processPluginPathEntry(result, pluginPath[i]); 111 return result; 112 } 113 114 private void processPluginPathEntry(PluginRegistryModel registry, URL location) { 115 if (debug) 116 debug("Path - " + location); if (location.getFile().endsWith("/")) { String [] members = getPathMembers(location); 120 for (int j = 0; j < members.length; j++) { 121 boolean found = false; 122 try { 123 found = processPluginPathFile(registry, new URL (location, members[j] + "/plugin.xml")); if (!found) 125 found = processPluginPathFile(registry, new URL (location, members[j] + "/fragment.xml")); } catch (MalformedURLException e) { 127 } 129 if (debug) 130 debug(found ? "Processed - " : "Processed (not found) - " + members[j]); } 132 } else { 133 boolean found = processPluginPathFile(registry, location); 135 if (debug) 136 debug(found ? "Processed - " : "Processed (not found) - " + location); } 138 } 139 140 143 private boolean processPluginPathFile(PluginRegistryModel registry, URL location) { 144 PluginModel entry = processManifestFile(location); 145 if (entry == null) 146 return false; 147 if (!requiredPluginModel(entry, location)) { 151 entry = null; 152 return false; 153 } 154 entry.setVersion(getQualifiedVersion(entry, location)); if (entry instanceof PluginDescriptorModel) { 156 if (entry.getId() == null || entry.getVersion() == null) { 157 return parseProblem(NLS.bind(Messages.parse_nullPluginIdentifier, location)); 158 } 159 if (registry.getPlugin(entry.getId(), entry.getVersion()) != null) { 161 return parseProblem(NLS.bind(Messages.parse_duplicatePlugin, entry.getId(), location)); 162 } 163 registry.addPlugin((PluginDescriptorModel) entry); 164 } else { 165 if (entry.getId() == null || entry.getVersion() == null) { 166 return parseProblem(NLS.bind(Messages.parse_nullFragmentIdentifier, location)); 167 } 168 if (entry instanceof PluginFragmentModel) { 169 registry.addFragment((PluginFragmentModel) entry); 170 } else { 171 return parseProblem(NLS.bind(Messages.parse_unknownEntry, location)); 172 } 173 } 174 String url = location.toString(); 175 url = url.substring(0, 1 + url.lastIndexOf('/')); 176 entry.setRegistry(registry); 177 entry.setLocation(url); 178 return true; 181 } 182 183 private String getQualifiedVersion(PluginModel entry, URL base) { 184 if (entry == null || entry.getVersion() == null || entry.getId() == null) 185 return null; 186 187 InputStream is = null; 188 try { 189 URL manifest = null; 191 manifest = new URL (base, "buildmanifest.properties"); Properties props = new Properties (); 193 is = manifest.openStream(); 194 props.load(is); 195 196 String key = "plugin@" + entry.getId(); String qualifier = props.getProperty(key); 199 if (qualifier == null) 200 return entry.getVersion(); 201 PluginVersionIdentifier v = new PluginVersionIdentifier(entry.getVersion()); 202 if (!v.getQualifierComponent().equals("")) return entry.getVersion(); 204 else 205 return (new PluginVersionIdentifier(v.getMajorComponent(), v.getMinorComponent(), v.getServiceComponent(), qualifier)).toString(); 206 } catch (Exception e) { 207 return entry.getVersion(); 208 } finally { 209 if (is != null) 210 try { 211 is.close(); 212 } catch (IOException e) { 213 } 215 } 216 } 217 218 private boolean requiredPluginModel(PluginModel plugin, URL location) { 219 String name = plugin.getName(); 220 String id = plugin.getId(); 221 String version = plugin.getVersion(); 222 int nameLength = name == null ? 0 : name.length(); 223 int idLength = id == null ? 0 : id.length(); 224 int versionLength = version == null ? 0 : version.length(); 225 226 if (nameLength <= 0) { 227 parseProblem(NLS.bind(Messages.parse_missingPluginName, location)); 228 return false; 229 } 230 if (idLength <= 0) { 231 parseProblem(NLS.bind(Messages.parse_missingPluginId, location)); 232 return false; 233 } 234 if (versionLength <= 0) { 235 parseProblem(NLS.bind(Messages.parse_missingPluginVersion, location)); 236 return false; 237 } 238 239 if (plugin instanceof PluginFragmentModel) { 240 String pluginName = ((PluginFragmentModel) plugin).getPlugin(); 241 String pluginVersion = ((PluginFragmentModel) plugin).getPluginVersion(); 242 int pNameLength = pluginName == null ? 0 : pluginName.length(); 243 int pNameVersion = pluginVersion == null ? 0 : pluginVersion.length(); 244 if (pNameLength <= 0) { 245 parseProblem(NLS.bind(Messages.parse_missingFPName, location)); 246 return false; 247 } 248 if (pNameVersion <= 0) { 249 parseProblem(NLS.bind(Messages.parse_missingFPVersion, location)); 250 return false; 251 } 252 } 253 return true; 254 } 255 } 256 | Popular Tags |