KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > bundle > BundleModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.pde.internal.core.bundle;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.Properties JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
65         } finally {
66         }
67     }
68     
69     public void load(BundleDescription desc, PDEState state) {
70         long id = desc.getBundleId();
71         Properties JavaDoc properties = new Properties JavaDoc();
72         properties.put(Constants.BUNDLE_SYMBOLICNAME, desc.getSymbolicName());
73         String JavaDoc 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"); //$NON-NLS-1$
87
if (state.isPatchFragment(id))
88             properties.put(ICoreConstants.PATCH_FRAGMENT, "true"); //$NON-NLS-1$
89
String JavaDoc[] libraries = state.getLibraryNames(id);
90         if (libraries.length > 0) {
91             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
92             for (int i = 0; i < libraries.length; i++) {
93                 if (buffer.length() > 0) {
94                     buffer.append(","); //$NON-NLS-1$
95
buffer.append(System.getProperty("line.separator")); //$NON-NLS-1$
96
buffer.append(" "); //$NON-NLS-1$
97
}
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 JavaDoc writeFragmentHost(HostSpecification host) {
111         String JavaDoc id = host.getName();
112         String JavaDoc version = host.getVersionRange().toString();
113         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
119
}
120         return buffer.toString();
121     }
122
123     public void reload(InputStream JavaDoc source, boolean outOfSync) {
124         load(source, outOfSync);
125         fireModelChanged(
126             new ModelChangedEvent(this,
127                 IModelChangedEvent.WORLD_CHANGED,
128                 new Object JavaDoc[0],
129                 null));
130     }
131 }
132
Popular Tags