Skip to main content
summaryrefslogtreecommitdiffstats
blob: 487e2145117d07a9d18f3565e7d3f38129348544 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.useradmin;

import java.util.Hashtable;
import org.osgi.framework.*;
import org.osgi.service.prefs.PreferencesService;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**  This is the bundle activator for the UserAdmin bundle.
 */

public class Activator implements BundleActivator, ServiceFactory, ServiceTrackerCustomizer {
	/*
	 * ----------------------------------------------------------------------
	 *      BundleActivator Interface implementation
	 * ----------------------------------------------------------------------
	 */

	protected ServiceRegistration registration;
	protected UserAdmin userAdmin;
	protected static String userAdminClazz = "org.osgi.service.useradmin.UserAdmin"; //$NON-NLS-1$
	protected PreferencesService prefs;
	protected BundleContext context;
	protected ServiceTracker prefsTracker;

    public Activator() {
    	//a public constructor is required for a BundleActivator
    }
	
	/**
	 * Required by BundleActivator Interface.
	 */
	public void start(BundleContext context_) throws Exception {
		this.context = context_;
		prefsTracker = new ServiceTracker(context, PreferencesService.class.getName(), this);
		prefsTracker.open();
	}

	/**
	 * Required by BundleActivator Interface.
	 */
	public void stop(BundleContext context_) throws Exception {
		prefsTracker.close();
        unregisterUserAdminService();
	}


	public Object getService(Bundle bundle, ServiceRegistration registration_) {
		userAdmin.setServiceReference(registration_.getReference());
		return userAdmin;
	}

	public void ungetService(Bundle bundle, ServiceRegistration registration_, Object service) {
	   //do nothing
	}

	public Object addingService(ServiceReference reference) {
		if (prefs == null) {
			prefs = (PreferencesService) context.getService(reference);
			try {
				registerUserAdminService();
			} catch (Exception ex) {
				return null;
			}
			return prefs;
		}
		return null; //we don't want to track a service we are not using
	}

	public void modifiedService(ServiceReference reference, Object service) {
            // do nothing
	}

	public void removedService(ServiceReference reference, Object service) {
		if (service == prefs) {
			prefs = null;
		}
		unregisterUserAdminService();
	}


	/**
	 * Register the UserAdmin service.
	 */
	protected void registerUserAdminService() throws Exception {
		Hashtable properties = new Hashtable(7);

		properties.put(Constants.SERVICE_VENDOR, UserAdminMsg.Service_Vendor);
		properties.put(Constants.SERVICE_DESCRIPTION, UserAdminMsg.OSGi_User_Admin_service_IBM_Implementation_3);
		properties.put(Constants.SERVICE_PID, getClass().getName());

		userAdmin = new UserAdmin(prefs, context);
		registration = context.registerService(userAdminClazz, this, properties);
		userAdmin.setServiceReference(registration.getReference());
	}
	
	protected void unregisterUserAdminService() {
		if(registration != null)
		{
			registration.unregister();
			registration = null;
			userAdmin.destroy();
			userAdmin = null;
		}
	}
}

Back to the top