View Javadoc
1   /*
2    * Copyright (c) 2016 ingenieux Labs
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package br.com.ingenieux.mojo.beanstalk.cmd;
18  
19  import com.amazonaws.services.elasticbeanstalk.AWSElasticBeanstalkClient;
20  
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.apache.maven.plugin.MojoFailureException;
23  import org.apache.maven.plugin.logging.Log;
24  
25  import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
26  
27  public abstract class BaseCommand<I, O> implements Command<I, O> {
28  
29    /**
30     * Logger
31     */
32    protected Log logger;
33  
34    /**
35     * Service
36     */
37    protected AWSElasticBeanstalkClient service;
38  
39    /**
40     * Parent Mojo
41     */
42    protected AbstractBeanstalkMojo parentMojo;
43  
44    /**
45     * Constructor
46     *
47     * @param parentMojo parent mojo
48     */
49    protected BaseCommand(AbstractBeanstalkMojo parentMojo) throws MojoExecutionException {
50      this.parentMojo = parentMojo;
51  
52      this.service = parentMojo.getService();
53  
54      this.logger = parentMojo.getLog();
55    }
56  
57    public boolean isDebugEnabled() {
58      return logger.isDebugEnabled();
59    }
60  
61    public void debug(CharSequence message, Object... args) {
62      logger.debug(String.format("" + message, args));
63    }
64  
65    public boolean isInfoEnabled() {
66      return logger.isInfoEnabled();
67    }
68  
69    public void info(CharSequence message, Object... args) {
70      logger.info(String.format("" + message, args));
71    }
72  
73    public final O execute(I context) throws MojoFailureException, MojoExecutionException {
74      try {
75        return executeInternal(context);
76      } catch (Exception exc) {
77        handleException(exc);
78  
79        throw new RuntimeException("Unlikely");
80      }
81    }
82  
83    private void handleException(Exception exc) throws MojoExecutionException, MojoFailureException {
84      parentMojo.handleException(exc);
85    }
86  
87    protected abstract O executeInternal(I context) throws Exception;
88  }