This project has retired. For details please refer to its
Attic page.
PlexusSisuBridge xref
1 package org.apache.archiva.common.plexusbridge;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42
43
44
45
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;
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
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
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
121
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 }