View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }