Skip to content

Commit 83e6fbc

Browse files
committed
Split Plexus CDC and Sisu indexer
1 parent 6134c04 commit 83e6fbc

29 files changed

+141
-71
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,15 @@ MBI provides the following tools that can be used as build steps:
119119

120120
* `modello` - invokes Modello tool to generate code from data models.
121121

122-
* `cdc` - component descriptor creator, extracts Plexus and Sisu
122+
* `sisu` - JSR-330 indexer, searches compiled classes for JSR-330
123+
components annotated with `javax.inject.Named` and generates Sisu
124+
index for them, allowing Sisu to use these components for dependency
125+
injection.
126+
127+
* `plexus` - Plexus component descriptor creator, extracts Plexus
123128
component descriptors from Javadoc comments or Java annotations,
124-
enabling Sisu and Plexus to discover and load components implemented
125-
by the module.
129+
enabling Plexus to discover and load components implemented by the
130+
module.
126131

127132
* `pdc` - plugin descriptor creator, invokes Maven Plugin Tools to
128133
generate plugin descriptors, allowing module to be used as a Maven

mbi/cdc/src/org/fedoraproject/mbi/tool/cdc/CdcTool.java renamed to mbi/plexus/src/org/fedoraproject/mbi/tool/plexus/PlexusTool.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.fedoraproject.mbi.tool.cdc;
16+
package org.fedoraproject.mbi.tool.plexus;
1717

18-
import java.io.PrintWriter;
1918
import java.lang.annotation.Annotation;
2019
import java.lang.reflect.Field;
2120
import java.nio.file.Files;
2221
import java.nio.file.Path;
23-
import java.util.ArrayList;
24-
import java.util.List;
2522
import java.util.stream.Collectors;
2623

