1 11 package org.eclipse.core.launcher; 12 13 import java.io.IOException ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.*; 17 import java.util.jar.JarFile ; 18 import java.util.jar.Manifest ; 19 20 24 public class WebStartMain extends Main { 26 private static final String PROP_WEBSTART_AUTOMATIC_INSTALLATION = "eclipse.webstart.automaticInstallation"; private static final String DEFAULT_OSGI_BUNDLES = "org.eclipse.equinox.common@2:start, org.eclipse.core.runtime@start"; private static final String PROP_OSGI_BUNDLES = "osgi.bundles"; private static final String PROP_WEBSTART_PRECISE_BUNDLEID = "eclipse.webstart.preciseBundleId"; 31 32 private String [] allJars = null; private Map bundleList = null; private Map bundleStartInfo = null; 36 private boolean preciseIdExtraction = false; 38 public static void main(String [] args) { 39 System.setSecurityManager(null); int result = new WebStartMain().run(args); 41 System.exit(result); 42 } 43 44 private void setDefaultBundles() { 45 if (System.getProperty(PROP_OSGI_BUNDLES) != null) 46 return; 47 System.getProperties().put(PROP_OSGI_BUNDLES, DEFAULT_OSGI_BUNDLES); 48 } 49 50 protected void basicRun(String [] args) throws Exception { 51 preciseIdExtraction = Boolean.getBoolean(PROP_WEBSTART_PRECISE_BUNDLEID); 52 setDefaultBundles(); 53 addOSGiBundle(); 54 initializeBundleListStructure(); 55 mapURLsToBundleList(); 56 String fwkURL = searchFor(framework, null); 58 System.getProperties().put(PROP_FRAMEWORK, fwkURL); 59 super.basicRun(args); 60 } 61 62 private void addOSGiBundle() { 63 System.getProperties().put(PROP_OSGI_BUNDLES, System.getProperty(PROP_OSGI_BUNDLES) + ',' + framework); 65 } 66 67 protected URL [] getBootPath(String base) throws IOException { 68 URL [] result = super.getBootPath(base); 69 buildOSGiBundleList(); 70 cleanup(); 71 return result; 72 } 73 74 77 private void cleanup() { 78 allJars = null; 79 bundleList = null; 80 bundleStartInfo = null; 81 } 82 83 87 protected String searchFor(final String target, String start) { 88 ArrayList matches = (ArrayList) bundleList.get(target); 89 int numberOfURLs = matches.size(); 90 if (numberOfURLs == 1) { 91 return extractInnerURL((String ) matches.get(0)); 92 } 93 if (numberOfURLs == 0) 94 return null; 95 String urls[] = new String [numberOfURLs]; 96 return extractInnerURL(urls[findMax((String []) matches.toArray(urls))]); 97 } 98 99 102 private String [] getAllJars() { 103 if (allJars != null) 104 return allJars; 105 106 ArrayList collector = new ArrayList(); 107 try { 108 Enumeration resources = WebStartMain.class.getClassLoader().getResources(JarFile.MANIFEST_NAME); 109 while (resources.hasMoreElements()) { 110 collector.add(((URL ) resources.nextElement()).toExternalForm()); 111 } 112 } catch (IOException e) { 113 e.printStackTrace(); 114 } 115 allJars = new String [collector.size()]; 116 collector.toArray(allJars); 117 if (debug) 118 printArray("Jars found on the webstart path:\n", allJars); 120 return allJars; 121 } 122 123 126 private String extractInnerURL(String url) { 127 if (url.startsWith(JAR_SCHEME)) { 128 url = url.substring(url.indexOf(JAR_SCHEME) + 4); 129 } 130 int lastBang = url.lastIndexOf('!'); 131 if (lastBang != -1) { 132 url = url.substring(0, lastBang); 133 } 134 return decode(url); 135 } 136 137 private void printArray(String header, String [] values) { 138 System.err.println(header); 139 for (int i = 0; i < values.length; i++) { 140 System.err.println("\t" + values[i]); } 142 } 143 144 145 148 private void initializeBundleListStructure() { 149 final char STARTLEVEL_SEPARATOR = '@'; 150 151 String prop = System.getProperty(PROP_OSGI_BUNDLES); 153 if (prop == null || prop.trim().equals("")) { bundleList = new HashMap(0); 155 return; 156 } 157 158 bundleList = new HashMap(10); 159 bundleStartInfo = new HashMap(10); 160 StringTokenizer tokens = new StringTokenizer(prop, ","); while (tokens.hasMoreTokens()) { 162 String token = tokens.nextToken().trim(); 163 String bundleId = token; 164 if (token.equals("")) continue; 166 int startLevelSeparator; 167 if ((startLevelSeparator = token.lastIndexOf(STARTLEVEL_SEPARATOR)) != -1) { 168 bundleId = token.substring(0, startLevelSeparator); 169 bundleStartInfo.put(bundleId, token.substring(startLevelSeparator)); 170 } 171 bundleList.put(bundleId, new ArrayList(1)); } 173 174 } 175 176 179 private void mapURLsToBundleList() { 180 String [] allJars = getAllJars(); 181 for (int i = 0; i < allJars.length; i++) { 182 String bundleId = extractBundleId(allJars[i]); 183 if (bundleId == null) 184 continue; 185 ArrayList bundleURLs = (ArrayList) bundleList.get(bundleId); 186 if (bundleURLs == null) { 187 int versionIdPosition = bundleId.lastIndexOf('_'); 188 if (versionIdPosition == -1) 189 continue; 190 bundleURLs = (ArrayList) bundleList.get(bundleId.substring(0, versionIdPosition)); 191 if (bundleURLs == null) 192 continue; 193 } 194 bundleURLs.add(allJars[i]); 195 allJars[i] = null; } 197 } 198 199 202 private String extractBundleId(String url) { 203 if (preciseIdExtraction) 204 return extractBundleIdFromManifest(url); 205 else 206 return extractBundleIdFromBundleURL(url); 207 } 208 209 private String extractBundleIdFromManifest(String url) { 210 final String BUNDLE_SYMBOLICNAME = "Bundle-SymbolicName"; final String BUNDLE_VERSION = "Bundle-Version"; 213 Manifest mf; 214 try { 215 mf = new Manifest (new URL (url).openStream()); 216 String symbolicNameString = mf.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); 217 if (symbolicNameString==null) 218 return null; 219 220 String bundleVersion = mf.getMainAttributes().getValue(BUNDLE_VERSION); 221 if (bundleVersion == null) 222 bundleVersion = ""; else 224 bundleVersion = '_' + bundleVersion; 225 226 int pos = symbolicNameString.lastIndexOf(';'); 227 if (pos != -1) 228 return symbolicNameString.substring(0, pos) + bundleVersion; 229 return symbolicNameString + bundleVersion; 230 } catch (MalformedURLException e) { 231 } catch (IOException e) { 233 } 235 return null; 236 237 } 238 239 private String extractBundleIdFromBundleURL(String url) { 240 int lastBang = url.lastIndexOf('!'); 241 if (lastBang == -1) 242 return null; 243 boolean jarSuffix = url.regionMatches(true, lastBang - 4, ".jar", 0, 4); int bundleIdStart = url.lastIndexOf('/', lastBang); 245 return url.substring(bundleIdStart + 3, lastBang - (jarSuffix ? 4 : 0)); } 247 248 private void buildOSGiBundleList() { 249 bundleList.remove(framework); 251 252 String [] jarsOnClasspath = getAllJars(); 253 StringBuffer finalBundleList = new StringBuffer (jarsOnClasspath.length * 25); 254 255 Collection allSelectedBundles = bundleList.entrySet(); 257 for (Iterator iter = allSelectedBundles.iterator(); iter.hasNext();) { 258 Map.Entry entry = (Map.Entry) iter.next(); 259 ArrayList matches = (ArrayList) entry.getValue(); 260 int numberOfURLs = matches.size(); 261 262 String startInfo = (String ) bundleStartInfo.get(entry.getKey()); 264 if (startInfo == null) 265 startInfo = ""; 267 if (numberOfURLs == 1) { 268 finalBundleList.append(REFERENCE_SCHEME).append(extractInnerURL((String ) matches.get(0))).append(startInfo).append(','); 269 continue; 270 } 271 if (numberOfURLs == 0) 272 continue; 273 String urls[] = new String [numberOfURLs]; 274 int found = findMax((String []) matches.toArray(urls)); 275 for (int i = 0; i < urls.length; i++) { 276 if (i != found) 277 continue; 278 finalBundleList.append(REFERENCE_SCHEME).append(extractInnerURL((String ) urls[found])).append(startInfo).append(','); 279 } 280 } 281 282 if (! Boolean.FALSE.toString().equalsIgnoreCase(System.getProperties().getProperty(PROP_WEBSTART_AUTOMATIC_INSTALLATION))) { 284 for (int i = 0; i < jarsOnClasspath.length; i++) { 285 if (jarsOnClasspath[i] != null) 286 finalBundleList.append(REFERENCE_SCHEME).append(extractInnerURL(jarsOnClasspath[i])).append(','); 287 } 288 } 289 290 System.getProperties().put(PROP_OSGI_BUNDLES, finalBundleList.toString()); 291 if (debug) 292 log(finalBundleList.toString()); 293 } 294 295 } 296 | Popular Tags |