Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 742927c9e0a867c2787e1015fb102f6b5960c537 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.ds;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import org.eclipse.equinox.ds.model.ComponentDescription;
import org.eclipse.equinox.ds.model.ComponentDescriptionCache;
import org.eclipse.equinox.ds.parser.XMLParserNotAvailableException;
import org.eclipse.equinox.ds.resolver.Resolver;
import org.eclipse.equinox.ds.tracker.BundleTracker;
import org.eclipse.equinox.ds.tracker.BundleTrackerCustomizer;
import org.eclipse.equinox.ds.workqueue.WorkDispatcher;
import org.eclipse.equinox.ds.workqueue.WorkQueue;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.SynchronousBundleListener;
import org.osgi.service.log.LogService;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;

/**
 * 
 * Main class for the SCR. This class will start the SCR bundle and begin
 * processing other bundles.
 * 
 * @version $Revision: 1.5 $
 */
public class Activator implements BundleActivator, BundleTrackerCustomizer, WorkDispatcher, SynchronousBundleListener {

	public BundleContext context;
	private FrameworkHook framework;
	private ComponentDescriptionCache cache;
	private BundleTracker bundleTracker;
	public Resolver resolver;
	private ServiceTracker packageAdminTracker;
	public WorkQueue workQueue;

	private Hashtable bundleToComponentDescriptions;
	private Hashtable bundleToLastModified;

	/**
	 * New Service Components (ComponentDescription)s are added
	 */
	public static final int ADD = 1;

	/**
	 * Start the SCR bundle.
	 * 
	 * @param bundleContext BundleContext for SCR implementation.
	 */
	public void start(BundleContext bundleContext) {
		this.context = bundleContext;
		framework = new FrameworkHook();
		Log.init(context);
		cache = new ComponentDescriptionCache(this);
		bundleToComponentDescriptions = new Hashtable();
		bundleToLastModified = new Hashtable();

		packageAdminTracker = new ServiceTracker(context, PackageAdmin.class.getName(), null);
		packageAdminTracker.open();

		//notify this object when bundles enter (or exit) the Bundle.ACTIVE state
		bundleTracker = new BundleTracker(context, Bundle.ACTIVE, this);

		workQueue = new WorkQueue("SCR Work Queue"); //$NON-NLS-1$
		workQueue.setDaemon(true); // make sure the work queue is daemon
		resolver = new Resolver(this);
		workQueue.start();
		bundleContext.addBundleListener(this); //track UNRESOLVED events
		bundleTracker.open();
	}

	/**
	 * Stop the SCR bundle.
	 * 
	 * @param bundleContext BundleContext for SCR implementation.
	 */
	public void stop(BundleContext bundleContext) {

		bundleTracker.close();

		//process all remaining events in queue and then shut it down
		workQueue.closeAndJoin();

		resolver.dispose();
		packageAdminTracker.close();

		//shut down cache (write to disk)
		cache.dispose();

		Log.dispose();

		cache = null;
		framework = null;
		resolver = null;
		bundleToComponentDescriptions = null;
		bundleToLastModified = null;
		this.context = null;
	}

