This project has retired. For details please refer to its Attic page.
PlexusSisuBridge xref
View Javadoc
1   package org.apache.archiva.common.plexusbridge;
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 org.codehaus.plexus.DefaultContainerConfiguration;
23  import org.codehaus.plexus.DefaultPlexusContainer;
24  import org.codehaus.plexus.PlexusConstants;
25  import org.codehaus.plexus.PlexusContainerException;
26  import org.codehaus.plexus.classworlds.ClassWorld;
27  import org.codehaus.plexus.classworlds.realm.ClassRealm;
28  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.stereotype.Service;
32  
33  import javax.annotation.PostConstruct;
34  import java.lang.reflect.Method;
35  import java.net.URL;
36  import java.net.URLClassLoader;
37  import java.util.List;
38  import java.util.Map;
39  
40  /**
41   * Simple component which will initiate the plexus shim component
42   * to see plexus components inside a guice container.<br>
43   * So move all of this here to be able to change quickly if needed.
44   *
45   * @author Olivier Lamy
46   */
47  @Service( "plexusSisuBridge" )
48  public class PlexusSisuBridge
49  {
50  
51      private Logger log = LoggerFactory.getLogger( getClass() );
52  
53      private boolean containerAutoWiring = true;
54  
55      private String containerClassPathScanning = PlexusConstants.SCANNING_INDEX;// PlexusConstants.SCANNING_ON;
56  
57      private String containerComponentVisibility = PlexusConstants.REALM_VISIBILITY;
58  
59      private URL overridingComponentsXml;
60  
61      private ClassRealm containerRealm;
62  
63      private DefaultPlexusContainer plexusContainer;
64  
65      @PostConstruct
66      public void initialize()
67          throws PlexusSisuBridgeException
68      {
69          DefaultContainerConfiguration conf = new DefaultContainerConfiguration();
70  
71          conf.setAutoWiring( containerAutoWiring );
72          conf.setClassPathScanning( containerClassPathScanning );
73          conf.setComponentVisibility( containerComponentVisibility );
74  
75          conf.setContainerConfigurationURL( overridingComponentsXml );
76  
77          ClassWorld classWorld = new ClassWorld();
78  
79          ClassLoader tccl = Thread.currentThread().getContextClassLoader();
80  
81          containerRealm = new ClassRealm( classWorld, "maven", tccl );
82  
83          // olamy hackhish but plexus-sisu need a URLClassLoader with URL filled
84  
85          if ( tccl instanceof URLClassLoader )
86          {
87              URL[] urls = ( (URLClassLoader) tccl ).getURLs();
88              for ( URL url : urls )
89              {
90                  containerRealm.addURL( url );
91              }
92          }
93  
94          conf.setRealm( containerRealm );
95  
96          //conf.setClassWorld( classWorld );
97  
98          ClassLoader ori = Thread.currentThread().getContextClassLoader();
99  
100         try
101         {
102             Thread.currentThread().setContextClassLoader( containerRealm );
103             plexusContainer = new DefaultPlexusContainer( conf );
104         }
105         catch ( PlexusContainerException e )
106         {
107             throw new PlexusSisuBridgeException( e.getMessage(), e );
108         }
109         finally
110         {
111             Thread.currentThread().setContextClassLoader( ori );
112         }
113     }
114 
115 
116     private URL[] getClassLoaderURLs( ClassLoader classLoader )
117     {
118         try
119         {
120             // can be WebappClassLoader when using tomcat maven plugin
121             //java.net.URL[] getURLs
122             Method method = classLoader.getClass().getMethod( "getURLs", new Class[]{ } );
123             if ( method != null )
124             {
125                 return (URL[]) method.invoke( classLoader );
126             }
127         }
128         catch ( Exception e )
129         {
130             log.info( "ignore issue trying to find url[] from classloader {}", e.getMessage() );
131         }
132         return new URL[]{ };
133     }
134 
135     public <T> T lookup( Class<T> clazz )
136         throws PlexusSisuBridgeException
137     {
138         ClassLoader ori = Thread.currentThread().getContextClassLoader();
139         try
140         {
141             Thread.currentThread().setContextClassLoader( containerRealm );
142             return plexusContainer.lookup( clazz );
143         }
144         catch ( ComponentLookupException e )
145         {
146             throw new PlexusSisuBridgeException( e.getMessage(), e );
147         }
148         finally
149         {
150             Thread.currentThread().setContextClassLoader( ori );
151         }
152     }
153 
154     public <T> T lookup( Class<T> clazz, String hint )
155         throws PlexusSisuBridgeException
156     {
157         ClassLoader ori = Thread.currentThread().getContextClassLoader();
158 
159         try
160         {
161             Thread.currentThread().setContextClassLoader( containerRealm );
162             return plexusContainer.lookup( clazz, hint );
163         }
164         catch ( ComponentLookupException e )
165         {
166             throw new PlexusSisuBridgeException( e.getMessage(), e );
167         }
168         finally
169         {
170             Thread.currentThread().setContextClassLoader( ori );
171         }
172     }
173 
174     public <T> List<T> lookupList( Class<T> clazz )
175         throws PlexusSisuBridgeException
176     {
177         ClassLoader ori = Thread.currentThread().getContextClassLoader();
178 
179         try
180         {
181             Thread.currentThread().setContextClassLoader( containerRealm );
182             return plexusContainer.lookupList( clazz );
183         }
184         catch ( ComponentLookupException e )
185         {
186             throw new PlexusSisuBridgeException( e.getMessage(), e );
187         }
188         finally
189         {
190             Thread.currentThread().setContextClassLoader( ori );
191         }
192     }
193 
194     public <T> Map<String, T> lookupMap( Class<T> clazz )
195         throws PlexusSisuBridgeException
196     {
197         ClassLoader ori = Thread.currentThread().getContextClassLoader();
198 
199         try
200         {
201             Thread.currentThread().setContextClassLoader( containerRealm );
202             return plexusContainer.lookupMap( clazz );
203         }
204         catch ( ComponentLookupException e )
205         {
206             throw new PlexusSisuBridgeException( e.getMessage(), e );
207         }
208         finally
209         {
210             Thread.currentThread().setContextClassLoader( ori );
211         }
212     }
213 }