5555import org .codehaus .plexus .component .repository .exception .ComponentLookupException ;
5656
5757@ Named
58- class SisuDiBridgeModule extends AbstractModule {
58+ public class SisuDiBridgeModule extends AbstractModule {
59+
60+ InjectorImpl injector ;
61+ final Set <String > loaded = new HashSet <>();
5962
6063 @ Override
6164 protected void configure () {
6265 Provider <PlexusContainer > containerProvider = getProvider (PlexusContainer .class );
6366
64- InjectorImpl injector = new InjectorImpl () {
67+ injector = new InjectorImpl () {
6568 @ Override
6669 public <Q > Supplier <Q > getCompiledBinding (Key <Q > key ) {
6770 Set <Binding <Q >> res = getBindings (key );
@@ -142,38 +145,12 @@ public <Q> Supplier<Q> getCompiledBinding(Key<Q> key) {
142145 });
143146 injector .bindInstance (Injector .class , injector );
144147 bind (Injector .class ).toInstance (injector );
148+ bind (SisuDiBridgeModule .class ).toInstance (this );
145149 ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
146150 if (classLoader == null ) {
147151 classLoader = getClass ().getClassLoader ();
148152 }
149- try {
150- for (Iterator <URL > it = classLoader
151- .getResources ("META-INF/maven/org.apache.maven.api.di.Inject" )
152- .asIterator ();
153- it .hasNext (); ) {
154- URL url = it .next ();
155- List <String > lines ;
156- try (InputStream is = url .openStream ();
157- BufferedReader reader = new BufferedReader (new InputStreamReader (is , StandardCharsets .UTF_8 ))) {
158- lines = reader .lines ()
159- .map (String ::trim )
160- .filter (s -> !s .isEmpty () && !s .startsWith ("#" ))
161- .toList ();
162- }
163- for (String className : lines ) {
164- try {
165- Class <?> clazz = classLoader .loadClass (className );
166- injector .bindImplicit (clazz );
167- } catch (ClassNotFoundException e ) {
168- // ignore
169- e .printStackTrace ();
170- }
171- }
172- }
173-
174- } catch (IOException e ) {
175- throw new MavenException (e );
176- }
153+ loadFromClassLoader (classLoader );
177154 injector .getBindings ().keySet ().stream ()
178155 .filter (k -> k .getQualifier () != null )
179156 .sorted (Comparator .comparing (k -> k .getRawType ().getName ()))
@@ -185,7 +162,7 @@ public <Q> Supplier<Q> getCompiledBinding(Key<Q> key) {
185162 : (Class <Object >) (clazz .getInterfaces ().length > 0 ? clazz .getInterfaces ()[0 ] : clazz ));
186163 if (itf != null ) {
187164 AnnotatedBindingBuilder <Object > binder = bind (itf );
188- if (key .getQualifier () instanceof String s ) {
165+ if (key .getQualifier () instanceof String s && ! s . isEmpty () ) {
189166 binder .annotatedWith (Names .named (s ));
190167 } else if (key .getQualifier () instanceof Annotation a ) {
191168 binder .annotatedWith (a );
@@ -194,4 +171,37 @@ public <Q> Supplier<Q> getCompiledBinding(Key<Q> key) {
194171 }
195172 });
196173 }
174+
175+ public void loadFromClassLoader (ClassLoader classLoader ) {
176+ try {
177+ for (Iterator <URL > it = classLoader
178+ .getResources ("META-INF/maven/org.apache.maven.api.di.Inject" )
179+ .asIterator ();
180+ it .hasNext (); ) {
181+ URL url = it .next ();
182+ if (loaded .add (url .toExternalForm ())) {
183+ List <String > lines ;
184+ try (InputStream is = url .openStream ();
185+ BufferedReader reader =
186+ new BufferedReader (new InputStreamReader (is , StandardCharsets .UTF_8 ))) {
187+ lines = reader .lines ()
188+ .map (String ::trim )
189+ .filter (s -> !s .isEmpty () && !s .startsWith ("#" ))
190+ .toList ();
191+ }
192+ for (String className : lines ) {
193+ try {
194+ Class <?> clazz = classLoader .loadClass (className );
195+ injector .bindImplicit (clazz );
196+ } catch (ClassNotFoundException e ) {
197+ // ignore
198+ e .printStackTrace ();
199+ }
200+ }
201+ }
202+ }
203+ } catch (IOException e ) {
204+ throw new MavenException (e );
205+ }
206+ }
197207}
0 commit comments