1 /*
2 * #%L
3 * License Maven Plugin
4 *
5 * $Id: AbstractLicenseNameMojo.java 13616 2011-02-14 11:38:39Z tchemit $
6 * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/AbstractLicenseNameMojo.java $
7 * %%
8 * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
9 * %%
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Lesser Public License for more details.
19 *
20 * You should have received a copy of the GNU General Lesser Public
21 * License along with this program. If not, see
22 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23 * #L%
24 */
25
26 package org.codehaus.mojo.license;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.codehaus.mojo.license.model.License;
30 import org.codehaus.mojo.license.model.LicenseStore;
31
32 import java.util.Arrays;
33
34 /**
35 * Abstract mojo which using a {@link #licenseName} and owns a
36 * {@link #licenseStore}.
37 *
38 * @author tchemit <chemit@codelutin.com>
39 * @since 1.0
40 */
41 public abstract class AbstractLicenseNameMojo
42 extends AbstractLicenseMojo
43 {
44
45 /**
46 * To specify an external extra licenses repository resolver (says the base
47 * url of the repository where the {@code license.properties} is present).
48 *
49 * @parameter expression="${license.licenseResolver}"
50 * @since 1.0
51 */
52 private String licenseResolver;
53
54 /**
55 * A flag to keep a backup of every modified file.
56 *
57 * @parameter expression="${license.keepBackup}" default-value="false"
58 * @since 1.0
59 */
60 private boolean keepBackup;
61
62 /**
63 * Name of the license to use in the project.
64 *
65 * @parameter expression="${license.licenseName}"
66 * @since 1.0
67 */
68 private String licenseName;
69
70 /**
71 * License loaded from the {@link #licenseName}.
72 */
73 private License license;
74
75 /**
76 * Store of available licenses.
77 */
78 private LicenseStore licenseStore;
79
80 /**
81 * When is sets to {@code true}, will skip execution.
82 * <p/>
83 * This will take effects in method {@link #checkSkip()}.
84 * So the method {@link #doAction()} will never be invoked.
85 *
86 * @return {@code true} if goal will not be executed
87 */
88 public abstract boolean isSkip();
89
90 /**
91 * Changes internal state {@code skip} to execute (or not) goal.
92 *
93 * @param skip new state value
94 */
95 public abstract void setSkip( boolean skip );
96
97 @Override
98 protected boolean checkSkip()
99 {
100 if ( isSkip() )
101 {
102 getLog().info( "skip flag is on, will skip goal." );
103 return false;
104 }
105 return super.checkSkip();
106 }
107
108 @Override
109 protected void init()
110 throws Exception
111 {
112
113 if ( isSkip() )
114 {
115 return;
116 }
117
118 // init licenses store
119 licenseStore = LicenseStore.createLicenseStore( getLog(), getLicenseResolver() );
120
121 // check licenseName exists
122 license = getLicense( licenseName, true );
123 }
124
125 public License getLicense( String licenseName, boolean checkIfExists )
126 throws IllegalArgumentException, IllegalStateException
127 {
128 if ( StringUtils.isEmpty( licenseName ) )
129 {
130 throw new IllegalArgumentException( "licenseName can not be null, nor empty" );
131 }
132 LicenseStore licenseStore = getLicenseStore();
133 if ( licenseStore == null )
134 {
135 throw new IllegalStateException( "No license store initialized!" );
136 }
137 License license = licenseStore.getLicense( licenseName );
138 if ( checkIfExists && license == null )
139 {
140 throw new IllegalArgumentException( "License named '" + licenseName + "' is unknown, use one of " +
141 Arrays.toString( licenseStore.getLicenseNames() ) );
142 }
143 return license;
144 }
145
146 public boolean isKeepBackup()
147 {
148 return keepBackup;
149 }
150
151 public String getLicenseName()
152 {
153 return licenseName;
154 }
155
156 public String getLicenseResolver()
157 {
158 return licenseResolver;
159 }
160
161 public LicenseStore getLicenseStore()
162 {
163 return licenseStore;
164 }
165
166 public License getLicense()
167 {
168 return license;
169 }
170
171 public void setKeepBackup( boolean keepBackup )
172 {
173 this.keepBackup = keepBackup;
174 }
175
176 public void setLicenseResolver( String licenseResolver )
177 {
178 this.licenseResolver = licenseResolver;
179 }
180
181 public void setLicenseName( String licenseName )
182 {
183 this.licenseName = licenseName;
184 }
185
186 public void setLicenseStore( LicenseStore licenseStore )
187 {
188 this.licenseStore = licenseStore;
189 }
190
191 }