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.shared.jarsigner;
20  
21  import javax.inject.Named;
22  
23  import org.apache.maven.shared.utils.cli.Commandline;
24  import org.apache.maven.shared.utils.cli.StreamConsumer;
25  import org.apache.maven.shared.utils.cli.javatool.AbstractJavaTool;
26  import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
27  
28  /**
29   * Default implementation of component {@link JarSigner}.
30   *
31   * @author Tony Chemit
32   * @since 1.0
33   */
34  @Named
35  public class DefaultJarSigner extends AbstractJavaTool<JarSignerRequest> implements JarSigner {
36  
37      /**
38       * default constructor
39       */
40      public DefaultJarSigner() {
41          super("jarsigner");
42      }
43  
44      @Override
45      protected Commandline createCommandLine(JarSignerRequest request, String javaToolFile) throws JavaToolException {
46          JarSignerCommandLineBuilder cliBuilder = new JarSignerCommandLineBuilder();
47          cliBuilder.setJarSignerFile(javaToolFile);
48          try {
49              Commandline cli = cliBuilder.build(request);
50              if (request.isVerbose()) {
51                  getLogger().info(cli.toString());
52              } else if (getLogger().isDebugEnabled()) {
53                  getLogger().debug(cli.toString());
54              }
55              return cli;
56          } catch (CommandLineConfigurationException e) {
57              throw new JavaToolException("Error configuring command-line. Reason: " + e.getMessage(), e);
58          }
59      }
60  
61      @Override
62      protected StreamConsumer createSystemOutStreamConsumer(JarSignerRequest request) {
63          StreamConsumer systemOut = request.getSystemOutStreamConsumer();
64  
65          if (systemOut == null) {
66  
67              final boolean verbose = request.isVerbose();
68  
69              systemOut = line -> {
70                  if (verbose) {
71                      getLogger().info(line);
72                  } else {
73                      getLogger().debug(line);
74                  }
75              };
76          }
77          return systemOut;
78      }
79  }