27-
import javax.inject.Named;
2824
import javax.xml.parsers.DocumentBuilder;
2925
import javax.xml.parsers.DocumentBuilderFactory;
3026
import javax.xml.transform.OutputKeys;
@@ -47,13 +43,11 @@
4743
/**
4844
* @author Mikolaj Izdebski
4945
*/
50-
public class CdcTool
46+
public class PlexusTool
5147
extends Tool
5248
{
5349
private static final String PLEXUS_DESCRIPTOR_PATH = "META-INF/plexus/components.xml";
5450

55-
private static final String SISU_DESCRIPTOR_PATH = "META-INF/sisu/javax.inject.Named";
56-
5751
private DocumentBuilder documentBuilder;
5852

5953
private Transformer transformer;
@@ -62,8 +56,6 @@ public class CdcTool
6256

6357
private Element plexusComponents;
6458

65-
private List<String> sisuComponents = new ArrayList<>();
66-
6759
@Override
6860
public void initialize()
6961
throws Exception
@@ -145,10 +137,6 @@ private void gleanFromClasses()
145137
String className =
146138
getClassesDir().relativize( classFile ).toString().replaceAll( ".class$", "" ).replace( '/', '.' );
147139
Class<?> cls = getClass().getClassLoader().loadClass( className );
148-
if ( cls.isAnnotationPresent( Named.class ) )
149-
{
150-
sisuComponents.add( cls.getName() );
151-
}
152140
if ( cls.isAnnotationPresent( Component.class ) )
153141
{
154142
Annotation plexus = cls.getAnnotationsByType( Component.class )[0];
@@ -183,7 +171,7 @@ public void execute()
183171
{
184172
gleanFromClasses();
185173

186-
if ( sisuComponents.isEmpty() && !plexusComponents.hasChildNodes() )
174+
if ( !plexusComponents.hasChildNodes() )
187175
{
188176
throw new RuntimeException( "No Plexus components were discovered by CDC for module "
189177
+ getModule().getName() );
@@ -194,15 +182,5 @@ public void execute()
194182
Source source = new DOMSource( plexusDescriptor );
195183
Result result = new StreamResult( plexusPath.toFile() );
196184
transformer.transform( source, result );
197-
198-
Path sisuPath = getClassesDir().resolve( SISU_DESCRIPTOR_PATH );
199-
Files.createDirectories( sisuPath.getParent() );
200-
try ( PrintWriter pw = new PrintWriter( Files.newBufferedWriter( sisuPath ) ) )
201-
{
202-
for ( String sisuComponent : sisuComponents )
203-
{
204-
pw.println( sisuComponent );
205-
}
206-
}
207185
}
208186
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*-
2+
* Copyright (c) 2020-2023 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.fedoraproject.mbi.tool.sisu;
17+
18+
import java.io.PrintWriter;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
25+
import javax.inject.Named;
26+
27+
import org.fedoraproject.mbi.tool.Tool;
28+
29+
/**
30+
* @author Mikolaj Izdebski
31+
*/
32+
public class SisuTool
33+
extends Tool
34+
{
35+
private static final String SISU_INDEX_PATH = "META-INF/sisu/javax.inject.Named";
36+
37+
private List<String> namedComponents = new ArrayList<>();
38+
39+
private void gleanFromClasses()
40+
throws Exception
41+
{
42+
for ( Path classFile : Files.walk( getClassesDir() ).filter( Files::isRegularFile ).filter( path -> path.toString().endsWith( ".class" ) ).collect( Collectors.toList() ) )
43+
{
44+
String className =
45+
getClassesDir().relativize( classFile ).toString().replaceAll( ".class$", "" ).replace( '/', '.' );
46+
Class<?> cls = getClass().getClassLoader().loadClass( className );
47+
if ( cls.isAnnotationPresent( Named.class ) )
48+
{
49+
namedComponents.add( cls.getName() );
50+
}
51+
}
52+
}
53+
54+
@Override
55+
public void execute()
56+
throws Exception
57+
{
58+
gleanFromClasses();
59+
60+
if ( namedComponents.isEmpty() )
61+
{
62+
throw new RuntimeException( "No JSR-330 components were discovered for module " + getModule().getName() );
63+
}
64+
65+
Path indexPath = getClassesDir().resolve( SISU_INDEX_PATH );
66+
Files.createDirectories( indexPath.getParent() );
67+
try ( PrintWriter pw = new PrintWriter( Files.newBufferedWriter( indexPath ) ) )
68+
{
69+
for ( String component : namedComponents )
70+
{
71+
pw.println( component );
72+
}
73+
}
74+
}
75+
}

project/extra-enforcer-rules.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<compiler>
2020
<addSourceRoot>src/main/java</addSourceRoot>
2121
</compiler>
22-
<cdc/>
22+
<sisu/>
2323
</build>
2424
</module>
2525
</project>

project/maven-artifact-transfer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<addSourceRoot>src/main/java</addSourceRoot>
1818
<excludeSourceMatching>/Maven30.*</excludeSourceMatching>
1919
</compiler>
20-
<cdc/>
20+
<plexus/>
2121
</build>
2222
</module>
2323
</project>

project/maven-assembly-plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<addSourceRoot>src/main/java</addSourceRoot>
3737
<addResource>src/main/resources</addResource>
3838
</compiler>
39-
<cdc/>
39+
<sisu/>
4040
<pdc/>
4141
</build>
4242
</module>

project/maven-dependency-analyzer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<compiler>
1515
<addSourceRoot>src/main/java</addSourceRoot>
1616
</compiler>
17-
<cdc/>
17+
<sisu/>
1818
</build>
1919
</module>
2020
</project>

project/maven-dependency-plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<excludeSourceClass>AnalyzeReportRenderer</excludeSourceClass>
4646
<addResource>src/main/resources</addResource>
4747
</compiler>
48-
<cdc/>
48+
<sisu/>
4949
<pdc/>
5050
</build>
5151
</module>

project/maven-dependency-tree.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<excludeSourceClass>Maven3DependencyGraphBuilder</excludeSourceClass>
1717
<excludeSourceClass>Maven3DependencyCollectorBuilder</excludeSourceClass>
1818
</compiler>
19-
<cdc/>
19+
<sisu/>
2020
</build>
2121
</module>
2222
</project>

project/maven-enforcer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<addSourceRoot>maven-enforcer-plugin/src/main/java</addSourceRoot>
2929
<excludeSourceClass>EvaluateBeanshell</excludeSourceClass>
3030
</compiler>
31-
<cdc/>
31+
<sisu/>
3232
<pdc>
3333
<artifactId>maven-enforcer-plugin</artifactId>
3434
</pdc>

0 commit comments

Comments
 (0)