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.plugin.descriptor.PluginDescriptor;
27 import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
28 import org.apache.maven.tools.plugin.extractor.ExtractionException;
29
30 import java.io.File;
31 import java.io.InputStreamReader;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37
38 /**
39 * @todo share constants
40 * @todo add example usage tag that can be shown in the doco
41 * @todo need to add validation directives so that systems embedding maven2 can
42 * get validation directives to help users in IDEs.
43 */
44 public class BeanshellMojoDescriptorExtractor
45 extends AbstractScriptedMojoDescriptorExtractor
46 {
47 private MojoDescriptor createMojoDescriptor( String basedir, String resource, PluginDescriptor pluginDescriptor )
48 throws InvalidPluginDescriptorException
49 {
50 MojoDescriptor mojoDescriptor = new MojoDescriptor();
51 mojoDescriptor.setPluginDescriptor( pluginDescriptor );
52
53 mojoDescriptor.setLanguage( "bsh" );
54 mojoDescriptor.setComponentConfigurator( "bsh" );
55
56 mojoDescriptor.setImplementation( resource );
57
58 Interpreter interpreter = new Interpreter();
59
60 try
61 {
62 interpreter.set( "file", new File( basedir, resource ) );
63
64 interpreter.set( "mojoDescriptor", mojoDescriptor );
65
66 interpreter.eval( new InputStreamReader( getClass().getResourceAsStream( "/extractor.bsh" ) ) );
67 }
68 catch ( EvalError evalError )
69 {
70 throw new InvalidPluginDescriptorException( "Error scanning beanshell script", evalError );
71 }
72
73 return mojoDescriptor;
74 }
75
76 protected String getScriptFileExtension()
77 {
78 return ".bsh";
79 }
80
81 protected List extractMojoDescriptors( Map scriptFilesKeyedByBasedir, PluginDescriptor pluginDescriptor )
82 throws ExtractionException, InvalidPluginDescriptorException
83 {
84 List descriptors = new ArrayList();
85
86 for ( Iterator mapIterator = scriptFilesKeyedByBasedir.entrySet().iterator(); mapIterator.hasNext(); )
87 {
88 Map.Entry entry = (Map.Entry) mapIterator.next();
89
90 String basedir = (String) entry.getKey();
91 Set metadataFiles = (Set) entry.getValue();
92
93 for ( Iterator it = metadataFiles.iterator(); it.hasNext(); )
94 {
95 File scriptFile = (File) it.next();
96
97 String relativePath = null;
98
99 if ( basedir.endsWith( "/" ) )
100 {
101 basedir = basedir.substring( 0, basedir.length() - 2 );
102 }
103
104 relativePath = scriptFile.getPath().substring( basedir.length() );
105
106 relativePath = relativePath.replace( '\\', '/' );
107
108 MojoDescriptor mojoDescriptor = createMojoDescriptor( basedir, relativePath, pluginDescriptor );
109 descriptors.add( mojoDescriptor );
110 }
111 }
112
113 return descriptors;
114 }
115 }