KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > defaultadaptor > DefaultAdaptor


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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
12 package org.eclipse.osgi.framework.internal.defaultadaptor;
13
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.security.AccessController JavaDoc;
17 import java.security.PrivilegedActionException JavaDoc;
18 import java.security.PrivilegedExceptionAction JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import org.eclipse.osgi.framework.adaptor.BundleData;
21 import org.eclipse.osgi.framework.adaptor.core.*;
22 import org.eclipse.osgi.framework.debug.Debug;
23 import org.eclipse.osgi.framework.log.FrameworkLog;
24 import org.osgi.framework.BundleException;
25 import org.osgi.framework.FrameworkEvent;
26
27 /**
28  * The DefaultAdaptor for the Framework. This adaptor uses
29  * root bundle store directory on the local filesystem to
30  * store bundle files and bundle data.
31  * <p>
32  * Each bundle installed in the Framework will have a unique
33  * directory using the bundle ID as the name. Each bundles
34  * unique directory has a generational directory a data directory
35  * and a metadata file.
36  * <p>
37  * The generational directory is used to store the different
38  * versions of the bundle that have been installed. Each time
39  * the bundle is updated a new generational directory will be
40  * created.
41  * <p>
42  * The data directory is used to create data file objects requested
43  * by the bundle. This directory will not change when updating
44  * a bundle
45  * <p>
46  * The metadata file contains persistent data about the bundle
47  * (e.g. startlevel, persistent start state, etc)
48  */

