1 package org.apache.maven.tools.plugin.extractor.beanshell;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import static java.nio.charset.StandardCharsets.UTF_8;
26
27 import bsh.EvalError;
28 import bsh.Interpreter;
29 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
30 import org.apache.maven.plugin.descriptor.MojoDescriptor;
31 import org.apache.maven.tools.plugin.PluginToolsRequest;
32 import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
33 import org.apache.maven.tools.plugin.extractor.ExtractionException;
34 import org.apache.maven.tools.plugin.extractor.GroupKey;
35
36 import java.io.File;
37 import java.io.InputStreamReader;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Set;
42
43
44
45
46
47
48 @Deprecated
49 @Named( BeanshellMojoDescriptorExtractor.NAME )
50 @Singleton
51 public class BeanshellMojoDescriptorExtractor
52 extends AbstractScriptedMojoDescriptorExtractor
53 {
54 public static final String NAME = "bsh";
55
56 private static final GroupKey GROUP_KEY = new GroupKey( "bsh", 100 );
57
58 @Override
59 public String getName()
60 {
61 return NAME;
62 }
63
64 @Override
65 public GroupKey getGroupKey()
66 {
67 return GROUP_KEY;
68 }
69
70
71
72
73 @Override
74 protected String getScriptFileExtension( PluginToolsRequest request )
75 {
76 return ".bsh";
77 }
78
79
80
81
82 @Override
83 protected List<MojoDescriptor> extractMojoDescriptors( Map<String, Set<File>> scriptFilesKeyedByBasedir,
84 PluginToolsRequest request )
85 throws ExtractionException, InvalidPluginDescriptorException
86 {
87 List<MojoDescriptor> descriptors = new ArrayList<>();
88
89 for ( Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet() )
90 {
91 String basedir = entry.getKey();
92 Set<File> metadataFiles = entry.getValue();
93
94 for ( File scriptFile : metadataFiles )
95 {
96 String relativePath = null;
97
98 if ( basedir.endsWith( "/" ) )
99 {
100 basedir = basedir.substring( 0, basedir.length() - 2 );
101 }
102
103 relativePath = scriptFile.getPath().substring( basedir.length() );
104
105 relativePath = relativePath.replace( '\\', '/' );
106
107 MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, relativePath, request );
108 descriptors.add( mojoDescriptor );
109 }
110 }
111
112 return descriptors;
113 }
114
115
116
117
118
119
120
121
122
123 private MojoDescriptor createMojoDescriptor( String basedir, String resource, PluginToolsRequest request )
124 throws InvalidPluginDescriptorException
125 {
126 MojoDescriptor mojoDescriptor = new MojoDescriptor();
127 mojoDescriptor.setPluginDescriptor( request.getPluginDescriptor() );
128
129 mojoDescriptor.setLanguage( "bsh" );
130 mojoDescriptor.setComponentConfigurator( "bsh" );
131
132 mojoDescriptor.setImplementation( resource );
133
134 Interpreter interpreter = new Interpreter();
135
136 try
137 {
138 interpreter.set( "file", new File( basedir, resource ) );
139
140 interpreter.set( "mojoDescriptor", mojoDescriptor );
141
142 interpreter.set( "encoding", "UTF-8" );
143
144 interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ), UTF_8 ) );
145 }
146 catch ( EvalError evalError )
147 {
148 throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
149 }
150
151
152 return mojoDescriptor;
153 }
154 }