KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > FeatureExecutableFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.net.URL JavaDoc;
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 /**
34  * FeatureFactory for Executable Features
35  */

36 public class FeatureExecutableFactory extends BaseFeatureFactory {
37
38     /*
39      * @see IFeatureFactory#createFeature(URL,ISite,IProgressMonitor)
40      */

41     public IFeature createFeature(URL JavaDoc url, ISite site, IProgressMonitor monitor) throws CoreException {
42
43         TargetFeature feature = null;
44         InputStream JavaDoc featureStream = null;
45         if (monitor == null)
46             monitor = new NullProgressMonitor();
47
48         if (url == null)
49             return createFeature(site);
50
51         // the URL should point to a directory
52
url = validate(url);
53
54         try {
55             IFeatureContentProvider contentProvider = new FeatureExecutableContentProvider(url);
56 // // PERF: Do not create FeatureContentConsumer
57
// bug 79893
58
IFeatureContentConsumer contentConsumer =new FeatureExecutableContentConsumer();
59
60             URL JavaDoc nonResolvedURL = contentProvider.getFeatureManifestReference(null).asURL();
61             URL JavaDoc 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 // // PERF: FeatureContentConsumer
70
// bug 79893
71
feature.setContentConsumer(contentConsumer);
72
73             feature.resolve(url, url);
74             feature.markReadOnly();
75         } catch (CoreException e) {
76             throw e;
77         } catch (Exception JavaDoc e) {
78             throw Utilities.newCoreException(NLS.bind(Messages.FeatureFactory_CreatingError, (new String JavaDoc[] { url.toExternalForm() })), e);
79         } finally {
80             try {
81                 if (featureStream != null)
82                     featureStream.close();
83             } catch (IOException JavaDoc e) {
84             }
85         }
86         return feature;
87     }
88
89     /*
90      * @see FeatureModelFactory#createFeatureModel()
91      */

92     public FeatureModel createFeatureModel() {
93         return new TargetFeature();
94     }
95
96     /*
97      * Creates an empty feature on the site
98      */

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         // do not mark read only yet...
110
return feature;
111     }
112
113     /*
114      * validates a URL as a directory URL
115      */

116     private URL JavaDoc validate(URL JavaDoc 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))) { //$NON-NLS-1$
122
try {
123                 String JavaDoc path = url.getFile() + "/"; //$NON-NLS-1$
124
url = new URL JavaDoc(url.getProtocol(), url.getHost(), url.getPort(), path);
125             } catch (MalformedURLException JavaDoc e) {
126                 throw Utilities.newCoreException(NLS.bind(Messages.FeatureExecutableFactory_CannotCreateURL, (new String JavaDoc[] { url.toExternalForm() })), e);
127             }
128         }
129         return url;
130     }
131
132 }
133
Popular Tags