49 public class DefaultAdaptor extends AbstractFrameworkAdaptor {
50     public static final String JavaDoc METADATA_ADAPTOR_NEXTID = "METADATA_ADAPTOR_NEXTID"; //$NON-NLS-1$
51
public static final String JavaDoc METADATA_ADAPTOR_IBSL = "METADATA_ADAPTOR_IBSL"; //$NON-NLS-1$
52

53     public static final String JavaDoc METADATA_BUNDLE_GEN = "METADATA_BUNDLE_GEN"; //$NON-NLS-1$
54
public static final String JavaDoc METADATA_BUNDLE_LOC = "METADATA_BUNDLE_LOC"; //$NON-NLS-1$
55
public static final String JavaDoc METADATA_BUNDLE_REF = "METADATA_BUNDLE_REF"; //$NON-NLS-1$
56
public static final String JavaDoc METADATA_BUNDLE_NAME = "METADATA_BUNDLE_NAME"; //$NON-NLS-1$
57
public static final String JavaDoc METADATA_BUNDLE_NCP = "METADATA_BUNDLE_NCP"; //$NON-NLS-1$
58
public static final String JavaDoc METADATA_BUNDLE_ABSL = "METADATA_BUNDLE_ABSL"; //$NON-NLS-1$
59
public static final String JavaDoc METADATA_BUNDLE_STATUS = "METADATA_BUNDLE_STATUS"; //$NON-NLS-1$
60
public static final String JavaDoc METADATA_BUNDLE_METADATA = "METADATA_BUNDLE_METADATA"; //$NON-NLS-1$
61
public static final String JavaDoc METADATA_LAST_MODIFIED = "METADATA_LAST_MODIFIED"; //$NON-NLS-1$
62

63     /**
64      * The MetaData for the default adaptor
65      */

66     protected MetaData fwMetadata;
67
68     public DefaultAdaptor(String JavaDoc[] args) {
69         super(args);
70     }
71
72     /**
73      * @see org.eclipse.osgi.framework.adaptor.FrameworkAdaptor#getInstalledBundles()
74      */

75     public BundleData[] getInstalledBundles() {
76         String JavaDoc list[] = getBundleStoreRootDir().list();
77
78         if (list == null) {
79             return null;
80         }
81         ArrayList JavaDoc bundleDatas = new ArrayList JavaDoc(list.length);
82
83         /* create bundle objects for all installed bundles. */
84         for (int i = 0; i < list.length; i++) {
85             try {
86                 DefaultBundleData data;
87
88                 long id = -1;
89                 try {
90                     id = Long.parseLong(list[i]);
91                 } catch (NumberFormatException JavaDoc nfe) {
92                     continue;
93                 }
94                 data = (DefaultBundleData) getElementFactory().createBundleData(this, id);
95                 loadMetaDataFor(data);
96                 data.initializeExistingBundle();
97
98                 if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
99                     Debug.println("BundleData created: " + data); //$NON-NLS-1$
100
}
101                 processExtension(data, EXTENSION_INITIALIZE);
102                 bundleDatas.add(data);
103             } catch (BundleException e) {
104                 if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
105                     Debug.println("Unable to open Bundle[" + list[i] + "]: " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
106
Debug.printStackTrace(e);
107                 }
108             } catch (IOException JavaDoc e) {
109                 if (Debug.DEBUG && Debug.DEBUG_GENERAL) {
110                     Debug.println("Unable to open Bundle[" + list[i] + "]: " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
111
Debug.printStackTrace(e);
112                 }
113             }
114         }
115
116         return (BundleData[]) bundleDatas.toArray(new BundleData[bundleDatas.size()]);
117     }
118
119     public void setInitialBundleStartLevel(int value) {
120         super.setInitialBundleStartLevel(value);
121         try {
122             persistInitialBundleStartLevel(value);
123         } catch (IOException JavaDoc e) {
124             eventPublisher.publishFrameworkEvent(FrameworkEvent.ERROR, context.getBundle(), e);
125         }
126     }
127
128     protected void persistInitialBundleStartLevel(int value) throws IOException JavaDoc {
129         fwMetadata.setInt(METADATA_ADAPTOR_IBSL, value);
130         try {
131             AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
132                 public Object JavaDoc run() throws Exception JavaDoc {
133                     fwMetadata.save();
134                     return null;
135                 }
136             });
137         } catch (PrivilegedActionException JavaDoc e) {
138             if (e.getException() instanceof IOException JavaDoc) {
139                 throw (IOException JavaDoc)e.getException();
140             }
141             throw (RuntimeException JavaDoc)e.getException();
142         }
143     }
144
145     public AdaptorElementFactory getElementFactory() {
146         if (elementFactory == null)
147             elementFactory = new DefaultElementFactory();
148         return elementFactory;
149     }
150
151     protected void loadMetaDataFor(DefaultBundleData data) throws IOException JavaDoc {
152         MetaData bundleMetaData = (new MetaData(new File JavaDoc(data.getBundleStoreDir(), ".bundle"), "Bundle metadata")); //$NON-NLS-1$ //$NON-NLS-2$
153
bundleMetaData.load();
154
155         data.setLocation(bundleMetaData.get(METADATA_BUNDLE_LOC, null));
156         data.setFileName(bundleMetaData.get(METADATA_BUNDLE_NAME, null));
157         data.setGeneration(bundleMetaData.getInt(METADATA_BUNDLE_GEN, -1));
158         data.setNativePaths(bundleMetaData.get(METADATA_BUNDLE_NCP, null));
159         data.setStartLevel(bundleMetaData.getInt(METADATA_BUNDLE_ABSL, 1));
160         data.setStatus(bundleMetaData.getInt(METADATA_BUNDLE_STATUS, 0));
161         data.setReference(bundleMetaData.getBoolean(METADATA_BUNDLE_REF, false));
162         data.setLastModified(bundleMetaData.getLong(METADATA_LAST_MODIFIED, 0));
163
164         if (data.getGeneration() == -1 || data.getFileName() == null || data.getLocation() == null) {
165             throw new IOException JavaDoc(AdaptorMsg.ADAPTOR_STORAGE_EXCEPTION); //$NON-NLS-1$
166
}
167     }
168
169     public void saveMetaDataFor(AbstractBundleData data) throws IOException JavaDoc {
170         MetaData bundleMetadata = (new MetaData(new File JavaDoc(((DefaultBundleData) data).createBundleStoreDir(), ".bundle"), "Bundle metadata")); //$NON-NLS-1$ //$NON-NLS-2$
171
bundleMetadata.load();
172
173         bundleMetadata.set(METADATA_BUNDLE_LOC, data.getLocation());
174         bundleMetadata.set(METADATA_BUNDLE_NAME, data.getFileName());
175         bundleMetadata.setInt(METADATA_BUNDLE_GEN, data.getGeneration());
176         String JavaDoc nativePaths = data.getNativePathsString();
177         if (nativePaths != null) {
178             bundleMetadata.set(METADATA_BUNDLE_NCP, nativePaths);
179         }
180         bundleMetadata.setInt(METADATA_BUNDLE_ABSL, data.getStartLevel());
181         bundleMetadata.setInt(METADATA_BUNDLE_STATUS, data.getStatus());
182         bundleMetadata.setBoolean(METADATA_BUNDLE_REF, data.isReference());
183         bundleMetadata.setLong(METADATA_LAST_MODIFIED, data.getLastModified());
184
185         bundleMetadata.save();
186     }
187
188     protected void persistNextBundleID(long id) throws IOException JavaDoc {
189         fwMetadata.setLong(METADATA_ADAPTOR_NEXTID, nextId);
190         fwMetadata.save();
191     }
192
193     protected void initializeMetadata() throws IOException JavaDoc {
194         fwMetadata = new MetaData(getMetaDataFile(), "Framework metadata"); //$NON-NLS-1$
195
fwMetadata.load();
196         nextId = fwMetadata.getLong(METADATA_ADAPTOR_NEXTID, 1);
197         initialBundleStartLevel = fwMetadata.getInt(METADATA_ADAPTOR_IBSL, 1);
198     }
199
200     protected FrameworkLog createFrameworkLog() {
201         return new DefaultLog();
202     }
203 }
204
Popular Tags