1 /*******************************************************************************
2  * Copyright (c) 2005, 2018 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  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 
15 package org.eclipse.equinox.internal.event.mapper;
16 
17 import java.util.HashMap;
18 import java.util.Map;
19 import org.osgi.framework.ServiceEvent;
20 import org.osgi.framework.ServiceReference;
21 import org.osgi.service.event.Event;
22 import org.osgi.service.event.EventAdmin;
23 
24 /**
25  * @version $Revision: 1.3 $
26  */
27 public class ServiceEventAdapter extends EventAdapter {
28 	// constants for Event topic substring
29 	public static final String HEADER = "org/osgi/framework/ServiceEvent"; //$NON-NLS-1$
30 	public static final String UNREGISTERING = "UNREGISTERING"; //$NON-NLS-1$
31 	public static final String MODIFIED = "MODIFIED"; //$NON-NLS-1$
32 	public static final String REGISTERED = "REGISTERED"; //$NON-NLS-1$
33 	private ServiceEvent event;
34 
ServiceEventAdapter(ServiceEvent event, EventAdmin eventAdmin)35 	public ServiceEventAdapter(ServiceEvent event, EventAdmin eventAdmin) {
36 		super(eventAdmin);
37 		this.event = event;
38 	}
39 
40 	@Override
convert()41 	public Event convert() {
42 		String typename = null;
43 		switch (event.getType()) {
44 			case ServiceEvent.REGISTERED :
45 				typename = REGISTERED;
46 				break;
47 			case ServiceEvent.MODIFIED :
48 				typename = MODIFIED;
49 				break;
50 			case ServiceEvent.UNREGISTERING :
51 				typename = UNREGISTERING;
52 				break;
53 			default :
54 				return null;
55 		}
56 		String topic = HEADER + Constants.TOPIC_SEPARATOR + typename;
57 		Map<String, Object> properties = new HashMap<>();
58 		ServiceReference<?> ref = event.getServiceReference();
59 		if (ref != null) {
60 			putServiceReferenceProperties(properties, ref);
61 		}
62 		properties.put(Constants.EVENT, event);
63 		Event converted = new Event(topic, properties);
64 		return converted;
65 	}
66 }