1 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 27 public SiteModel createSiteMapModel() { 28 return new MirrorSite(this); 29 } 30 33 public ISite createSite(URL url) 34 throws CoreException, InvalidSiteTypeException { 35 return createSite(new File(url.getFile())); 36 } 37 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 " + siteLocation.getAbsolutePath() 50 + " could not be created.", 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.", null); 59 60 MirrorSite site = null; 61 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 parseDownloadedPluginsAndFragments( 80 site, 81 new File(siteLocation, Site.DEFAULT_PLUGIN_PATH)); 82 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 " + siteLocation.getAbsolutePath() 94 + " could not be created.", 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 " + (url == null ? "" : url.toExternalForm()), mue); 109 } 110 return site; 111 } 112 115 private void parseDownloadedPluginsAndFragments(MirrorSite site, 116 File pluginDir) throws CoreException { 117 if (!pluginDir.exists()) { 118 return; 119 } 120 String [] 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); 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); if (ref == null) { 140 ref = jarReference.peek("fragment.xml", null, null); } 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 167 private void parseDownloadedFeatures(MirrorSite site, File featureDir) 168 throws CoreException { 169 if (featureDir.exists()) { 170 String [] dir; 171 SiteFeatureReferenceModel featureRef; 172 URL featureURL; 173 File currentFeatureFile; 174 String newFilePath = null; 175 176 try { 177 dir = featureDir.list(FeaturePackagedContentProvider.filter); 179 for (int index = 0; index < dir.length; index++) { 180 181 currentFeatureFile = new File(featureDir, dir[index]); 183 JarContentReference ref = 184 new JarContentReference("", currentFeatureFile); 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:" + currentFeatureFile, 192 e); 193 } 194 if (result == null) { 195 UpdateCore.warn( 196 "Unable to find feature.xml in file:" + 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 + ".", e); 220 } 221 } 222 } 223 224 } 225 | Popular Tags |