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 281148 | Differences between
and this patch

Collapse All | Expand All

(-)schema/org.eclipse.e4.services.exsd (-120 lines)
Removed Link Here
1
<?xml version='1.0' encoding='UTF-8'?>
2
<!-- Schema file written by PDE -->
3
<schema targetNamespace="org.eclipse.e4.workbench.core" xmlns="http://www.w3.org/2001/XMLSchema">
4
<annotation>
5
      <appinfo>
6
         <meta.schema plugin="org.eclipse.e4.workbench.core" id="org.eclipse.e4.services" name="E4 Services"/>
7
      </appinfo>
8
      <documentation>
9
         [Enter description of this extension point.]
10
      </documentation>
11
   </annotation>
12
13
   <element name="extension">
14
      <annotation>
15
         <appinfo>
16
            <meta.element />
17
         </appinfo>
18
      </annotation>
19
      <complexType>
20
         <sequence>
21
            <element ref="servicefactory"/>
22
         </sequence>
23
         <attribute name="point" type="string" use="required">
24
            <annotation>
25
               <documentation>
26
                  
27
               </documentation>
28
            </annotation>
29
         </attribute>
30
         <attribute name="id" type="string">
31
            <annotation>
32
               <documentation>
33
                  
34
               </documentation>
35
            </annotation>
36
         </attribute>
37
         <attribute name="name" type="string">
38
            <annotation>
39
               <documentation>
40
                  
41
               </documentation>
42
               <appinfo>
43
                  <meta.attribute translatable="true"/>
44
               </appinfo>
45
            </annotation>
46
         </attribute>
47
      </complexType>
48
   </element>
49
50
   <element name="servicefactory">
51
      <complexType>
52
         <sequence minOccurs="1" maxOccurs="unbounded">
53
            <element ref="service"/>
54
         </sequence>
55
         <attribute name="class" type="string" use="required">
56
            <annotation>
57
               <documentation>
58
                  
59
               </documentation>
60
               <appinfo>
61
                  <meta.attribute kind="java" basedOn="org.eclipse.e4.core.services.ComputedValue:"/>
62
               </appinfo>
63
            </annotation>
64
         </attribute>
65
      </complexType>
66
   </element>
67
68
   <element name="service">
69
      <complexType>
70
         <attribute name="api" type="string" use="required">
71
            <annotation>
72
               <documentation>
73
                  
74
               </documentation>
75
               <appinfo>
76
                  <meta.attribute kind="java"/>
77
               </appinfo>
78
            </annotation>
79
         </attribute>
80
      </complexType>
81
   </element>
82
83
   <annotation>
84
      <appinfo>
85
         <meta.section type="since"/>
86
      </appinfo>
87
      <documentation>
88
         [Enter the first release in which this extension point appears.]
89
      </documentation>
90
   </annotation>
91
92
   <annotation>
93
      <appinfo>
94
         <meta.section type="examples"/>
95
      </appinfo>
96
      <documentation>
97
         [Enter extension point usage example here.]
98
      </documentation>
99
   </annotation>
100
101
   <annotation>
102
      <appinfo>
103
         <meta.section type="apiinfo"/>
104
      </appinfo>
105
      <documentation>
106
         [Enter API information here.]
107
      </documentation>
108
   </annotation>
109
110
   <annotation>
111
      <appinfo>
112
         <meta.section type="implementation"/>
113
      </appinfo>
114
      <documentation>
115
         [Enter information about supplied implementation of this extension point.]
116
      </documentation>
117
   </annotation>
118
119
120
</schema>
(-)src/org/eclipse/e4/internal/core/services/osgi/OSGiContextStrategy.java (-10 / +32 lines)
Lines 10-20 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.e4.internal.core.services.osgi;
11
package org.eclipse.e4.internal.core.services.osgi;
12
12
13
import java.util.*;
13
import java.util.Collections;
14
import java.util.HashMap;
15
import java.util.Iterator;
16
import java.util.Map;
17
import java.util.WeakHashMap;
14
import org.eclipse.e4.core.services.IDisposable;
18
import org.eclipse.e4.core.services.IDisposable;
19
import org.eclipse.e4.core.services.context.IContextFunction;
15
import org.eclipse.e4.core.services.context.IEclipseContext;
20
import org.eclipse.e4.core.services.context.IEclipseContext;
16
import org.eclipse.e4.core.services.context.spi.ILookupStrategy;
21
import org.eclipse.e4.core.services.context.spi.ILookupStrategy;
17
import org.osgi.framework.*;
22
import org.osgi.framework.BundleContext;
23
import org.osgi.framework.Constants;
24
import org.osgi.framework.InvalidSyntaxException;
25
import org.osgi.framework.ServiceReference;
18
import org.osgi.util.tracker.ServiceTracker;
26
import org.osgi.util.tracker.ServiceTracker;
19
import org.osgi.util.tracker.ServiceTrackerCustomizer;
27
import org.osgi.util.tracker.ServiceTrackerCustomizer;
20
28
Lines 25-35 Link Here
25
 */
