001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.bundle;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.util.Properties;
015:
016: import org.eclipse.osgi.service.resolver.BundleDescription;
017: import org.eclipse.osgi.service.resolver.HostSpecification;
018: import org.eclipse.osgi.util.ManifestElement;
019: import org.eclipse.pde.core.IModelChangedEvent;
020: import org.eclipse.pde.core.ModelChangedEvent;
021: import org.eclipse.pde.internal.core.AbstractModel;
022: import org.eclipse.pde.internal.core.ICoreConstants;
023: import org.eclipse.pde.internal.core.PDEState;
024: import org.eclipse.pde.internal.core.ibundle.IBundle;
025: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
026: import org.osgi.framework.BundleException;
027: import org.osgi.framework.Constants;
028:
029: public abstract class BundleModel extends AbstractModel implements
030: IBundleModel {
031:
032: private Bundle fBundle;
033:
034: public BundleModel() {
035: fBundle = new Bundle();
036: fBundle.setModel(this );
037: }
038:
039: public IBundle getBundle() {
040: if (!isLoaded())
041: load();
042: return fBundle;
043: }
044:
045: public String getInstallLocation() {
046: return null;
047: }
048:
049: public abstract void load();
050:
051: public boolean isFragmentModel() {
052: return fBundle.getHeader(Constants.FRAGMENT_HOST) != null;
053: }
054:
055: public void load(InputStream source, boolean outOfSync) {
056: try {
057: fBundle.load(ManifestElement.parseBundleManifest(source,
058: null));
059: if (!outOfSync)
060: updateTimeStamp();
061: setLoaded(true);
062: } catch (BundleException e) {
063: } catch (IOException e) {
064: } finally {
065: }
066: }
067:
068: public void load(BundleDescription desc, PDEState state) {
069: long id = desc.getBundleId();
070: Properties properties = new Properties();
071: properties.put(Constants.BUNDLE_SYMBOLICNAME, desc
072: .getSymbolicName());
073: String value = state.getPluginName(id);
074: if (value != null)
075: properties.put(Constants.BUNDLE_NAME, value);
076: value = state.getProviderName(id);
077: if (value != null)
078: properties.put(Constants.BUNDLE_VENDOR, value);
079: value = state.getClassName(id);
080: if (value != null)
081: properties.put(Constants.BUNDLE_ACTIVATOR, value);
082: value = state.getBundleLocalization(id);
083: if (value != null)
084: properties.put(Constants.BUNDLE_LOCALIZATION, value);
085: if (state.hasExtensibleAPI(id))
086: properties.put(ICoreConstants.EXTENSIBLE_API, "true"); //$NON-NLS-1$
087: if (state.isPatchFragment(id))
088: properties.put(ICoreConstants.PATCH_FRAGMENT, "true"); //$NON-NLS-1$
089: String[] libraries = state.getLibraryNames(id);
090: if (libraries.length > 0) {
091: StringBuffer buffer = new StringBuffer();
092: for (int i = 0; i < libraries.length; i++) {
093: if (buffer.length() > 0) {
094: buffer.append(","); //$NON-NLS-1$
095: buffer.append(System.getProperty("line.separator")); //$NON-NLS-1$
096: buffer.append(" "); //$NON-NLS-1$
097: }
098: buffer.append(libraries[i]);
099: }
100: properties.put(Constants.BUNDLE_CLASSPATH, buffer
101: .toString());
102: }
103: if (desc.getHost() != null) {
104: properties.put(Constants.FRAGMENT_HOST,
105: writeFragmentHost(desc.getHost()));
106: }
107: fBundle.load(properties);
108: updateTimeStamp();
109: setLoaded(true);
110: }
111:
112: private String writeFragmentHost(HostSpecification host) {
113: String id = host.getName();
114: String version = host.getVersionRange().toString();
115: StringBuffer buffer = new StringBuffer();
116: if (id != null)
117: buffer.append(id);
118:
119: if (version != null && version.trim().length() > 0) {
120: buffer
121: .append(";" + Constants.BUNDLE_VERSION_ATTRIBUTE + "=\"" + version + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
122: }
123: return buffer.toString();
124: }
125:
126: public void reload(InputStream source, boolean outOfSync) {
127: load(source, outOfSync);
128: fireModelChanged(new ModelChangedEvent(this ,
129: IModelChangedEvent.WORLD_CHANGED, new Object[0], null));
130: }
131: }
|