001 /**
002 * Copyright 2010-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.mojo.license;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.codehaus.mojo.license.model.License;
020 import org.codehaus.mojo.license.model.LicenseStore;
021
022 import java.util.Arrays;
023
024 /**
025 * Abstract mojo which using a {@link #licenseName} and owns a
026 * {@link #licenseStore}.
027 *
028 * @author tchemit <chemit@codelutin.com>
029 * @since 1.0
030 */
031 public abstract class AbstractLicenseNameMojo
032 extends AbstractLicenseMojo
033 {
034
035 /**
036 * To specify an external extra licenses repository resolver (says the base
037 * url of the repository where the {@code license.properties} is present).
038 *
039 * @parameter expression="${license.licenseResolver}"
040 * @since 1.0
041 */
042 private String licenseResolver;
043
044 /**
045 * A flag to keep a backup of every modified file.
046 *
047 * @parameter expression="${license.keepBackup}" default-value="false"
048 * @since 1.0
049 */
050 private boolean keepBackup;
051
052 /**
053 * Name of the license to use in the project.
054 *
055 * @parameter expression="${license.licenseName}"
056 * @since 1.0
057 */
058 private String licenseName;
059
060 /**
061 * License loaded from the {@link #licenseName}.
062 */
063 private License license;
064
065 /**
066 * Store of available licenses.
067 */
068 private LicenseStore licenseStore;
069
070 /**
071 * When is sets to {@code true}, will skip execution.
072 * <p/>
073 * This will take effects in method {@link #checkSkip()}.
074 * So the method {@link #doAction()} will never be invoked.
075 *
076 * @return {@code true} if goal will not be executed
077 */
078 public abstract boolean isSkip();
079
080 /**
081 * Changes internal state {@code skip} to execute (or not) goal.
082 *
083 * @param skip new state value
084 */
085 public abstract void setSkip( boolean skip );
086
087 @Override
088 protected boolean checkSkip()
089 {
090 if ( isSkip() )
091 {
092 getLog().info( "skip flag is on, will skip goal." );
093 return false;
094 }
095 return super.checkSkip();
096 }
097
098 @Override
099 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 }