View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.codehaus.mojo.license;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.codehaus.mojo.license.model.License;
20  import org.codehaus.mojo.license.model.LicenseStore;
21  
22  import java.util.Arrays;
23  
24  /**
25   * Abstract mojo which using a {@link #licenseName} and owns a
26   * {@link #licenseStore}.
27   *
28   * @author tchemit <chemit@codelutin.com>
29   * @since 1.0
30   */
31  public abstract class AbstractLicenseNameMojo
32      extends AbstractLicenseMojo
33  {
34  
35      /**
36       * To specify an external extra licenses repository resolver (says the base
37       * url of the repository where the {@code license.properties} is present).
38       *
39       * @parameter expression="${license.licenseResolver}"
40       * @since 1.0
41       */
42      private String licenseResolver;
43  
44      /**
45       * A flag to keep a backup of every modified file.
46       *
47       * @parameter expression="${license.keepBackup}"  default-value="false"
48       * @since 1.0
49       */
50      private boolean keepBackup;
51  
52      /**
53       * Name of the license to use in the project.
54       *
55       * @parameter expression="${license.licenseName}"
56       * @since 1.0
57       */
58      private String licenseName;
59  
60      /**
61       * License loaded from the {@link #licenseName}.
62       */
63      private License license;
64  
65      /**
66       * Store of available licenses.
67       */
68      private LicenseStore licenseStore;
69  
70      /**
71       * When is sets to {@code true}, will skip execution.
72       * <p/>
73       * This will take effects in method {@link #checkSkip()}.
74       * So the method {@link #doAction()} will never be invoked.
75       *
76       * @return {@code true} if goal will not be executed
77       */
78      public abstract boolean isSkip();
79  
80      /**
81       * Changes internal state {@code skip} to execute (or not) goal.
82       *
83       * @param skip new state value
84       */
85      public abstract void setSkip( boolean skip );
86  
87      @Override
88      protected boolean checkSkip()
89      {
90          if ( isSkip() )
91          {
92              getLog().info( "skip flag is on, will skip goal." );
93              return false;
94          }
95          return super.checkSkip();
96      }
97  
98      @Override
99      protected void init()
100         throws Exception
101     {
102 
103         if ( isSkip() )
104         {
105             return;
106         }
107 
108         // init licenses store
109         licenseStore = LicenseStore.createLicenseStore( getLog(), getLicenseResolver() );
110 
111         // check licenseName exists
112         license = getLicense( licenseName, true );
113     }
114 
115     public License getLicense( String licenseName, boolean checkIfExists )
116         throws IllegalArgumentException, IllegalStateException
117     {
118         if ( StringUtils.isEmpty( licenseName ) )
119         {
120             throw new IllegalArgumentException( "licenseName can not be null, nor empty" );
121         }
122         LicenseStore licenseStore = getLicenseStore();
123         if ( licenseStore == null )
124         {
125             throw new IllegalStateException( "No license store initialized!" );
126         }
127         License license = licenseStore.getLicense( licenseName );
128         if ( checkIfExists && license == null )
129         {
130             throw new IllegalArgumentException( "License named '" + licenseName + "' is unknown, use one of " +
131                                                     Arrays.toString( licenseStore.getLicenseNames() ) );
132         }
133         return license;
134     }
135 
136     public boolean isKeepBackup()
137     {
138         return keepBackup;
139     }
140 
141     public String getLicenseName()
142     {
143         return licenseName;
144     }
145 
146     public String getLicenseResolver()
147     {
148         return licenseResolver;
149     }
150 
151     public LicenseStore getLicenseStore()
152     {
153         return licenseStore;
154     }
155 
156     public License getLicense()
157     {
158         return license;
159     }
160 
161     public void setKeepBackup( boolean keepBackup )
162     {
163         this.keepBackup = keepBackup;
164     }
165 
166     public void setLicenseResolver( String licenseResolver )
167     {
168         this.licenseResolver = licenseResolver;
169     }
170 
171     public void setLicenseName( String licenseName )
172     {
173         this.licenseName = licenseName;
174     }
175 
176     public void setLicenseStore( LicenseStore licenseStore )
177     {
178         this.licenseStore = licenseStore;
179     }
180 
181 }