1 11 package org.eclipse.update.internal.core; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.NullProgressMonitor; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.update.core.BaseFeatureFactory; 24 import org.eclipse.update.core.Feature; 25 import org.eclipse.update.core.IFeature; 26 import org.eclipse.update.core.IFeatureContentConsumer; 27 import org.eclipse.update.core.IFeatureContentProvider; 28 import org.eclipse.update.core.ISite; 29 import org.eclipse.update.core.Utilities; 30 import org.eclipse.update.core.model.FeatureModel; 31 import org.eclipse.update.internal.core.connection.ConnectionFactory; 32 33 36 public class FeatureExecutableFactory extends BaseFeatureFactory { 37 38 41 public IFeature createFeature(URL url, ISite site, IProgressMonitor monitor) throws CoreException { 42 43 TargetFeature feature = null; 44 InputStream featureStream = null; 45 if (monitor == null) 46 monitor = new NullProgressMonitor(); 47 48 if (url == null) 49 return createFeature(site); 50 51 url = validate(url); 53 54 try { 55 IFeatureContentProvider contentProvider = new FeatureExecutableContentProvider(url); 56 IFeatureContentConsumer contentConsumer =new FeatureExecutableContentConsumer(); 59 60 URL nonResolvedURL = contentProvider.getFeatureManifestReference(null).asURL(); 61 URL resolvedURL = URLEncoder.encode(nonResolvedURL); 62 featureStream = ConnectionFactory.get(resolvedURL).getInputStream(); 63 64 feature = (TargetFeature) this.parseFeature(featureStream, resolvedURL.toExternalForm()); 65 monitor.worked(1); 66 feature.setSite(site); 67 68 feature.setFeatureContentProvider(contentProvider); 69 feature.setContentConsumer(contentConsumer); 72 73 feature.resolve(url, url); 74 feature.markReadOnly(); 75 } catch (CoreException e) { 76 throw e; 77 } catch (Exception e) { 78 throw Utilities.newCoreException(NLS.bind(Messages.FeatureFactory_CreatingError, (new String [] { url.toExternalForm() })), e); 79 } finally { 80 try { 81 if (featureStream != null) 82 featureStream.close(); 83 } catch (IOException e) { 84 } 85 } 86 return feature; 87 } 88 89 92 public FeatureModel createFeatureModel() { 93 return new TargetFeature(); 94 } 95 96 99 private IFeature createFeature(ISite site) throws CoreException { 100 TargetFeature feature = null; 101 102 IFeatureContentProvider contentProvider = new FeatureExecutableContentProvider(null); 103 IFeatureContentConsumer contentConsumer = new FeatureExecutableContentConsumer(); 104 feature = (TargetFeature) createFeatureModel(); 105 feature.setSite(site); 106 feature.setFeatureContentProvider(contentProvider); 107 feature.setContentConsumer(contentConsumer); 108 109 return feature; 111 } 112 113 116 private URL validate(URL url) throws CoreException { 117 118 if (url == null) 119 throw Utilities.newCoreException(Messages.FeatureExecutableFactory_NullURL, null); 120 121 if (!(url.getFile().endsWith("/") || url.getFile().endsWith(File.separator) || url.getFile().endsWith(Feature.FEATURE_XML))) { try { 123 String path = url.getFile() + "/"; url = new URL (url.getProtocol(), url.getHost(), url.getPort(), path); 125 } catch (MalformedURLException e) { 126 throw Utilities.newCoreException(NLS.bind(Messages.FeatureExecutableFactory_CannotCreateURL, (new String [] { url.toExternalForm() })), e); 127 } 128 } 129 return url; 130 } 131 132 } 133 | Popular Tags |