Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1cd041cc9df30e689b992b0a09528eb579fc5825 (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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
/*******************************************************************************
 * Copyright (c) 1997-2011 by ProSyst Software GmbH
 * http://www.prosyst.com
 * 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:
 *    ProSyst Software GmbH - initial API and implementation
 *    Simon Archer 		    - bug.id = 225624 
 *    Bryan Hunt 		    - bug.id = 275997 
 *    Lazar Kirchev		 	- bug.id = 320377
 *******************************************************************************/
package org.eclipse.equinox.internal.ds.model;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import org.apache.felix.scr.Component;
import org.apache.felix.scr.Reference;
import org.eclipse.equinox.internal.ds.*;
import org.eclipse.equinox.internal.ds.impl.ReadOnlyDictionary;
import org.eclipse.equinox.internal.util.io.Externalizable;
import org.eclipse.equinox.internal.util.io.ExternalizableDictionary;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.*;
import org.osgi.service.component.*;
import org.osgi.service.log.LogService;

/**
 * This is an OO wrapper for the XML representing the components. It also caches
 * the activate and deactivate methods of the component.
 * 
 * @author Valentin Valchev
 * @author Pavlin Dobrev
 * @author Stoyan Boshev
 */
public class ServiceComponent implements Externalizable, Component {

	//constants defining possible SCR namespaces XML schemas
	public static final int NAMESPACE_1_0 = 0;
	public static final int NAMESPACE_1_1 = 1;
	public static final int NAMESPACE_1_2 = 2;

	public static final String CONF_POLICY_OPTIONAL = "optional"; //$NON-NLS-1$
	public static final String CONF_POLICY_REQUIRE = "require"; //$NON-NLS-1$
	public static final String CONF_POLICY_IGNORE = "ignore"; //$NON-NLS-1$

	public Vector componentProps = null;

	// --- begin: XML def
	public String name;
	public String factory;
	String implementation; // the class name
	Properties properties; // property or properties
	String configurationPolicy = CONF_POLICY_OPTIONAL;
	String activateMethodName = "activate"; //$NON-NLS-1$
	String deactivateMethodName = "deactivate"; //$NON-NLS-1$
	public String modifyMethodName = ""; //$NON-NLS-1$

	//Since DS 1.2
	public String configurationPID;

	// service
	public Vector serviceInterfaces; // all strings
	public String[] provides; // the same as above, but as String[]
	public boolean serviceFactory = false;

	public Vector references; // ComponentReference

	public boolean autoenable = true;
	public boolean immediate = false;
	public int namespace = NAMESPACE_1_0;
	// --- end: XML def

	// --- begin: cache
	private boolean activateCached = false;
	private boolean deactivateCached = false;
	private boolean modifyCached = false;
	private Method activateMethod;
	private Method deactivateMethod;
	private Method modifyMethod;
	// --- end: cache

	// --- begin: model
	public boolean enabled;
	public Bundle bundle;
	public BundleContext bc;
	boolean activateMethodDeclared = false;
	boolean deactivateMethodDeclared = false;
	int state = Component.STATE_UNSATISFIED;
	// --- end: model

	public HashSet componentIssues = new HashSet(1, 1);
	private ReadOnlyDictionary readOnlyProps;

	public String getComponentIssues() {
		if (!componentIssues.isEmpty()) {
			String result = ""; //$NON-NLS-1$
			Object[] issues = componentIssues.toArray();
			for (int i = 0; i < issues.length; i++) {
				result += issues[i] + "\n"; //$NON-NLS-1$
			}
			return result;
		}
		return null;
	}

	private static final Class ACTIVATE_METHODS_PARAMETERS[] = new Class[] {ComponentContext.class};

	public ServiceComponent() {
		//
	}

	private final Method getMethod(Object instance, String methodName, boolean isActivate) throws Exception {
		if (Activator.DEBUG) {
			Activator.log.debug("ServiceComponent.getMethod(): " + methodName, null); //$NON-NLS-1$
		}
		Method method = null;
		int methodPriority = Integer.MAX_VALUE;
		Class clazz = instance != null ? instance.getClass() : null;

		while (method == null && clazz != null) {
			if (isNamespaceAtLeast11()) {
				Method[] methods = clazz.getDeclaredMethods();
				for (int i = 0; i < methods.length; i++) {
					if (methods[i].getName().equals(methodName)) {
						Class[] params = methods[i].getParameterTypes();
						boolean accepted = true;
						for (int j = 0; j < params.length; j++) {
							if (params[j] == ComponentContext.class || params[j] == BundleContext.class || params[j] == Map.class) {
								// correct parameter for both activate and deactivate methods
							} else if (!isActivate && (params[j] == Integer.class || params[j] == int.class)) {
								//we are checking int/Integer as special deactivate parameters

							} else {
								//the parameter is not recognized
								accepted = false;
								break;
							}
						}
						if (accepted && SCRUtil.checkMethodAccess(instance.getClass(), clazz, methods[i], true)) {
							//check if the newly accepted method has higher priority than the previous one and use it
							int prio = getMethodPriority(params);
							if (prio < methodPriority) {
								//found a method with a higher priority
								method = methods[i];
								methodPriority = prio;
							}
						}
					}
				}
			} else {
				try {
					method = clazz.getDeclaredMethod(methodName, ACTIVATE_METHODS_PARAMETERS);
					if (method != null) {
						if (!SCRUtil.checkMethodAccess(instance.getClass(), clazz, method, false)) {
							//the method is not accessible. Stop the search
							Activator.log(bc, LogService.LOG_WARNING, NLS.bind(Messages.METHOD_UNACCESSABLE, methodName, clazz), null);
							componentIssues.add(NLS.bind(Messages.METHOD_UNACCESSABLE, methodName, clazz));
							method = null;
							break;
						}
					}
				} catch (NoSuchMethodException e) {
					// the method activate/deactivate may not exist in the component implementation class
				}
			}

			if (method != null)
				break;

			// search for the method in the parent classes!
			clazz = clazz.getSuperclass();
		}
		if (method != null) {
			int modifiers = method.getModifiers();
			if (!Modifier.isPublic(modifiers)) {
				SCRUtil.setAccessible(method);
			}
		}
		return method;
	}

	private int getMethodPriority(Class[] params) {
		int priority = Integer.MAX_VALUE;
		if (params.length == 1) {
			if (params[0] == ComponentContext.class) {
				priority = 0; //highest priority
			} else if (params[0] == BundleContext.class) {
				priority = 1;
			} else if (params[0] == Map.class) {
				priority = 2;
			} else if (params[0] == int.class) {
				priority = 3;
			} else if (params[0] == Integer.class) {
				priority = 4;
			}
		} else if (params.length >= 2) {
			priority = 5;
		} else if (params.length == 0) {
			priority = 6;
		}
		return priority;
	}

	void activate(Object instance, ComponentContext context) throws ComponentException {
		try {
			if (isNamespaceAtLeast11()) {
				if (!activateCached) {
					activateCached = true;
					activateMethod = getMethod(instance, activateMethodName, true);
				}
				// invoke the method if any
				if (activateMethod != null) {
					Class[] paramTypes = activateMethod.getParameterTypes();
					Object[] params = null;
					if (paramTypes.length == 1) {
						params = SCRUtil.getObjectArray();
					} else {
						params = new Object[paramTypes.length];
					}
					for (int i = 0; i < params.length; i++) {
						if (paramTypes[i] == ComponentContext.class) {
							params[i] = context;
						} else if (paramTypes[i] == BundleContext.class) {
							params[i] = context.getBundleContext();
						} else if (paramTypes[i] == Map.class) {
							params[i] = context.getProperties();
						}
					}

					try {
						activateMethod.invoke(instance, params);
					} finally {
						if (params.length == 1) {
							SCRUtil.release(params);
						}
					}
				} else {
					if (activateMethodName != "activate") { //$NON-NLS-1$
						//the activate method is specified in the component description XML by the user.
						//It is expected to find it in the implementation class
						componentIssues.add("Can not activate instance of component " + this.implementation + ". The specified activate method [" + activateMethodName + "] was not found."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
						throw new ComponentException(NLS.bind(Messages.SPECIFIED_ACTIVATE_METHOD_NOT_FOUND, instance, this));
					}
				}
			} else {
				// retrieve the activate method from cache
				if (!activateCached) {
					activateCached = true;
					activateMethod = getMethod(instance, "activate", true); //$NON-NLS-1$
				}
				// invoke the method if any
				if (activateMethod != null) {
					Object[] params = SCRUtil.getObjectArray();
					params[0] = context;
					try {
						activateMethod.invoke(instance, params);
					} finally {
						SCRUtil.release(params);
					}
				}
			}
		} catch (Throwable t) {
			if (t instanceof ComponentException) {
				throw (ComponentException) t;
			}
			String cause = t.toString();
			if (t instanceof InvocationTargetException) {
				cause = t.getCause().toString();
			}
			componentIssues.add("Can not activate instance of component " + this.implementation + ". The activation throws: " + cause); //$NON-NLS-1$ //$NON-NLS-2$ 
			throw new ComponentException(NLS.bind(Messages.EXCEPTION_ACTIVATING_INSTANCE, instance, name), t);
			// rethrow exception so resolver is eventually notified that
			// the processed SCP is bad
		}
	}

	void modified(Object instance, ComponentContext context) throws ComponentException {
		try {
			if (isNamespaceAtLeast11()) {
				if (!modifyCached) {
					modifyCached = true;
					if (modifyMethodName != "") { //$NON-NLS-1$
						modifyMethod = getMethod(instance, modifyMethodName, true);
					}
				}
				// invoke the method if any
				if (modifyMethod != null) {
					Class[] paramTypes = modifyMethod.getParameterTypes();
					Object[] params = null;
					if (paramTypes.length == 1) {
						params = SCRUtil.getObjectArray();
					} else {
						params = new Object[paramTypes.length];
					}
					for (int i = 0; i < params.length; i++) {
						if (paramTypes[i] == ComponentContext.class) {
							params[i] = context;
						} else if (paramTypes[i] == BundleContext.class) {
							params[i] = context.getBundleContext();
						} else if (paramTypes[i] == Map.class) {
							params[i] = context.getProperties();
						}
					}

					try {
						modifyMethod.invoke(instance, params);
					} finally {
						if (params.length == 1) {
							SCRUtil.release(params);
						}
					}
				} else {
					if (modifyMethodName != "") { //$NON-NLS-1$
						//the modify method is specified in the component description XML by the user.
						//It is expected to find it in the implementation class
						throw new ComponentException(NLS.bind(Messages.CANNOT_MODIFY_INSTANCE__MODIFY_METHOD_NOT_FOUND, instance, this));
					}
				}
			}
		} catch (Throwable t) {
			if (t instanceof ComponentException) {
				throw (ComponentException) t;
			}
			Activator.log(bc, LogService.LOG_ERROR, NLS.bind(Messages.EXCEPTION_MODIFYING_COMPONENT, instance, this), t);
		}
	}

	void deactivate(Object instance, ComponentContext context, int deactivateReason) {
		try {
			if (isNamespaceAtLeast11()) {
				if (!deactivateCached) {
					deactivateCached = true;
					deactivateMethod = getMethod(instance, deactivateMethodName, false);
				}
				// invoke the method if any
				if (deactivateMethod != null) {
					Class[] paramTypes = deactivateMethod.getParameterTypes();
					Object[] params = null;
					if (paramTypes.length == 1) {
						params = SCRUtil.getObjectArray();
					} else {
						params = new Object[paramTypes.length];
					}
					for (int i = 0; i < params.length; i++) {
						if (paramTypes[i] == ComponentContext.class) {
							params[i] = context;
						} else if (paramTypes[i] == BundleContext.class) {
							params[i] = context.getBundleContext();
						} else if (paramTypes[i] == Map.class) {
							params[i] = context.getProperties();
						} else if (paramTypes[i] == int.class) {
							params[i] = new Integer(deactivateReason);
						} else if (paramTypes[i] == Integer.class) {
							params[i] = new Integer(deactivateReason);
						}
					}

					try {
						deactivateMethod.invoke(instance, params);
					} finally {
						if (params.length == 1) {
							SCRUtil.release(params);
						}
					}
				} else {
					if (deactivateMethodName != "deactivate") { //$NON-NLS-1$
						//the deactivate method is specified in the component description XML by the user.
						//It is expected to find it in the implementation class
						Activator.log(bc, LogService.LOG_ERROR, NLS.bind(Messages.SPECIFIED_DEACTIVATE_METHOD_NOT_FOUND, instance, this), null);
					}
				}
			} else {
				// retrieve the activate method from cache
				if (!deactivateCached) {
					deactivateCached = true;
					deactivateMethod = getMethod(instance, "deactivate", false); //$NON-NLS-1$
				}
				// invoke the method
				if (deactivateMethod != null) {
					Object[] params = SCRUtil.getObjectArray();
					params[0] = context;
					try {
						deactivateMethod.invoke(instance, params);
					} finally {
						SCRUtil.release(params);
					}
				}
			}
		} catch (Throwable t) {
			Activator.log(bc, LogService.LOG_ERROR, NLS.bind(Messages.ERROR_DEACTIVATING_INSTANCE, this), t);
		}
	}

	/**
	 * This method is called from the XML parser to validate the component once
	 * it is fully loaded!
	 * 
	 * @param line
	 *            the line at which the the component definition ends
	 * @param _namespace specify the namespace of the component according to XML SCR schema
	 */
	void validate(int line, int _namespace) {
		//		System.out.println("Validating component " + name + " with namespace " + (namespace11 ? "1.1" : "1.0"));
		this.namespace = _namespace;
		if (name == null) {
			if (isNamespaceAtLeast11()) {
				name = implementation;
			} else {
				throw new IllegalArgumentException(NLS.bind(Messages.NO_NAME_ATTRIBUTE, Integer.toString(line)));
			}
		}
		if (isNamespaceAtLeast11()) {
			if (!(configurationPolicy == CONF_POLICY_OPTIONAL || configurationPolicy == CONF_POLICY_REQUIRE || configurationPolicy == CONF_POLICY_IGNORE)) {
				throw new IllegalArgumentException(NLS.bind(Messages.INCORRECT_ACTIVATION_POLICY, name, Integer.toString(line)));
			}
		}

		if (isNamespaceAtLeast12()) {
			if (configurationPID == null) {
				configurationPID = name;
			}
		}

		if (implementation == null) {
			throw new IllegalArgumentException(NLS.bind(Messages.NO_IMPLEMENTATION_ATTRIBUTE, name, Integer.toString(line)));
		}

		// component factory is incompatible with service factory
		if (factory != null && serviceFactory) {
			throw new IllegalArgumentException(NLS.bind(Messages.INVALID_COMPONENT_FACTORY_AND_SERVICE_FACTORY, name));
		}

		if (immediate) {
			if (serviceFactory)
				throw new IllegalArgumentException(NLS.bind(Messages.INVALID_COMPONENT_IMMEDIATE_AND_SERVICE_FACTORY, name));
			if (factory != null)
				throw new IllegalArgumentException(NLS.bind(Messages.INVALID_COMPONENT_IMMEDIATE_AND_FACTORY, name));
		} else {
			if ((serviceInterfaces == null) && (factory == null)) {
				throw new IllegalArgumentException(NLS.bind(Messages.INVALID_COMPONENT_NO_SERVICES_NO_IMMEDIATE, name));
			}
		}

		// make sure that references are also valid
		if (references != null) {
			for (int i = 0; i < references.size(); i++) {
				ComponentReference r = (ComponentReference) references.elementAt(i);
				if (r.name == null) {
					if (isNamespaceAtLeast11()) {
						r.name = r.interfaceName;
					} else {
						throw new IllegalArgumentException(NLS.bind(Messages.COMPONENT_HAS_ILLEGAL_REFERENCE, new Object[] {name, Integer.toString(line), r}));
					}
				}
				if (r.interfaceName == null || r.name.length() == 0 || r.interfaceName.length() == 0) {
					throw new IllegalArgumentException(NLS.bind(Messages.COMPONENT_HAS_ILLEGAL_REFERENCE, new Object[] {name, Integer.toString(line), r}));
				}
				for (int j = i + 1; j < references.size(); j++) {
					ComponentReference ref2 = (ComponentReference) references.elementAt(j);
					if (r.name.equals(ref2.name)) {
						throw new IllegalArgumentException(NLS.bind(Messages.DUPLICATED_REFERENCE_NAMES, name, Integer.toString(line)));
					}
				}
			}
		}

		// cache the service interfaces as String[] too.
		if (serviceInterfaces != null && !serviceInterfaces.isEmpty()) {
			provides = new String[serviceInterfaces.size()];
			serviceInterfaces.copyInto(provides);
		}

		// make sure that the component will get automatically enabled!
		enabled = autoenable;
	}

	/**
	 * This method will instantiate the implementation class!
	 * 
	 * @return instance of the component implementation class. If the components
	 *         exports some services, the implementation must implement all of
	 *         them
	 * @throws Exception
	 *             is thrown if the implementation cannot be instantiated for
	 *             some reasons.
	 */
	final Object createInstance() throws Exception {
		try {
			return bundle.loadClass(implementation).newInstance();
		} catch (Throwable t) {
			throw new ComponentException(NLS.bind(Messages.EXCEPTION_CREATING_COMPONENT_INSTANCE, this), t);
		}
	}

	/**
	 * This method will dispose everything
	 */
	// TODO : this method is not used - should be removed?
	public final void dispose() {

		activateCached = deactivateCached = false;
		activateMethod = deactivateMethod = null;

		enabled = false;
		bundle = null;
		// bc = null;

		if (references != null) {
			for (int i = 0; i < references.size(); i++) {
				ComponentReference ref = (ComponentReference) references.elementAt(i);
				ref.dispose();
			}
			references.removeAllElements();
			references = null;
		}

		if (properties != null) {
			properties.clear();
			properties = null;
		}

		if (serviceInterfaces != null) {
			serviceInterfaces.removeAllElements();
			serviceInterfaces = null;
		}
	}

	public boolean provides(String interfaceName) {
		return serviceInterfaces != null && serviceInterfaces.contains(interfaceName);
	}

	public String getConfigurationPID() {
		if (isNamespaceAtLeast12()) {
			return configurationPID;
		}
		return name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public final String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("Component["); //$NON-NLS-1$
		buffer.append("\n\tname = ").append(name); //$NON-NLS-1$
		if (isNamespaceAtLeast11()) {
			buffer.append("\n\tactivate = ").append(activateMethodName); //$NON-NLS-1$
			buffer.append("\n\tdeactivate = ").append(deactivateMethodName); //$NON-NLS-1$
			buffer.append("\n\tmodified = ").append(modifyMethodName); //$NON-NLS-1$
			buffer.append("\n\tconfiguration-policy = ").append(configurationPolicy); //$NON-NLS-1$
		}
		if (isNamespaceAtLeast12()) {
			buffer.append("\n\tconfiguration-pid = ").append(configurationPID); //$NON-NLS-1$
		}
		buffer.append("\n\tfactory = ").append(factory); //$NON-NLS-1$
		buffer.append("\n\tautoenable = ").append(autoenable); //$NON-NLS-1$
		buffer.append("\n\timmediate = ").append(immediate); //$NON-NLS-1$

		buffer.append("\n\timplementation = ").append(implementation); //$NON-NLS-1$
		buffer.append("\n\tstate = ").append(SCRUtil.getStateStringRepresentation(state)); //$NON-NLS-1$
		StringBuffer buf = new StringBuffer(200);
		if (properties != null) {
			buf.append('{');
			Enumeration keys = properties.keys();
			while (keys.hasMoreElements()) {
				Object key = keys.nextElement();
				buf.append(key).append('=').append(SCRUtil.getStringRepresentation(properties.get(key)));
				if (keys.hasMoreElements()) {
					buf.append(", "); //$NON-NLS-1$
				}
			}
			buf.append('}');
		}
		buffer.append("\n\tproperties = ").append(buf.toString()); //$NON-NLS-1$

		buffer.append("\n\tserviceFactory = ").append(serviceFactory); //$NON-NLS-1$
		buffer.append("\n\tserviceInterface = ").append(serviceInterfaces); //$NON-NLS-1$

		if (references == null) {
			buffer.append("\n\treferences = ").append("null"); //$NON-NLS-1$ //$NON-NLS-2$
		} else {
			buffer.append("\n\treferences = {"); //$NON-NLS-1$
			for (int i = 0; i < references.size(); i++) {
				buffer.append("\n\t\t").append(references.elementAt(i)); //$NON-NLS-1$
			}
			buffer.append("\n\t}"); //$NON-NLS-1$
		}
		buffer.append("\n\tlocated in bundle = ").append(bundle); //$NON-NLS-1$
		buffer.append("\n]"); //$NON-NLS-1$
		return buffer.toString();
	}

	public synchronized void writeObject(OutputStream s) throws Exception {
		try {
			DataOutputStream out;
			if (s instanceof DataOutputStream) {
				out = (DataOutputStream) s;
			} else {
				out = new DataOutputStream(s);
			}

			boolean flag;
			int count;

			out.writeUTF(name);
			out.writeUTF(implementation);
			out.writeBoolean(serviceFactory);
			out.writeBoolean(autoenable);
			out.writeBoolean(immediate);

			flag = factory != null;
			out.writeBoolean(flag);
			if (flag)
				out.writeUTF(factory);

			count = serviceInterfaces == null ? 0 : serviceInterfaces.size();
			out.writeInt(count);
			for (int i = 0; i < count; i++) {
				out.writeUTF(serviceInterfaces.elementAt(i).toString());
			}

			count = references == null ? 0 : references.size();
			out.writeInt(count);
			for (int i = 0; i < count; i++) {
				ComponentReference ref = (ComponentReference) references.elementAt(i);
				ref.writeObject(out);
			}

			flag = properties != null && !properties.isEmpty();
			out.writeBoolean(flag);
			if (flag) {
				ExternalizableDictionary dictionary = new ExternalizableDictionary();
				dictionary.copyFrom(properties);
				dictionary.writeObject(out);
			}

			out.writeInt(namespace);
			if (isNamespaceAtLeast11()) {
				if (configurationPolicy == CONF_POLICY_OPTIONAL) {
					//this is the default value. Do not write it. Just add a mark
					out.writeBoolean(false);
				} else {
					out.writeBoolean(true);
					out.writeUTF(configurationPolicy);
				}
				if (!activateMethodDeclared) {
					//this is the default value. Do not write it. Just add a mark
					out.writeBoolean(false);
				} else {
					out.writeBoolean(true);
					out.writeUTF(activateMethodName);
				}
				if (!deactivateMethodDeclared) {
					//this is the default value. Do not write it. Just add a mark
					out.writeBoolean(false);
				} else {
					out.writeBoolean(true);
					out.writeUTF(deactivateMethodName);
				}
				if (modifyMethodName == "") { //$NON-NLS-1$
					//this is the default value. Do not write it. Just add a mark
					out.writeBoolean(false);
				} else {
					out.writeBoolean(true);
					out.writeUTF(modifyMethodName);
				}
			}
			if (isNamespaceAtLeast12()) {
				if (configurationPID == name) {
					out.writeBoolean(false);
				} else {
					out.writeBoolean(true);
					out.writeUTF(configurationPID);
				}
			}
		} catch (Exception e) {
			Activator.log(null, LogService.LOG_ERROR, Messages.ERROR_WRITING_OBJECT, e);
			throw e;
		}
	}

	/**
	 * Don't forget to set the bundle & the bc attributes!!!!!!
	 * 
	 * @param s
	 *            the input stream from which to read the object
	 * @throws Exception
	 * @see org.eclipse.equinox.internal.util.io.Externalizable#readObject(java.io.InputStream)
	 */
	public synchronized void readObject(InputStream s) throws Exception {
		try {
			DataInputStream in;
			if (s instanceof DataInputStream) {
				in = (DataInputStream) s;
			} else {
				in = new DataInputStream(s);
			}
			boolean flag;
			int count;

			name = in.readUTF();
			implementation = in.readUTF();
			serviceFactory = in.readBoolean();
			autoenable = in.readBoolean();
			immediate = in.readBoolean();

			flag = in.readBoolean();
			if (flag)
				factory = in.readUTF();

			count = in.readInt();
			if (count > 0) {
				serviceInterfaces = new Vector(count);
				provides = new String[count];
				for (int i = 0; i < count; i++) {
					String entry = in.readUTF();
					serviceInterfaces.addElement(entry);
					provides[i] = entry;
				}
			}

			count = in.readInt();
			if (count > 0) {
				references = new Vector(count);
				for (int i = 0; i < count; i++) {
					ComponentReference ref = new ComponentReference(this);
					ref.readObject(in);
				}
			}

			flag = in.readBoolean();
			if (flag) {
				ExternalizableDictionary dictionary = new ExternalizableDictionary();
				dictionary.readObject(in);
				Properties props = new Properties();
				for (Enumeration keys = dictionary.keys(); keys.hasMoreElements();) {
					String key = (String) keys.nextElement();
					props.put(key, dictionary.get(key));
				}
				properties = props;
			}
			namespace = in.readInt();
			if (isNamespaceAtLeast11()) {
				flag = in.readBoolean();
				if (flag) {
					configurationPolicy = in.readUTF();
					if (configurationPolicy.equals(CONF_POLICY_IGNORE)) {
						configurationPolicy = CONF_POLICY_IGNORE;
					} else {
						configurationPolicy = CONF_POLICY_REQUIRE;
					}
				}
				flag = in.readBoolean();
				if (flag) {
					activateMethodName = in.readUTF();
					activateMethodDeclared = true;
				}
				flag = in.readBoolean();
				if (flag) {
					deactivateMethodName = in.readUTF();
					deactivateMethodDeclared = true;
				}
				flag = in.readBoolean();
				if (flag)
					modifyMethodName = in.readUTF();
			}
			if (isNamespaceAtLeast12()) {
				flag = in.readBoolean();
				if (flag) {
					configurationPID = in.readUTF();
				} else {
					configurationPID = name;
				}
			}
		} catch (Exception e) {
			Activator.log(null, LogService.LOG_ERROR, Messages.ERROR_READING_OBJECT, e);
			throw e;
		}
	}

	public ServiceComponentProp getComponentPropByPID(String pid) {
		if (componentProps != null) {
			for (int i = 0; i < componentProps.size(); i++) {
				ServiceComponentProp scp = (ServiceComponentProp) componentProps.elementAt(i);
				if (scp.getProperties() != null) {
					if (pid.equals(scp.getProperties().get(Constants.SERVICE_PID))) {
						return scp;
					}
				}
			}
		}
		return null;
	}

	public void addServiceComponentProp(ServiceComponentProp scp) {
		if (componentProps == null) {
			componentProps = new Vector(1);
		}
		componentProps.addElement(scp);
	}

	/**
	 * Return the ServiceComponentProp object created for this component. Note there might be more than one SCP objects. 
	 * This method will return always the first one
	 * @return the ServiceComponentProp object created for this component 
	 */
	public ServiceComponentProp getServiceComponentProp() {
		if (componentProps != null) {
			synchronized (componentProps) {
				if (!componentProps.isEmpty()) {
					return (ServiceComponentProp) componentProps.elementAt(0);
				}
			}
		}
		return null;
	}

	public boolean isNamespaceAtLeast11() {
		return namespace >= NAMESPACE_1_1;
	}

	public boolean isNamespaceAtLeast12() {
		return namespace >= NAMESPACE_1_2;
	}

	public boolean isImmediate() {
		return immediate;
	}

	public void setImmediate(boolean immediate) {
		this.immediate = immediate;
	}

	public String getConfigurationPolicy() {
		return configurationPolicy;
	}

	public void disable() {
		if (getState() == STATE_DISPOSED) {
			throw new IllegalStateException(Messages.COMPONENT_DISPOSED);
		} else if (getState() != STATE_DISABLED) {
			InstanceProcess.resolver.mgr.disableComponent(name, bundle);
		}
	}

	public void enable() {
		if (getState() == STATE_DISPOSED) {
			throw new IllegalStateException(Messages.COMPONENT_DISPOSED);
		} else if (getState() == STATE_DISABLED) {
			InstanceProcess.resolver.mgr.enableComponent(name, bundle);
		}
	}

	public String getActivate() {
		return activateMethodName;
	}

	public Bundle getBundle() {
		return bundle;
	}

	public String getClassName() {
		return implementation;
	}

	public ComponentInstance getComponentInstance() {
		if (componentProps != null && !componentProps.isEmpty()) {
			//get the first built compoent's instance 
			Vector instances = ((ServiceComponentProp) componentProps.elementAt(0)).instances;
			if (!instances.isEmpty()) {
				return (ComponentInstance) instances.elementAt(0);
			}
		}
		//The component is not yet built
		return null;
	}

	public String getDeactivate() {
		return deactivateMethodName;
	}

	public String getFactory() {
		return factory;
	}

	public long getId() {
		if (componentProps != null && !componentProps.isEmpty()) {
			//get the first built component's ID 
			return ((Long) ((ServiceComponentProp) componentProps.elementAt(0)).properties.get(ComponentConstants.COMPONENT_ID)).longValue();
		}
		//The component is not yet given an ID by the SRC because it is not active
		return -1;
	}

	public String getModified() {
		if (!isNamespaceAtLeast11()) {
			return null;
		}
		return modifyMethodName;
	}

	public String getName() {
		return name;
	}

	public Dictionary getProperties() {
		if (readOnlyProps == null) {
			readOnlyProps = new ReadOnlyDictionary(properties);
		} else {
			// the scp properties may have been modified by configuration
			// update the instance with the current properties
			readOnlyProps.updateDelegate(properties);
		}
		return readOnlyProps;
	}

	public Reference[] getReferences() {
		if (references != null && !references.isEmpty()) {
			org.apache.felix.scr.Reference[] res = new org.apache.felix.scr.Reference[references.size()];
			references.copyInto(res);
			return res;
		}
		return null;
	}

	public String[] getServices() {
		return provides;
	}

	public int getState() {
		//check if there is at least one SCP created and return its state
		if (componentProps != null && !componentProps.isEmpty()) {
			//get the first component's state
			return ((ServiceComponentProp) componentProps.elementAt(0)).getState();
		}
		//return the current state of the component
		return state;
	}

	public boolean isActivateDeclared() {
		if (!isNamespaceAtLeast11()) {
			return false;
		}
		return activateMethodDeclared;
	}

	public boolean isDeactivateDeclared() {
		if (!isNamespaceAtLeast11()) {
			return false;
		}
		return deactivateMethodDeclared;
	}

	public boolean isDefaultEnabled() {
		return autoenable;
	}

	public boolean isServiceFactory() {
		return serviceFactory;
	}

	public void setState(int newState) {
		state = newState;
	}
}

Back to the top