KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > mirror > MirrorSiteFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.mirror;
12 import java.io.*;
13 import java.net.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.update.core.*;
17 import org.eclipse.update.core.model.*;
18 import org.eclipse.update.internal.core.*;
19 import org.eclipse.update.internal.model.*;
20 import org.eclipse.update.standalone.*;
21 import org.xml.sax.*;
22
23 public class MirrorSiteFactory extends BaseSiteFactory {
24     /*
25      * @see SiteModelFactory#createSiteMapModel()
26      */

27     public SiteModel createSiteMapModel() {
28         return new MirrorSite(this);
29     }
30     /*
31      * @see ISiteFactory#createSite(URL)
32      */

33     public ISite createSite(URL url)
34         throws CoreException, InvalidSiteTypeException {
35         return createSite(new File(url.getFile()));
36     }
37     /*
38      * @see ISiteFactory#createSite(URL)
39      */

40     public ISite createSite(File siteLocation)
41         throws CoreException, InvalidSiteTypeException {
42
43         InputStream siteStream = null;
44
45         if (!siteLocation.exists()) {
46             if (!siteLocation.mkdirs()) {
47                 throw Utilities.newCoreException(
48                     "Directory " //$NON-NLS-1$
49
+ siteLocation.getAbsolutePath()
50                         + " could not be created.", //$NON-NLS-1$
51
null);
52             }
53         }
54         if (!siteLocation.isDirectory() || !siteLocation.canWrite())
55             throw Utilities.newCoreException(
56                 siteLocation.getAbsolutePath()
57                     + " is not a directory or is not writtable.", //$NON-NLS-1$
58
null);
59
60         MirrorSite site = null;
61         // parse public features
62
if (new File(siteLocation, Site.SITE_XML).exists()) {
63             try {
64                 siteStream =
65                     new FileInputStream(new File(siteLocation, Site.SITE_XML));
66             } catch (FileNotFoundException fnfe) {
67             }
68             site = (MirrorSite) parseSite(siteStream);
69             try {
70                 if (siteStream != null)
71                     siteStream.close();
72             } catch (IOException e) {
73             }
74         }
75         if (site == null) {
76             site = (MirrorSite) createSiteMapModel();
77         }
78         // parse downloaded plugins and fragments
79
parseDownloadedPluginsAndFragments(
80             site,
81             new File(siteLocation, Site.DEFAULT_PLUGIN_PATH));
82         // parse downloaded features
83
parseDownloadedFeatures(
84             site,
85             new File(siteLocation, Site.DEFAULT_FEATURE_PATH));
86
87         URL url;
88         try {
89             url = siteLocation.toURL();
90         } catch (MalformedURLException mue) {
91             throw Utilities.newCoreException(
92                 "A URL for site " //$NON-NLS-1$
93
+ siteLocation.getAbsolutePath()
94                     + " could not be created.", //$NON-NLS-1$
95
mue);
96         }
97         SiteContentProvider contentProvider = null;
98         contentProvider = new SiteFileContentProvider(url);
99
100         site.setSiteContentProvider(contentProvider);
101         contentProvider.setSite(site);
102         try {
103             site.resolve(url, url);
104         } catch (MalformedURLException mue) {
105             throw Utilities.newCoreException(
106                 "Unable to resolve URL " //$NON-NLS-1$
107
+ (url == null ? "" : url.toExternalForm()), //$NON-NLS-1$
108
mue);
109         }
110         return site;
111     }
112     /**
113      *
114      */

115     private void parseDownloadedPluginsAndFragments(MirrorSite site,
116             File pluginDir) throws CoreException {
117         if (!pluginDir.exists()) {
118             return;
119         }
120         String JavaDoc[] dir = pluginDir.list(FeaturePackagedContentProvider.filter);
121         for (int i = 0; i < dir.length; i++) {
122             InputStream in = null;
123             try {
124                 File file = new File(pluginDir, dir[i]);
125                 JarContentReference jarReference = new JarContentReference(
126                         null, file);
127                 ContentReference ref = jarReference.peek("META-INF/MANIFEST.MF", null, null); //$NON-NLS-1$
128
if (ref != null) {
129                     in = ref.getInputStream();
130                     BundleManifest manifest = new BundleManifest(in);
131                     if (manifest.exists()) {
132                         site
133                                 .addDownloadedPluginEntry(manifest
134                                         .getPluginEntry());
135                         continue;
136                     }
137                 }
138                 ref = jarReference.peek("plugin.xml", null, null); //$NON-NLS-1$
139
if (ref == null) {
140                     ref = jarReference.peek("fragment.xml", null, null); //$NON-NLS-1$
141
}
142                 if (ref != null) {
143                     in = ref.getInputStream();
144                     PluginEntry entry = new DefaultPluginParser().parse(in);
145                     site.addDownloadedPluginEntry(entry);
146                 }
147             } catch (IOException e) {
148                 StandaloneUpdateApplication.exceptionLogged();
149                 UpdateCore.log(e);
150             } catch (SAXException e) {
151                 StandaloneUpdateApplication.exceptionLogged();
152                 UpdateCore.log(e);
153             } finally {
154                 if(in !=null){
155                     try{
156                         in.close();
157                     }catch(IOException ce){
158                     }
159                 }
160             }
161         }
162     }
163     /**
164     * Method parseFeature.
165     * @throws CoreException
166     */

167     private void parseDownloadedFeatures(MirrorSite site, File featureDir)
168         throws CoreException {
169         if (featureDir.exists()) {
170             String JavaDoc[] dir;
171             SiteFeatureReferenceModel featureRef;
172             URL featureURL;
173             File currentFeatureFile;
174             String JavaDoc newFilePath = null;
175
176             try {
177                 // only list JAR files
178
dir = featureDir.list(FeaturePackagedContentProvider.filter);
179                 for (int index = 0; index < dir.length; index++) {
180
181                     // check if the JAR file contains a feature.xml
182
currentFeatureFile = new File(featureDir, dir[index]);
183                     JarContentReference ref =
184                         new JarContentReference("", currentFeatureFile); //$NON-NLS-1$
185
ContentReference result = null;
186                     try {
187                         result = ref.peek(Feature.FEATURE_XML, null, null);
188                     } catch (IOException e) {
189                         UpdateCore.warn(
190                             "Exception retrieving feature.xml in file:" //$NON-NLS-1$
191
+ currentFeatureFile,
192                             e);
193                     }
194                     if (result == null) {
195                         UpdateCore.warn(
196                             "Unable to find feature.xml in file:" //$NON-NLS-1$
197
+ currentFeatureFile);
198                     } else {
199                         featureURL = currentFeatureFile.toURL();
200                         featureRef = createFeatureReferenceModel();
201                         featureRef.setSiteModel(site);
202                         featureRef.setURLString(featureURL.toExternalForm());
203                         featureRef.setType(ISite.DEFAULT_PACKAGED_FEATURE_TYPE);
204                         featureRef.setFeatureIdentifier(
205                             featureRef
206                                 .getVersionedIdentifier()
207                                 .getIdentifier());
208                         featureRef.setFeatureVersion(
209                             featureRef
210                                 .getVersionedIdentifier()
211                                 .getVersion()
212                                 .toString());
213                         site.addDownloadedFeatureReferenceModel(featureRef);
214                     }
215                 }
216             } catch (MalformedURLException e) {
217                 throw Utilities.newCoreException(
218                     "Unable to create URL for file " + newFilePath + ".", //$NON-NLS-1$ //$NON-NLS-2$
219
e);
220             }
221         }
222     }
223
224 }
225
Popular Tags