1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.lang.reflect.Method;
22 import java.util.List;
23 import javax.inject.Inject;
24 import javax.inject.Named;
25 import javax.inject.Singleton;
26 import org.apache.maven.api.services.Prompter;
27 import org.apache.maven.api.services.PrompterException;
28 import org.codehaus.plexus.PlexusContainer;
29
30 @Named
31 @Singleton
32 public class DefaultPrompter implements Prompter {
33
34 private static final String PROMPTER_CLASS = "org.codehaus.plexus.components.interactivity.Prompter";
35 private final PlexusContainer container;
36
37 @Inject
38 public DefaultPrompter(PlexusContainer container) {
39 this.container = container;
40 }
41
42 @Override
43 public String prompt(String message, List<String> possibleValues, String defaultReply) throws PrompterException {
44 try {
45 Class<?> clazz = container.getContainerRealm().loadClass(PROMPTER_CLASS);
46 Object instance = container.lookup(clazz);
47 Method method = clazz.getMethod("prompt", String.class, List.class, String.class);
48 return (String) method.invoke(instance, message, possibleValues, defaultReply);
49 } catch (Exception e) {
50 throw new PrompterException("Unable to call prompter", e);
51 }
52 }
53
54 @Override
55 public String promptForPassword(String message) throws PrompterException {
56 try {
57 Class<?> clazz = container.getContainerRealm().loadClass(PROMPTER_CLASS);
58 Object instance = container.lookup(clazz);
59 Method method = clazz.getMethod("promptForPassword", String.class);
60 return (String) method.invoke(instance, message);
61 } catch (Exception e) {
62 throw new PrompterException("Unable to call prompter", e);
63 }
64 }
65
66 @Override
67 public void showMessage(String message) throws PrompterException {
68 try {
69 Class<?> clazz = container.getContainerRealm().loadClass(PROMPTER_CLASS);
70 Object instance = container.lookup(clazz);
71 Method method = clazz.getMethod("showMessage", String.class);
72 method.invoke(instance, message);
73 } catch (Exception e) {
74 throw new PrompterException("Unable to call prompter", e);
75 }
76 }
77 }