	/**
	 * Returns the bundle's Service-Component manifest header.  If the bundle
	 * has header, then the bundle will be tracked. If not, null is returned 
	 * and the bundle will not be tracked.
	 * 
	 * If the bundle contains service components, parse the service component xml 
	 * file(s) and create an {@link ComponentDescription ComponentDescription} object for every service 
	 * component in the bundle.  Add the {@link ComponentDescription ComponentDescriptions} to 
	 * the queue to be sent to the resolver.
	 * 
	 * @param bundle Candidate bundle to be tracked.
	 * @return the bundle's Service-Component manifest header or null if the 
	 * bundle does not specify the header.
	 */
	public Object addingBundle(Bundle bundle) {

		List enableComponentDescriptions = new ArrayList();

		PackageAdmin packageAdmin = (PackageAdmin) packageAdminTracker.getService();
		if (packageAdmin.getBundleType(bundle) != 0) {
			return null; // don't process fragments.
		}

		Long bundleID = new Long(bundle.getBundleId());

		// get the bundle's last modified date
		Long bundleLastModified = new Long(bundle.getLastModified());

		// get the last saved value for the bundle's last modified date
		Long bundleOldLastModified = (Long) bundleToLastModified.get(bundleID);

		try {
			// compare the two and if changed ( or if first time ) update the maps
			if ((!bundleLastModified.equals(bundleOldLastModified)) || (bundleOldLastModified == null)) {

				// get the BundleContext for this bundle (framework impl dependent)
				BundleContext bundleContext = framework.getBundleContext(bundle);

				// get all ComponentDescriptions for this bundle
				// throws XMLParserNotAvailableException if no XML parser
				List componentDescriptions = cache.getComponentDescriptions(bundleContext);

				// update map of bundle to ComponentDescriptions
				bundleToComponentDescriptions.put(bundleID, componentDescriptions);

				// update bundle:lastModifiedDate map
				bundleToLastModified.put(bundleID, bundleLastModified);

				// for each CD in bundle set enable flag based on autoenable

				Iterator it = componentDescriptions.iterator();
				while (it.hasNext()) {
					ComponentDescription componentDescription = (ComponentDescription) it.next();
					validate(componentDescription);
					if (componentDescription.isAutoenable() && componentDescription.isValid()) {
						componentDescription.setEnabled(true);
						enableComponentDescriptions.add(componentDescription);
					}
				}
			}

			// publish all CDs to be enabled, to resolver (add to the workqueue and
			// publish event)
			if (!enableComponentDescriptions.isEmpty()) {
				workQueue.enqueueWork(this, ADD, enableComponentDescriptions);
			}

		} catch (XMLParserNotAvailableException e) {
			//cache.getComponentDescriptions(bundleContext); threw this exception
			//because we needed an XML parser and we didn't have one
			//when an XML parser becomes available this method will be called again
			//log a message but still return the bundleId so this bundle will be tracked
			Log.log(LogService.LOG_INFO, "Declarative Services waiting for XML parser for bundle " + bundleID);
		}
		return bundleID;
	}

	/**
	 * Empty implementation. No work is needed for modifiedBundle.
	 * 
	 * @param bundle
	 * @param object
	 */
	public void modifiedBundle(Bundle bundle, Object object) {

		Long bundleID = new Long(bundle.getBundleId());

		// flush map
		bundleToComponentDescriptions.remove(bundleID);

		// flush map
		bundleToLastModified.remove(bundleID);

	}

	/**
	 * A bundle is going to an in-ACTIVE state.  Dispose and remove it's service
	 * components from the system.  Disposal is done synchronously so all of the 
	 * service components have been disposed before this method returns.
	 * 
	 * @param bundle Bundle becoming untracked.
	 * @param object Value returned by addingBundle.
	 */

	public void removedBundle(Bundle bundle, Object object) {

		List disableComponentDescriptions = new ArrayList();
		Long bundleID = new Long(bundle.getBundleId());

		// get CD's for this bundle
		List ComponentDescriptions = (List) bundleToComponentDescriptions.get(new Long(bundle.getBundleId()));
		if (ComponentDescriptions != null) {
			Iterator it = ComponentDescriptions.iterator();

			// for each CD in bundle
			while (it.hasNext()) {
				ComponentDescription ComponentDescription = (ComponentDescription) it.next();

				// check if enabled && satisfied
				if ((ComponentDescription.isEnabled())) {

					// add to disabled list
					disableComponentDescriptions.add(ComponentDescription);

					// mark disabled
					ComponentDescription.setEnabled(false);
				}
			}
		}

		// remove the bundle from the lists/maps
		bundleToComponentDescriptions.remove(bundleID);
		bundleToLastModified.remove(bundleID);

		resolver.disableComponents(disableComponentDescriptions);
		return;
	}

	/**
	 * Called by Service Component code via ComponentContext
	 * 
	 * Enable the component(s) and put them on the queue for the resolver.
	 * 
	 * @param name The name of a component or <code>null</code> to indicate
	 *        all components in the bundle.
	 * 
	 * @param bundle The bundle which contains the Service Component to be
	 *        enabled
	 */
	public void enableComponent(String name, Bundle bundle) {

		// get all ComponentDescriptions for this bundle
		List componentDescriptions = (List) bundleToComponentDescriptions.get(new Long(bundle.getBundleId()));

		// Create the list of CD's to be enabled
		List enableCDs = new ArrayList();

		if (componentDescriptions != null) {
			Iterator it = componentDescriptions.iterator();

			// for each CD in list
			while (it.hasNext()) {
				ComponentDescription componentDescription = (ComponentDescription) it.next();
				validate(componentDescription);

				// if name is null then enable ALL Component Descriptions in
				// this bundle
				if (name == null) {

					// if CD is valid and is disabled then enable it
					if (componentDescription.isValid() && !componentDescription.isEnabled()) {

						// add to list of CDs to enable
						enableCDs.add(componentDescription);

						// set CD enabled
						componentDescription.setEnabled(true);
					}
				} else {
					if (componentDescription.getName().equals(name)) {

						// if CD is valid and is disabled then enable it
						if (componentDescription.isValid() && !componentDescription.isEnabled()) {

							// add to list of CDs to enable
							enableCDs.add(componentDescription);

							// set CD enabled
							componentDescription.setEnabled(true);
						}
					}
				}
				// else it is either not valid or it is already enabled - do
				// nothing
			}
		}

		// publish to resolver the list of CDs to enable
		if (!enableCDs.isEmpty())
			workQueue.enqueueWork(this, ADD, enableCDs);
	}

