1 11 package org.eclipse.pde.internal.core.bundle; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.Properties ; 16 17 import org.eclipse.osgi.service.resolver.BundleDescription; 18 import org.eclipse.osgi.service.resolver.HostSpecification; 19 import org.eclipse.osgi.util.ManifestElement; 20 import org.eclipse.pde.core.IModelChangedEvent; 21 import org.eclipse.pde.core.ModelChangedEvent; 22 import org.eclipse.pde.internal.core.AbstractModel; 23 import org.eclipse.pde.internal.core.ICoreConstants; 24 import org.eclipse.pde.internal.core.PDEState; 25 import org.eclipse.pde.internal.core.ibundle.IBundle; 26 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 27 import org.osgi.framework.BundleException; 28 import org.osgi.framework.Constants; 29 30 public abstract class BundleModel 31 extends AbstractModel 32 implements IBundleModel { 33 34 private Bundle fBundle; 35 36 public BundleModel() { 37 fBundle = new Bundle(); 38 fBundle.setModel(this); 39 } 40 41 public IBundle getBundle() { 42 if (!isLoaded()) 43 load(); 44 return fBundle; 45 } 46 47 public String getInstallLocation() { 48 return null; 49 } 50 51 public abstract void load(); 52 53 public boolean isFragmentModel() { 54 return fBundle.getHeader(Constants.FRAGMENT_HOST) != null; 55 } 56 57 public void load(InputStream source, boolean outOfSync) { 58 try { 59 fBundle.load(ManifestElement.parseBundleManifest(source, null)); 60 if (!outOfSync) 61 updateTimeStamp(); 62 setLoaded(true); 63 } catch (BundleException e) { 64 } catch (IOException e) { 65 } finally { 66 } 67 } 68 69 public void load(BundleDescription desc, PDEState state) { 70 long id = desc.getBundleId(); 71 Properties properties = new Properties (); 72 properties.put(Constants.BUNDLE_SYMBOLICNAME, desc.getSymbolicName()); 73 String value = state.getPluginName(id); 74 if (value != null) 75 properties.put(Constants.BUNDLE_NAME, value); 76 value = state.getProviderName(id); 77 if (value != null) 78 properties.put(Constants.BUNDLE_VENDOR, value); 79 value = state.getClassName(id); 80 if (value != null) 81 properties.put(Constants.BUNDLE_ACTIVATOR, value); 82 value = state.getBundleLocalization(id); 83 if (value != null) 84 properties.put(Constants.BUNDLE_LOCALIZATION, value); 85 if (state.hasExtensibleAPI(id)) 86 properties.put(ICoreConstants.EXTENSIBLE_API, "true"); if (state.isPatchFragment(id)) 88 properties.put(ICoreConstants.PATCH_FRAGMENT, "true"); String [] libraries = state.getLibraryNames(id); 90 if (libraries.length > 0) { 91 StringBuffer buffer = new StringBuffer (); 92 for (int i = 0; i < libraries.length; i++) { 93 if (buffer.length() > 0) { 94 buffer.append(","); buffer.append(System.getProperty("line.separator")); buffer.append(" "); } 98 buffer.append(libraries[i]); 99 } 100 properties.put(Constants.BUNDLE_CLASSPATH, buffer.toString()); 101 } 102 if (desc.getHost() != null) { 103 properties.put(Constants.FRAGMENT_HOST, writeFragmentHost(desc.getHost())); 104 } 105 fBundle.load(properties); 106 updateTimeStamp(); 107 setLoaded(true); 108 } 109 110 private String writeFragmentHost(HostSpecification host) { 111 String id = host.getName(); 112 String version = host.getVersionRange().toString(); 113 StringBuffer buffer = new StringBuffer (); 114 if (id != null) 115 buffer.append(id); 116 117 if (version != null && version.trim().length() > 0) { 118 buffer.append(";" + Constants.BUNDLE_VERSION_ATTRIBUTE + "=\"" + version + "\""); } 120 return buffer.toString(); 121 } 122 123 public void reload(InputStream source, boolean outOfSync) { 124 load(source, outOfSync); 125 fireModelChanged( 126 new ModelChangedEvent(this, 127 IModelChangedEvent.WORLD_CHANGED, 128 new Object [0], 129 null)); 130 } 131 } 132 | Popular Tags |