1 package org.apache.velocity.site.plexus;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Field;
23
24 import org.codehaus.classworlds.ClassRealm;
25 import org.codehaus.classworlds.NoSuchRealmException;
26 import org.codehaus.plexus.PlexusContainer;
27
28 public abstract class PlexusKludgeUtils
29 {
30
31 private PlexusKludgeUtils()
32 {
33 }
34
35 public static void bindRealm(final PlexusContainer srcContainer, final PlexusContainer targetContainer, final String packageName)
36 throws PlexusKludgeException
37 {
38 try
39 {
40 if (srcContainer != targetContainer)
41 {
42 ClassRealm srcRealm = srcContainer.getContainerRealm();
43 ClassRealm targetRealm = targetContainer.getContainerRealm();
44 targetRealm.importFrom(srcRealm.getId(), packageName);
45 }
46 }
47 catch (NoSuchRealmException nsre)
48 {
49 throw new PlexusKludgeException(nsre);
50 }
51 }
52
53 public static PlexusContainer lookupContainer(final PlexusContainer container, final String childName)
54 throws PlexusKludgeException
55 {
56
57 try
58 {
59 Field parentContainerField = container.getClass().getDeclaredField("parentContainer");
60 parentContainerField.setAccessible(true);
61 PlexusContainer parentContainer = (PlexusContainer) parentContainerField.get(container);
62
63 if (parentContainer == null)
64 {
65
66 return container.getChildContainer(childName);
67 }
68 else
69 {
70 return lookupContainer(parentContainer, childName);
71 }
72 }
73 catch (SecurityException se)
74 {
75 throw new PlexusKludgeException(se);
76 }
77 catch (NoSuchFieldException nsme)
78 {
79 throw new PlexusKludgeException(nsme);
80 }
81 catch (IllegalArgumentException iae)
82 {
83 throw new PlexusKludgeException(iae);
84 }
85 catch (IllegalAccessException iae)
86 {
87 throw new PlexusKludgeException(iae);
88 }
89 }
90 }