	/**
	 * Disable a Service Component - The specified component name must be in the 
	 * bundle as this component. Called by Service Component via ComponentContext.
	 * 
	 * Synchronously disable the component.  All component configurations (component configurations)
	 * are disposed before this method returns.
	 * 
	 * @param name The name of a component to disable
	 * 
	 * @param bundle The bundle which contains the Service Component to be
	 *        disabled
	 */

	public void disableComponent(String name, Bundle bundle) {

		List disableComponentDescriptions = new ArrayList();

		// Get the list of CDs for this bundle
		List componentDescriptionsList = (List) bundleToComponentDescriptions.get(new Long(bundle.getBundleId()));

		if (componentDescriptionsList != null) {
			Iterator it = componentDescriptionsList.iterator();

			// for each ComponentDescription in list
			while (it.hasNext()) {
				ComponentDescription componentDescription = (ComponentDescription) it.next();

				// find the ComponentDescription with the specified name
				if (componentDescription.getName().equals(name)) {

					// if enabled then add to disabled list and mark disabled
					if (componentDescription.isEnabled()) {

						disableComponentDescriptions.add(componentDescription);

						componentDescription.setEnabled(false);

					}
				}
			}
		}

		// publish to resolver the list of CDs to disable
		resolver.disableComponents(disableComponentDescriptions);
		return;
	}

	/**
	 * Put a job on the work queue to be done later (asynchronously) by the work 
	 * queue thread.
	 * @param workAction currently only valid value is Main.ADD
	 * @param workObject work object to be acted upon
	 * @see org.eclipse.equinox.ds.workqueue.WorkDispatcher#dispatchWork(int,
	 *      java.lang.Object)
	 */
	public void dispatchWork(int workAction, Object workObject) {

		List descriptions;
		descriptions = (List) workObject;
		switch (workAction) {
			case ADD :
				resolver.enableComponents(descriptions);
				break;
		}
	}

	/**
	 * Validate the Component Description
	 * 
	 * If error is found log and throw exception.
	 * 
	 * @param cd to be validated
	 * @throws Throwable if fatal problem is found
	 */
	private void validate(ComponentDescription componentDescription) {

		if ((componentDescription.getFactory() != null) && (componentDescription.getService() != null) && (componentDescription.getService().isServicefactory())) {
			componentDescription.setValid(false);
			Log.log(1, "validate cd: ", new Throwable("invalid to specify both ComponentFactory and ServiceFactory"));
		} else if ((componentDescription.isImmediate()) && (componentDescription.getService() != null) && (componentDescription.getService().isServicefactory())) {
			componentDescription.setValid(false);
			Log.log(1, "validate cd: ", new Throwable("invalid to specify both immediate and ServiceFactory"));
		} else if ((componentDescription.isImmediate()) && (componentDescription.getFactory() != null)) {
			componentDescription.setValid(false);
			Log.log(1, "validate cd: ", new Throwable("invalid to specify both immediate and ComponentFactory"));
		} else if ((!componentDescription.isImmediate()) && (componentDescription.getService() == null) && (componentDescription.getFactory() == null)) {
			componentDescription.setValid(false);
			Log.log(1, "validate cd: ", new Throwable("invalid set immediate to false and provide no Service"));
		} else {
			componentDescription.setValid(true);
		}

	}

	/**
	 * SynchronousBundleListener - when a bundle we are tracking is UNRESOLVED, 
	 * clear it's reflection cache.
	 */
	public void bundleChanged(BundleEvent event) {
		if (event.getType() == BundleEvent.UNRESOLVED) {

			// get cached ComponentDescriptions for this bundle, if any
			List componentDescriptions = cache.getCachedComponentDescriptions(event.getBundle());

			Iterator itr = componentDescriptions.iterator();
			while(itr.hasNext()) {
				ComponentDescription componentDescription = (ComponentDescription)itr.next();

				componentDescription.clearReflectionMethods();
			}
		} //end if event type == UNRESOLVED
	}

}

Back to the top