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

package org.eclipse.equinox.internal.transforms;

import java.util.HashMap;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
import org.eclipse.osgi.internal.framework.EquinoxContainer;
import org.eclipse.osgi.internal.log.EquinoxLogServices;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;

/**
 * A dynamic list of transformers.
 */
public class TransformerList extends ServiceTracker<Object, Object> {

	/**
	 * The stale state of this list.  Set to true every time a new matching service instance is detected.
	 */
	private volatile boolean stale = true;

	/**
	 * Local cache of transformers.
	 */
	private HashMap<String, StreamTransformer> transformers = new HashMap<String, StreamTransformer>();
	private final EquinoxLogServices logServices;

	/**
	 * Create a new instance of this list.
	 * @param context the context to track
	 * @throws InvalidSyntaxException thrown if there's an issue listening for changes to the given transformer type
	 */
	public TransformerList(BundleContext context, EquinoxLogServices logServices) throws InvalidSyntaxException {
		super(context, context.createFilter("(&(objectClass=" //$NON-NLS-1$
				+ Object.class.getName() + ")(" + TransformTuple.TRANSFORMER_TYPE //$NON-NLS-1$
				+ "=*))"), null); //$NON-NLS-1$
		this.logServices = logServices;
		open();
	}

	/**
	 * Return the transformer of the given type being monitored by this list.  
	 * If the list is stale it will first be rebuilt.
	 * @param type the type of transformer
	 * @return the transformer or null if no transformer of the given type is available.
	 */
	public synchronized StreamTransformer getTransformer(String type) {
		if (stale) {
			rebuildTransformersMap();
		}
		return transformers.get(type);
	}

	public synchronized boolean hasTransformers() {
		if (stale) {
			rebuildTransformersMap();
		}
		return transformers.size() > 0;
	}

	/**
	 * Consults the bundle context for services of the transformer type and builds the internal cache.
	 */
	private void rebuildTransformersMap() {
		transformers.clear();
		ServiceReference<Object>[] serviceReferences = getServiceReferences();
		stale = false;
		if (serviceReferences == null)
			return;

		for (int i = 0; i < serviceReferences.length; i++) {
			ServiceReference<Object> serviceReference = serviceReferences[i];
			String type = serviceReference.getProperty(TransformTuple.TRANSFORMER_TYPE).toString();
			if (type == null || transformers.get(type) != null)
				continue;
			Object object = getService(serviceReference);
			if (object instanceof StreamTransformer)
				transformers.put(type, (StreamTransformer) object);
			else {
				ProxyStreamTransformer transformer;
				try {
					transformer = new ProxyStreamTransformer(object);
					transformers.put(type, transformer);
				} catch (SecurityException e) {
					logServices.log(EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Problem creating transformer", e); //$NON-NLS-1$
				} catch (NoSuchMethodException e) {
					logServices.log(EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Problem creating transformer", e); //$NON-NLS-1$
				}
			}
		}
	}

	public Object addingService(ServiceReference<Object> reference) {
		try {
			return super.addingService(reference);
		} finally {
			stale = true;
		}
	}

	public void modifiedService(ServiceReference<Object> reference, Object service) {
		super.modifiedService(reference, service);
		stale = true;
	}

	public void removedService(ServiceReference<Object> reference, Object service) {
		super.removedService(reference, service);
		stale = true;
	}
}

Back to the top