Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a018f01e1a5e43a107b36e628d6c45d3569f78cf (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
 * 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.io.IOException;
import java.net.URL;
import java.util.*;
import org.eclipse.osgi.internal.log.EquinoxLogServices;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;

/**
 * Class that represents a dynamic list of TransformTuples that have been registered against a particular transform type.
 */
public class TransformInstanceListData extends ServiceTracker<URL, URL> {
	/**
	 * Used when there are no transform data types
	 */
	private static final String[] EMPTY_TYPES = new String[0];
	/**
	 * Stale state of the transform list. Set to true whenever one of the ServiceTrackerCustomization methods are invoked.
	 */
	private volatile boolean stale = true;

	/**
	 * Map from transformer class -> tuple array
	 */
	private Map<String, TransformTuple[]> transformerToTuple = new HashMap<String, TransformTuple[]>();

	/**
	 * List of all tuples in the system.
	 */
	private List<TransformTuple> rawTuples = new ArrayList<TransformTuple>();

	/**
	 * Map from bundle ID -> boolean representing whether or not a given bundle currently has any transforms registered against it.
	 */
	private Map<String, Boolean> bundleIdToTransformPresence = new HashMap<String, Boolean>();
	private final EquinoxLogServices logServices;

	/**
	 * Create a new transform list bound to the given context. If new transforms are registered against the given context the contents of this list will change.
	 * @param context the bundle context
	 * @param logServices 
	 * @throws InvalidSyntaxException thrown if there's an issue listening for changes to the given transformer type
	 */
	public TransformInstanceListData(BundleContext context, EquinoxLogServices logServices) throws InvalidSyntaxException {
		super(context, context.createFilter("(&(objectClass=" //$NON-NLS-1$
				+ URL.class.getName() + ")(" + TransformTuple.TRANSFORMER_TYPE //$NON-NLS-1$
				+ "=*))"), null); //$NON-NLS-1$
		this.logServices = logServices;
		open();
	}

	/**
	 * Return the transforms types currently held by this list. If a change has been detected since the last request this list will be rebuilt.
	 * @return the transforms types currently held by this list
	 */
	public synchronized String[] getTransformTypes() {
		if (stale)
			rebuildTransformMap();

		if (transformerToTuple.size() == 0)
			return EMPTY_TYPES;
		return transformerToTuple.keySet().toArray(new String[transformerToTuple.size()]);
	}

	/**
	 * Return the transforms of a particular type currently held by this list. If a change has been detected since the last request this list will be rebuilt.
	 * @return the transforms currently held by this list
	 */
	public synchronized TransformTuple[] getTransformsFor(String type) {
		if (stale)
			rebuildTransformMap();

		return transformerToTuple.get(type);
	}

	/**
	 * Return whether or not there are any transforms who's bundle pattern matches the ID of the provided bundle. Only transforms with a present transform handler are considered during the invocation of this method.
	 * @param bundle the bundle to test
	 * @return the presence of associated transforms.
	 */
	public synchronized boolean hasTransformsFor(Bundle bundle) {
		if (stale)
			rebuildTransformMap();

		String bundleName = bundle.getSymbolicName();
		Boolean hasTransformsFor = bundleIdToTransformPresence.get(bundleName);

		if (hasTransformsFor == null) {
			hasTransformsFor = Boolean.FALSE;
			for (Iterator<TransformTuple> i = rawTuples.iterator(); i.hasNext();) {
				TransformTuple tuple = i.next();
				if (tuple.bundlePattern.matcher(bundleName).matches()) {
					hasTransformsFor = Boolean.TRUE;
				}
			}

			bundleIdToTransformPresence.put(bundleName, hasTransformsFor);
		}

		return hasTransformsFor.booleanValue();
	}

	/**
	 * Consults the bundle context for services of the transformer type type and builds the internal cache.
	 */
	private void rebuildTransformMap() {
		transformerToTuple.clear();
		rawTuples.clear();
		bundleIdToTransformPresence.clear();

		ServiceReference<URL>[] serviceReferences = getServiceReferences();
		stale = false;
		if (serviceReferences == null)
			return;

		for (int i = 0; i < serviceReferences.length; i++) {
			ServiceReference<URL> serviceReference = serviceReferences[i];
			String type = serviceReference.getProperty(TransformTuple.TRANSFORMER_TYPE).toString();

			URL url = getService(serviceReference);
			TransformTuple[] transforms;
			try {
				transforms = CSVParser.parse(url, logServices);
				TransformTuple[] existing = transformerToTuple.get(type);
				if (existing != null) {
					TransformTuple[] newTransforms = new TransformTuple[existing.length + transforms.length];
					System.arraycopy(existing, 0, newTransforms, 0, existing.length);
					System.arraycopy(transforms, 0, newTransforms, existing.length, transforms.length);
					transformerToTuple.put(type, newTransforms);
				} else
					transformerToTuple.put(type, transforms);

				for (int j = 0; j < transforms.length; j++) {
					rawTuples.add(transforms[j]);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

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

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

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

Back to the top