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 bsh.EvalError;
23 import bsh.Interpreter;
24 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
25 import org.apache.maven.plugin.descriptor.MojoDescriptor;
26 import org.apache.maven.tools.plugin.PluginToolsRequest;
27 import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
28 import org.apache.maven.tools.plugin.extractor.ExtractionException;
29 import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
30 import org.codehaus.plexus.component.annotations.Component;
31
32 import java.io.File;
33 import java.io.InputStreamReader;
34 import java.io.UnsupportedEncodingException;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39
40
41
42
43
44
45
46
47
48
49 @Component( role = MojoDescriptorExtractor.class, hint = "bsh" )
50 public class BeanshellMojoDescriptorExtractor
51 extends AbstractScriptedMojoDescriptorExtractor
52 implements MojoDescriptorExtractor
53 {
54
55
56
57 protected String getScriptFileExtension( PluginToolsRequest request )
58 {
59 return ".bsh";
60 }
61
62
63
64
65 protected List<MojoDescriptor> extractMojoDescriptors( Map<String, Set<File>> scriptFilesKeyedByBasedir,
66 PluginToolsRequest request )
67 throws ExtractionException, InvalidPluginDescriptorException
68 {
69 List<MojoDescriptor> descriptors = new ArrayList<MojoDescriptor>();
70
71 for ( Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet() )
72 {
73 String basedir = entry.getKey();
74 Set<File> metadataFiles = entry.getValue();
75
76 for ( File scriptFile : metadataFiles )
77 {
78 String relativePath = null;
79
80 if ( basedir.endsWith( "/" ) )
81 {
82 basedir = basedir.substring( 0, basedir.length() - 2 );
83 }
84
85 relativePath = scriptFile.getPath().substring( basedir.length() );
86
87 relativePath = relativePath.replace( '\\', '/' );
88
89 MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, relativePath, request );
90 descriptors.add( mojoDescriptor );
91 }
92 }
93
94 return descriptors;
95 }
96
97
98
99
100
101
102
103
104
105 private MojoDescriptor createMojoDescriptor( String basedir, String resource, PluginToolsRequest request )
106 throws InvalidPluginDescriptorException
107 {
108 MojoDescriptor mojoDescriptor = new MojoDescriptor();
109 mojoDescriptor.setPluginDescriptor( request.getPluginDescriptor() );
110
111 mojoDescriptor.setLanguage( "bsh" );
112 mojoDescriptor.setComponentConfigurator( "bsh" );
113
114 mojoDescriptor.setImplementation( resource );
115
116 Interpreter interpreter = new Interpreter();
117
118 try
119 {
120 interpreter.set( "file", new File( basedir, resource ) );
121
122 interpreter.set( "mojoDescriptor", mojoDescriptor );
123
124 interpreter.set( "encoding", "UTF-8" );
125
126 interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ), "UTF-8" ) );
127 }
128 catch ( EvalError evalError )
129 {
130 throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
131 }
132 catch ( UnsupportedEncodingException uee )
133 {
134
135 throw new InvalidPluginDescriptorException( "Unsupported encoding while reading beanshell script", uee );
136 }
137
138 return mojoDescriptor;
139 }
140 }