Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 202278 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/equinox/event/mapper/LogReaderServiceListener.java (-26 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005 IBM Corporation.
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.equinox.event.mapper;
13
14
import org.osgi.framework.ServiceReference;
15
import org.osgi.service.log.LogReaderService;
16
17
/**
18
 * @version $Revision: 1.1 $
19
 */
20
public interface LogReaderServiceListener {
21
	public void logReaderServiceAdding(ServiceReference reference,
22
			LogReaderService service);
23
24
	public void logReaderServiceRemoved(ServiceReference reference,
25
			LogReaderService service);
26
}
(-)src/org/eclipse/equinox/event/mapper/FrameworkEventAdapter.java (-72 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.event.mapper;
13
14
import java.util.Hashtable;
15
import org.osgi.framework.Bundle;
16
import org.osgi.framework.FrameworkEvent;
17
import org.osgi.service.event.Event;
18
import org.osgi.service.event.EventAdmin;
19
20
/**
21
 * @version $Revision: 1.2 $
22
 */
23
public class FrameworkEventAdapter extends EventAdapter {
24
	// constants for Event topic substring
25
	public static final String HEADER = "org/osgi/framework/FrameworkEvent";
26
	public static final String STARTLEVEL_CHANGED = "STARTLEVEL_CHANGED";
27
	public static final String STARTED = "STARTED";
28
	public static final String PACKAGES_REFRESHED = "PACKAGES_REFRESHED";
29
	public static final String ERROR = "ERROR";
30
	protected FrameworkEvent event;
31
32
	public FrameworkEventAdapter(FrameworkEvent event, EventAdmin eventAdmin) {
33
		super(eventAdmin);
34
		this.event = event;
35
	}
36
37
	/**
38
	 * @see org.eclipse.equinox.event.mapper.EventAdapter#convert()
39
	 */
40
	public Event convert() {
41
		String typename = null;
42
		switch (event.getType()) {
43
			case FrameworkEvent.ERROR :
44
				typename = ERROR;
45
				break;
46
			case FrameworkEvent.PACKAGES_REFRESHED :
47
				typename = PACKAGES_REFRESHED;
48
				break;
49
			case FrameworkEvent.STARTED :
50
				typename = STARTED;
51
				break;
52
			case FrameworkEvent.STARTLEVEL_CHANGED :
53
				typename = STARTLEVEL_CHANGED;
54
				break;
55
			default :
56
				return null;
57
		}
58
		String topic = HEADER + Constants.TOPIC_SEPARATOR + typename;
59
		Hashtable properties = new Hashtable();
60
		Bundle bundle = event.getBundle();
61
		if (bundle != null) {
62
			putBundleProperties(properties, bundle);
63
		}
64
		Throwable t = event.getThrowable();
65
		if (t != null) {
66
			putExceptionProperties(properties, t);
67
		}
68
		properties.put(Constants.EVENT, event);
69
		Event converted = new Event(topic, properties);
70
		return converted;
71
	}
72
}
(-)src/org/eclipse/equinox/event/mapper/WireAdminEventAdapter.java (-107 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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
package org.eclipse.equinox.event.mapper;
12
13
import java.util.Hashtable;
14
import org.osgi.framework.ServiceReference;
15
import org.osgi.service.event.Event;
16
import org.osgi.service.event.EventAdmin;
17
import org.osgi.service.wireadmin.Wire;
18
import org.osgi.service.wireadmin.WireAdminEvent;
19
20
/**
21
 * @version $Revision: 1.2 $
22
 */
23
public class WireAdminEventAdapter extends EventAdapter {
24
	// constants for Event topic substring
25
	public static final String TOPIC = "org/osgi/service/wireadmin/WireAdminEvent";
26
	public static final String WIRE_CREATED = "WIRE_CREATED";
27
	public static final String WIRE_CONNECTED_T = "WIRE_CONNECTED";
28
	public static final String WIRE_UPDATED = "WIRE_UPDATED";
29
	public static final String WIRE_TRACE = "WIRE_TRACE";
30
	public static final String WIRE_DISCONNECTED = "WIRE_DISCONNECTED";
31
	public static final String WIRE_DELETED = "WIRE_DELETED";
32
	public static final String PRODUCER_EXCEPTION = "PRODUCER_EXCEPTION";
33
	public static final String CONSUMER_EXCEPTION = "CONSUMER_EXCEPTION";
34
	// constants for Event properties
35
	public static final String WIRE = "wire";
36
	public static final String WIRE_FLAVORS = "wire.flavors";
37
	public static final String WIRE_SCOPE = "wire.scope";
38
	public static final String WIRE_CONNECTED_P = "wire.connected";
39
	public static final String WIRE_VALID = "wire.valid";
40
	private WireAdminEvent event;
41
42
	public WireAdminEventAdapter(WireAdminEvent event, EventAdmin admin) {
43
		super(admin);
44
		this.event = event;
45
	}
46
47
	/**
48
	 * @see org.eclipse.equinox.event.mapper.EventAdapter#convert()
49
	 */
50
	public Event convert() {
51
		String typename = null;
52
		switch (event.getType()) {
53
			case WireAdminEvent.WIRE_CONNECTED :
54
				typename = WIRE_CONNECTED_T;
55
				break;
56
			case WireAdminEvent.WIRE_CREATED :
57
				typename = WIRE_CREATED;
58
				break;
59
			case WireAdminEvent.WIRE_UPDATED :
60
				typename = WIRE_UPDATED;
61
				break;
62
			case WireAdminEvent.WIRE_DELETED :
63
				typename = WIRE_DELETED;
64
				break;
65
			case WireAdminEvent.WIRE_DISCONNECTED :
66
				typename = WIRE_DISCONNECTED;
67
				break;
68
			case WireAdminEvent.WIRE_TRACE :
69
				typename = WIRE_TRACE;
70
				break;
71
			case WireAdminEvent.PRODUCER_EXCEPTION :
72
				typename = PRODUCER_EXCEPTION;
73
				break;
74
			case WireAdminEvent.CONSUMER_EXCEPTION :
75
				typename = CONSUMER_EXCEPTION;
76
				break;
77
			default :
78
				return null;
79
		}
80
		String topic = TOPIC + Constants.TOPIC_SEPARATOR + typename;
81
		Hashtable properties = new Hashtable();
82
		Throwable t = event.getThrowable();
83
		if (t != null) {
84
			putExceptionProperties(properties, t);
85
		}
86
		ServiceReference ref = event.getServiceReference();
87
		if (ref == null) {
88
			throw new RuntimeException("WireAdminEvent's getServiceReference() returns null.");
89
		}
90
		putServiceReferenceProperties(properties, ref);
91
		Wire wire = event.getWire();
92
		if (wire != null) {
93
			properties.put(WIRE, wire);
94
			if (wire.getFlavors() != null) {
95
				properties.put(WIRE_FLAVORS, classes2strings(wire.getFlavors()));
96
			}
97
			if (wire.getScope() != null) {
98
				properties.put(WIRE_SCOPE, wire.getScope());
99
			}
100
			properties.put(WIRE_CONNECTED_P, new Boolean(wire.isConnected()));
101
			properties.put(WIRE_VALID, new Boolean(wire.isValid()));
102
		}
103
		properties.put(Constants.EVENT, event);
104
		Event converted = new Event(topic, properties);
105
		return converted;
106
	}
107
}
(-)src/org/eclipse/equinox/event/mapper/EventAdapter.java (-100 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.event.mapper;
13
14
import java.util.Hashtable;
15
import org.osgi.framework.Bundle;
16
import org.osgi.framework.ServiceReference;
17
import org.osgi.service.event.Event;
18
import org.osgi.service.event.EventAdmin;
19
20
/**
21
 * @version $Revision: 1.2 $
22
 */
23
public abstract class EventAdapter {
24
	final EventAdmin eventAdmin;
25
26
	/**
27
	 * @param event
28
	 * @param eventAdmin
29
	 */
30
	public EventAdapter(EventAdmin eventAdmin) {
31
		this.eventAdmin = eventAdmin;
32
	}
33
34
	/**
35
	 * @return event
36
	 */
37
	public abstract Event convert();
38
39
	public void redeliver() {
40
		Event converted = convert();
41
		if (converted != null) {
42
			redeliverInternal(converted);
43
		}
44
	}
45
46
	/**
47
	 * subclasses should override this method if it wants to use sendEvent()
48
	 * instead.
49
	 */
50
	protected void redeliverInternal(Event converted) {
51
		eventAdmin.postEvent(converted);
52
	}
53
54
	public void putBundleProperties(Hashtable properties, Bundle bundle) {
55
		// assertion bundle != null
56
		properties.put(Constants.BUNDLE_ID, new Long(bundle.getBundleId()));
57
		String symbolicName = bundle.getSymbolicName();
58
		if (symbolicName != null) {
59
			properties.put(Constants.BUNDLE_SYMBOLICNAME, symbolicName);
60
		}
61
		properties.put(Constants.BUNDLE, bundle);
62
	}
63
64
	public void putExceptionProperties(Hashtable properties, Throwable t) {
65
		// assertion t != null
66
		properties.put(Constants.EXCEPTION, t);
67
		properties.put(Constants.EXCEPTION_CLASS, t.getClass().getName());
68
		String message = t.getMessage();
69
		if (message != null) {
70
			properties.put(Constants.EXCEPTION_MESSAGE, t.getMessage());
71
		}
72
	}
73
74
	public void putServiceReferenceProperties(Hashtable properties, ServiceReference ref) {
75
		// assertion ref != null
76
		properties.put(Constants.SERVICE, ref);
77
		properties.put(Constants.SERVICE_ID, ref.getProperty(org.osgi.framework.Constants.SERVICE_ID));
78
		Object o = ref.getProperty(org.osgi.framework.Constants.SERVICE_PID);
79
		if ((o != null) && (o instanceof String)) {
80
			properties.put(Constants.SERVICE_PID, (String) o);
81
		}
82
		Object o2 = ref.getProperty(org.osgi.framework.Constants.OBJECTCLASS);
83
		if ((o2 != null) && (o2 instanceof String[])) {
84
			properties.put(Constants.SERVICE_OBJECTCLASS, (String[]) o2);
85
		}
86
	}
87
88
	/*
89
	 * Utility function for converting classes into strings
90
	 */
91
	public String[] classes2strings(Class classes[]) {
92
		if ((classes == null) || (classes.length == 0))
93
			return null;
94
		String[] strings = new String[classes.length];
95
		for (int i = 0; i < classes.length; i++) {
96
			strings[i] = classes[i].getName();
97
		}
98
		return strings;
99
	}
100
}
(-)src/org/eclipse/equinox/event/mapper/ServiceEventAdapter.java (-64 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.event.mapper;
13
14
import java.util.Hashtable;
15
import org.osgi.framework.ServiceEvent;
16
import org.osgi.framework.ServiceReference;
17
import org.osgi.service.event.Event;
18
import org.osgi.service.event.EventAdmin;
19
20
/**
21
 * @version $Revision: 1.3 $
22
 */
23
public class ServiceEventAdapter extends EventAdapter {
24
	// constants for Event topic substring
25
	public static final String HEADER = "org/osgi/framework/ServiceEvent";
26
	public static final String UNREGISTERING = "UNREGISTERING";
27
	public static final String MODIFIED = "MODIFIED";
28
	public static final String REGISTERED = "REGISTERED";
29
	private ServiceEvent event;
30
31
	public ServiceEventAdapter(ServiceEvent event, EventAdmin eventAdmin) {
32
		super(eventAdmin);
33
		this.event = event;
34
	}
35
36
	/**
37
	 * @see org.eclipse.equinox.event.mapper.EventAdapter#convert()
38
	 */
39
	public Event convert() {
40
		String typename = null;
41
		switch (event.getType()) {
42
			case ServiceEvent.REGISTERED :
43
				typename = REGISTERED;
44
				break;
45
			case ServiceEvent.MODIFIED :
46
				typename = MODIFIED;
47
				break;
48
			case ServiceEvent.UNREGISTERING :
49
				typename = UNREGISTERING;
50
				break;
51
			default :
52
				return null;
53
		}
54
		String topic = HEADER + Constants.TOPIC_SEPARATOR + typename;
55
		Hashtable properties = new Hashtable();
56
		ServiceReference ref = event.getServiceReference();
57
		if (ref != null) {
58
			putServiceReferenceProperties(properties, ref);
59
		}
60
		properties.put(Constants.EVENT, event);
61
		Event converted = new Event(topic, properties);
62
		return converted;
63
	}
64
}
(-)src/org/eclipse/equinox/event/mapper/Constants.java (-33 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005 IBM Corporation.
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
package org.eclipse.equinox.event.mapper;
12
13
/**
14
 * @version $Revision: 1.1 $
15
 */
16
public interface Constants {
17
	// constants for Event common properties; event specific properties are
18
	// defined in the corresponding event adapter.
19
	public static final String	BUNDLE				= "bundle";
20
	public static final String	BUNDLE_ID			= "bundle.id";
21
	public static final String	BUNDLE_SYMBOLICNAME	= "bundle.symbolicName";
22
	public static final String	EVENT				= "event";
23
	public static final String	EXCEPTION			= "exception";
24
	public static final String	EXCEPTION_CLASS		= "exception.class";
25
	public static final String	EXCEPTION_MESSAGE	= "exception.message";
26
	public static final String	MESSAGE				= "message";
27
	public static final String	SERVICE				= "service";
28
	public static final String	SERVICE_ID			= "service.id";
29
	public static final String	SERVICE_OBJECTCLASS	= "service.objectClass";
30
	public static final String	SERVICE_PID			= "service.pid";
31
	public static final String	TIMESTAMP			= "timestamp";
32
	public static final char	TOPIC_SEPARATOR		= '/';
33
}
(-)src/org/eclipse/equinox/event/mapper/UserAdminEventAdapter.java (-79 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.event.mapper;
13
14
import java.util.Hashtable;
15
import org.osgi.framework.ServiceReference;
16
import org.osgi.service.event.Event;
17
import org.osgi.service.event.EventAdmin;
18
import org.osgi.service.useradmin.Role;
19
import org.osgi.service.useradmin.UserAdminEvent;
20
21
/**
22
 * @version $Revision: 1.2 $
23
 */
24
public class UserAdminEventAdapter extends EventAdapter {
25
	// constants for Event topic substring
26
	public static final String TOPIC = "org/osgi/service/useradmin/UserAdminEvent";
27
	public static final String ROLE_CREATED = "ROLE_CREATED";
28
	public static final String ROLE_CHANGED = "ROLE_CHANGED";
29
	public static final String ROLE_REMOVED = "ROLE_REMOVED";
30
	// constants for Event properties
31
	public static final String ROLE = "role";
32
	public static final String ROLE_NAME = "role.name";
33
	public static final String ROLE_TYPE = "role.type";
34
	private UserAdminEvent event;
35
36
	public UserAdminEventAdapter(UserAdminEvent event, EventAdmin eventAdmin) {
37
		super(eventAdmin);
38
		this.event = event;
39
	}
40
41
	/**
42
	 * @see org.eclipse.equinox.event.mapper.EventAdapter#convert()
43
	 */
44
	public Event convert() {
45
		String typename = null;
46
		switch (event.getType()) {
47
			case UserAdminEvent.ROLE_CREATED :
48
				typename = ROLE_CREATED;
49
				break;
50
			case UserAdminEvent.ROLE_CHANGED :
51
				typename = ROLE_CHANGED;
52
				break;
53
			case UserAdminEvent.ROLE_REMOVED :
54
				typename = ROLE_REMOVED;
55
				break;
56
			default :
57
				return null;
58
		}
59
		String topic = TOPIC + Constants.TOPIC_SEPARATOR + typename;
60
		Hashtable properties = new Hashtable();
61
		ServiceReference ref = event.getServiceReference();
62
		if (ref == null) {
63
			throw new RuntimeException("UserAdminEvent's getServiceReference() returns null.");
64
		}
65
		putServiceReferenceProperties(properties, ref);
66
		Role role = event.getRole();
67
		if (role == null) {
68
			throw new RuntimeException("UserAdminEvent's getRole() returns null.");
69
		}
70
		if (role != null) {
71
			properties.put(ROLE, role);
72
			properties.put(ROLE_NAME, role.getName());
73
			properties.put(ROLE_TYPE, new Integer(role.getType()));
74
		}
75
		properties.put(Constants.EVENT, event);
76
		Event converted = new Event(topic, properties);
77
		return converted;
78
	}
79
}
(-)src/org/eclipse/equinox/event/mapper/LogReaderServiceTracker.java (-53 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005 IBM Corporation.
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.equinox.event.mapper;
13
14
import org.osgi.framework.BundleContext;
15
import org.osgi.framework.ServiceReference;
16
import org.osgi.service.log.LogReaderService;
17
import org.osgi.util.tracker.ServiceTracker;
18
19
/**
20
 * @version $Revision: 1.1 $
21
 */
22
public class LogReaderServiceTracker extends ServiceTracker {
23
	private final LogReaderServiceListener	listener;
24
	private ServiceReference				reference;
25
26
	public LogReaderServiceTracker(BundleContext context,
27
			LogReaderServiceListener listener) {
28
		super(context, LogReaderService.class.getName(), null);
29
		this.listener = listener;
30
	}
31
32
	public Object addingService(ServiceReference reference) {
33
		Object object = super.addingService(reference);
34
		if ((object != null) && (this.reference == null)
35
				&& (object instanceof LogReaderService)) {
36
			this.reference = reference;
37
			listener.logReaderServiceAdding(reference,
38
					(LogReaderService) object);
39
		}
40
		return object;
41
	}
42
43
	public void removedService(ServiceReference reference, Object service) {
44
		if ((service != null) && (this.reference.equals(reference))
45
				&& (service instanceof LogReaderService)) {
46
			listener.logReaderServiceRemoved(reference,
47
					(LogReaderService) service);
48
			this.reference = null;
49
		}
50
		super.removedService(reference, service);
51
		//this method calls ungetService()
52
	}
53
}
(-)src/org/eclipse/equinox/event/mapper/EventRedeliverer.java (-307 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005 IBM Corporation.
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.equinox.event.mapper;
13
14
import java.util.Dictionary;
15
import java.util.Hashtable;
16
import org.osgi.framework.*;
17
import org.osgi.service.cm.ConfigurationEvent;
18
import org.osgi.service.cm.ConfigurationListener;
19
import org.osgi.service.event.EventAdmin;
20
import org.osgi.service.log.LogEntry;
21
import org.osgi.service.log.LogListener;
22
import org.osgi.service.log.LogReaderService;
23
import org.osgi.service.upnp.UPnPDevice;
24
import org.osgi.service.upnp.UPnPEventListener;
25
import org.osgi.service.upnp.UPnPService;
26
import org.osgi.service.useradmin.UserAdminEvent;
27
import org.osgi.service.useradmin.UserAdminListener;
28
import org.osgi.service.wireadmin.WireAdminEvent;
29
import org.osgi.service.wireadmin.WireAdminListener;
30
import org.osgi.service.wireadmin.WireConstants;
31
import org.osgi.util.tracker.ServiceTracker;
32
33
/**
34
 * Main class for redeliver special events like FrameworkEvents via EventAdmin.
35
 * 
36
 * 
37
 * @version $Revision: 1.1 $
38
 */
39
public class EventRedeliverer implements FrameworkListener, BundleListener,
40
		ServiceListener, LogListener, LogReaderServiceListener,
41
		ConfigurationListener, WireAdminListener, UPnPEventListener,
42
		UserAdminListener {
43
	private ServiceTracker			eventAdminTracker;
44
	private LogReaderServiceTracker	logTracker;
45
	private LogReaderService		reader;
46
	private final static boolean	DEBUG	= false;
47
	private BundleContext			bc;
48
	private ServiceRegistration		configurationListenerReg;
49
	private ServiceRegistration		wireAdminListenerReg;
50
	private ServiceRegistration		upnpEventListenerReg;
51
	private ServiceRegistration		userAdminListenerReg;
52
53
	public EventRedeliverer(BundleContext bc) {
54
		this.bc = bc;
55
	}
56
57
	public void close() {
58
		if (logTracker != null) {
59
			logTracker.close();
60
			logTracker = null;
61
		}
62
		if (eventAdminTracker != null) {
63
			eventAdminTracker.close();
64
			eventAdminTracker = null;
65
		}
66
		if (reader != null) {
67
			reader.removeLogListener(this);
68
		}
69
		reader = null;
70
		bc.removeFrameworkListener(this);
71
		bc.removeBundleListener(this);
72
		bc.removeServiceListener(this);
73
		if (configurationListenerReg != null) {
74
			configurationListenerReg.unregister();
75
			configurationListenerReg = null;
76
		}
77
		if (wireAdminListenerReg != null) {
78
			wireAdminListenerReg.unregister();
79
			wireAdminListenerReg = null;
80
		}
81
		if (upnpEventListenerReg != null) {
82
			upnpEventListenerReg.unregister();
83
			upnpEventListenerReg = null;
84
		}
85
		if (userAdminListenerReg != null) {
86
			userAdminListenerReg.unregister();
87
			userAdminListenerReg = null;
88
		}
89
	}
90
91
	/**
92
	 * prepare any service trackers and register event listeners which are
93
	 * necessary to obtain events to be mapped
94
	 */
95
	public void open() {
96
		// open ServiceTracker for EventAdmin
97
		eventAdminTracker = new ServiceTracker(bc, EventAdmin.class.getName(),
98
				null);
99
		eventAdminTracker.open();
100
		// open ServiceTracker for LogReaderService
101
		logTracker = new LogReaderServiceTracker(bc, this);
102
		logTracker.open();
103
		// add legacy event listener for framework level event
104
		bc.addFrameworkListener(this);
105
		bc.addBundleListener(this);
106
		bc.addServiceListener(this);
107
		// register configurationListener
108
		configurationListenerReg = bc.registerService(
109
				ConfigurationListener.class.getName(), this, null);
110
		// register WireAdminListener
111
		Hashtable ht = new Hashtable();
112
		// create an event mask to receive all the types of WireAdminEvent
113
		Integer mask = new Integer(WireAdminEvent.WIRE_CONNECTED
114
				| WireAdminEvent.WIRE_CREATED | WireAdminEvent.WIRE_DELETED
115
				| WireAdminEvent.WIRE_DISCONNECTED | WireAdminEvent.WIRE_TRACE
116
				| WireAdminEvent.WIRE_UPDATED
117
				| WireAdminEvent.CONSUMER_EXCEPTION
118
				| WireAdminEvent.PRODUCER_EXCEPTION);
119
		ht.put(WireConstants.WIREADMIN_EVENTS, mask);
120
		wireAdminListenerReg = bc.registerService(WireAdminListener.class
121
				.getName(), this, ht);
122
		// register UPnPEventListener
123
		// create a Filter object to receive all the UPnP events
124
		Hashtable ht2 = new Hashtable();
125
		Filter filter = null;
126
		try {
127
			filter = bc.createFilter("(|(|(" + UPnPDevice.TYPE + "=*)("
128
					+ UPnPDevice.ID + "=*))(|(" + UPnPService.TYPE + "=*)("
129
					+ UPnPService.ID + "=*)))");
130
		}
131
		catch (InvalidSyntaxException e) {
132
			System.out
133
					.println("Exception thrown while trying to create Filter. "
134
							+ e);
135
			e.printStackTrace(System.out);
136
		}
137
		if (filter != null) {
138
			ht2.put(UPnPEventListener.UPNP_FILTER, filter);
139
			upnpEventListenerReg = bc.registerService(UPnPEventListener.class
140
					.getName(), this, ht2);
141
		}
142
		// register usrAdminListener
143
		userAdminListenerReg = bc.registerService(UserAdminListener.class
144
				.getName(), this, null);
145
	}
146
147
	private EventAdmin getEventAdmin() {
148
		if (eventAdminTracker == null)
149
			return null;
150
		return (EventAdmin) eventAdminTracker.getService();
151
	}
152
153
	/**
154
	 * @param event
155
	 * @see org.osgi.framework.FrameworkListener#frameworkEvent(org.osgi.framework.FrameworkEvent)
156
	 */
157
	public void frameworkEvent(FrameworkEvent event) {
158
		EventAdmin eventAdmin = getEventAdmin();
159
		if (eventAdmin != null) {
160
			(new FrameworkEventAdapter(event, eventAdmin)).redeliver();
161
		}
162
		else {
163
			printNoEventAdminError();
164
		}
165
	}
166
167
	private void printNoEventAdminError() {
168
		if (DEBUG) {
169
			System.out.println(this.getClass().getName()
170
					+ ": Cannot find the EventAdmin.");
171
		}
172
	}
173
174
	/**
175
	 * @param event
176
	 * @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent)
177
	 */
178
	public void bundleChanged(BundleEvent event) {
179
		EventAdmin eventAdmin = getEventAdmin();
180
		if (eventAdmin != null) {
181
			(new BundleEventAdapter(event, eventAdmin)).redeliver();
182
		}
183
		else {
184
			printNoEventAdminError();
185
		}
186
	}
187
188
	/**
189
	 * @param event
190
	 * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
191
	 */
192
	public void serviceChanged(ServiceEvent event) {
193
		EventAdmin eventAdmin = getEventAdmin();
194
		if (eventAdmin != null) {
195
			(new ServiceEventAdapter(event, eventAdmin)).redeliver();
196
		}
197
		else {
198
			printNoEventAdminError();
199
		}
200
	}
201
202
	/**
203
	 * @param entry
204
	 * @see org.osgi.service.log.LogListener#logged(org.osgi.service.log.LogEntry)
205
	 */
206
	public void logged(LogEntry entry) {
207
		EventAdmin eventAdmin = getEventAdmin();
208
		if (eventAdmin != null) {
209
			(new LogEntryAdapter(entry, eventAdmin)).redeliver();
210
		}
211
		else {
212
			printNoEventAdminError();
213
		}
214
	}
215
216
	/**
217
	 * @param reference
218
	 * @param service
219
	 * @see org.eclipse.equinox.event.mapper.LogReaderServiceListener#logReaderServiceAdding(org.osgi.framework.ServiceReference,
220
	 *      org.osgi.service.log.LogReaderService)
221
	 */
222
	public void logReaderServiceAdding(ServiceReference reference,
223
			LogReaderService service) {
224
		if (reader != null) {
225
			return;
226
		}
227
		reader = service;
228
		reader.addLogListener(this);
229
	}
230
231
	/**
232
	 * @param reference
233
	 * @param service
234
	 * @see org.eclipse.equinox.event.mapper.LogReaderServiceListener#logReaderServiceRemoved(org.osgi.framework.ServiceReference,
235
	 *      org.osgi.service.log.LogReaderService)
236
	 */
237
	public void logReaderServiceRemoved(ServiceReference reference,
238
			LogReaderService service) {
239
		if ((reader != null) && reader.equals(service)) {
240
			reader.removeLogListener(this);
241
			reader = null;
242
		}
243
		// ungetService() will be called after returning to
244
		// LogReaderServiceTracker's removedService() method.
245
	}
246
247
	/**
248
	 * @param event
249
	 * @see org.osgi.service.cm.ConfigurationListener#configurationEvent(org.osgi.service.cm.ConfigurationEvent)
250
	 */
251
	public void configurationEvent(ConfigurationEvent event) {
252
		EventAdmin eventAdmin = getEventAdmin();
253
		if (eventAdmin != null) {
254
			(new ConfigurationEventAdapter(event, eventAdmin)).redeliver();
255
		}
256
		else {
257
			printNoEventAdminError();
258
		}
259
	}
260
261
	/**
262
	 * @param event
263
	 * @see org.osgi.service.wireadmin.WireAdminListener#wireAdminEvent(org.osgi.service.wireadmin.WireAdminEvent)
264
	 */
265
	public void wireAdminEvent(WireAdminEvent event) {
266
		EventAdmin eventAdmin = getEventAdmin();
267
		if (eventAdmin != null) {
268
			(new WireAdminEventAdapter(event, eventAdmin)).redeliver();
269
		}
270
		else {
271
			printNoEventAdminError();
272
		}
273
	}
274
275
	/**
276
	 * @param deviceId
277
	 * @param serviceId
278
	 * @param events
279
	 * @see org.osgi.service.upnp.UPnPEventListener#notifyUPnPEvent(java.lang.String,
280
	 *      java.lang.String, java.util.Dictionary)
281
	 */
282
	public void notifyUPnPEvent(String deviceId, String serviceId,
283
			Dictionary events) {
284
		EventAdmin eventAdmin = getEventAdmin();
285
		if (eventAdmin != null) {
286
			(new UPnPEventAdapter(deviceId, serviceId, events, eventAdmin))
287
					.redeliver();
288
		}
289
		else {
290
			printNoEventAdminError();
291
		}
292
	}
293
294
	/**
295
	 * @param event
296
	 * @see org.osgi.service.useradmin.UserAdminListener#roleChanged(org.osgi.service.useradmin.UserAdminEvent)
297
	 */
298
	public void roleChanged(UserAdminEvent event) {
299
		EventAdmin eventAdmin = getEventAdmin();
300
		if (eventAdmin != null) {
301
			(new UserAdminEventAdapter(event, eventAdmin)).redeliver();
302
		}
303
		else {
304
			printNoEventAdminError();
305
		}
306
	}
307
}
(-)src/org/eclipse/equinox/event/mapper/UPnPEventAdapter.java (-57 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.event.mapper;
13
14
import java.util.Dictionary;
15
import java.util.Hashtable;
16
import org.osgi.service.event.Event;
17
import org.osgi.service.event.EventAdmin;
18
19
/**
20
 * @version $Revision: 1.2 $
21
 */
22
public class UPnPEventAdapter extends EventAdapter {
23
	// constants for Event topic substring
24
	public static final String TOPIC = "org/osgi/service/upnp/UPnPEvent";
25
	// constants for Event properties
26
	public static final String UPNP_DEVICEID = "upnp.deviceId";
27
	public static final String UPNP_SERVICEID = "upnp.serviceId";
28
	public static final String UPNP_EVENTS = "upnp.events";
29
	private String deviceId;
30
	private String serviceId;
31
	private Dictionary events;
32
33
	public UPnPEventAdapter(String deviceId, String serviceId, Dictionary events, EventAdmin eventAdmin) {
34
		super(eventAdmin);
35
		this.deviceId = deviceId;
36
		this.serviceId = serviceId;
37
		this.events = events;
38
	}
39
40
	/**
41
	 * @see org.eclipse.equinox.event.mapper.EventAdapter#convert()
42
	 */
43
	public Event convert() {
44
		Hashtable properties = new Hashtable();
45
		if (deviceId != null) {
46
			properties.put(UPNP_DEVICEID, deviceId);
47
		}
48
		if (serviceId != null) {
49
			properties.put(UPNP_SERVICEID, serviceId);
50
		}
51
		if (events != null) {
52
			properties.put(UPNP_EVENTS, events);
53
		}
54
		Event converted = new Event(TOPIC, properties);
55
		return converted;
56
	}
57
}
(-)src/org/eclipse/equinox/event/mapper/ConfigurationEventAdapter.java (-68 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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
package org.eclipse.equinox.event.mapper;
12
13
import java.util.Hashtable;
14
import org.osgi.framework.ServiceReference;
15
import org.osgi.service.cm.ConfigurationEvent;
16
import org.osgi.service.event.Event;
17
import org.osgi.service.event.EventAdmin;
18
19
/**
20
 * @version $Revision: 1.2 $
21
 */
22
public class ConfigurationEventAdapter extends EventAdapter {
23
	// constants for Event topic substring
24
	public static final String HEADER = "org/osgi/service/cm/ConfigurationEvent";
25
	public static final String CM_UPDATED = "CM_UPDATED";
26
	public static final String CM_DELETED = "CM_DELETED";
27
	// constants for Event properties
28
	public static final String CM_FACTORY_PID = "cm.factoryPid";
29
	public static final String CM_PID = "cm.pid";
30
	private ConfigurationEvent event;
31
32
	public ConfigurationEventAdapter(ConfigurationEvent event, EventAdmin eventAdmin) {
33
		super(eventAdmin);
34
		this.event = event;
35
	}
36
37
	/**
38
	 * @see org.eclipse.equinox.event.mapper.EventAdapter#convert()
39
	 */
40
	public Event convert() {
41
		String typename = null;
42
		switch (event.getType()) {
43
			case ConfigurationEvent.CM_UPDATED :
44
				typename = CM_UPDATED;
45
				break;
46
			case ConfigurationEvent.CM_DELETED :
47
				typename = CM_DELETED;
48
				break;
49
			default :
50
				return null;
51
		}
52
		String topic = HEADER + Constants.TOPIC_SEPARATOR + typename;
53
		Hashtable properties = new Hashtable();
54
		ServiceReference ref = event.getReference();
55
		if (ref == null) {
56
			throw new RuntimeException("ServiceEvent.getServiceReference() is null");
57
		}
58
		properties.put(CM_PID, event.getPid());
59
		if (event.getFactoryPid() != null) {
60
			properties.put(CM_FACTORY_PID, event.getFactoryPid());
61
		}
62
		putServiceReferenceProperties(properties, ref);
63
		// assert objectClass includes
64
		// "org.osgi.service.cm.ConfigurationAdmin"
65
		Event converted = new Event(topic, properties);
66
		return converted;
67
	}
68
}
(-)src/org/eclipse/equinox/event/mapper/LogEntryAdapter.java (-78 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.event.mapper;
13
14
import java.util.Hashtable;
15
import org.osgi.framework.Bundle;
16
import org.osgi.framework.ServiceReference;
17
import org.osgi.service.event.Event;
18
import org.osgi.service.event.EventAdmin;
19
import org.osgi.service.log.LogEntry;
20
import org.osgi.service.log.LogService;
21
22
/**
23
 * @version $Revision: 1.2 $
24
 */
25
public class LogEntryAdapter extends EventAdapter {
26
	// constants for Event topic substring
27
	public static final String TOPIC = "org/osgi/service/log/LogEntry";
28
	// constants for Event properties
29
	public static final String TIMESTAMP = "timestamp";
30
	public static final String MESSAGE = "message";
31
	public static final String LOG_LEVEL = "log.level";
32
	public static final String LOG_ENTRY = "log.entry";
33
	private LogEntry entry;
34
35
	public LogEntryAdapter(LogEntry entry, EventAdmin eventAdmin) {
36
		super(eventAdmin);
37
		this.entry = entry;
38
	}
39
40
	/**
41
	 * @see org.eclipse.equinox.event.mapper.EventAdapter#convert()
42
	 */
43
	public Event convert() {
44
		String topic = TOPIC;
45
		int level = entry.getLevel();
46
		switch (level) {
47
			case LogService.LOG_ERROR :
48
			case LogService.LOG_WARNING :
49
			case LogService.LOG_INFO :
50
			case LogService.LOG_DEBUG :
51
				break;
52
			default :
53
				// other log levels are represented by their decimal value
54
				topic += Constants.TOPIC_SEPARATOR + level;
55
		}
56
		Hashtable properties = new Hashtable();
57
		Bundle bundle = entry.getBundle();
58
		if (bundle == null) {
59
			throw new RuntimeException("LogEntry.getBundle() returns null");
60
		}
61
		putBundleProperties(properties, bundle);
62
		Throwable t = entry.getException();
63
		if (t != null) {
64
			putExceptionProperties(properties, t);
65
		}
66
		ServiceReference ref = entry.getServiceReference();
67
		if (ref != null) {
68
			putServiceReferenceProperties(properties, ref);
69
		}
70
		properties.put(LOG_ENTRY, entry);
71
		properties.put(LOG_LEVEL, new Integer(entry.getLevel()));
72
		if (entry.getMessage() != null)
73
			properties.put(MESSAGE, entry.getMessage());
74
		properties.put(TIMESTAMP, new Long(entry.getTime()));
75
		Event converted = new Event(topic, properties);
76
		return converted;
77
	}
78
}
(-)src/org/eclipse/equinox/event/mapper/BundleEventAdapter.java (-83 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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
package org.eclipse.equinox.event.mapper;
12
13
import java.util.Hashtable;
14
import org.osgi.framework.Bundle;
15
import org.osgi.framework.BundleEvent;
16
import org.osgi.service.event.Event;
17
import org.osgi.service.event.EventAdmin;
18
19
/**
20
 * @version $Revision: 1.2 $
21
 */
22
public class BundleEventAdapter extends EventAdapter {
23
	// constants for Event topic substring
24
	public static final String HEADER = "org/osgi/framework/BundleEvent";
25
	public static final String INSTALLED = "INSTALLED";
26
	public static final String STOPPED = "STOPPED";
27
	public static final String STARTED = "STARTED";
28
	public static final String UPDATED = "UPDATED";
29
	public static final String UNINSTALLED = "UNINSTALLED";
30
	public static final String RESOLVED = "RESOLVED";
31
	public static final String UNRESOLVED = "UNRESOLVED";
32
	private BundleEvent event;
33
34
	public BundleEventAdapter(BundleEvent event, EventAdmin eventAdmin) {
35
		super(eventAdmin);
36
		this.event = event;
37
	}
38
39
	/**
40
	 * @return event
41
	 * @see org.eclipse.equinox.event.mapper.EventAdapter#convert()
42
	 */
43
	public Event convert() {
44
		String typename = null;
45
		switch (event.getType()) {
46
			case BundleEvent.INSTALLED :
47
				typename = INSTALLED;
48
				break;
49
			case BundleEvent.STOPPED :
50
				typename = STOPPED;
51
				break;
52
			case BundleEvent.STARTED :
53
				typename = STARTED;
54
				break;
55
			case BundleEvent.UPDATED :
56
				typename = UPDATED;
57
				break;
58
			case BundleEvent.UNINSTALLED :
59
				typename = UNINSTALLED;
60
				break;
61
			case BundleEvent.RESOLVED :
62
				typename = RESOLVED;
63
				break;
64
			case BundleEvent.UNRESOLVED :
65
				typename = UNRESOLVED;
66
				break;
67
			default :
68
				// unknown events must be send as their decimal value
69
				typename = "" + event.getType();
70
		}
71
		String topic = HEADER + Constants.TOPIC_SEPARATOR + typename;
72
		Hashtable properties = new Hashtable();
73
		Bundle bundle = event.getBundle();
74
		if (bundle == null) {
75
			throw new RuntimeException("BundleEvent.getBundle() returns null");
76
		} else {
77
			putBundleProperties(properties, bundle);
78
		}
79
		properties.put(Constants.EVENT, event);
80
		Event converted = new Event(topic, properties);
81
		return converted;
82
	}
83
}
(-)src/org/eclipse/equinox/event/EventAdminImpl.java (-156 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.event;
13
14
import java.security.Permission;
15
import java.util.Iterator;
16
import java.util.Set;
17
import org.eclipse.osgi.framework.eventmgr.*;
18
import org.eclipse.osgi.util.NLS;
19
import org.osgi.framework.BundleContext;
20
import org.osgi.service.event.*;
21
import org.osgi.service.log.LogService;
22
23
/**
24
 * Implementation of org.osgi.service.event.EventAdmin. EventAdminImpl uses
25
 * org.eclipse.osgi.framework.eventmgr.EventManager. It is assumeed
26
 * org.eclipse.osgi.framework.eventmgr package is exported by some other bundle.
27
 */
28
public class EventAdminImpl implements EventAdmin {
29
	private final LogTracker log;
30
	private final EventHandlerTracker handlers;
31
	private volatile EventManager		eventManager;
32
33
	/**
34
	 * Constructor for EventAdminImpl.
35
	 * 
36
	 * @param context BundleContext
37
	 */
38
	EventAdminImpl(BundleContext context) {
39
		super();
40
		log = new LogTracker(context, System.out);
41
		handlers = new EventHandlerTracker(context, log);
42
	}
43
	
44
	/**
45
	 * This method should be called before registering EventAdmin service
46
	 */
47
	void start() {
48
		log.open();
49
		ThreadGroup eventGroup = new ThreadGroup("Equinox Event Admin"); //$NON-NLS-1$
50
		eventGroup.setDaemon(true);
51
		eventManager = new EventManager(EventAdminMsg.EVENT_ASYNC_THREAD_NAME, eventGroup);
52
		handlers.open();
53
	}
54
55
	/**
56
	 * This method should be called after unregistering EventAdmin service
57
	 */
58
	void stop() {
59
		handlers.close();
60
		eventManager.close();
61
		eventManager = null;	// signify we have stopped
62
		log.close();
63
	}
64
65
	/**
66
	 * @param event
67
	 * @see org.osgi.service.event.EventAdmin#postEvent(org.osgi.service.event.Event)
68
	 */
69
	public void postEvent(Event event) {
70
		dispatchEvent(event, true);
71
	}
72
73
	/**
74
	 * @param event
75
	 * @see org.osgi.service.event.EventAdmin#sendEvent(org.osgi.service.event.Event)
76
	 */
77
	public void sendEvent(Event event) {
78
		dispatchEvent(event, false);
79
	}
80
81
	/**
82
	 * Internal main method for sendEvent() and postEvent(). Dispatching an
83
	 * event to EventHandler. All exceptions are logged except when dealing with
84
	 * LogEntry.
85
	 * 
86
	 * @param event to be delivered
87
	 * @param isAsync must be set to true for syncronous event delivery, false
88
	 *        for asyncronous delivery.
89
	 */
90
	private void dispatchEvent(Event event, boolean isAsync) {
91
		// keep a local copy in case we are stopped in the middle of dispatching
92
		EventManager currentManager = eventManager;
93
		if (currentManager == null) {
94
			// EventAdmin is stopped
95
			return;
96
		}
97
		if (event == null) {
98
			log.log(LogService.LOG_ERROR, EventAdminMsg.EVENT_NULL_EVENT);
99
			// continue from here will result in an NPE below; the spec for EventAdmin does not allow for null here
100
		}
101
		
102
		String topic = event.getTopic();
103
		
104
		try {
105
			checkTopicPermissionPublish(topic);
106
		} catch (SecurityException e) {
107
			String msg = NLS.bind(EventAdminMsg.EVENT_NO_TOPICPERMISSION_PUBLISH, event.getTopic());
108
			log.log(LogService.LOG_ERROR, msg);
109
			// must throw a security exception here according to the EventAdmin spec
110
			throw e;
111
		}
112
		
113
		Set eventHandlers = handlers.getHandlers(topic);
114
		// If there are no handlers, then we are done
115
		if (eventHandlers.size() == 0) {
116
			return;
117
		}
118
		
119
		SecurityManager sm = System.getSecurityManager();
120
		Permission perm = (sm == null) ? null : new TopicPermission(topic, TopicPermission.SUBSCRIBE);
121
		
122
		EventListeners listeners = new EventListeners();
123
		Iterator iter = eventHandlers.iterator();
124
		while (iter.hasNext()) {
125
			EventHandlerWrapper wrapper = (EventHandlerWrapper) iter.next();
126
			listeners.addListener(wrapper, perm);
127
		}
128
		
129
		// Create the listener queue for this event delivery
130
		ListenerQueue listenerQueue = new ListenerQueue(currentManager);
131
		// Add the listeners to the queue and associate them with the event
132
		// dispatcher
133
		listenerQueue.queueListeners(listeners, handlers);
134
		// Deliver the event to the listeners.
135
		if (isAsync) {
136
			listenerQueue.dispatchEventAsynchronous(0, event);
137
		}
138
		else {
139
			listenerQueue.dispatchEventSynchronous(0, event);
140
		}
141
	}
142
143
	/**
144
	 * Checks if the caller bundle has right PUBLISH TopicPermision.
145
	 * 
146
	 * @param topic
147
	 * @throws SecurityException if the caller does not have the right to PUBLISH TopicPermission
148
	 */
149
	private void checkTopicPermissionPublish(String topic) throws SecurityException{
150
		SecurityManager sm = System.getSecurityManager();
151
		if (sm == null)
152
			return;
153
		sm.checkPermission(new TopicPermission(topic, TopicPermission.PUBLISH));
154
	}
155
156
}
(-)src/org/eclipse/equinox/event/EventHandlerTracker.java (-200 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 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.equinox.event;
13
14
import java.security.Permission;
15
import java.util.*;
16
import org.eclipse.osgi.framework.eventmgr.EventDispatcher;
17
import org.osgi.framework.BundleContext;
18
import org.osgi.framework.ServiceReference;
19
import org.osgi.service.event.Event;
20
import org.osgi.service.event.EventHandler;
21
import org.osgi.service.log.LogService;
22
import org.osgi.util.tracker.ServiceTracker;
23
24
public class EventHandlerTracker extends ServiceTracker implements EventDispatcher {
25
	private final LogService log;
26
	//* List<EventHandlerWrapper> of all handlers with topic of "*"
27
	private final List globalWildcard;
28
	// Map<String,List<EventHandlerWrapper>> key is topic prefix of partial wildcard
29
	private final Map partialWildcard;
30
	// Map<String,List<EventHandlerWrapper>> key is topic name
31
	private final Map topicName;
32
33
	public EventHandlerTracker(BundleContext context, LogService log) {
34
		super(context, EventHandler.class.getName(), null);
35
		this.log = log;
36
		globalWildcard = new ArrayList();
37
		partialWildcard = new HashMap();
38
		topicName = new HashMap();
39
	}
40
41
	public Object addingService(ServiceReference reference) {
42
		EventHandlerWrapper wrapper = new EventHandlerWrapper(reference, context, log);
43
		synchronized (this) {
44
			if (wrapper.init()) {
45
				bucket(wrapper);
46
			}
47
		}
48
		return wrapper;
49
	}
50
51
	public void modifiedService(ServiceReference reference, Object service) {
52
		EventHandlerWrapper wrapper = (EventHandlerWrapper) service;
53
		synchronized (this) {
54
			unbucket(wrapper);
55
			if (wrapper.init()) {
56
				bucket(wrapper);
57
				return;
58
			}
59
		}
60
61
		wrapper.flush(); // needs to be called outside sync region
62
	}
63
64
	public void removedService(ServiceReference reference, Object service) {
65
		EventHandlerWrapper wrapper = (EventHandlerWrapper) service;
66
		synchronized (this) {
67
			unbucket(wrapper);
68
		}
69
		wrapper.flush(); // needs to be called outside sync region
70
	}
71
72
	/**
73
	 * Place the wrapper into the appropriate buckets.
74
	 * This is a performance optimization for event delivery.
75
	 * 
76
	 * @param wrapper The wrapper to place in buckets.
77
	 * @GuardedBy this
78
	 */
79
	private void bucket(EventHandlerWrapper wrapper) {
80
		final String[] topics = wrapper.getTopics();
81
		final int length = (topics == null) ? 0 : topics.length;
82
		for (int i = 0; i < length; i++) {
83
			String topic = topics[i];
84
			// global wildcard
85
			if (topic.equals("*")) { //$NON-NLS-1$
86
				globalWildcard.add(wrapper);
87
			}
88
			// partial wildcard
89
			else if (topic.endsWith("/*")) { //$NON-NLS-1$
90
				String key = topic.substring(0, topic.length() - 2); // Strip off "/*" from the end
91
				List wrappers = (List) partialWildcard.get(key);
92
				if (wrappers == null) {
93
					wrappers = new ArrayList();
94
					partialWildcard.put(key, wrappers);
95
				}
96
				wrappers.add(wrapper);
97
			}
98
			// simple topic name
99
			else {
100
				List wrappers = (List) topicName.get(topic);
101
				if (wrappers == null) {
102
					wrappers = new ArrayList();
103
					topicName.put(topic, wrappers);
104
				}
105
				wrappers.add(wrapper);
106
			}
107
		}
108
	}
109
110
	/**
111
	 * Remove the wrapper from the buckets.
112
	 * 
113
	 * @param wrapper The wrapper to remove from the buckets.
114
	 * @GuardedBy this
115
	 */
116
	private void unbucket(EventHandlerWrapper wrapper) {
117
		final String[] topics = wrapper.getTopics();
118
		final int length = (topics == null) ? 0 : topics.length;
119
		for (int i = 0; i < length; i++) {
120
			String topic = topics[i];
121
			// global wilcard
122
			if (topic.equals("*")) { //$NON-NLS-1$
123
				globalWildcard.remove(wrapper);
124
			}
125
			// partial wildcard
126
			else if (topic.endsWith("/*")) { //$NON-NLS-1$
127
				String key = topic.substring(0, topic.length() - 2); // Strip off "/*" from the end
128
				List wrappers = (List) partialWildcard.get(key);
129
				if (wrappers != null) {
130
					wrappers.remove(wrapper);
131
					if (wrappers.size() == 0) {
132
						partialWildcard.remove(key);
133
					}
134
				}
135
			}
136
			// simple topic name
137
			else {
138
				List wrappers = (List) topicName.get(topic);
139
				if (wrappers != null) {
140
					wrappers.remove(wrapper);
141
					if (wrappers.size() == 0) {
142
						topicName.remove(topic);
143
					}
144
				}
145
			}
146
		}
147
	}
148
149
	/**
150
	 * Return the set of handlers which subscribe to the event topic.
151
	 * A set is used to ensure a handler is not called for an event more than once.
152
	 * 
153
	 * @param topic
154
	 * @return a set of handlers
155
	 */
156
	public synchronized Set getHandlers(final String topic) {
157
		// Use a set to remove duplicates
158
		Set handlers = new HashSet();
159
160
		// Add the "*" handlers
161
		handlers.addAll(globalWildcard);
162
163
		// Add the handlers with partial matches
164
		if (partialWildcard.size() > 0) {
165
			int index = topic.length();
166
			while (index >= 0) {
167
				String subTopic = topic.substring(0, index); // First subtopic is the complete topic.
168
				List wrappers = (List) partialWildcard.get(subTopic);
169
				if (wrappers != null) {
170
					handlers.addAll(wrappers);
171
				}
172
				// Strip the last level from the topic. For example, org/osgi/framework becomes org/osgi.
173
				// Wildcard topics are inserted into the map with the "/*" stripped off.
174
				index = subTopic.lastIndexOf('/');
175
			}
176
		}
177
178
		// Add the handlers for matching topic names
179
		List wrappers = (List) topicName.get(topic);
180
		if (wrappers != null) {
181
			handlers.addAll(wrappers);
182
		}
183
184
		return handlers;
185
	}
186
187
	/**
188
	 * Dispatches Event to EventHandlers
189
	 * 
190
	 * @param eventListener
191
	 * @param listenerObject
192
	 * @param eventAction
193
	 * @param eventObject
194
	 * @see org.eclipse.osgi.framework.eventmgr.EventDispatcher#dispatchEvent(java.lang.Object,
195
	 *      java.lang.Object, int, java.lang.Object)
196
	 */
197
	public void dispatchEvent(Object eventListener, Object listenerObject, int eventAction, Object eventObject) {
198
		((EventHandlerWrapper) eventListener).handleEvent((Event) eventObject, (Permission) listenerObject);
199
	}
200
}
(-)src/org/eclipse/equinox/event/EventHandlerWrapper.java (-184 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 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.equinox.event;
13
14
import java.security.Permission;
15
import org.eclipse.osgi.util.NLS;
16
import org.osgi.framework.*;
17
import org.osgi.service.event.*;
18
import org.osgi.service.log.LogService;
19
20
/**
21
 * A wrapper for EventHandlers. This class caches property values and 
22
 * performs final checks before calling the wrapped handler.
23
 *
24
 */
25
public class EventHandlerWrapper {
26
	private final ServiceReference reference;
27
	private final LogService log;
28
	private final BundleContext context;
29
	private EventHandler handler;
30
	private String[] topics;
31
	private Filter filter;
32
	
33
	/**
34
	 * Create an EventHandlerWrapper. 
35
36
	 * @param reference Reference to the EventHandler
37
	 * @param context Bundle Context of the Event Admin bundle
38
	 * @param log LogService object for logging
39
	 */
40
	public EventHandlerWrapper(ServiceReference reference, BundleContext context, LogService log) {
41
		this.reference = reference;
42
		this.context = context;
43
		this.log = log;
44
	}
45
46
	/**
47
	 * Cache values from service properties
48
	 * 
49
	 * @return true if the handler should be called; false if the handler should not be called
50
	 */
51
	public synchronized boolean init() {
52
		topics = null;
53
		filter = null;
54
55
		// Get topic names
56
		Object o = reference.getProperty(EventConstants.EVENT_TOPIC);
57
		if (o instanceof String) {
58
			topics = new String[] {(String)o};
59
		}
60
		else if (o instanceof String[]) {
61
			topics = (String[]) o;
62
		}
63
		
64
		if (topics == null) {
65
			return false;
66
		}
67
		
68
		// get filter
69
		o = reference.getProperty(EventConstants.EVENT_FILTER);
70
		if (o instanceof String) {
71
			try {
72
				filter = context.createFilter((String)o);
73
			}
74
			catch (InvalidSyntaxException e) {
75
				log.log(LogService.LOG_ERROR,NLS.bind(EventAdminMsg.EVENT_INVALID_HANDLER_FILTER, o), e);
76
				return false;
77
			}
78
		}
79
		
80
		return true;
81
	}
82
83
	/**
84
	 * Flush the handler service if it has been obtained.
85
	 */
86
	public void flush() {
87
		synchronized (this) {
88
			if (handler == null) {
89
				return;
90
			}
91
			handler = null;
92
		}
93
		context.ungetService(reference);
94
	}
95
	
96
	/**
97
	 * Get the event topics for the wrapped handler.
98
	 * 
99
	 * @return The wrapped handler's event topics
100
	 */
101
	public synchronized String[] getTopics()  {
102
		return topics;
103
	}
104
	
105
	/**
106
	 * Return the wrapped handler. 
107
	 * @return The wrapped handler.
108
	 */
109
	private EventHandler getHandler() {
110
		synchronized (this) {
111
			// if we already have a handler, return it
112
			if (handler != null) {
113
				return handler;
114
			}
115
		}
116
		
117
		// we don't have the handler, so lets get it outside the sync region
118
		EventHandler tempHandler = (EventHandler)context.getService(reference);
119
120
		synchronized (this) {
121
			// do we still need the handler we just got?
122
			if (handler == null) {
123
				handler = tempHandler;
124
				return handler;
125
			}
126
			// get the current handler
127
			tempHandler = handler;
128
		}
129
		
130
		// unget the handler we just got since we don't need it
131
		context.ungetService(reference);
132
		
133
		// return the current handler (copied into the local var)
134
		return tempHandler;
135
	}
136
	
137
	/**
138
	 * Get the filter object
139
	 * 
140
	 * @return The handler's filter
141
	 */
142
	private synchronized Filter getFilter() {
143
		return filter;
144
	}
145
146
	/**
147
	 * Dispatch event to handler. Perform final tests before actually calling the handler.
148
	 * 
149
	 * @param event The event to dispatch
150
	 * @param perm The permission to be checked
151
	 */
152
	public void handleEvent(Event event, Permission perm) {
153
		Bundle bundle = reference.getBundle();
154
		// is service unregistered?
155
		if (bundle == null) {
156
			return;
157
		}
158
		
159
		// filter match
160
		Filter eventFilter = getFilter();
161
		if ((eventFilter != null) && !event.matches(eventFilter)) {
162
			return;
163
		}
164
		
165
		// permission check
166
		if ((perm != null) && (!bundle.hasPermission(perm))) {
167
			return;
168
		}
169
		
170
		// get handler service
171
		EventHandler handlerService = getHandler();
172
		if (handlerService == null) {
173
			return;
174
		}
175
	
176
		try {
177
			handlerService.handleEvent(event);
178
		}
179
		catch (Throwable t) {
180
			// log/handle any Throwable thrown by the listener
181
			log.log(LogService.LOG_ERROR, NLS.bind(EventAdminMsg.EVENT_DISPATCH_HANDLER_EXCEPTION, event, handlerService), t);
182
		}
183
	}
184
}
(-)src/org/eclipse/equinox/event/LogMessages.properties (-17 lines)
Removed Link Here
1
###############################################################################
2
# Copyright (c) 2005, 2006 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
# NLS_MESSAGEFORMAT_ALL 
12
13
Unknown_Log_level=Unknown Log Level
14
Info=Log Info
15
Warning=Log Warning
16
Error=Log Error
17
Debug=Log Debug
(-)src/org/eclipse/equinox/event/EventAdminMsg.java (-29 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 1999, 2007 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.equinox.event;
13
14
import org.eclipse.osgi.util.NLS;
15
16
public class EventAdminMsg extends NLS {
17
	private static final String BUNDLE_NAME = "org.eclipse.equinox.event.ExternalMessages"; //$NON-NLS-1$
18
19
	public static String EVENT_ASYNC_THREAD_NAME;
20
	public static String EVENT_NULL_EVENT;
21
	public static String EVENT_NO_TOPICPERMISSION_PUBLISH;
22
	public static String EVENT_DISPATCH_HANDLER_EXCEPTION;
23
	public static String EVENT_INVALID_HANDLER_FILTER;
24
25
	static {
26
		// initialize resource bundles
27
		NLS.initializeMessages(BUNDLE_NAME, EventAdminMsg.class);
28
	}
29
}
(-)src/org/eclipse/equinox/event/Activator.java (-36 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 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.equinox.event;
13
14
import org.eclipse.equinox.event.mapper.EventRedeliverer;
15
import org.osgi.framework.*;
16
17
public class Activator implements BundleActivator {
18
	private EventRedeliverer    eventRedeliverer;
19
	private ServiceRegistration eventAdminService;
20
	private EventAdminImpl eventAdmin;
21
	
22
	public void start(BundleContext bundleContext) {
23
		eventAdmin = new EventAdminImpl(bundleContext);
24
		eventAdmin.start();
25
		eventAdminService = bundleContext.registerService("org.osgi.service.event.EventAdmin", //$NON-NLS-1$
26
				eventAdmin,null);
27
		eventRedeliverer  = new EventRedeliverer(bundleContext);
28
		eventRedeliverer.open();
29
	}
30
	
31
	public void stop(BundleContext bundleContext) {
32
		eventRedeliverer.close();
33
		eventAdminService.unregister();
34
		eventAdmin.stop();
35
	}
36
}
(-)src/org/eclipse/equinox/event/LogTracker.java (-174 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 1998, 2007 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
package org.eclipse.equinox.event;
12
13
import java.io.PrintStream;
14
import java.util.Calendar;
15
import java.util.Date;
16
17
import org.osgi.framework.BundleContext;
18
import org.osgi.framework.ServiceReference;
19
import org.osgi.service.log.LogService;
20
import org.osgi.util.tracker.ServiceTracker;
21
22
/**
23
 * LogTracker class. This class encapsulates the LogService
24
 * and handles all issues such as the service coming and going.
25
 */
26
27
public class LogTracker extends ServiceTracker implements LogService {
28
	/** LogService interface class name */
29
	protected final static String clazz = "org.osgi.service.log.LogService"; //$NON-NLS-1$
30
31
	/** PrintStream to use if LogService is unavailable */
32
	private final PrintStream out;
33
34
	/**
35
	 * Create new LogTracker.
36
	 *
37
	 * @param context BundleContext of parent bundle.
38
	 * @param out Default PrintStream to use if LogService is unavailable.
39
	 */
40
	public LogTracker(BundleContext context, PrintStream out) {
41
		super(context, clazz, null);
42
		this.out = out;
43
	}
44
45
	/*
46
	 * ----------------------------------------------------------------------
47
	 *      LogService Interface implementation
48
	 * ----------------------------------------------------------------------
49
	 */
50
51
	public void log(int level, String message) {
52
		log(null, level, message, null);
53
	}
54
55
	public void log(int level, String message, Throwable exception) {
56
		log(null, level, message, exception);
57
	}
58
59
	public void log(ServiceReference reference, int level, String message) {
60
		log(reference, level, message, null);
61
	}
62
63
	public synchronized void log(ServiceReference reference, int level, String message, Throwable exception) {
64
		ServiceReference[] references = getServiceReferences();
65
66
		if (references != null) {
67
			int size = references.length;
68
69
			for (int i = 0; i < size; i++) {
70
				LogService service = (LogService) getService(references[i]);
71
				if (service != null) {
72
					try {
73
						service.log(reference, level, message, exception);
74
					} catch (Exception e) {
75
						// TODO: consider printing to System Error
76
					}
77
				}
78
			}
79
80
			return;
81
		}
82
83
		noLogService(level, message, exception, reference);
84
	}
85
86
	/**
87
	 * The LogService is not available so we write the message to a PrintStream.
88
	 *
89
	 * @param level Logging level
90
	 * @param message Log message.
91
	 * @param throwable Log exception or null if none.
92
	 * @param reference ServiceReference associated with message or null if none.
93
	 */
94
	protected void noLogService(int level, String message, Throwable throwable, ServiceReference reference) {
95
		if (out != null) {
96
			synchronized (out) {
97
				// Bug #113286.  If no log service present and messages are being
98
				// printed to stdout, prepend message with a timestamp.
99
				String timestamp = getDate(new Date());
100
				out.print(timestamp + " "); //$NON-NLS-1$
101
102
				switch (level) {
103
					case LOG_DEBUG : {
104
						out.print(LogTrackerMsg.Debug);
105
106
						break;
107
					}
108
					case LOG_INFO : {
109
						out.print(LogTrackerMsg.Info); 
110
111
						break;
112
					}
113
					case LOG_WARNING : {
114
						out.print(LogTrackerMsg.Warning);
115
116
						break;
117
					}
118
					case LOG_ERROR : {
119
						out.print(LogTrackerMsg.Error);
120
121
						break;
122
					}
123
					default : {
124
						out.print("["); //$NON-NLS-1$
125
						out.print(LogTrackerMsg.Unknown_Log_level);         
126
						out.print("]: "); //$NON-NLS-1$
127
128
						break;
129
					}
130
				}
131
132
				out.println(message);
133
134
				if (reference != null) {
135
					out.println(reference);
136
				}
137
138
				if (throwable != null) {
139
					throwable.printStackTrace(out);
140
				}
141
			}
142
		}
143
	}
144
	
145
	// from EclipseLog to avoid using DateFormat -- see bug 149892#c10
146
	private String getDate(Date date) {
147
			Calendar c = Calendar.getInstance();
148
			c.setTime(date);
149
			StringBuffer sb = new StringBuffer();
150
			appendPaddedInt(c.get(Calendar.YEAR), 4, sb).append('-');
151
			appendPaddedInt(c.get(Calendar.MONTH) + 1, 2, sb).append('-');
152
			appendPaddedInt(c.get(Calendar.DAY_OF_MONTH), 2, sb).append(' ');
153
			appendPaddedInt(c.get(Calendar.HOUR_OF_DAY), 2, sb).append(':');
154
			appendPaddedInt(c.get(Calendar.MINUTE), 2, sb).append(':');
155
			appendPaddedInt(c.get(Calendar.SECOND), 2, sb).append('.');
156
			appendPaddedInt(c.get(Calendar.MILLISECOND), 3, sb);
157
			return sb.toString();
158
	}
159
160
	private StringBuffer appendPaddedInt(int value, int pad, StringBuffer buffer) {
161
		pad = pad - 1;
162
		if (pad == 0)
163
			return buffer.append(Integer.toString(value));
164
		int padding = (int) Math.pow(10, pad);
165
		if (value >= padding)
166
			return buffer.append(Integer.toString(value));
167
		while (padding > value && padding > 1) {
168
			buffer.append('0');
169
			padding = padding / 10;
170
		}
171
		buffer.append(value);
172
		return buffer;
173
	}
174
}
(-)src/org/eclipse/equinox/event/LogTrackerMsg.java (-28 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 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
package org.eclipse.equinox.event;
12
13
import org.eclipse.osgi.util.NLS;
14
15
public class LogTrackerMsg extends NLS {
16
	private static final String BUNDLE_NAME = "org.eclipse.equinox.event.LogMessages"; //$NON-NLS-1$
17
18
	public static String Unknown_Log_level;
19
	public static String Info;
20
	public static String Warning;
21
	public static String Error;
22
	public static String Debug;
23
24
	static {
25
		// initialize resource bundles
26
		NLS.initializeMessages(BUNDLE_NAME, LogTrackerMsg.class);
27
	}
28
}
(-)src/org/eclipse/equinox/event/ExternalMessages.properties (-17 lines)
Removed Link Here
1
###############################################################################
2
# Copyright (c) 2007 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
# NLS_MESSAGEFORMAT_ALL 
12
13
EVENT_ASYNC_THREAD_NAME=EventAdmin Async Event Dispatcher Thread
14
EVENT_NULL_EVENT=Null event is passed to EventAdmin. Ignored.
15
EVENT_NO_TOPICPERMISSION_PUBLISH=Caller bundle doesn't have TopicPermission to publish topic {0}
16
EVENT_DISPATCH_HANDLER_EXCEPTION=Exception while dispatching event {0} to handler {1}
17
EVENT_INVALID_HANDLER_FILTER=Invalid handler filter {0}
(-)META-INF/MANIFEST.MF (-9 / +4 lines)
Lines 1-22 Link Here
1
Bundle-ManifestVersion: 2
1
Bundle-ManifestVersion: 2
2
Bundle-Name: %bundleName
2
Bundle-Name: %bundleName
3
Bundle-Version: 1.0.200.qualifier
3
Bundle-Version: 1.1.0.qualifier
4
Bundle-SymbolicName: org.eclipse.equinox.event
4
Bundle-SymbolicName: org.eclipse.equinox.event
5
Bundle-Activator: org.eclipse.equinox.event.Activator
5
Bundle-Activator: org.eclipse.equinox.internal.event.Activator
6
Bundle-Copyright: %bundleCopyright
6
Bundle-Copyright: %bundleCopyright
7
Import-Package: org.eclipse.osgi.framework.eventmgr;version="1.1.0",
7
Import-Package: org.eclipse.osgi.framework.eventmgr;version="1.1.0",
8
 org.eclipse.osgi.util,
8
 org.eclipse.osgi.util,
9
 org.osgi.framework,
9
 org.osgi.framework,
10
 org.osgi.service.cm,
11
 org.osgi.service.event,
10
 org.osgi.service.event,
12
 org.osgi.service.log,
11
 org.osgi.service.log,
13
 org.osgi.service.upnp,
14
 org.osgi.service.useradmin,
15
 org.osgi.service.wireadmin,
16
 org.osgi.util.tracker
12
 org.osgi.util.tracker
17
Export-Package: org.eclipse.equinox.event; x-internal:=true,
13
Export-Package: org.eclipse.equinox.internal.event;x-internal:=true,
18
 org.eclipse.equinox.event.mapper; x-internal:=true
14
 org.eclipse.equinox.internal.event.mapper;x-internal:=true
19
Bundle-Vendor: %bundleVendor
15
Bundle-Vendor: %bundleVendor
20
Export-Service: org.osgi.service.event.EventAdmin
21
Bundle-Localization: plugin
16
Bundle-Localization: plugin
22
Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.1
17
Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.1
(-)src/org/eclipse/equinox/internal/event/ExternalMessages.properties (+17 lines)
Added Link Here
1
###############################################################################
2
# Copyright (c) 2007 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
# NLS_MESSAGEFORMAT_ALL 
12
13
EVENT_ASYNC_THREAD_NAME=EventAdmin Async Event Dispatcher Thread
14
EVENT_NULL_EVENT=Null event is passed to EventAdmin. Ignored.
15
EVENT_NO_TOPICPERMISSION_PUBLISH=Caller bundle doesn't have TopicPermission to publish topic {0}
16
EVENT_DISPATCH_HANDLER_EXCEPTION=Exception while dispatching event {0} to handler {1}
17
EVENT_INVALID_HANDLER_FILTER=Invalid handler filter {0}
(-)src/org/eclipse/equinox/internal/event/mapper/EventCourier.java (+183 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.internal.event.mapper;
13
14
import org.osgi.framework.*;
15
import org.osgi.service.event.EventAdmin;
16
import org.osgi.service.log.*;
17
import org.osgi.util.tracker.ServiceTracker;
18
19
/**
20
 * Main class for redeliver special events like FrameworkEvents via EventAdmin.
21
 * 
22
 * @version $Revision: 1.1 $
23
 */
24
public class EventCourier implements FrameworkListener, BundleListener, ServiceListener, LogListener, LogReaderServiceListener {
25
	private ServiceTracker eventAdminTracker;
26
	private LogReaderServiceTracker logTracker;
27
	private LogReaderService reader;
28
	private final static boolean DEBUG = false;
29
	private BundleContext bc;
30
	private ServiceRegistration configurationListenerReg;
31
	private ServiceRegistration wireAdminListenerReg;
32
	private ServiceRegistration upnpEventListenerReg;
33
	private ServiceRegistration userAdminListenerReg;
34
35
	public EventCourier(BundleContext bc) {
36
		this.bc = bc;
37
	}
38
39
	public void close() {
40
		if (logTracker != null) {
41
			logTracker.close();
42
			logTracker = null;
43
		}
44
		if (eventAdminTracker != null) {
45
			eventAdminTracker.close();
46
			eventAdminTracker = null;
47
		}
48
		if (reader != null) {
49
			reader.removeLogListener(this);
50
		}
51
		reader = null;
52
		bc.removeFrameworkListener(this);
53
		bc.removeBundleListener(this);
54
		bc.removeServiceListener(this);
55
		if (configurationListenerReg != null) {
56
			configurationListenerReg.unregister();
57
			configurationListenerReg = null;
58
		}
59
		if (wireAdminListenerReg != null) {
60
			wireAdminListenerReg.unregister();
61
			wireAdminListenerReg = null;
62
		}
63
		if (upnpEventListenerReg != null) {
64
			upnpEventListenerReg.unregister();
65
			upnpEventListenerReg = null;
66
		}
67
		if (userAdminListenerReg != null) {
68
			userAdminListenerReg.unregister();
69
			userAdminListenerReg = null;
70
		}
71
	}
72
73
	/**
74
	 * prepare any service trackers and register event listeners which are
75
	 * necessary to obtain events to be mapped
76
	 */
77
	public void open() {
78
		// open ServiceTracker for EventAdmin
79
		eventAdminTracker = new ServiceTracker(bc, EventAdmin.class.getName(), null);
80
		eventAdminTracker.open();
81
		// open ServiceTracker for LogReaderService
82
		logTracker = new LogReaderServiceTracker(bc, this);
83
		logTracker.open();
84
		// add legacy event listener for framework level event
85
		bc.addFrameworkListener(this);
86
		bc.addBundleListener(this);
87
		bc.addServiceListener(this);
88
	}
89
90
	private EventAdmin getEventAdmin() {
91
		if (eventAdminTracker == null)
92
			return null;
93
		return (EventAdmin) eventAdminTracker.getService();
94
	}
95
96
	/**
97
	 * @param event
98
	 * @see org.osgi.framework.FrameworkListener#frameworkEvent(org.osgi.framework.FrameworkEvent)
99
	 */
100
	public void frameworkEvent(FrameworkEvent event) {
101
		EventAdmin eventAdmin = getEventAdmin();
102
		if (eventAdmin != null) {
103
			(new FrameworkEventAdapter(event, eventAdmin)).redeliver();
104
		} else {
105
			printNoEventAdminError();
106
		}
107
	}
108
109
	private void printNoEventAdminError() {
110
		if (DEBUG) {
111
			System.out.println(this.getClass().getName() + ": Cannot find the EventAdmin."); //$NON-NLS-1$
112
		}
113
	}
114
115
	/**
116
	 * @param event
117
	 * @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent)
118
	 */
119
	public void bundleChanged(BundleEvent event) {
120
		EventAdmin eventAdmin = getEventAdmin();
121
		if (eventAdmin != null) {
122
			(new BundleEventAdapter(event, eventAdmin)).redeliver();
123
		} else {
124
			printNoEventAdminError();
125
		}
126
	}
127
128
	/**
129
	 * @param event
130
	 * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
131
	 */
132
	public void serviceChanged(ServiceEvent event) {
133
		EventAdmin eventAdmin = getEventAdmin();
134
		if (eventAdmin != null) {
135
			(new ServiceEventAdapter(event, eventAdmin)).redeliver();
136
		} else {
137
			printNoEventAdminError();
138
		}
139
	}
140
141
	/**
142
	 * @param entry
143
	 * @see org.osgi.service.log.LogListener#logged(org.osgi.service.log.LogEntry)
144
	 */
145
	public void logged(LogEntry entry) {
146
		EventAdmin eventAdmin = getEventAdmin();
147
		if (eventAdmin != null) {
148
			(new LogEntryAdapter(entry, eventAdmin)).redeliver();
149
		} else {
150
			printNoEventAdminError();
151
		}
152
	}
153
154
	/**
155
	 * @param reference
156
	 * @param service
157
	 * @see org.eclipse.equinox.internal.event.mapper.LogReaderServiceListener#logReaderServiceAdding(org.osgi.framework.ServiceReference,
158
	 *      org.osgi.service.log.LogReaderService)
159
	 */
160
	public void logReaderServiceAdding(ServiceReference reference, LogReaderService service) {
161
		if (reader != null) {
162
			return;
163
		}
164
		reader = service;
165
		reader.addLogListener(this);
166
	}
167
168
	/**
169
	 * @param reference
170
	 * @param service
171
	 * @see org.eclipse.equinox.internal.event.mapper.LogReaderServiceListener#logReaderServiceRemoved(org.osgi.framework.ServiceReference,
172
	 *      org.osgi.service.log.LogReaderService)
173
	 */
174
	public void logReaderServiceRemoved(ServiceReference reference, LogReaderService service) {
175
		if ((reader != null) && reader.equals(service)) {
176
			reader.removeLogListener(this);
177
			reader = null;
178
		}
179
		// ungetService() will be called after returning to
180
		// LogReaderServiceTracker's removedService() method.
181
	}
182
183
}
(-)src/org/eclipse/equinox/internal/event/mapper/ServiceEventAdapter.java (+65 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.internal.event.mapper;
13
14
import java.util.Hashtable;
15
import org.osgi.framework.ServiceEvent;
16
import org.osgi.framework.ServiceReference;
17
import org.osgi.service.event.Event;
18
import org.osgi.service.event.EventAdmin;
19
20
/**
21
 * @version $Revision: 1.3 $
22
 */
23
public class ServiceEventAdapter extends EventAdapter {
24
	// constants for Event topic substring
25
	public static final String HEADER = "org/osgi/framework/ServiceEvent"; //$NON-NLS-1$
26
	public static final String UNREGISTERING = "UNREGISTERING"; //$NON-NLS-1$
27
	public static final String MODIFIED = "MODIFIED"; //$NON-NLS-1$
28
	public static final String REGISTERED = "REGISTERED"; //$NON-NLS-1$
29
	private ServiceEvent event;
30
31
	public ServiceEventAdapter(ServiceEvent event, EventAdmin eventAdmin) {
32
		super(eventAdmin);
33
		this.event = event;
34
	}
35
36
	/**
37
	 * @see org.eclipse.equinox.internal.event.mapper.EventAdapter#convert()
38
	 */
39
	public Event convert() {
40
		String typename = null;
41
		switch (event.getType()) {
42
			case ServiceEvent.REGISTERED :
43
				typename = REGISTERED;
44
				break;
45
			case ServiceEvent.MODIFIED :
46
				typename = MODIFIED;
47
				break;
48
			case ServiceEvent.UNREGISTERING :
49
				typename = UNREGISTERING;
50
				break;
51
			default :
52
				return null;
53
		}
54
		String topic = HEADER + Constants.TOPIC_SEPARATOR + typename;
55
		Hashtable properties = new Hashtable();
56
		ServiceReference ref = event.getServiceReference();
57
		if (ref != null) {
58
			putServiceReferenceProperties(properties, ref);
59
		}
60
		properties.put(Constants.EVENT, event);
61
		Event converted = new Event(topic, properties);
62
		return converted;
63
	}
64
65
}
(-)src/org/eclipse/equinox/internal/event/EventAdminImpl.java (+156 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.internal.event;
13
14
import java.security.Permission;
15
import java.util.Iterator;
16
import java.util.Set;
17
import org.eclipse.osgi.framework.eventmgr.*;
18
import org.eclipse.osgi.util.NLS;
19
import org.osgi.framework.BundleContext;
20
import org.osgi.service.event.*;
21
import org.osgi.service.log.LogService;
22
23
/**
24
 * Implementation of org.osgi.service.event.EventAdmin. EventAdminImpl uses
25
 * org.eclipse.osgi.framework.eventmgr.EventManager. It is assumeed
26
 * org.eclipse.osgi.framework.eventmgr package is exported by some other bundle.
27
 */
28
public class EventAdminImpl implements EventAdmin {
29
	private final LogTracker log;
30
	private final EventHandlerTracker handlers;
31
	private volatile EventManager		eventManager;
32
33
	/**
34
	 * Constructor for EventAdminImpl.
35
	 * 
36
	 * @param context BundleContext
37
	 */
38
	EventAdminImpl(BundleContext context) {
39
		super();
40
		log = new LogTracker(context, System.out);
41
		handlers = new EventHandlerTracker(context, log);
42
	}
43
	
44
	/**
45
	 * This method should be called before registering EventAdmin service
46
	 */
47
	void start() {
48
		log.open();
49
		ThreadGroup eventGroup = new ThreadGroup("Equinox Event Admin"); //$NON-NLS-1$
50
		eventGroup.setDaemon(true);
51
		eventManager = new EventManager(EventAdminMsg.EVENT_ASYNC_THREAD_NAME, eventGroup);
52
		handlers.open();
53
	}
54
55
	/**
56
	 * This method should be called after unregistering EventAdmin service
57
	 */
58
	void stop() {
59
		handlers.close();
60
		eventManager.close();
61
		eventManager = null;	// signify we have stopped
62
		log.close();
63
	}
64
65
	/**
66
	 * @param event
67
	 * @see org.osgi.service.event.EventAdmin#postEvent(org.osgi.service.event.Event)
68
	 */
69
	public void postEvent(Event event) {
70
		dispatchEvent(event, true);
71
	}
72
73
	/**
74
	 * @param event
75
	 * @see org.osgi.service.event.EventAdmin#sendEvent(org.osgi.service.event.Event)
76
	 */
77
	public void sendEvent(Event event) {
78
		dispatchEvent(event, false);
79
	}
80
81
	/**
82
	 * Internal main method for sendEvent() and postEvent(). Dispatching an
83
	 * event to EventHandler. All exceptions are logged except when dealing with
84
	 * LogEntry.
85
	 * 
86
	 * @param event to be delivered
87
	 * @param isAsync must be set to true for syncronous event delivery, false
88
	 *        for asyncronous delivery.
89
	 */
90
	private void dispatchEvent(Event event, boolean isAsync) {
91
		// keep a local copy in case we are stopped in the middle of dispatching
92
		EventManager currentManager = eventManager;
93
		if (currentManager == null) {
94
			// EventAdmin is stopped
95
			return;
96
		}
97
		if (event == null) {
98
			log.log(LogService.LOG_ERROR, EventAdminMsg.EVENT_NULL_EVENT);
99
			// continue from here will result in an NPE below; the spec for EventAdmin does not allow for null here
100
		}
101
		
102
		String topic = event.getTopic();
103
		
104
		try {
105
			checkTopicPermissionPublish(topic);
106
		} catch (SecurityException e) {
107
			String msg = NLS.bind(EventAdminMsg.EVENT_NO_TOPICPERMISSION_PUBLISH, event.getTopic());
108
			log.log(LogService.LOG_ERROR, msg);
109
			// must throw a security exception here according to the EventAdmin spec
110
			throw e;
111
		}
112
		
113
		Set eventHandlers = handlers.getHandlers(topic);
114
		// If there are no handlers, then we are done
115
		if (eventHandlers.size() == 0) {
116
			return;
117
		}
118
		
119
		SecurityManager sm = System.getSecurityManager();
120
		Permission perm = (sm == null) ? null : new TopicPermission(topic, TopicPermission.SUBSCRIBE);
121
		
122
		EventListeners listeners = new EventListeners();
123
		Iterator iter = eventHandlers.iterator();
124
		while (iter.hasNext()) {
125
			EventHandlerWrapper wrapper = (EventHandlerWrapper) iter.next();
126
			listeners.addListener(wrapper, perm);
127
		}
128
		
129
		// Create the listener queue for this event delivery
130
		ListenerQueue listenerQueue = new ListenerQueue(currentManager);
131
		// Add the listeners to the queue and associate them with the event
132
		// dispatcher
133
		listenerQueue.queueListeners(listeners, handlers);
134
		// Deliver the event to the listeners.
135
		if (isAsync) {
136
			listenerQueue.dispatchEventAsynchronous(0, event);
137
		}
138
		else {
139
			listenerQueue.dispatchEventSynchronous(0, event);
140
		}
141
	}
142
143
	/**
144
	 * Checks if the caller bundle has right PUBLISH TopicPermision.
145
	 * 
146
	 * @param topic
147
	 * @throws SecurityException if the caller does not have the right to PUBLISH TopicPermission
148
	 */
149
	private void checkTopicPermissionPublish(String topic) throws SecurityException{
150
		SecurityManager sm = System.getSecurityManager();
151
		if (sm == null)
152
			return;
153
		sm.checkPermission(new TopicPermission(topic, TopicPermission.PUBLISH));
154
	}
155
156
}
(-)src/org/eclipse/equinox/internal/event/EventHandlerWrapper.java (+184 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 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.equinox.internal.event;
13
14
import java.security.Permission;
15
import org.eclipse.osgi.util.NLS;
16
import org.osgi.framework.*;
17
import org.osgi.service.event.*;
18
import org.osgi.service.log.LogService;
19
20
/**
21
 * A wrapper for EventHandlers. This class caches property values and 
22
 * performs final checks before calling the wrapped handler.
23
 *
24
 */
25
public class EventHandlerWrapper {
26
	private final ServiceReference reference;
27
	private final LogService log;
28
	private final BundleContext context;
29
	private EventHandler handler;
30
	private String[] topics;
31
	private Filter filter;
32
	
33
	/**
34
	 * Create an EventHandlerWrapper. 
35
36
	 * @param reference Reference to the EventHandler
37
	 * @param context Bundle Context of the Event Admin bundle
38
	 * @param log LogService object for logging
39
	 */
40
	public EventHandlerWrapper(ServiceReference reference, BundleContext context, LogService log) {
41
		this.reference = reference;
42
		this.context = context;
43
		this.log = log;
44
	}
45
46
	/**
47
	 * Cache values from service properties
48
	 * 
49
	 * @return true if the handler should be called; false if the handler should not be called
50
	 */
51
	public synchronized boolean init() {
52
		topics = null;
53
		filter = null;
54
55
		// Get topic names
56
		Object o = reference.getProperty(EventConstants.EVENT_TOPIC);
57
		if (o instanceof String) {
58
			topics = new String[] {(String)o};
59
		}
60
		else if (o instanceof String[]) {
61
			topics = (String[]) o;
62
		}
63
		
64
		if (topics == null) {
65
			return false;
66
		}
67
		
68
		// get filter
69
		o = reference.getProperty(EventConstants.EVENT_FILTER);
70
		if (o instanceof String) {
71
			try {
72
				filter = context.createFilter((String)o);
73
			}
74
			catch (InvalidSyntaxException e) {
75
				log.log(LogService.LOG_ERROR,NLS.bind(EventAdminMsg.EVENT_INVALID_HANDLER_FILTER, o), e);
76
				return false;
77
			}
78
		}
79
		
80
		return true;
81
	}
82
83
	/**
84
	 * Flush the handler service if it has been obtained.
85
	 */
86
	public void flush() {
87
		synchronized (this) {
88
			if (handler == null) {
89
				return;
90
			}
91
			handler = null;
92
		}
93
		context.ungetService(reference);
94
	}
95
	
96
	/**
97
	 * Get the event topics for the wrapped handler.
98
	 * 
99
	 * @return The wrapped handler's event topics
100
	 */
101
	public synchronized String[] getTopics()  {
102
		return topics;
103
	}
104
	
105
	/**
106
	 * Return the wrapped handler. 
107
	 * @return The wrapped handler.
108
	 */
109
	private EventHandler getHandler() {
110
		synchronized (this) {
111
			// if we already have a handler, return it
112
			if (handler != null) {
113
				return handler;
114
			}
115
		}
116
		
117
		// we don't have the handler, so lets get it outside the sync region
118
		EventHandler tempHandler = (EventHandler)context.getService(reference);
119
120
		synchronized (this) {
121
			// do we still need the handler we just got?
122
			if (handler == null) {
123
				handler = tempHandler;
124
				return handler;
125
			}
126
			// get the current handler
127
			tempHandler = handler;
128
		}
129
		
130
		// unget the handler we just got since we don't need it
131
		context.ungetService(reference);
132
		
133
		// return the current handler (copied into the local var)
134
		return tempHandler;
135
	}
136
	
137
	/**
138
	 * Get the filter object
139
	 * 
140
	 * @return The handler's filter
141
	 */
142
	private synchronized Filter getFilter() {
143
		return filter;
144
	}
145
146
	/**
147
	 * Dispatch event to handler. Perform final tests before actually calling the handler.
148
	 * 
149
	 * @param event The event to dispatch
150
	 * @param perm The permission to be checked
151
	 */
152
	public void handleEvent(Event event, Permission perm) {
153
		Bundle bundle = reference.getBundle();
154
		// is service unregistered?
155
		if (bundle == null) {
156
			return;
157
		}
158
		
159
		// filter match
160
		Filter eventFilter = getFilter();
161
		if ((eventFilter != null) && !event.matches(eventFilter)) {
162
			return;
163
		}
164
		
165
		// permission check
166
		if ((perm != null) && (!bundle.hasPermission(perm))) {
167
			return;
168
		}
169
		
170
		// get handler service
171
		EventHandler handlerService = getHandler();
172
		if (handlerService == null) {
173
			return;
174
		}
175
	
176
		try {
177
			handlerService.handleEvent(event);
178
		}
179
		catch (Throwable t) {
180
			// log/handle any Throwable thrown by the listener
181
			log.log(LogService.LOG_ERROR, NLS.bind(EventAdminMsg.EVENT_DISPATCH_HANDLER_EXCEPTION, event, handlerService), t);
182
		}
183
	}
184
}
(-)src/org/eclipse/equinox/internal/event/mapper/EventAdapter.java (+100 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.internal.event.mapper;
13
14
import java.util.Hashtable;
15
import org.osgi.framework.Bundle;
16
import org.osgi.framework.ServiceReference;
17
import org.osgi.service.event.Event;
18
import org.osgi.service.event.EventAdmin;
19
20
/**
21
 * @version $Revision: 1.2 $
22
 */
23
public abstract class EventAdapter {
24
	final EventAdmin eventAdmin;
25
26
	/**
27
	 * @param event
28
	 * @param eventAdmin
29
	 */
30
	public EventAdapter(EventAdmin eventAdmin) {
31
		this.eventAdmin = eventAdmin;
32
	}
33
34
	/**
35
	 * @return event
36
	 */
37
	public abstract Event convert();
38
39
	public void redeliver() {
40
		Event converted = convert();
41
		if (converted != null) {
42
			redeliverInternal(converted);
43
		}
44
	}
45
46
	/**
47
	 * subclasses should override this method if it wants to use sendEvent()
48
	 * instead.
49
	 */
50
	protected void redeliverInternal(Event converted) {
51
		eventAdmin.postEvent(converted);
52
	}
53
54
	public void putBundleProperties(Hashtable properties, Bundle bundle) {
55
		// assertion bundle != null
56
		properties.put(Constants.BUNDLE_ID, new Long(bundle.getBundleId()));
57
		String symbolicName = bundle.getSymbolicName();
58
		if (symbolicName != null) {
59
			properties.put(Constants.BUNDLE_SYMBOLICNAME, symbolicName);
60
		}
61
		properties.put(Constants.BUNDLE, bundle);
62
	}
63
64
	public void putExceptionProperties(Hashtable properties, Throwable t) {
65
		// assertion t != null
66
		properties.put(Constants.EXCEPTION, t);
67
		properties.put(Constants.EXCEPTION_CLASS, t.getClass().getName());
68
		String message = t.getMessage();
69
		if (message != null) {
70
			properties.put(Constants.EXCEPTION_MESSAGE, t.getMessage());
71
		}
72
	}
73
74
	public void putServiceReferenceProperties(Hashtable properties, ServiceReference ref) {
75
		// assertion ref != null
76
		properties.put(Constants.SERVICE, ref);
77
		properties.put(Constants.SERVICE_ID, ref.getProperty(org.osgi.framework.Constants.SERVICE_ID));
78
		Object o = ref.getProperty(org.osgi.framework.Constants.SERVICE_PID);
79
		if ((o != null) && (o instanceof String)) {
80
			properties.put(Constants.SERVICE_PID, o);
81
		}
82
		Object o2 = ref.getProperty(org.osgi.framework.Constants.OBJECTCLASS);
83
		if ((o2 != null) && (o2 instanceof String[])) {
84
			properties.put(Constants.SERVICE_OBJECTCLASS, o2);
85
		}
86
	}
87
88
	/*
89
	 * Utility function for converting classes into strings
90
	 */
91
	public String[] classes2strings(Class classes[]) {
92
		if ((classes == null) || (classes.length == 0))
93
			return null;
94
		String[] strings = new String[classes.length];
95
		for (int i = 0; i < classes.length; i++) {
96
			strings[i] = classes[i].getName();
97
		}
98
		return strings;
99
	}
100
}
(-)src/org/eclipse/equinox/internal/event/LogTrackerMsg.java (+28 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 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
package org.eclipse.equinox.internal.event;
12
13
import org.eclipse.osgi.util.NLS;
14
15
public class LogTrackerMsg extends NLS {
16
	private static final String BUNDLE_NAME = "org.eclipse.equinox.event.LogMessages"; //$NON-NLS-1$
17
18
	public static String Unknown_Log_level;
19
	public static String Info;
20
	public static String Warning;
21
	public static String Error;
22
	public static String Debug;
23
24
	static {
25
		// initialize resource bundles
26
		NLS.initializeMessages(BUNDLE_NAME, LogTrackerMsg.class);
27
	}
28
}
(-)src/org/eclipse/equinox/internal/event/mapper/FrameworkEventAdapter.java (+72 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.internal.event.mapper;
13
14
import java.util.Hashtable;
15
import org.osgi.framework.Bundle;
16
import org.osgi.framework.FrameworkEvent;
17
import org.osgi.service.event.Event;
18
import org.osgi.service.event.EventAdmin;
19
20
/**
21
 * @version $Revision: 1.2 $
22
 */
23
public class FrameworkEventAdapter extends EventAdapter {
24
	// constants for Event topic substring
25
	public static final String HEADER = "org/osgi/framework/FrameworkEvent"; //$NON-NLS-1$
26
	public static final String STARTLEVEL_CHANGED = "STARTLEVEL_CHANGED"; //$NON-NLS-1$
27
	public static final String STARTED = "STARTED"; //$NON-NLS-1$
28
	public static final String PACKAGES_REFRESHED = "PACKAGES_REFRESHED"; //$NON-NLS-1$
29
	public static final String ERROR = "ERROR"; //$NON-NLS-1$
30
	protected FrameworkEvent event;
31
32
	public FrameworkEventAdapter(FrameworkEvent event, EventAdmin eventAdmin) {
33
		super(eventAdmin);
34
		this.event = event;
35
	}
36
37
	/**
38
	 * @see org.eclipse.equinox.internal.event.mapper.EventAdapter#convert()
39
	 */
40
	public Event convert() {
41
		String typename = null;
42
		switch (event.getType()) {
43
			case FrameworkEvent.ERROR :
44
				typename = ERROR;
45
				break;
46
			case FrameworkEvent.PACKAGES_REFRESHED :
47
				typename = PACKAGES_REFRESHED;
48
				break;
49
			case FrameworkEvent.STARTED :
50
				typename = STARTED;
51
				break;
52
			case FrameworkEvent.STARTLEVEL_CHANGED :
53
				typename = STARTLEVEL_CHANGED;
54
				break;
55
			default :
56
				return null;
57
		}
58
		String topic = HEADER + Constants.TOPIC_SEPARATOR + typename;
59
		Hashtable properties = new Hashtable();
60
		Bundle bundle = event.getBundle();
61
		if (bundle != null) {
62
			putBundleProperties(properties, bundle);
63
		}
64
		Throwable t = event.getThrowable();
65
		if (t != null) {
66
			putExceptionProperties(properties, t);
67
		}
68
		properties.put(Constants.EVENT, event);
69
		Event converted = new Event(topic, properties);
70
		return converted;
71
	}
72
}
(-)src/org/eclipse/equinox/internal/event/mapper/LogReaderServiceTracker.java (+49 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.internal.event.mapper;
13
14
import org.osgi.framework.BundleContext;
15
import org.osgi.framework.ServiceReference;
16
import org.osgi.service.log.LogReaderService;
17
import org.osgi.util.tracker.ServiceTracker;
18
19
/**
20
 * @version $Revision: 1.1 $
21
 */
22
public class LogReaderServiceTracker extends ServiceTracker {
23
	private final LogReaderServiceListener listener;
24
	private ServiceReference reference;
25
26
	public LogReaderServiceTracker(BundleContext context, LogReaderServiceListener listener) {
27
		super(context, LogReaderService.class.getName(), null);
28
		this.listener = listener;
29
	}
30
31
	public Object addingService(ServiceReference ref) {
32
		Object object = super.addingService(ref);
33
		if ((object != null) && (this.reference == null) && (object instanceof LogReaderService)) {
34
			this.reference = ref;
35
			listener.logReaderServiceAdding(ref, (LogReaderService) object);
36
		}
37
		return object;
38
	}
39
40
	public void removedService(ServiceReference ref, Object service) {
41
		if ((service != null) && (this.reference.equals(ref)) && (service instanceof LogReaderService)) {
42
			listener.logReaderServiceRemoved(ref, (LogReaderService) service);
43
			this.reference = null;
44
		}
45
		super.removedService(ref, service);
46
		//this method calls ungetService()
47
	}
48
49
}
(-)src/org/eclipse/equinox/internal/event/LogMessages.properties (+17 lines)
Added Link Here
1
###############################################################################
2
# Copyright (c) 2005, 2006 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
# NLS_MESSAGEFORMAT_ALL 
12
13
Unknown_Log_level=Unknown Log Level
14
Info=Log Info
15
Warning=Log Warning
16
Error=Log Error
17
Debug=Log Debug
(-)src/org/eclipse/equinox/internal/event/mapper/LogEntryAdapter.java (+79 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.internal.event.mapper;
13
14
import java.util.Hashtable;
15
import org.osgi.framework.Bundle;
16
import org.osgi.framework.ServiceReference;
17
import org.osgi.service.event.Event;
18
import org.osgi.service.event.EventAdmin;
19
import org.osgi.service.log.LogEntry;
20
import org.osgi.service.log.LogService;
21
22
/**
23
 * @version $Revision: 1.2 $
24
 */
25
public class LogEntryAdapter extends EventAdapter {
26
	// constants for Event topic substring
27
	public static final String TOPIC = "org/osgi/service/log/LogEntry"; //$NON-NLS-1$
28
	// constants for Event properties
29
	public static final String TIMESTAMP = "timestamp"; //$NON-NLS-1$
30
	public static final String MESSAGE = "message"; //$NON-NLS-1$
31
	public static final String LOG_LEVEL = "log.level"; //$NON-NLS-1$
32
	public static final String LOG_ENTRY = "log.entry"; //$NON-NLS-1$
33
	private LogEntry entry;
34
35
	public LogEntryAdapter(LogEntry entry, EventAdmin eventAdmin) {
36
		super(eventAdmin);
37
		this.entry = entry;
38
	}
39
40
	/**
41
	 * @see org.eclipse.equinox.internal.event.mapper.EventAdapter#convert()
42
	 */
43
	public Event convert() {
44
		String topic = TOPIC;
45
		int level = entry.getLevel();
46
		switch (level) {
47
			case LogService.LOG_ERROR :
48
			case LogService.LOG_WARNING :
49
			case LogService.LOG_INFO :
50
			case LogService.LOG_DEBUG :
51
				break;
52
			default :
53
				// other log levels are represented by their decimal value
54
				topic += Constants.TOPIC_SEPARATOR + level;
55
		}
56
		Hashtable properties = new Hashtable();
57
		Bundle bundle = entry.getBundle();
58
		if (bundle == null) {
59
			throw new RuntimeException("LogEntry.getBundle() returns null"); //$NON-NLS-1$
60
		}
61
		putBundleProperties(properties, bundle);
62
		Throwable t = entry.getException();
63
		if (t != null) {
64
			putExceptionProperties(properties, t);
65
		}
66
		ServiceReference ref = entry.getServiceReference();
67
		if (ref != null) {
68
			putServiceReferenceProperties(properties, ref);
69
		}
70
		properties.put(LOG_ENTRY, entry);
71
		properties.put(LOG_LEVEL, new Integer(entry.getLevel()));
72
		if (entry.getMessage() != null)
73
			properties.put(MESSAGE, entry.getMessage());
74
		properties.put(TIMESTAMP, new Long(entry.getTime()));
75
		Event converted = new Event(topic, properties);
76
		return converted;
77
	}
78
79
}
(-)src/org/eclipse/equinox/internal/event/mapper/BundleEventAdapter.java (+82 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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
package org.eclipse.equinox.internal.event.mapper;
12
13
import java.util.Hashtable;
14
import org.osgi.framework.Bundle;
15
import org.osgi.framework.BundleEvent;
16
import org.osgi.service.event.Event;
17
import org.osgi.service.event.EventAdmin;
18
19
/**
20
 * @version $Revision: 1.2 $
21
 */
22
public class BundleEventAdapter extends EventAdapter {
23
	// constants for Event topic substring
24
	public static final String HEADER = "org/osgi/framework/BundleEvent"; //$NON-NLS-1$
25
	public static final String INSTALLED = "INSTALLED"; //$NON-NLS-1$
26
	public static final String STOPPED = "STOPPED"; //$NON-NLS-1$
27
	public static final String STARTED = "STARTED"; //$NON-NLS-1$
28
	public static final String UPDATED = "UPDATED"; //$NON-NLS-1$
29
	public static final String UNINSTALLED = "UNINSTALLED"; //$NON-NLS-1$
30
	public static final String RESOLVED = "RESOLVED"; //$NON-NLS-1$
31
	public static final String UNRESOLVED = "UNRESOLVED"; //$NON-NLS-1$
32
	private BundleEvent event;
33
34
	public BundleEventAdapter(BundleEvent event, EventAdmin eventAdmin) {
35
		super(eventAdmin);
36
		this.event = event;
37
	}
38
39
	/**
40
	 * @return event
41
	 * @see org.eclipse.equinox.internal.event.mapper.EventAdapter#convert()
42
	 */
43
	public Event convert() {
44
		String typename = null;
45
		switch (event.getType()) {
46
			case BundleEvent.INSTALLED :
47
				typename = INSTALLED;
48
				break;
49
			case BundleEvent.STOPPED :
50
				typename = STOPPED;
51
				break;
52
			case BundleEvent.STARTED :
53
				typename = STARTED;
54
				break;
55
			case BundleEvent.UPDATED :
56
				typename = UPDATED;
57
				break;
58
			case BundleEvent.UNINSTALLED :
59
				typename = UNINSTALLED;
60
				break;
61
			case BundleEvent.RESOLVED :
62
				typename = RESOLVED;
63
				break;
64
			case BundleEvent.UNRESOLVED :
65
				typename = UNRESOLVED;
66
				break;
67
			default :
68
				// unknown events must be send as their decimal value
69
				typename = Integer.toString(event.getType());
70
		}
71
		String topic = HEADER + Constants.TOPIC_SEPARATOR + typename;
72
		Hashtable properties = new Hashtable();
73
		Bundle bundle = event.getBundle();
74
		if (bundle == null) {
75
			throw new RuntimeException("BundleEvent.getBundle() returns null"); //$NON-NLS-1$
76
		}
77
		putBundleProperties(properties, bundle);
78
		properties.put(Constants.EVENT, event);
79
		Event converted = new Event(topic, properties);
80
		return converted;
81
	}
82
}
(-)src/org/eclipse/equinox/internal/event/mapper/LogReaderServiceListener.java (+26 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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.equinox.internal.event.mapper;
13
14
import org.osgi.framework.ServiceReference;
15
import org.osgi.service.log.LogReaderService;
16
17
/**
18
 * @version $Revision: 1.1 $
19
 */
20
public interface LogReaderServiceListener {
21
22
	public void logReaderServiceAdding(ServiceReference reference, LogReaderService service);
23
24
	public void logReaderServiceRemoved(ServiceReference reference, LogReaderService service);
25
26
}
(-)src/org/eclipse/equinox/internal/event/EventAdminMsg.java (+29 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 1999, 2007 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.equinox.internal.event;
13
14
import org.eclipse.osgi.util.NLS;
15
16
public class EventAdminMsg extends NLS {
17
	private static final String BUNDLE_NAME = "org.eclipse.equinox.event.ExternalMessages"; //$NON-NLS-1$
18
19
	public static String EVENT_ASYNC_THREAD_NAME;
20
	public static String EVENT_NULL_EVENT;
21
	public static String EVENT_NO_TOPICPERMISSION_PUBLISH;
22
	public static String EVENT_DISPATCH_HANDLER_EXCEPTION;
23
	public static String EVENT_INVALID_HANDLER_FILTER;
24
25
	static {
26
		// initialize resource bundles
27
		NLS.initializeMessages(BUNDLE_NAME, EventAdminMsg.class);
28
	}
29
}
(-)src/org/eclipse/equinox/internal/event/LogTracker.java (+174 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 1998, 2007 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
package org.eclipse.equinox.internal.event;
12
13
import java.io.PrintStream;
14
import java.util.Calendar;
15
import java.util.Date;
16
17
import org.osgi.framework.BundleContext;
18
import org.osgi.framework.ServiceReference;
19
import org.osgi.service.log.LogService;
20
import org.osgi.util.tracker.ServiceTracker;
21
22
/**
23
 * LogTracker class. This class encapsulates the LogService
24
 * and handles all issues such as the service coming and going.
25
 */
26
27
public class LogTracker extends ServiceTracker implements LogService {
28
	/** LogService interface class name */
29
	protected final static String clazz = "org.osgi.service.log.LogService"; //$NON-NLS-1$
30
31
	/** PrintStream to use if LogService is unavailable */
32
	private final PrintStream out;
33
34
	/**
35
	 * Create new LogTracker.
36
	 *
37
	 * @param context BundleContext of parent bundle.
38
	 * @param out Default PrintStream to use if LogService is unavailable.
39
	 */
40
	public LogTracker(BundleContext context, PrintStream out) {
41
		super(context, clazz, null);
42
		this.out = out;
43
	}
44
45
	/*
46
	 * ----------------------------------------------------------------------
47
	 *      LogService Interface implementation
48
	 * ----------------------------------------------------------------------
49
	 */
50
51
	public void log(int level, String message) {
52
		log(null, level, message, null);
53
	}
54
55
	public void log(int level, String message, Throwable exception) {
56
		log(null, level, message, exception);
57
	}
58
59
	public void log(ServiceReference reference, int level, String message) {
60
		log(reference, level, message, null);
61
	}
62
63
	public synchronized void log(ServiceReference reference, int level, String message, Throwable exception) {
64
		ServiceReference[] references = getServiceReferences();
65
66
		if (references != null) {
67
			int size = references.length;
68
69
			for (int i = 0; i < size; i++) {
70
				LogService service = (LogService) getService(references[i]);
71
				if (service != null) {
72
					try {
73
						service.log(reference, level, message, exception);
74
					} catch (Exception e) {
75
						// TODO: consider printing to System Error
76
					}
77
				}
78
			}
79
80
			return;
81
		}
82
83
		noLogService(level, message, exception, reference);
84
	}
85
86
	/**
87
	 * The LogService is not available so we write the message to a PrintStream.
88
	 *
89
	 * @param level Logging level
90
	 * @param message Log message.
91
	 * @param throwable Log exception or null if none.
92
	 * @param reference ServiceReference associated with message or null if none.
93
	 */
94
	protected void noLogService(int level, String message, Throwable throwable, ServiceReference reference) {
95
		if (out != null) {
96
			synchronized (out) {
97
				// Bug #113286.  If no log service present and messages are being
98
				// printed to stdout, prepend message with a timestamp.
99
				String timestamp = getDate(new Date());
100
				out.print(timestamp + " "); //$NON-NLS-1$
101
102
				switch (level) {
103
					case LOG_DEBUG : {
104
						out.print(LogTrackerMsg.Debug);
105
106
						break;
107
					}
108
					case LOG_INFO : {
109
						out.print(LogTrackerMsg.Info); 
110
111
						break;
112
					}
113
					case LOG_WARNING : {
114
						out.print(LogTrackerMsg.Warning);
115
116
						break;
117
					}
118
					case LOG_ERROR : {
119
						out.print(LogTrackerMsg.Error);
120
121
						break;
122
					}
123
					default : {
124
						out.print("["); //$NON-NLS-1$
125
						out.print(LogTrackerMsg.Unknown_Log_level);         
126
						out.print("]: "); //$NON-NLS-1$
127
128
						break;
129
					}
130
				}
131
132
				out.println(message);
133
134
				if (reference != null) {
135
					out.println(reference);
136
				}
137
138
				if (throwable != null) {
139
					throwable.printStackTrace(out);
140
				}
141
			}
142
		}
143
	}
144
	
145
	// from EclipseLog to avoid using DateFormat -- see bug 149892#c10
146
	private String getDate(Date date) {
147
			Calendar c = Calendar.getInstance();
148
			c.setTime(date);
149
			StringBuffer sb = new StringBuffer();
150
			appendPaddedInt(c.get(Calendar.YEAR), 4, sb).append('-');
151
			appendPaddedInt(c.get(Calendar.MONTH) + 1, 2, sb).append('-');
152
			appendPaddedInt(c.get(Calendar.DAY_OF_MONTH), 2, sb).append(' ');
153
			appendPaddedInt(c.get(Calendar.HOUR_OF_DAY), 2, sb).append(':');
154
			appendPaddedInt(c.get(Calendar.MINUTE), 2, sb).append(':');
155
			appendPaddedInt(c.get(Calendar.SECOND), 2, sb).append('.');
156
			appendPaddedInt(c.get(Calendar.MILLISECOND), 3, sb);
157
			return sb.toString();
158
	}
159
160
	private StringBuffer appendPaddedInt(int value, int pad, StringBuffer buffer) {
161
		pad = pad - 1;
162
		if (pad == 0)
163
			return buffer.append(Integer.toString(value));
164
		int padding = (int) Math.pow(10, pad);
165
		if (value >= padding)
166
			return buffer.append(Integer.toString(value));
167
		while (padding > value && padding > 1) {
168
			buffer.append('0');
169
			padding = padding / 10;
170
		}
171
		buffer.append(value);
172
		return buffer;
173
	}
174
}
(-)src/org/eclipse/equinox/internal/event/Activator.java (+36 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 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.equinox.internal.event;
13
14
import org.eclipse.equinox.internal.event.mapper.EventCourier;
15
import org.osgi.framework.*;
16
17
public class Activator implements BundleActivator {
18
	private EventCourier    eventRedeliverer;
19
	private ServiceRegistration eventAdminService;
20
	private EventAdminImpl eventAdmin;
21
	
22
	public void start(BundleContext bundleContext) {
23
		eventAdmin = new EventAdminImpl(bundleContext);
24
		eventAdmin.start();
25
		eventAdminService = bundleContext.registerService("org.osgi.service.event.EventAdmin", //$NON-NLS-1$
26
				eventAdmin,null);
27
		eventRedeliverer  = new EventCourier(bundleContext);
28
		eventRedeliverer.open();
29
	}
30
	
31
	public void stop(BundleContext bundleContext) {
32
		eventRedeliverer.close();
33
		eventAdminService.unregister();
34
		eventAdmin.stop();
35
	}
36
}
(-)src/org/eclipse/equinox/internal/event/mapper/Constants.java (+33 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation.
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
package org.eclipse.equinox.internal.event.mapper;
12
13
/**
14
 * @version $Revision: 1.1 $
15
 */
16
public interface Constants {
17
	// constants for Event common properties; event specific properties are
18
	// defined in the corresponding event adapter.
19
	public static final String BUNDLE = "bundle"; //$NON-NLS-1$
20
	public static final String BUNDLE_ID = "bundle.id"; //$NON-NLS-1$
21
	public static final String BUNDLE_SYMBOLICNAME = "bundle.symbolicName"; //$NON-NLS-1$
22
	public static final String EVENT = "event"; //$NON-NLS-1$
23
	public static final String EXCEPTION = "exception"; //$NON-NLS-1$
24
	public static final String EXCEPTION_CLASS = "exception.class"; //$NON-NLS-1$
25
	public static final String EXCEPTION_MESSAGE = "exception.message"; //$NON-NLS-1$
26
	public static final String MESSAGE = "message"; //$NON-NLS-1$
27
	public static final String SERVICE = "service"; //$NON-NLS-1$
28
	public static final String SERVICE_ID = "service.id"; //$NON-NLS-1$
29
	public static final String SERVICE_OBJECTCLASS = "service.objectClass"; //$NON-NLS-1$
30
	public static final String SERVICE_PID = "service.pid"; //$NON-NLS-1$
31
	public static final String TIMESTAMP = "timestamp"; //$NON-NLS-1$
32
	public static final char TOPIC_SEPARATOR = '/';
33
}
(-)src/org/eclipse/equinox/internal/event/EventHandlerTracker.java (+200 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 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.equinox.internal.event;
13
14
import java.security.Permission;
15
import java.util.*;
16
import org.eclipse.osgi.framework.eventmgr.EventDispatcher;
17
import org.osgi.framework.BundleContext;
18
import org.osgi.framework.ServiceReference;
19
import org.osgi.service.event.Event;
20
import org.osgi.service.event.EventHandler;
21
import org.osgi.service.log.LogService;
22
import org.osgi.util.tracker.ServiceTracker;
23
24
public class EventHandlerTracker extends ServiceTracker implements EventDispatcher {
25
	private final LogService log;
26
	//* List<EventHandlerWrapper> of all handlers with topic of "*"
27
	private final List globalWildcard;
28
	// Map<String,List<EventHandlerWrapper>> key is topic prefix of partial wildcard
29
	private final Map partialWildcard;
30
	// Map<String,List<EventHandlerWrapper>> key is topic name
31
	private final Map topicName;
32
33
	public EventHandlerTracker(BundleContext context, LogService log) {
34
		super(context, EventHandler.class.getName(), null);
35
		this.log = log;
36
		globalWildcard = new ArrayList();
37
		partialWildcard = new HashMap();
38
		topicName = new HashMap();
39
	}
40
41
	public Object addingService(ServiceReference reference) {
42
		EventHandlerWrapper wrapper = new EventHandlerWrapper(reference, context, log);
43
		synchronized (this) {
44
			if (wrapper.init()) {
45
				bucket(wrapper);
46
			}
47
		}
48
		return wrapper;
49
	}
50
51
	public void modifiedService(ServiceReference reference, Object service) {
52
		EventHandlerWrapper wrapper = (EventHandlerWrapper) service;
53
		synchronized (this) {
54
			unbucket(wrapper);
55
			if (wrapper.init()) {
56
				bucket(wrapper);
57
				return;
58
			}
59
		}
60
61
		wrapper.flush(); // needs to be called outside sync region
62
	}
63
64
	public void removedService(ServiceReference reference, Object service) {
65
		EventHandlerWrapper wrapper = (EventHandlerWrapper) service;
66
		synchronized (this) {
67
			unbucket(wrapper);
68
		}
69
		wrapper.flush(); // needs to be called outside sync region
70
	}
71
72
	/**
73
	 * Place the wrapper into the appropriate buckets.
74
	 * This is a performance optimization for event delivery.
75
	 * 
76
	 * @param wrapper The wrapper to place in buckets.
77
	 * @GuardedBy this
78
	 */
79
	private void bucket(EventHandlerWrapper wrapper) {
80
		final String[] topics = wrapper.getTopics();
81
		final int length = (topics == null) ? 0 : topics.length;
82
		for (int i = 0; i < length; i++) {
83
			String topic = topics[i];
84
			// global wildcard
85
			if (topic.equals("*")) { //$NON-NLS-1$
86
				globalWildcard.add(wrapper);
87
			}
88
			// partial wildcard
89
			else if (topic.endsWith("/*")) { //$NON-NLS-1$
90
				String key = topic.substring(0, topic.length() - 2); // Strip off "/*" from the end
91
				List wrappers = (List) partialWildcard.get(key);
92
				if (wrappers == null) {
93
					wrappers = new ArrayList();
94
					partialWildcard.put(key, wrappers);
95
				}
96
				wrappers.add(wrapper);
97
			}
98
			// simple topic name
99
			else {
100
				List wrappers = (List) topicName.get(topic);
101
				if (wrappers == null) {
102
					wrappers = new ArrayList();
103
					topicName.put(topic, wrappers);
104
				}
105
				wrappers.add(wrapper);
106
			}
107
		}
108
	}
109
110
	/**
111
	 * Remove the wrapper from the buckets.
112
	 * 
113
	 * @param wrapper The wrapper to remove from the buckets.
114
	 * @GuardedBy this
115
	 */
116
	private void unbucket(EventHandlerWrapper wrapper) {
117
		final String[] topics = wrapper.getTopics();
118
		final int length = (topics == null) ? 0 : topics.length;
119
		for (int i = 0; i < length; i++) {
120
			String topic = topics[i];
121
			// global wilcard
122
			if (topic.equals("*")) { //$NON-NLS-1$
123
				globalWildcard.remove(wrapper);
124
			}
125
			// partial wildcard
126
			else if (topic.endsWith("/*")) { //$NON-NLS-1$
127
				String key = topic.substring(0, topic.length() - 2); // Strip off "/*" from the end
128
				List wrappers = (List) partialWildcard.get(key);
129
				if (wrappers != null) {
130
					wrappers.remove(wrapper);
131
					if (wrappers.size() == 0) {
132
						partialWildcard.remove(key);
133
					}
134
				}
135
			}
136
			// simple topic name
137
			else {
138
				List wrappers = (List) topicName.get(topic);
139
				if (wrappers != null) {
140
					wrappers.remove(wrapper);
141
					if (wrappers.size() == 0) {
142
						topicName.remove(topic);
143
					}
144
				}
145
			}
146
		}
147
	}
148
149
	/**
150
	 * Return the set of handlers which subscribe to the event topic.
151
	 * A set is used to ensure a handler is not called for an event more than once.
152
	 * 
153
	 * @param topic
154
	 * @return a set of handlers
155
	 */
156
	public synchronized Set getHandlers(final String topic) {
157
		// Use a set to remove duplicates
158
		Set handlers = new HashSet();
159
160
		// Add the "*" handlers
161
		handlers.addAll(globalWildcard);
162
163
		// Add the handlers with partial matches
164
		if (partialWildcard.size() > 0) {
165
			int index = topic.length();
166
			while (index >= 0) {
167
				String subTopic = topic.substring(0, index); // First subtopic is the complete topic.
168
				List wrappers = (List) partialWildcard.get(subTopic);
169
				if (wrappers != null) {
170
					handlers.addAll(wrappers);
171
				}
172
				// Strip the last level from the topic. For example, org/osgi/framework becomes org/osgi.
173
				// Wildcard topics are inserted into the map with the "/*" stripped off.
174
				index = subTopic.lastIndexOf('/');
175
			}
176
		}
177
178
		// Add the handlers for matching topic names
179
		List wrappers = (List) topicName.get(topic);
180
		if (wrappers != null) {
181
			handlers.addAll(wrappers);
182
		}
183
184
		return handlers;
185
	}
186
187
	/**
188
	 * Dispatches Event to EventHandlers
189
	 * 
190
	 * @param eventListener
191
	 * @param listenerObject
192
	 * @param eventAction
193
	 * @param eventObject
194
	 * @see org.eclipse.osgi.framework.eventmgr.EventDispatcher#dispatchEvent(java.lang.Object,
195
	 *      java.lang.Object, int, java.lang.Object)
196
	 */
197
	public void dispatchEvent(Object eventListener, Object listenerObject, int eventAction, Object eventObject) {
198
		((EventHandlerWrapper) eventListener).handleEvent((Event) eventObject, (Permission) listenerObject);
199
	}
200
}

Return to bug 202278