1 /*******************************************************************************
2  * Copyright (c) 2006, 2018 Cognos Incorporated, IBM Corporation
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Cognos Incorporated - initial API and implementation
13  *     Chris Aniszczyk <zx@us.ibm.com> - bug 209294
14  *******************************************************************************/
15 package org.eclipse.equinox.internal.cm;
16 
17 import org.osgi.framework.*;
18 import org.osgi.service.cm.ConfigurationAdmin;
19 
20 /**
21  * Activator start the ConfigurationAdminFactory but also handles passing in the Service
22  * Registration needed by Asynch threads. Asynch threads are controlled by ConfigurationAdminFactory
23  * start and stop. It requires some care to handle pending events as the service is registered before
24  * activating the threads. (see EventDispatcher)
25  */
26 public class Activator implements BundleActivator {
27 	private static final String EVENT_ADMIN_CLASS = "org.osgi.service.event.EventAdmin"; //$NON-NLS-1$
28 	private LogTracker logTracker;
29 	private ServiceRegistration<?> registration;
30 	private ConfigurationAdminFactory factory;
31 	private ConfigurationEventAdapter eventAdapter;
32 	private static BundleContext bundleContext;
33 
setBundleContext(BundleContext context)34 	private static synchronized void setBundleContext(BundleContext context) {
35 		bundleContext = context;
36 	}
37 
getProperty(String key)38 	public static synchronized String getProperty(String key) {
39 		if (bundleContext != null)
40 			return bundleContext.getProperty(key);
41 
42 		return null;
43 	}
44 
45 	@Override
start(BundleContext context)46 	public void start(BundleContext context) throws Exception {
47 		setBundleContext(context);
48 		logTracker = new LogTracker(context, System.err);
49 		logTracker.open();
50 		if (checkEventAdmin()) {
51 			eventAdapter = new ConfigurationEventAdapter(context);
52 			eventAdapter.start();
53 		}
54 		factory = new ConfigurationAdminFactory(context, logTracker);
55 		factory.start();
56 		context.addBundleListener(factory);
57 		registration = context.registerService(ConfigurationAdmin.class.getName(), factory, null);
58 	}
59 
60 	@Override
stop(BundleContext context)61 	public void stop(BundleContext context) throws Exception {
62 		registration.unregister();
63 		registration = null;
64 		context.removeBundleListener(factory);
65 		factory.stop();
66 		factory = null;
67 		if (eventAdapter != null) {
68 			eventAdapter.stop();
69 			eventAdapter = null;
70 		}
71 		logTracker.close();
72 		logTracker = null;
73 		setBundleContext(null);
74 	}
75 
checkEventAdmin()76 	private static boolean checkEventAdmin() {
77 		// cannot support scheduling without the event admin package
78 		try {
79 			Class.forName(EVENT_ADMIN_CLASS);
80 			return true;
81 		} catch (ClassNotFoundException e) {
82 			return false;
83 		}
84 	}
85 }
86