33
 */
26
public class OSGiContextStrategy implements ILookupStrategy, IDisposable, ServiceTrackerCustomizer {
34
public class OSGiContextStrategy implements ILookupStrategy, IDisposable, ServiceTrackerCustomizer {
27
	class ServiceData {
35
	class ServiceData {
28
		//the service name
36
		// the service name
29
		String name;
37
		String name;
30
38
31
		ServiceTracker tracker;
39
		ServiceTracker tracker;
32
		//the contexts using this service (IEclipseContext -> null)
40
		// the contexts using this service (IEclipseContext -> null)
33
		final Map users = new WeakHashMap();
41
		final Map users = new WeakHashMap();
34
42
35
		ServiceData(String name) {
43
		ServiceData(String name) {
Lines 57-63 Link Here
57
		Object newValue = bundleContext.getService(reference);
65
		Object newValue = bundleContext.getService(reference);
58
		if (newValue == null)
66
		if (newValue == null)
59
			return null;
67
			return null;
60
		//for performance we store the concrete service object with each context that requested it
68
		// for performance we store the concrete service object with each
69
		// context that requested it
61
		ServiceData data = (ServiceData) services.get(name);
70
		ServiceData data = (ServiceData) services.get(name);
62
		for (Iterator it = data.users.keySet().iterator(); it.hasNext();)
71
		for (Iterator it = data.users.keySet().iterator(); it.hasNext();)
63
			((IEclipseContext) it.next()).set(name, newValue);
72
			((IEclipseContext) it.next()).set(name, newValue);
Lines 65-78 Link Here
65
	}
74
	}
66
75
67
	/**
76
	/**
68
	 * Discards any services that are no longer used by any strongly
77
	 * Discards any services that are no longer used by any strongly reachable
69
	 * reachable contexts.
78
	 * contexts.
70
	 */
79
	 */
71
	private void cleanReferences() {
80
	private void cleanReferences() {
72
		synchronized (services) {
81
		synchronized (services) {
73
			for (Iterator it = services.values().iterator(); it.hasNext();) {
82
			for (Iterator it = services.values().iterator(); it.hasNext();) {
74
				ServiceData data = (ServiceData) it.next();
83
				ServiceData data = (ServiceData) it.next();
75
				//if there are no more references, discard the service
84
				// if there are no more references, discard the service
76
				if (data.users.isEmpty()) {
85
				if (data.users.isEmpty()) {
77
					data.tracker.close();
86
					data.tracker.close();
78
					it.remove();
87
					it.remove();
Lines 98-107 Link Here
98
		cleanReferences();
107
		cleanReferences();
99
		ServiceData data = (ServiceData) services.get(name);
108
		ServiceData data = (ServiceData) services.get(name);
100
		if (data == null) {
109
		if (data == null) {
110
			// see if there is an IContextFunction registered for this name
111
			try {
112
				ServiceReference[] refs = bundleContext.getServiceReferences(
113
						IContextFunction.SERVICE_NAME, "(" //$NON-NLS-1$
114
								+ IContextFunction.SERVICE_CONTEXT_KEY + '=' + name + ')');
115
				if (refs != null && refs.length > 0)
116
					return bundleContext.getService(refs[0]);
117
			} catch (InvalidSyntaxException e) {
118
				// the name is not a valid service name, so just carry on
119
			}
120
121
			// create a tracker to retrieve the service with the given name
101
			data = new ServiceData(name);
122
			data = new ServiceData(name);
102
			data.tracker = new ServiceTracker(bundleContext, name, this);
123
			data.tracker = new ServiceTracker(bundleContext, name, this);
103
			services.put(name, data);
124
			services.put(name, data);
104
			//just opening a tracker will cause values to be set by the tracker callback methods
125
			// just opening a tracker will cause values to be set by the tracker
126
			// callback methods
105
			data.tracker.open();
127
			data.tracker.open();
106
		}
128
		}
107
		data.addContext(originatingContext);
129
		data.addContext(originatingContext);
Lines 117-123 Link Here
117
139
118
	public void removedService(ServiceReference reference, Object service) {
140
	public void removedService(ServiceReference reference, Object service) {
119
		String name = serviceName(reference);
141
		String name = serviceName(reference);
120
		//must set to null rather than removing so injection continues to work
142
		// must set to null rather than removing so injection continues to work
121
		ServiceData data = (ServiceData) services.get(name);
143
		ServiceData data = (ServiceData) services.get(name);
122
		for (Iterator it = data.users.keySet().iterator(); it.hasNext();)
144
		for (Iterator it = data.users.keySet().iterator(); it.hasNext();)
123
			((IEclipseContext) it.next()).set(name, null);
145
			((IEclipseContext) it.next()).set(name, null);
(-)src/org/eclipse/e4/core/services/context/IContextFunction.java (-14 / +44 lines)
Lines 12-42 Link Here
12
package org.eclipse.e4.core.services.context;
12
package org.eclipse.e4.core.services.context;
13
13
14
import org.eclipse.e4.core.services.context.spi.ContextFunction;
14
import org.eclipse.e4.core.services.context.spi.ContextFunction;
15
import org.osgi.framework.BundleContext;
15
16
16
/**
17
/**
17
 * A context function encapsulates evaluation of some code within an {@link IEclipseContext}.
18
 * A context function encapsulates evaluation of some code within an
18
 * The result of the function must be derived purely from the provided arguments and
19
 * {@link IEclipseContext}. The result of the function must be derived purely
19
 * context objects, and must be free from side-effects other than the function's return value.
20
 * from the provided arguments and context objects, and must be free from
20
 * In particular, the function must be idempotent - subsequent invocations of the same function
21
 * side-effects other than the function's return value. In particular, the
22
 * function must be idempotent - subsequent invocations of the same function
21
 * with the same inputs must produce the same result.
23
 * with the same inputs must produce the same result.
22
 * <p>
24
 * <p>
23
 * A common use for context functions is as a place holder for an object that has not yet
25
 * A common use for context functions is as a place holder for an object that
24
 * been created. These place holders can be stored as values in an {@link IEclipseContext}, 
26
 * has not yet been created. These place holders can be stored as values in an
25
 * allowing the concrete value they represent to be computed lazily only when requested.
27
 * {@link IEclipseContext}, allowing the concrete value they represent to be
28
 * computed lazily only when requested.
29
 * </p>
30
 * <p>
31
 * Context functions can optionally be registered as OSGi services. Context
32
 * implementations may use such registered services to seed context instances
33
 * with initial values. Registering your context function as a service is a
34
 * signal that contexts are free to add an instance of your function to their
35
 * context automatically, using the key specified by the
36
 * {@link #SERVICE_CONTEXT_KEY} service property.
26
 * </p>
37
 * </p>
27
 * 
38
 * 
28
 * @see IEclipseContext#set(String, Object)
39
 * @see IEclipseContext#set(String, Object)
29
 * @noimplement This interface is not intended to be implemented by clients. Function
40
 * @noimplement This interface is not intended to be implemented by clients.
30
 * implementations must subclass {@link ContextFunction} instead.
41
 *              Function implementations must subclass {@link ContextFunction}
42
 *              instead.
31
 */
43
 */
32
public interface IContextFunction {
44
public interface IContextFunction {
33
	/**
45
	/**
34
	 * Evaluates the function based on the provided arguments and context to produce
46
	 * The OSGi service name for a context function service. This name can be
35
	 * a consistent result.
47
	 * used to obtain instances of the service.
48
	 * 
49
	 * @see BundleContext#getServiceReference(String)
50
	 */
51
	public static final String SERVICE_NAME = IContextFunction.class.getName();
52
53
	/**
54
	 * An OSGi service property used to indicate the context key this function
55
	 * should be registered in.
56
	 * 
57
	 * @see BundleContext#getServiceReference(String)
58
	 */
59
	public static final String SERVICE_CONTEXT_KEY = "service.context.key"; //$NON-NLS-1$
60
61
	/**
62
	 * Evaluates the function based on the provided arguments and context to
63
	 * produce a consistent result.
36
	 * 
64
	 * 
37
	 * @param context The context in which to perform the value computation.
65
	 * @param context
38
	 * @param arguments The arguments required to compute the value, or
66
	 *            The context in which to perform the value computation.
39
	 * <code>null</code> if no arguments are applicable
67
	 * @param arguments
68
	 *            The arguments required to compute the value, or
69
	 *            <code>null</code> if no arguments are applicable
40
	 * @return The concrete value.
70
	 * @return The concrete value.
41
	 */
71
	 */
42
	public Object compute(IEclipseContext context, Object[] arguments);
72
	public Object compute(IEclipseContext context, Object[] arguments);
(-)plugin.xml (-2 lines)
Lines 1-7 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
1
<?xml version="1.0" encoding="UTF-8"?>
2
<?eclipse version="3.2"?>
2
<?eclipse version="3.2"?>
3
<plugin>
3
<plugin>
4
   <extension-point id="org.eclipse.e4.services" name="E4 Services" schema="schema/org.eclipse.e4.services.exsd"/>
5
   <extension-point id="org.eclipse.e4.languages" name="E4 Languages" schema="schema/org.eclipse.e4.languages.exsd"/>
4
   <extension-point id="org.eclipse.e4.languages" name="E4 Languages" schema="schema/org.eclipse.e4.languages.exsd"/>
6
7
</plugin>
5
</plugin>

Return to bug 281148