KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > internal > jsp > jasper > registry > Activator


1 /*******************************************************************************
2  * Copyright (c) 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.equinox.internal.jsp.jasper.registry;
12
13 import org.osgi.framework.Bundle;
14 import org.osgi.framework.BundleActivator;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.framework.ServiceReference;
17 import org.osgi.service.packageadmin.PackageAdmin;
18 import org.osgi.util.tracker.ServiceTracker;
19 import org.osgi.util.tracker.ServiceTrackerCustomizer;
20
21 public class Activator implements BundleActivator, ServiceTrackerCustomizer {
22
23     private ServiceTracker packageAdminTracker;
24     private static PackageAdmin packageAdmin;
25     private BundleContext context;
26
27     public void start(BundleContext context) throws Exception JavaDoc {
28         this.context = context;
29         packageAdminTracker = new ServiceTracker(context, PackageAdmin.class.getName(), this);
30         packageAdminTracker.open();
31     }
32
33     public void stop(BundleContext context) throws Exception JavaDoc {
34         packageAdminTracker.close();
35         packageAdminTracker = null;
36         this.context = null;
37     }
38
39     public static synchronized Bundle getBundle(String JavaDoc symbolicName) {
40         if (packageAdmin == null)
41             throw new IllegalStateException JavaDoc("Not started"); //$NON-NLS-1$
42

43         Bundle[] bundles = packageAdmin.getBundles(symbolicName, null);
44         if (bundles == null)
45             return null;
46         //Return the first bundle that is not installed or uninstalled
47
for (int i = 0; i < bundles.length; i++) {
48             if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
49                 return bundles[i];
50             }
51         }
52         return null;
53     }
54
55     public Object JavaDoc addingService(ServiceReference reference) {
56         synchronized (Activator.class) {
57             packageAdmin = (PackageAdmin) context.getService(reference);
58         }
59         return packageAdmin;
60     }
61
62     public void modifiedService(ServiceReference reference, Object JavaDoc service) {
63     }
64
65     public void removedService(ServiceReference reference, Object JavaDoc service) {
66         synchronized (Activator.class) {
67             context.ungetService(reference);
68             packageAdmin = null;
69         }
70     }
71 }
72
Popular Tags