KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > BundleHelper


1 /*******************************************************************************
2  * Copyright (c) 2006 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.core.internal.registry;
12
13 import org.eclipse.core.internal.registry.osgi.Activator;
14 import org.osgi.framework.Bundle;
15 import org.osgi.service.packageadmin.PackageAdmin;
16 import org.osgi.util.tracker.ServiceTracker;
17
18 public class BundleHelper {
19
20     private ServiceTracker bundleTracker = null;
21
22     private static final BundleHelper singleton = new BundleHelper();
23
24     public static BundleHelper getDefault() {
25         return singleton;
26     }
27
28     /**
29      * Private constructor to block instance creation.
30      */

31     private BundleHelper() {
32         super();
33     }
34
35     private PackageAdmin getPackageAdmin() {
36         if (bundleTracker == null) {
37             bundleTracker = new ServiceTracker(Activator.getContext(), PackageAdmin.class.getName(), null);
38             bundleTracker.open();
39         }
40         return (PackageAdmin) bundleTracker.getService();
41     }
42
43     public Bundle getBundle(String JavaDoc symbolicName) {
44         PackageAdmin packageAdmin = getPackageAdmin();
45         if (packageAdmin == null)
46             return null;
47         Bundle[] bundles = packageAdmin.getBundles(symbolicName, null);
48         if (bundles == null)
49             return null;
50         //Return the first bundle that is not installed or uninstalled
51
for (int i = 0; i < bundles.length; i++) {
52             if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
53                 return bundles[i];
54             }
55         }
56         return null;
57     }
58
59     public Bundle[] getHosts(Bundle bundle) {
60         PackageAdmin packageAdmin = getPackageAdmin();
61         if (packageAdmin == null)
62             return null;
63         return packageAdmin.getHosts(bundle);
64     }
65
66 }
67
Popular Tags