1 11 package org.eclipse.update.internal.core; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.Date ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.update.core.BaseSiteFactory; 23 import org.eclipse.update.core.ISite; 24 import org.eclipse.update.core.ISiteFactoryExtension; 25 import org.eclipse.update.core.Site; 26 import org.eclipse.update.core.SiteFeatureReferenceModel; 27 import org.eclipse.update.core.Utilities; 28 import org.eclipse.update.core.model.InvalidSiteTypeException; 29 import org.eclipse.update.core.model.SiteModelFactory; 30 import org.eclipse.update.internal.core.connection.ConnectionFactory; 31 import org.eclipse.update.internal.core.connection.IResponse; 32 import org.eclipse.update.internal.model.SiteWithTimestamp; 33 34 38 public class SiteURLFactory extends BaseSiteFactory implements ISiteFactoryExtension { 39 40 43 public ISite createSite(URL url) throws CoreException, InvalidSiteTypeException { 44 return createSite(url, null); 45 } 46 65 public ISite createSite(URL url, IProgressMonitor monitor) throws CoreException, InvalidSiteTypeException { 66 Site site = null; 67 InputStream siteStream = null; 68 69 try { 70 SiteURLContentProvider contentProvider = new SiteURLContentProvider(url); 71 72 URL resolvedURL = URLEncoder.encode(url); 73 IResponse response = ConnectionFactory.get(resolvedURL); 74 UpdateManagerUtils.checkConnectionResult(response, resolvedURL); 75 siteStream = response.getInputStream(monitor); 76 if (siteStream==null) return null; 78 79 SiteModelFactory factory = this; 80 site = (Site) factory.parseSite(siteStream); 81 site.setSiteContentProvider(contentProvider); 83 contentProvider.setSite(site); 84 site.resolve(url, url); 85 site.markReadOnly(); 86 89 ((SiteWithTimestamp)site).setTimestamp( new Date (response.getLastModified())); 90 } catch (MalformedURLException e) { 91 throw Utilities.newCoreException(NLS.bind(Messages.SiteURLFactory_UnableToCreateURL, (new String [] { url == null ? "" : url.toExternalForm() })), e); } catch (IOException e) { 93 throw Utilities.newCoreException(NLS.bind(Messages.SiteURLFactory_UnableToAccessSiteStream, (new String [] { url == null ? "" : url.toExternalForm() })), ISite.SITE_ACCESS_EXCEPTION, e); } finally { 95 if (siteStream != null) { 96 try { 97 siteStream.close(); 98 } catch (IOException e) { 99 } 100 } 101 } 102 return site; 103 } 104 105 108 public boolean canParseSiteType(String type) { 109 return (super.canParseSiteType(type) || SiteURLContentProvider.SITE_TYPE.equalsIgnoreCase(type)); 110 } 111 114 public SiteFeatureReferenceModel createFeatureReferenceModel() { 115 return new UpdateSiteFeatureReference(); 116 } 117 118 } 119 | Popular Tags |