1 /******************************************************************************* 2 * Copyright (c) 2001, 2008 IBM Corporation and others. 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 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.equinox.internal.useradmin; 15 16 import java.util.Hashtable; 17 import org.osgi.framework.*; 18 import org.osgi.service.prefs.PreferencesService; 19 import org.osgi.util.tracker.ServiceTracker; 20 import org.osgi.util.tracker.ServiceTrackerCustomizer; 21 22 /** This is the bundle activator for the UserAdmin bundle. 23 */ 24 25 public class Activator implements BundleActivator, ServiceFactory, ServiceTrackerCustomizer { 26 /* 27 * ---------------------------------------------------------------------- 28 * BundleActivator Interface implementation 29 * ---------------------------------------------------------------------- 30 */ 31 private static final String EVENT_ADMIN_CLASS = "org.osgi.service.event.EventAdmin"; //$NON-NLS-1$ 32 protected ServiceRegistration registration; 33 protected UserAdmin userAdmin; 34 protected static String userAdminClazz = "org.osgi.service.useradmin.UserAdmin"; //$NON-NLS-1$ 35 protected PreferencesService prefs; 36 protected BundleContext context; 37 protected ServiceTracker prefsTracker; 38 protected UserAdminEventAdapter eventAdapter; 39 Activator()40 public Activator() { 41 //a public constructor is required for a BundleActivator 42 } 43 44 /** 45 * Required by BundleActivator Interface. 46 */ start(BundleContext context_)47 public void start(BundleContext context_) throws Exception { 48 this.context = context_; 49 prefsTracker = new ServiceTracker(context, PreferencesService.class.getName(), this); 50 prefsTracker.open(); 51 52 if (checkEventAdmin()) { 53 eventAdapter = new UserAdminEventAdapter(context_); 54 eventAdapter.start(); 55 } 56 } 57 checkEventAdmin()58 private static boolean checkEventAdmin() { 59 // cannot support scheduling without the event admin package 60 try { 61 Class.forName(EVENT_ADMIN_CLASS); 62 return true; 63 } catch (ClassNotFoundException e) { 64 return false; 65 } 66 } 67 68 /** 69 * Required by BundleActivator Interface. 70 */ stop(BundleContext context_)71 public void stop(BundleContext context_) throws Exception { 72 if (eventAdapter != null) { 73 eventAdapter.stop(); 74 eventAdapter = null; 75 } 76 77 prefsTracker.close(); 78 unregisterUserAdminService(); 79 } 80 getService(Bundle bundle, ServiceRegistration registration_)81 public Object getService(Bundle bundle, ServiceRegistration registration_) { 82 userAdmin.setServiceReference(registration_.getReference()); 83 return userAdmin; 84 } 85 ungetService(Bundle bundle, ServiceRegistration registration_, Object service)86 public void ungetService(Bundle bundle, ServiceRegistration registration_, Object service) { 87 //do nothing 88 } 89 addingService(ServiceReference reference)90 public Object addingService(ServiceReference reference) { 91 if (prefs == null) { 92 prefs = (PreferencesService) context.getService(reference); 93 try { 94 registerUserAdminService(); 95 } catch (Exception ex) { 96 return null; 97 } 98 return prefs; 99 } 100 return null; //we don't want to track a service we are not using 101 } 102 modifiedService(ServiceReference reference, Object service)103 public void modifiedService(ServiceReference reference, Object service) { 104 // do nothing 105 } 106 removedService(ServiceReference reference, Object service)107 public void removedService(ServiceReference reference, Object service) { 108 if (service == prefs) { 109 prefs = null; 110 unregisterUserAdminService(); 111 } 112 context.ungetService(reference); 113 } 114 115 /** 116 * Register the UserAdmin service. 117 */ registerUserAdminService()118 protected void registerUserAdminService() throws Exception { 119 Hashtable properties = new Hashtable(7); 120 121 properties.put(Constants.SERVICE_VENDOR, UserAdminMsg.Service_Vendor); 122 properties.put(Constants.SERVICE_DESCRIPTION, UserAdminMsg.OSGi_User_Admin_service_IBM_Implementation_3); 123 properties.put(Constants.SERVICE_PID, getClass().getName()); 124 125 userAdmin = new UserAdmin(prefs, context); 126 registration = context.registerService(userAdminClazz, this, properties); 127 userAdmin.setServiceReference(registration.getReference()); 128 } 129 unregisterUserAdminService()130 protected void unregisterUserAdminService() { 131 if (registration != null) { 132 registration.unregister(); 133 registration = null; 134 userAdmin.destroy(); 135 userAdmin = null; 136 } 137 } 138 } 139