1 11 package org.eclipse.equinox.launcher; 12 13 import java.io.IOException ; 14 import java.net.JarURLConnection ; 15 import java.net.URL ; 16 import java.net.URLConnection ; 17 import java.util.ArrayList ; 18 import java.util.Enumeration ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.StringTokenizer ; 24 import java.util.jar.JarFile ; 25 import java.util.jar.Manifest ; 26 27 37 public class WebStartMain extends Main { 39 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_CHECK_CONFIG = "osgi.checkConfiguration"; 44 private Map allBundles = null; private List bundleList = null; 47 private class BundleInfo { 48 String bsn; 49 String version; 50 String startData; 51 String location; 52 } 53 54 public static void main(String [] args) { 55 System.setSecurityManager(null); int result = new WebStartMain().run(args); 57 if (!Boolean.getBoolean(PROP_NOSHUTDOWN)) 58 System.exit(result); 59 } 60 61 private void setDefaultBundles() { 62 if (System.getProperty(PROP_OSGI_BUNDLES) != null) 63 return; 64 System.getProperties().put(PROP_OSGI_BUNDLES, DEFAULT_OSGI_BUNDLES); 65 } 66 67 protected void basicRun(String [] args) throws Exception { 68 setDefaultBundles(); 69 initializeBundleListStructure(); 70 discoverBundles(); 71 String fwkURL = searchFor(framework, null); 73 if (fwkURL == null) { 74 } 76 allBundles.remove(framework); 77 System.getProperties().put(PROP_FRAMEWORK, fwkURL); 78 super.basicRun(args); 79 } 80 81 protected void beforeFwkInvocation() { 82 if (System.getProperty(PROP_CHECK_CONFIG) == null) 84 System.getProperties().put(PROP_CHECK_CONFIG, "true"); buildOSGiBundleList(); 86 cleanup(); 87 } 88 89 92 private void cleanup() { 93 allBundles = null; 94 bundleList = null; 95 } 96 97 101 protected String searchFor(final String target, String start) { 102 ArrayList matches = (ArrayList ) allBundles.get(target); 103 if (matches == null) 104 return null; 105 int numberOfMatches = matches.size(); 106 if (numberOfMatches == 1) { 107 return ((BundleInfo) matches.get(0)).location; 108 } 109 if (numberOfMatches == 0) 110 return null; 111 112 String [] versions = new String [numberOfMatches]; 113 int highest = 0; 114 for (int i = 0; i < versions.length; i++) { 115 versions[i] = ((BundleInfo) matches.get(i)).version; 116 highest = findMax(versions); 117 } 118 return ((BundleInfo) matches.get(highest)).location; 119 } 120 121 private BundleInfo findBundle(final String target, String version, boolean removeMatch) { 122 ArrayList matches = (ArrayList ) allBundles.get(target); 123 int numberOfMatches = matches.size(); 124 if (numberOfMatches == 1) { 125 return (BundleInfo) matches.remove(0); 127 } 128 if (numberOfMatches == 0) 129 return null; 130 131 if (version != null) { 132 for (Iterator iterator = matches.iterator(); iterator.hasNext();) { 133 BundleInfo bi = (BundleInfo) iterator.next(); 134 if (bi.version.equals(version)) { 135 iterator.remove(); 136 return bi; 137 } 138 } 139 return null; 141 } 142 String [] versions = new String [numberOfMatches]; 143 int highest = 0; 144 for (int i = 0; i < versions.length; i++) { 145 versions[i] = ((BundleInfo) matches.get(i)).version; 146 highest = findMax(versions); 147 } 148 return (BundleInfo) matches.remove(highest); 149 } 150 151 154 private void discoverBundles() { 155 allBundles = new HashMap (); 156 try { 157 Enumeration resources = WebStartMain.class.getClassLoader().getResources(JarFile.MANIFEST_NAME); 158 while (resources.hasMoreElements()) { 159 BundleInfo found = getBundleInfo((URL ) resources.nextElement()); 160 if (found == null) 161 continue; 162 ArrayList matching = (ArrayList ) allBundles.get(found.bsn); 163 if (matching == null) { 164 matching = new ArrayList (1); 165 allBundles.put(found.bsn, matching); 166 } 167 matching.add(found); 168 } 169 } catch (IOException e) { 170 e.printStackTrace(); 171 } 172 } 173 174 private String extractInnerURL(URL url) { 175 try { 176 URLConnection connection = null; 177 try { 178 connection = url.openConnection(); 179 if (connection instanceof JarURLConnection ) { 180 return "file:" + ((JarURLConnection ) connection).getJarFile().getName(); 181 } 182 } finally { 183 if (connection != null) 184 connection.getInputStream().close(); 185 } 186 } catch (IOException e) { 187 } 189 return url.toExternalForm(); 190 } 191 192 195 private void initializeBundleListStructure() { 196 final char STARTLEVEL_SEPARATOR = '@'; 197 198 String prop = System.getProperty(PROP_OSGI_BUNDLES); 200 if (prop == null || prop.trim().equals("")) { bundleList = new ArrayList (0); 202 return; 203 } 204 205 bundleList = new ArrayList (10); 206 StringTokenizer tokens = new StringTokenizer (prop, ","); while (tokens.hasMoreTokens()) { 208 String token = tokens.nextToken().trim(); 209 String bundleId = token; 210 if (token.equals("")) continue; 212 int startLevelSeparator; 213 BundleInfo toAdd = new BundleInfo(); 214 toAdd.bsn = bundleId; 215 if ((startLevelSeparator = token.lastIndexOf(STARTLEVEL_SEPARATOR)) != -1) { 216 toAdd.bsn = token.substring(0, startLevelSeparator); 217 toAdd.startData = token.substring(startLevelSeparator); 218 } 220 bundleList.add(toAdd); 221 } 222 } 223 224 private BundleInfo getBundleInfo(URL manifestURL) { 225 final String BUNDLE_SYMBOLICNAME = "Bundle-SymbolicName"; final String BUNDLE_VERSION = "Bundle-Version"; final String DEFAULT_VERSION = "0.0.0"; 229 Manifest mf; 230 try { 231 mf = new Manifest (manifestURL.openStream()); 232 String symbolicNameString = mf.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); 233 if (symbolicNameString == null) 234 return null; 235 236 BundleInfo result = new BundleInfo(); 237 String version = mf.getMainAttributes().getValue(BUNDLE_VERSION); 238 result.version = (version != null) ? version : DEFAULT_VERSION; 239 result.location = extractInnerURL(manifestURL); 240 int pos = symbolicNameString.lastIndexOf(';'); 241 if (pos != -1) { 242 result.bsn = symbolicNameString.substring(0, pos); 243 return result; 244 } 245 result.bsn = symbolicNameString; 246 return result; 247 } catch (IOException e) { 248 if (debug) 249 e.printStackTrace(); 250 } 251 return null; 252 } 253 254 private void buildOSGiBundleList() { 256 StringBuffer finalBundleList = new StringBuffer (allBundles.size() * 30); 257 for (Iterator iterator = bundleList.iterator(); iterator.hasNext();) { 259 BundleInfo searched = (BundleInfo) iterator.next(); 260 BundleInfo found = findBundle(searched.bsn, searched.version, true); 261 finalBundleList.append(REFERENCE_SCHEME).append(found.location).append(searched.startData).append(','); 262 } 263 264 if (!Boolean.FALSE.toString().equalsIgnoreCase(System.getProperties().getProperty(PROP_WEBSTART_AUTOMATIC_INSTALLATION))) { 265 for (Iterator iterator = allBundles.values().iterator(); iterator.hasNext();) { 266 ArrayList toAdd = (ArrayList ) iterator.next(); 267 for (Iterator iterator2 = toAdd.iterator(); iterator2.hasNext();) { 268 BundleInfo bi = (BundleInfo) iterator2.next(); 269 finalBundleList.append(REFERENCE_SCHEME).append(bi.location).append(','); 270 } 271 } 272 } 273 System.getProperties().put(PROP_OSGI_BUNDLES, finalBundleList.toString()); 274 } 275 } 276 | Popular Tags |