1 package org.codehaus.plexus.component.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23
24 import org.codehaus.plexus.PlexusConstants;
25 import org.codehaus.plexus.classworlds.realm.ClassRealm;
26 import org.codehaus.plexus.configuration.PlexusConfiguration;
27
28
29
30
31
32
33
34
35 public class ComponentDescriptor<T> {
36 private String alias = null;
37
38 private String role = null;
39
40 private Class<T> roleClass;
41
42 private String roleHint = PlexusConstants.PLEXUS_DEFAULT_HINT;
43
44 private String implementation;
45
46 private Class<? extends T> implementationClass;
47
48 private String version;
49
50 private String componentType;
51
52 private PlexusConfiguration configuration;
53
54 private String instantiationStrategy;
55
56 private String lifecycleHandler;
57
58 private String componentProfile;
59
60 private final List<ComponentRequirement> requirements = new ArrayList<ComponentRequirement>();
61
62 private String componentFactory;
63
64 private String componentComposer;
65
66 private String componentConfigurator;
67
68 private String description;
69
70 private ClassRealm realm;
71
72
73
74
75
76
77
78
79 private boolean isolatedRealm;
80
81
82
83 private ComponentSetDescriptor componentSetDescriptor;
84
85 private String source;
86
87
88
89
90
91 public ComponentDescriptor() {}
92
93 public ComponentDescriptor(Class<T> implementationClass, ClassRealm realm) {
94 this.implementationClass = implementationClass;
95 this.implementation = implementationClass.getName();
96 this.realm = realm;
97 }
98
99
100
101
102
103 public void setSource(String source) {
104 this.source = source;
105 }
106
107
108
109
110
111 public String getSource() {
112 return source;
113 }
114
115
116
117
118
119
120 public String getHumanReadableKey() {
121 StringBuilder key = new StringBuilder();
122
123 key.append("role: '").append(getRole()).append("'");
124
125 key.append(", implementation: '").append(getImplementation()).append("'");
126
127 if (roleHint != null) {
128 key.append(", role hint: '").append(getRoleHint()).append("'");
129 }
130
131 if (alias != null) {
132 key.append(", alias: '").append(getAlias()).append("'");
133 }
134
135 return key.toString();
136 }
137
138
139
140
141
142
143 public String getAlias() {
144 return alias;
145 }
146
147
148
149
150
151
152 public void setAlias(String alias) {
153 this.alias = alias;
154 }
155
156
157
158
159
160
161 public String getRole() {
162 return role;
163 }
164
165 public Class<T> getRoleClass() {
166 attemptRoleLoad();
167
168 if (roleClass == null) {
169 return (Class<T>) Object.class;
170 }
171 return (Class<T>) roleClass;
172 }
173
174 private void attemptRoleLoad() {
175 if (roleClass == null && getRole() != null && getRealm() != null) {
176 try {
177 roleClass = (Class<T>) getRealm().loadClass(getRole());
178 Thread.currentThread();
179 } catch (Throwable ignored) {
180 Thread.currentThread();
181 }
182 }
183 }
184
185
186
187
188
189
190 public void setRole(String role) {
191 this.role = role;
192
193
194 roleClass = null;
195 attemptRoleLoad();
196 }
197
198 public void setRoleClass(Class<T> roleClass) {
199 this.roleClass = roleClass;
200
201 if (roleClass == null) {
202 role = null;
203 } else {
204 role = roleClass.getName();
205 }
206 }
207
208
209
210
211
212
213 public String getRoleHint() {
214 return roleHint;
215 }
216
217
218
219
220
221
222 public void setRoleHint(String roleHint) {
223 if ((roleHint == null) || roleHint.trim().equals("")) {
224 this.roleHint = PlexusConstants.PLEXUS_DEFAULT_HINT;
225 } else {
226 this.roleHint = roleHint;
227 }
228 }
229
230
231
232
233
234
235
236 public String getImplementation() {
237 return implementation;
238 }
239
240
241
242
243
244
245
246 public void setImplementation(String implementation) {
247 this.implementation = implementation;
248
249
250 implementationClass = null;
251 attemptImplementationLoad();
252 }
253
254
255
256
257
258
259 public Class<? extends T> getImplementationClass() {
260 attemptImplementationLoad();
261
262 if (implementationClass == null) {
263 return (Class<T>) Object.class;
264 }
265 return (Class<T>) implementationClass;
266 }
267
268 private void attemptImplementationLoad() {
269 if (implementationClass == null && getImplementation() != null && getRealm() != null) {
270 try {
271 implementationClass = (Class<? extends T>) getRealm().loadClass(getImplementation());
272 Thread.currentThread();
273 } catch (Throwable ignored) {
274 Thread.currentThread();
275 }
276 }
277 }
278
279 public void setImplementationClass(Class<? extends T> implementationClass) {
280 this.implementationClass = implementationClass;
281 if (implementationClass == null) {
282 implementation = null;
283 } else {
284 implementation = implementationClass.getName();
285 }
286 }
287
288
289
290
291
292
293 public String getVersion() {
294 return version;
295 }
296
297
298
299
300
301
302 public void setVersion(String version) {
303 this.version = version;
304 }
305
306
307
308
309
310
311 public String getComponentType() {
312 return componentType;
313 }
314
315
316
317
318
319
320 public void setComponentType(String componentType) {
321 this.componentType = componentType;
322 }
323
324
325
326
327
328
329 public String getInstantiationStrategy() {
330 return instantiationStrategy;
331 }
332
333
334
335
336
337
338 public PlexusConfiguration getConfiguration() {
339 return configuration;
340 }
341
342
343
344
345
346
347 public void setConfiguration(PlexusConfiguration configuration) {
348 this.configuration = configuration;
349 }
350
351
352
353
354
355
356 public boolean hasConfiguration() {
357 return configuration != null;
358 }
359
360
361
362
363
364
365 public String getLifecycleHandler() {
366 return lifecycleHandler;
367 }
368
369
370
371
372
373
374 public void setLifecycleHandler(String lifecycleHandler) {
375 this.lifecycleHandler = lifecycleHandler;
376 }
377
378 public String getComponentProfile() {
379 return componentProfile;
380 }
381
382 public void setComponentProfile(String componentProfile) {
383 this.componentProfile = componentProfile;
384 }
385
386
387
388
389
390
391 public void addRequirement(ComponentRequirement requirement) {
392 this.requirements.add(requirement);
393 }
394
395
396
397
398
399
400 public void addRequirement(ComponentRequirement... requirement) {
401 this.requirements.addAll(Arrays.asList(requirement));
402 }
403
404
405
406
407
408
409 public void addRequirements(List<ComponentRequirement> requirements) {
410 this.requirements.addAll(requirements);
411 }
412
413
414
415
416
417
418 public void removeRequirement(ComponentRequirement... requirement) {
419 this.requirements.removeAll(Arrays.asList(requirement));
420 }
421
422
423
424
425
426
427 public void removeRequirements(List<ComponentRequirement> requirements) {
428 this.requirements.removeAll(requirements);
429 }
430
431
432
433
434
435
436 public List<ComponentRequirement> getRequirements() {
437 return Collections.unmodifiableList(requirements);
438 }
439
440
441
442
443
444
445 public String getComponentFactory() {
446 return componentFactory;
447 }
448
449
450
451
452
453
454 public void setComponentFactory(String componentFactory) {
455 this.componentFactory = componentFactory;
456 }
457
458
459
460
461
462
463
464 public String getComponentComposer() {
465 return componentComposer;
466 }
467
468
469
470
471
472
473 public void setComponentComposer(String componentComposer) {
474 this.componentComposer = componentComposer;
475 }
476
477
478
479
480
481
482 public String getDescription() {
483 return description;
484 }
485
486
487
488
489
490
491 public void setDescription(String description) {
492 this.description = description;
493 }
494
495
496
497
498
499
500 public void setInstantiationStrategy(String instantiationStrategy) {
501 this.instantiationStrategy = instantiationStrategy;
502 }
503
504
505
506
507
508
509
510
511
512
513 public boolean isIsolatedRealm() {
514 return isolatedRealm;
515 }
516
517
518
519
520
521
522 public void setComponentSetDescriptor(ComponentSetDescriptor componentSetDescriptor) {
523 this.componentSetDescriptor = componentSetDescriptor;
524 }
525
526
527
528
529
530
531 public ComponentSetDescriptor getComponentSetDescriptor() {
532 return componentSetDescriptor;
533 }
534
535
536
537
538
539
540 public void setIsolatedRealm(boolean isolatedRealm) {
541 this.isolatedRealm = isolatedRealm;
542 }
543
544
545
546
547
548
549
550 public String getComponentConfigurator() {
551 return componentConfigurator;
552 }
553
554
555
556
557
558
559 public void setComponentConfigurator(String componentConfigurator) {
560 this.componentConfigurator = componentConfigurator;
561 }
562
563
564
565
566
567
568 public ClassRealm getRealm() {
569 return realm;
570 }
571
572
573
574
575
576
577 public void setRealm(ClassRealm realm) {
578 this.realm = realm;
579
580
581 implementationClass = null;
582 attemptImplementationLoad();
583
584
585 roleClass = null;
586 attemptRoleLoad();
587 }
588
589 public String toString() {
590 return getClass().getName() + " [role: '" + getRole() + "', hint: '" + getRoleHint() + "', realm: "
591 + (realm == null ? "NULL" : "'" + realm + "'") + "]";
592 }
593
594
595 public boolean equals(Object other) {
596 if (this == other) {
597 return true;
598 }
599
600 if (!(other instanceof ComponentDescriptor)) {
601 return false;
602 }
603
604 ComponentDescriptor<?> that = (ComponentDescriptor<?>) other;
605
606 return eq(getRole(), that.getRole())
607 && eq(getRoleHint(), that.getRoleHint())
608 && eq(getRealm(), that.getRealm());
609 }
610
611 private static <T> boolean eq(T o1, T o2) {
612 return (o1 != null) ? o1.equals(o2) : o2 == null;
613 }
614
615 public int hashCode() {
616 int hash = 17;
617
618 hash = hash * 31 + hash(getRole());
619 hash = hash * 31 + hash(getRoleHint());
620 hash = hash * 31 + hash(getRealm());
621
622 return hash;
623 }
624
625 private static int hash(Object obj) {
626 return (obj != null) ? obj.hashCode() : 0;
627 }
628 }