1 11 package org.eclipse.update.internal.core; 12 13 14 import java.io.File ; 15 import java.io.FileFilter ; 16 import java.io.FileInputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Status; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.update.core.BaseSiteFactory; 27 import org.eclipse.update.core.ContentReference; 28 import org.eclipse.update.core.Feature; 29 import org.eclipse.update.core.ISite; 30 import org.eclipse.update.core.JarContentReference; 31 import org.eclipse.update.core.PluginEntry; 32 import org.eclipse.update.core.Site; 33 import org.eclipse.update.core.SiteContentProvider; 34 import org.eclipse.update.core.SiteFeatureReferenceModel; 35 import org.eclipse.update.core.Utilities; 36 import org.eclipse.update.core.model.ArchiveReferenceModel; 37 import org.eclipse.update.core.model.InvalidSiteTypeException; 38 import org.eclipse.update.core.model.SiteModel; 39 import org.eclipse.update.core.model.SiteModelFactory; 40 import org.eclipse.update.internal.model.BundleManifest; 41 import org.eclipse.update.internal.model.DefaultPluginParser; 42 import org.xml.sax.SAXException ; 43 44 public class SiteFileFactory extends BaseSiteFactory { 45 46 private SiteFile site; 48 49 50 53 public ISite createSite(URL url) throws CoreException, InvalidSiteTypeException { 54 55 Site site = null; 56 InputStream siteStream = null; 57 SiteModelFactory factory = this; 58 59 try { 60 String path = url.getFile(); 63 File siteLocation = new File (path); 64 if (siteLocation.isDirectory()) { 65 url = siteLocation.toURL(); 66 File siteXMLFile = new File (siteLocation, Site.SITE_XML); 67 if (siteXMLFile.exists()) { 68 siteStream = new FileInputStream (siteXMLFile); 69 site = (Site) factory.parseSite(siteStream); 70 } else { 71 site = parseSite(siteLocation); 73 } 74 } else { 75 try { 78 URL resolvedURL = URLEncoder.encode(url); 79 siteStream = openStream(resolvedURL); 80 site = (Site) factory.parseSite(siteStream); 81 } catch (IOException e) { 82 83 File file = new File (url.getFile()); 85 File parentDirectory = file.getParentFile(); 86 87 if (parentDirectory != null && !parentDirectory.exists()) 90 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_DirectoryDoesNotExist, (new String [] { file.getAbsolutePath() })), null); 91 92 if (parentDirectory == null || !parentDirectory.isDirectory()) 93 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToObtainParentDirectory, (new String [] { file.getAbsolutePath() })), null); 94 95 site = parseSite(parentDirectory); 96 97 } 98 } 99 100 SiteContentProvider contentProvider = new SiteFileContentProvider(url); 101 site.setSiteContentProvider(contentProvider); 102 contentProvider.setSite(site); 103 site.resolve(url, url); 104 105 } catch (MalformedURLException e) { 108 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToCreateURL, (new String [] { url == null ? "" : url.toExternalForm() })), e); } catch (IOException e) { 110 throw Utilities.newCoreException(Messages.SiteFileFactory_UnableToAccessSite,ISite.SITE_ACCESS_EXCEPTION, e); 111 } finally { 112 try { 113 if (siteStream != null) 114 siteStream.close(); 115 } catch (IOException e) { 116 } 117 } 118 return site; 119 } 120 123 private Site parseSite(File directory) throws CoreException { 124 125 this.site = (SiteFile) createSiteMapModel(); 126 127 if (!directory.exists()) 128 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_FileDoesNotExist, (new String [] { directory.getAbsolutePath() })), null); 129 130 File pluginPath = new File (directory, Site.DEFAULT_PLUGIN_PATH); 131 132 try { 134 parsePackagedFeature(directory); } catch (EmptyDirectoryException ede) { 136 UpdateCore.log(ede.getStatus()); 137 } 138 139 try { 140 parsePackagedPlugins(pluginPath); 141 } catch (EmptyDirectoryException ede) { 142 UpdateCore.log(ede.getStatus()); 143 } 144 145 try { 147 parseInstalledFeature(directory); 148 } catch (EmptyDirectoryException ede) { 149 UpdateCore.log(ede.getStatus()); 150 } 151 152 try { 153 parseInstalledPlugins(pluginPath); 154 } catch (EmptyDirectoryException ede) { 155 UpdateCore.log(ede.getStatus()); 156 } 157 158 return site; 159 160 } 161 162 166 private void parseInstalledFeature(File directory) throws CoreException { 167 168 File featureDir = new File (directory, Site.DEFAULT_INSTALLED_FEATURE_PATH); 169 if (featureDir.exists()) { 170 String [] dir; 171 SiteFeatureReferenceModel featureRef; 172 URL featureURL; 173 File currentFeatureDir; 174 String newFilePath = null; 175 176 try { 177 dir = featureDir.list(); 179 if (dir == null) { 180 throw new EmptyDirectoryException( new Status(IStatus.WARNING, UpdateCore.getPlugin().getBundle().getSymbolicName(), IStatus.OK, directory.getName() + File.separator + directory.getName() + "directory is empty", null)); } 182 for (int index = 0; index < dir.length; index++) { 183 184 newFilePath = dir[index] + (dir[index].endsWith("/") ? "/" : ""); currentFeatureDir = new File (featureDir, newFilePath); 187 File featureXMLFile = new File (currentFeatureDir, Feature.FEATURE_XML); 189 if (!featureXMLFile.exists()) { 190 UpdateCore.warn("Unable to find feature.xml in directory:" + currentFeatureDir); } else { 192 featureURL = currentFeatureDir.toURL(); 195 featureRef = createFeatureReferenceModel(); 196 featureRef.setSiteModel(site); 197 featureRef.setURLString(featureURL.toExternalForm()); 198 featureRef.setType(ISite.DEFAULT_INSTALLED_FEATURE_TYPE); 199 ((Site) site).addFeatureReferenceModel(featureRef); 200 } 201 } 202 } catch (MalformedURLException e) { 203 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToCreateURLForFile, (new String [] { newFilePath })), e); 204 } 205 } 206 } 207 208 212 private void parsePackagedFeature(File directory) throws CoreException { 213 214 File featureDir = new File (directory, Site.DEFAULT_FEATURE_PATH); 216 if (featureDir.exists()) { 217 String [] dir; 218 SiteFeatureReferenceModel featureRef; 219 URL featureURL; 220 File currentFeatureFile; 221 String newFilePath = null; 222 223 try { 224 dir = featureDir.list(FeaturePackagedContentProvider.filter); 226 if (dir == null) { 227 throw new EmptyDirectoryException( new Status(IStatus.WARNING, UpdateCore.getPlugin().getBundle().getSymbolicName(), IStatus.OK, directory.getName() + File.separator + directory.getName() + "directory is empty", null)); } 229 230 for (int index = 0; index < dir.length; index++) { 231 232 currentFeatureFile = new File (featureDir, dir[index]); 234 JarContentReference ref = new JarContentReference("", currentFeatureFile); ContentReference result = null; 236 try { 237 result = ref.peek(Feature.FEATURE_XML, null, null); 238 } catch (IOException e) { 239 UpdateCore.warn("Exception retrieving feature.xml in file:" + currentFeatureFile, e); } 241 if (result == null) { 242 UpdateCore.warn("Unable to find feature.xml in file:" + currentFeatureFile); } else { 244 featureURL = currentFeatureFile.toURL(); 245 featureRef = createFeatureReferenceModel(); 248 featureRef.setSiteModel(site); 249 featureRef.setURLString(featureURL.toExternalForm()); 250 featureRef.setType(ISite.DEFAULT_PACKAGED_FEATURE_TYPE); 251 site.addFeatureReferenceModel(featureRef); 252 } 253 } 254 } catch (MalformedURLException e) { 255 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToCreateURLForFile, (new String [] { newFilePath })), e); 256 } 257 } 258 } 259 260 268 private void parseInstalledPlugins(File pluginsDir) throws CoreException { 269 if (!pluginsDir.exists() || !pluginsDir.isDirectory()) { 270 return; 271 } 272 File [] dirs = pluginsDir.listFiles(new FileFilter () { 273 public boolean accept(File f) { 274 return f.isDirectory(); 275 } 276 }); 277 DefaultPluginParser parser = new DefaultPluginParser(); 278 279 if (dirs == null) { 280 throw new EmptyDirectoryException( new Status(IStatus.WARNING, UpdateCore.getPlugin().getBundle().getSymbolicName(), IStatus.OK, pluginsDir.getName() + File.separator + pluginsDir.getName() + "directory is empty", null)); } 282 for (int i = 0; i < dirs.length; i++) { 283 File pluginFile = new File (dirs[i], "META-INF/MANIFEST.MF"); InputStream in = null; 285 try { 286 BundleManifest bundleManifest = new BundleManifest(pluginFile); 287 if (bundleManifest.exists()) { 288 PluginEntry entry = bundleManifest.getPluginEntry(); 289 addParsedPlugin(entry, dirs[i]); 290 } else { 291 if (!(pluginFile = new File (dirs[i], "plugin.xml")) .exists()) { 293 pluginFile = new File (dirs[i], "fragment.xml"); } 295 if (pluginFile != null && pluginFile.exists() 296 && !pluginFile.isDirectory()) { 297 in = new FileInputStream (pluginFile); 298 PluginEntry entry = parser.parse(in); 299 addParsedPlugin(entry, dirs[i]); 300 } 301 } 302 } catch (IOException e) { 303 String pluginFileString = (pluginFile == null) 304 ? null 305 : pluginFile.getAbsolutePath(); 306 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_ErrorAccessing, (new String [] { pluginFileString })), e); 307 } catch (SAXException e) { 308 String pluginFileString = (pluginFile == null) 309 ? null 310 : pluginFile.getAbsolutePath(); 311 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_ErrorParsingFile, (new String [] { pluginFileString })), 312 e); 313 } finally { 314 if (in != null){ 315 try{ 316 in.close(); 317 } catch(IOException e){ 318 } 319 } 320 } 321 } 322 } 323 324 328 private void addParsedPlugin(PluginEntry entry,File file) throws CoreException { 330 331 String location = null; 332 try { 333 if (entry != null) { 334 335 ((Site) site).addPluginEntry(entry); 337 338 ArchiveReferenceModel archive = createArchiveReferenceModel(); 343 String id = (entry.getVersionedIdentifier().toString()); 344 String pluginID = Site.DEFAULT_PLUGIN_PATH + id + FeaturePackagedContentProvider.JAR_EXTENSION; 345 archive.setPath(pluginID); 346 location = file.toURL().toExternalForm(); 347 archive.setURLString(location); 348 ((Site) site).addArchiveReferenceModel(archive); 349 350 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING) { 352 UpdateCore.debug("Added archive to site:" + pluginID + " pointing to: " + location); } 354 } 355 } catch (MalformedURLException e) { 356 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_UnableToCreateURLForFile, (new String [] { location })), e); 357 } 358 } 359 360 363 private void parsePackagedPlugins(File pluginDir) throws CoreException { 364 if (!pluginDir.exists()) { 365 return; 366 } 367 String [] dir = pluginDir.list(FeaturePackagedContentProvider.filter); 368 369 if (dir == null) { 370 throw new EmptyDirectoryException( new Status(IStatus.WARNING, UpdateCore.getPlugin().getBundle().getSymbolicName(), IStatus.OK, pluginDir.getName() + File.separator + pluginDir.getName() + "directory is empty", null)); } 372 for (int i = 0; i < dir.length; i++) { 373 ContentReference ref = null; 374 String refString = null; 375 InputStream in = null; 376 JarContentReference jarReference = null; 377 try { 378 File file = new File (pluginDir, dir[i]); 379 jarReference = new JarContentReference( 380 null, file); 381 ref = jarReference.peek("META-INF/MANIFEST.MF", null, null); if (ref != null) { 383 in = ref.getInputStream(); 384 BundleManifest manifest = new BundleManifest(in); 385 if (manifest.exists()) { 386 addParsedPlugin(manifest.getPluginEntry(), file); 387 continue; 388 } 389 } 390 ref = jarReference.peek("plugin.xml", null, null); if (ref == null) { 392 ref = jarReference.peek("fragment.xml", null, null); } 394 if (ref != null) { 395 in = ref.getInputStream(); 396 PluginEntry entry = new DefaultPluginParser().parse(in); 397 addParsedPlugin(entry, file); 398 } 399 } catch (IOException e) { 400 try { 401 refString = (ref == null) ? null : ref.asURL() 402 .toExternalForm(); 403 } catch (IOException ioe) { 404 } 405 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_ErrorAccessing, (new String [] { refString })), e); 406 } catch (SAXException e) { 407 try { 408 refString = (ref == null) ? null : ref.asURL() 409 .toExternalForm(); 410 } catch (IOException ioe) { 411 } 412 throw Utilities.newCoreException(NLS.bind(Messages.SiteFileFactory_ErrorParsingFile, (new String [] { refString })), e); 413 } finally { 414 if(in != null){ 415 try{ 416 in.close(); 417 }catch(IOException ce){ 418 } 419 } 420 if (jarReference != null) { 421 try { 422 jarReference.closeArchive(); 423 } catch (IOException e) { 424 e.printStackTrace(); 426 } 427 } 428 } 429 } 430 } 431 432 435 public SiteModel createSiteMapModel() { 436 return new SiteFile(); 437 } 438 439 442 public boolean canParseSiteType(String type) { 443 return (super.canParseSiteType(type) || SiteFileContentProvider.SITE_TYPE.equalsIgnoreCase(type)); 444 } 445 446 } 447 | Popular Tags |