KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > SystemBundleActivator


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.core;
13
14 import java.util.Dictionary JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import org.eclipse.osgi.framework.debug.FrameworkDebugOptions;
17 import org.osgi.framework.*;
18 import org.osgi.service.condpermadmin.ConditionalPermissionAdmin;
19
20 /**
21  * This class activates the System Bundle.
22  */

23
24 public class SystemBundleActivator implements BundleActivator {
25     protected BundleContext context;
26     protected SystemBundle bundle;
27     protected Framework framework;
28     protected ServiceRegistration packageAdmin;
29     protected ServiceRegistration permissionAdmin;
30     protected ServiceRegistration condPermAdmin;
31     protected ServiceRegistration startLevel;
32     protected ServiceRegistration debugOptions;
33
34     public SystemBundleActivator() {
35     }
36
37     public void start(BundleContext context) throws Exception JavaDoc {
38         this.context = context;
39         bundle = (SystemBundle) context.getBundle();
40         framework = bundle.framework;
41
42         if (framework.packageAdmin != null)
43             packageAdmin = register(Constants.OSGI_PACKAGEADMIN_NAME, framework.packageAdmin);
44         if (framework.permissionAdmin != null)
45             permissionAdmin = register(Constants.OSGI_PERMISSIONADMIN_NAME, framework.permissionAdmin);
46         if (framework.startLevelManager != null)
47             startLevel = register(Constants.OSGI_STARTLEVEL_NAME, framework.startLevelManager);
48         if (framework.condPermAdmin != null)
49             condPermAdmin = register(ConditionalPermissionAdmin.class.getName(), framework.condPermAdmin);
50         FrameworkDebugOptions dbgOptions = null;
51         if ((dbgOptions = FrameworkDebugOptions.getDefault()) != null)
52             debugOptions = register(org.eclipse.osgi.service.debug.DebugOptions.class.getName(), dbgOptions);
53
54         // Always call the adaptor.frameworkStart() at the end of this method.
55
framework.adaptor.frameworkStart(context);
56         // attempt to resolve all bundles
57
// this is done after the adaptor.frameworkStart has been called
58
// this should be the first time the resolver State is accessed
59
framework.packageAdmin.setResolvedBundles(bundle);
60     }
61
62     public void stop(BundleContext context) throws Exception JavaDoc {
63         // Always call the adaptor.frameworkStop() at the begining of this method.
64
framework.adaptor.frameworkStop(context);
65
66         if (packageAdmin != null)
67             packageAdmin.unregister();
68         if (permissionAdmin != null)
69             permissionAdmin.unregister();
70         if (condPermAdmin != null)
71             condPermAdmin.unregister();
72         if (startLevel != null)
73             startLevel.unregister();
74         if (debugOptions != null)
75             debugOptions.unregister();
76
77         framework = null;
78         bundle = null;
79         this.context = null;
80     }
81
82     /**
83      * Register a service object.
84      *
85      */

86     protected ServiceRegistration register(String JavaDoc name, Object JavaDoc service) {
87         Hashtable JavaDoc properties = new Hashtable JavaDoc(7);
88         Dictionary JavaDoc headers = bundle.getHeaders();
89         properties.put(Constants.SERVICE_VENDOR, headers.get(Constants.BUNDLE_VENDOR));
90         properties.put(Constants.SERVICE_RANKING, new Integer JavaDoc(Integer.MAX_VALUE));
91         properties.put(Constants.SERVICE_PID, bundle.getBundleId() + "." + service.getClass().getName()); //$NON-NLS-1$
92
return context.registerService(name, service, properties);
93     }
94
95 }
96
Popular Tags