1 11 package org.eclipse.update.internal.core; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.MultiStatus; 24 import org.eclipse.core.runtime.NullProgressMonitor; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.update.configuration.ILocalSite; 27 import org.eclipse.update.core.ISite; 28 import org.eclipse.update.core.ISiteFactory; 29 import org.eclipse.update.core.ISiteFactoryExtension; 30 import org.eclipse.update.core.JarContentReference; 31 import org.eclipse.update.core.Site; 32 import org.eclipse.update.core.Utilities; 33 import org.eclipse.update.core.model.InvalidSiteTypeException; 34 import org.eclipse.update.internal.core.connection.ConnectionFactory; 35 import org.eclipse.update.internal.core.connection.IResponse; 36 import org.eclipse.update.internal.model.ITimestamp; 37 38 41 public class InternalSiteManager { 42 43 public static ILocalSite localSite; 44 45 public static final String DEFAULT_SITE_TYPE = SiteURLContentProvider.SITE_TYPE; 46 private static final String DEFAULT_EXECUTABLE_SITE_TYPE = SiteFileContentProvider.SITE_TYPE; 47 48 private static Map estimates; 49 50 private static Map sites = new HashMap (); 52 private static Map httpSitesUpdatedUrls = new HashMap (); 54 private static Map siteTimestamps = new HashMap (); 56 public static boolean globalUseCache = true; 57 58 private static CoreException exceptionOccured = null; 61 62 65 public static ILocalSite getLocalSite() throws CoreException { 66 return internalGetLocalSite(); 67 } 68 69 72 private static ILocalSite internalGetLocalSite() throws CoreException { 73 74 if (exceptionOccured != null) 77 throw exceptionOccured; 78 79 if (localSite == null) { 80 try { 81 localSite = LocalSite.internalGetLocalSite(); 82 } catch (CoreException e) { 83 exceptionOccured = e; 84 throw e; 85 } 86 } 87 return localSite; 88 } 89 90 private static boolean isValidCachedSite(URL siteURL) { 91 if (!sites.containsKey(siteURL.toExternalForm())) 92 return false; 93 94 Long timestamp = (Long )siteTimestamps.get(siteURL); 95 if (timestamp == null) 96 return false; 97 long localLastModified = timestamp.longValue(); 98 99 return UpdateManagerUtils.isSameTimestamp(siteURL, localLastModified); 100 } 101 102 105 public static ISite getSite(URL siteURL, boolean useCache, IProgressMonitor monitor) throws CoreException { 106 ISite site = null; 107 if (monitor==null) monitor = new NullProgressMonitor(); 108 109 if (siteURL == null) 110 return null; 111 112 if (httpSitesUpdatedUrls.containsKey(siteURL.toExternalForm())) { 115 siteURL = (URL )httpSitesUpdatedUrls.get(siteURL.toExternalForm()); 116 } 117 String siteURLString = siteURL.toExternalForm(); 118 if ((useCache && globalUseCache) && isValidCachedSite(siteURL)) { 119 site = (ISite) sites.get(siteURLString); 120 UpdateCore.getPlugin().getUpdateSession().markVisited(site.getURL()); 121 return site; 122 } 123 124 if ("file".equals(siteURL.getProtocol()) ) { File f = new File (siteURL.getFile()); 127 if (f.isDirectory() && !"eclipse".equals(f.getName())) { f = new File (f, "eclipse"); try { 130 if ((useCache && globalUseCache) && isValidCachedSite(f.toURL())) { 131 site = (ISite) sites.get(f.toURL().toExternalForm()); 132 return site; 133 } 134 } catch (MalformedURLException e) { 135 } 136 } 137 } 138 139 boolean fileProtocol = "file".equalsIgnoreCase(siteURL.getProtocol()); boolean directoryExists = false; 145 if (fileProtocol) { 146 File dir; 147 dir = new File (siteURL.getFile()); 148 if (dir != null && dir.isDirectory()) { 149 if (!(new File (dir, Site.SITE_XML).exists())) 150 directoryExists = true; 151 } 152 } 153 154 monitor.beginTask(Messages.InternalSiteManager_ConnectingToSite, 8); 156 if (fileProtocol && directoryExists) { 157 site = attemptCreateSite(DEFAULT_EXECUTABLE_SITE_TYPE, siteURL, monitor); 158 monitor.worked(4); } else { 160 try { 161 monitor.worked(3); 162 site = attemptCreateSite(DEFAULT_SITE_TYPE, siteURL, monitor); 163 monitor.worked(1); 164 } catch (CoreException preservedException) { 165 if (!monitor.isCanceled()) { 166 if (!fileProtocol) 168 throw preservedException; 169 170 try { 171 site = attemptCreateSite(DEFAULT_EXECUTABLE_SITE_TYPE, siteURL, monitor); 172 } catch (CoreException retryException) { 173 IStatus firstStatus = preservedException.getStatus(); 174 MultiStatus multi = new MultiStatus(firstStatus.getPlugin(), IStatus.OK, Messages.InternalSiteManager_FailedRetryAccessingSite, retryException); 175 multi.addAll(firstStatus); 176 throw preservedException; 177 } 178 } 179 } 180 } 181 182 if (site != null) { 183 sites.put(site.getURL().toExternalForm(), site); 184 UpdateCore.getPlugin().getUpdateSession().markVisited(site.getURL()); 185 if (site instanceof ITimestamp) { 186 siteTimestamps.put(site.getURL(), new Long (((ITimestamp)site).getTimestamp().getTime())); 187 } else { 188 try { 189 IResponse response = ConnectionFactory.get(URLEncoder.encode(siteURL)); 190 siteTimestamps.put(siteURL, new Long (response.getLastModified())); 191 } catch (MalformedURLException e) { 192 } catch (IOException e) { 193 } 194 } 195 } 196 197 JarContentReference.shutdown(); 201 208 return site; 209 } 210 211 216 private static ISite attemptCreateSite(String guessedTypeSite, URL siteURL, IProgressMonitor monitor) throws CoreException { 217 if (monitor == null) monitor = new NullProgressMonitor(); 218 ISite site = null; 219 220 try { 221 monitor.worked(1); 222 site = createSite(guessedTypeSite, siteURL, monitor); 223 monitor.worked(1); } catch (InvalidSiteTypeException e) { 225 if (monitor.isCanceled()) return null; 226 227 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_TYPE) { 231 UpdateCore.debug("The Site :" + siteURL.toExternalForm() + " is a different type than the guessed type based on the protocol. new Type:" + e.getNewType()); } 233 234 try { 235 if (e.getNewType() == null) 236 throw e; 237 site = createSite(e.getNewType(), siteURL, monitor); 238 } catch (InvalidSiteTypeException e1) { 239 throw Utilities.newCoreException(NLS.bind(Messages.InternalSiteManager_UnableToCreateSiteWithType, (new String [] { e.getNewType(), siteURL.toExternalForm() })), e1); 240 } 241 } 242 243 return site; 244 } 245 246 268 private static ISite createSite(String siteType, URL url, IProgressMonitor monitor) throws CoreException, InvalidSiteTypeException { 269 270 if (monitor == null) monitor = new NullProgressMonitor(); 271 ISiteFactory factory = SiteTypeFactory.getInstance().getFactory(siteType); 273 URL fixedUrl; 274 275 try { 277 if ( (url.getRef() != null) || (url.getFile().endsWith(Site.SITE_XML) || (url.getProtocol().equalsIgnoreCase("file")))) { fixedUrl = url; 279 } else if (url.getFile().endsWith("/")) { fixedUrl = new URL (url, Site.SITE_XML); 281 } else { 282 fixedUrl = new URL (url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "/" + Site.SITE_XML); } 284 } catch (MalformedURLException mue) { 285 fixedUrl = url; 286 } 287 288 try { 289 try { 290 monitor.worked(1); 291 return createSite( factory, fixedUrl, url, monitor); 292 } catch (CoreException ce) { 293 if (monitor.isCanceled()) 294 return null; 295 296 if (!fixedUrl.equals(url)) { 297 return createSite( factory, url, url, monitor); 299 } else if (url.getProtocol().equalsIgnoreCase("file") && ! url.getFile().endsWith(Site.SITE_XML)){ try { 301 if (url.getFile().endsWith("/")) { return createSite(factory, new URL (url, 303 Site.SITE_XML), url, monitor); 304 } else { 305 return createSite(factory, new URL (url 306 .getProtocol(), url.getHost(), url 307 .getPort(), url.getFile() 308 + "/" + Site.SITE_XML), url, monitor); } 310 } catch (MalformedURLException mue) { 311 throw ce; 312 } 313 } else { 314 throw ce; 315 } 316 } 317 } catch(CoreException ce) { 318 throw Utilities.newCoreException(NLS.bind(Messages.InternalSiteManager_UnableToAccessURL, (new String [] { url.toExternalForm() })), ce); 319 } 320 } 321 322 private static ISite createSite(ISiteFactory factory, URL url, URL originalUrl, IProgressMonitor monitor) throws CoreException, InvalidSiteTypeException { 323 324 ISite site; 325 326 site = createSite(factory, url, monitor); 327 httpSitesUpdatedUrls.put(originalUrl.toExternalForm(), url); 328 329 return site; 330 } 331 332 private static ISite createSite(ISiteFactory factory, URL url, IProgressMonitor monitor) throws CoreException, InvalidSiteTypeException { 333 if (factory instanceof ISiteFactoryExtension) 334 return ((ISiteFactoryExtension)factory).createSite(url, monitor); 335 else 336 return factory.createSite(url); 337 } 338 339 346 public static ISite createSite(File siteLocation) throws CoreException { 347 ISite site = null; 348 if (siteLocation != null) { 349 try { 350 URL siteURL = siteLocation.toURL(); 351 site = getSite(siteURL, false, null); 352 } catch (MalformedURLException e) { 353 throw Utilities.newCoreException(NLS.bind(Messages.InternalSiteManager_UnableToCreateURL, (new String [] { siteLocation.getAbsolutePath() })), e); 354 } 355 } 356 return site; 357 } 358 359 360 366 public static void downloaded(long downloadSize, long time, URL url) { 367 if (downloadSize <= 0 || time < 0) 368 return; 369 String host = url.getHost(); 370 long sizeByTime = (time == 0) ? 0 : downloadSize / time; 371 Long value = new Long (sizeByTime); 372 if (estimates == null) { 373 estimates = new HashMap (); 374 } else { 375 Long previous = (Long ) estimates.get(host); 376 if (previous != null) { 377 value = new Long ((previous.longValue() + sizeByTime) / 2); 378 } 379 } 380 estimates.put(host, value); 381 } 382 387 public static long getEstimatedTransferRate(String host) { 388 if (estimates == null) 389 return 0; 390 Long value = (Long ) estimates.get(host); 391 if (value == null) 392 return 0; 393 return value.longValue(); 394 } 395 396 } 397 | Popular Tags |