View Javadoc

1   package org.apache.velocity.site.plexus;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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                  // Found the root container
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  }